directory_listing_unittest.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/base/directory_listing.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "base/time/time.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace net {
  9. namespace {
  10. struct GetDirectoryListingEntryCase {
  11. const wchar_t* name;
  12. const char* const raw_bytes;
  13. bool is_dir;
  14. int64_t filesize;
  15. base::Time time;
  16. const char* const expected;
  17. };
  18. TEST(DirectoryListingTest, GetDirectoryListingEntry) {
  19. const GetDirectoryListingEntryCase test_cases[] = {
  20. {L"Foo", "", false, 10000, base::Time(),
  21. "<script>addRow(\"Foo\",\"Foo\",0,10000,\"9.8 kB\",0,\"\");</script>\n"},
  22. {L"quo\"tes", "", false, 10000, base::Time(),
  23. "<script>addRow(\"quo\\\"tes\",\"quo%22tes\",0,10000,\"9.8 kB\",0,\"\""
  24. ");</script>\n"},
  25. {L"quo\"tes", "quo\"tes", false, 10000, base::Time(),
  26. "<script>addRow(\"quo\\\"tes\",\"quo%22tes\",0,10000,\"9.8 kB\",0,\"\""
  27. ");</script>\n"},
  28. // U+D55C0 U+AE00. raw_bytes is empty (either a local file with
  29. // UTF-8/UTF-16 encoding or a remote file on an ftp server using UTF-8
  30. {L"\xD55C\xAE00.txt", "", false, 10000, base::Time(),
  31. "<script>addRow(\"\xED\x95\x9C\xEA\xB8\x80.txt\","
  32. "\"%ED%95%9C%EA%B8%80.txt\",0,10000,\"9.8 kB\",0,\"\");</script>\n"},
  33. // U+D55C0 U+AE00. raw_bytes is the corresponding EUC-KR sequence:
  34. // a local or remote file in EUC-KR.
  35. {L"\xD55C\xAE00.txt", "\xC7\xD1\xB1\xDB.txt", false, 10000, base::Time(),
  36. "<script>addRow(\"\xED\x95\x9C\xEA\xB8\x80.txt\",\"%C7%D1%B1%DB.txt\""
  37. ",0,10000,\"9.8 kB\",0,\"\");</script>\n"},
  38. };
  39. for (const auto& test_case : test_cases) {
  40. const std::string results = GetDirectoryListingEntry(
  41. base::WideToUTF16(test_case.name), test_case.raw_bytes,
  42. test_case.is_dir, test_case.filesize, test_case.time);
  43. EXPECT_EQ(test_case.expected, results);
  44. }
  45. }
  46. } // namespace
  47. } // namespace net