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