File: arc_documents_provider_util_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (295 lines) | stat: -rw-r--r-- 12,337 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/fileapi/arc_documents_provider_util.h"

#include "base/files/file_path.h"
#include "chromeos/ash/experiences/arc/mojom/file_system.mojom.h"
#include "storage/browser/file_system/file_system_url.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/gurl.h"

namespace arc {

namespace {

mojom::DocumentPtr MakeDocument(const std::string& display_name,
                                const std::string& mime_type) {
  mojom::DocumentPtr document = mojom::Document::New();
  document->display_name = display_name;
  document->mime_type = mime_type;
  return document;
}

TEST(ArcDocumentsProviderUtilTest, EscapePathComponent) {
  EXPECT_EQ("", EscapePathComponent(""));
  EXPECT_EQ("%2E", EscapePathComponent("."));
  EXPECT_EQ("%2E%2E", EscapePathComponent(".."));
  EXPECT_EQ("...", EscapePathComponent("..."));
  EXPECT_EQ("example.com", EscapePathComponent("example.com"));
  EXPECT_EQ("%2F%2F%2F", EscapePathComponent("///"));
  EXPECT_EQ("100%25", EscapePathComponent("100%"));
  EXPECT_EQ("a b", EscapePathComponent("a b"));
  EXPECT_EQ("ねこ", EscapePathComponent("ねこ"));
}

TEST(ArcDocumentsProviderUtilTest, UnescapePathComponent) {
  EXPECT_EQ("", UnescapePathComponent(""));
  EXPECT_EQ(".", UnescapePathComponent("%2E"));
  EXPECT_EQ("..", UnescapePathComponent("%2E%2E"));
  EXPECT_EQ("...", UnescapePathComponent("..."));
  EXPECT_EQ("example.com", UnescapePathComponent("example.com"));
  EXPECT_EQ("///", UnescapePathComponent("%2F%2F%2F"));
  EXPECT_EQ("100%", UnescapePathComponent("100%25"));
  EXPECT_EQ("a b", UnescapePathComponent("a b"));
  EXPECT_EQ("ねこ", UnescapePathComponent("ねこ"));
}

TEST(ArcDocumentsProviderUtilTest, GetDocumentsProviderMountPath) {
  EXPECT_EQ("/special/arc-documents-provider/authority/root_id",
            GetDocumentsProviderMountPath("authority", "root_id").value());
  EXPECT_EQ("/special/arc-documents-provider/a b/a b",
            GetDocumentsProviderMountPath("a b", "a b").value());
  EXPECT_EQ("/special/arc-documents-provider/a%2Fb/a%2Fb",
            GetDocumentsProviderMountPath("a/b", "a/b").value());
  EXPECT_EQ("/special/arc-documents-provider/%2E/%2E",
            GetDocumentsProviderMountPath(".", ".").value());
  EXPECT_EQ("/special/arc-documents-provider/%2E%2E/%2E%2E",
            GetDocumentsProviderMountPath("..", "..").value());
  EXPECT_EQ("/special/arc-documents-provider/.../...",
            GetDocumentsProviderMountPath("...", "...").value());
}

TEST(ArcDocumentsProviderUtilTest, ParseDocumentsProviderUrl) {
  std::string authority;
  std::string root_id;
  base::FilePath path;

  EXPECT_TRUE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(kDocumentsProviderMountPointPath)
              .Append(FILE_PATH_LITERAL("cats/root/home/calico.jpg"))),
      &authority, &root_id, &path));
  EXPECT_EQ("cats", authority);
  EXPECT_EQ("root", root_id);
  EXPECT_EQ(FILE_PATH_LITERAL("home/calico.jpg"), path.value());
}

TEST(ArcDocumentsProviderUtilTest, ParseDocumentsProviderUrlEmptyPath) {
  std::string authority;
  std::string root_id;
  // Assign a non-empty arbitrary path to make sure an empty path is
  // set in ParseDocumentsProviderUrl().
  base::FilePath path(FILE_PATH_LITERAL("foobar"));

  // Should accept a path pointing to a root directory.
  EXPECT_TRUE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(kDocumentsProviderMountPointPath)
              .Append(FILE_PATH_LITERAL("cats/root"))),
      &authority, &root_id, &path));
  EXPECT_EQ("cats", authority);
  EXPECT_EQ("root", root_id);
  EXPECT_EQ(FILE_PATH_LITERAL(""), path.value());
}

TEST(ArcDocumentsProviderUtilTest, ParseDocumentsProviderUrlEmptyPathSlash) {
  std::string authority;
  std::string root_id;
  // Assign a non-empty arbitrary path to make sure an empty path is
  // set in ParseDocumentsProviderUrl().
  base::FilePath path(FILE_PATH_LITERAL("foobar"));

  // Should accept a path pointing to a root directory.
  EXPECT_TRUE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(kDocumentsProviderMountPointPath)
              .Append(FILE_PATH_LITERAL("cats/root/"))),
      &authority, &root_id, &path));
  EXPECT_EQ("cats", authority);
  EXPECT_EQ("root", root_id);
  EXPECT_EQ(FILE_PATH_LITERAL(""), path.value());
}

TEST(ArcDocumentsProviderUtilTest, ParseDocumentsProviderUrlInvalidType) {
  std::string authority;
  std::string root_id;
  base::FilePath path;

  // Not storage::kFileSystemTypeArcDocumentsProvider.
  EXPECT_FALSE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcContent,
          base::FilePath(kDocumentsProviderMountPointPath)
              .Append(FILE_PATH_LITERAL("cats/root/home/calico.jpg"))),
      &authority, &root_id, &path));
}

TEST(ArcDocumentsProviderUtilTest, ParseDocumentsProviderUrlInvalidPath) {
  std::string authority;
  std::string root_id;
  base::FilePath path;

  // root_id part is missing.
  EXPECT_FALSE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(kDocumentsProviderMountPointPath)
              .Append(FILE_PATH_LITERAL("root-missing"))),
      &authority, &root_id, &path));

  // Leading / is missing.
  EXPECT_FALSE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(FILE_PATH_LITERAL(
              "special/arc-documents-provider/cats/root/home/calico.jpg"))),
      &authority, &root_id, &path));

  // Not under /special.
  EXPECT_FALSE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(FILE_PATH_LITERAL(
              "/invalid/arc-documents-provider/cats/root/home/calico.jpg"))),
      &authority, &root_id, &path));

  // Not under /special/arc-documents-provider.
  EXPECT_FALSE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(FILE_PATH_LITERAL(
              "/special/something-else/cats/root/home/calico.jpg"))),
      &authority, &root_id, &path));
}

TEST(ArcDocumentsProviderUtilTest, ParseDocumentsProviderUrlUnescape) {
  std::string authority;
  std::string root_id;
  base::FilePath path;

  EXPECT_TRUE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(
              "/special/arc-documents-provider/cats/ro%2Fot/home/calico.jpg")),
      &authority, &root_id, &path));
  EXPECT_EQ("cats", authority);
  EXPECT_EQ("ro/ot", root_id);
  EXPECT_EQ(FILE_PATH_LITERAL("home/calico.jpg"), path.value());
}

TEST(ArcDocumentsProviderUtilTest, ParseDocumentsProviderUrlUtf8) {
  std::string authority;
  std::string root_id;
  base::FilePath path;

  EXPECT_TRUE(ParseDocumentsProviderUrl(
      storage::FileSystemURL::CreateForTest(
          blink::StorageKey(), storage::kFileSystemTypeArcDocumentsProvider,
          base::FilePath(
              "/special/arc-documents-provider/cats/root/home/みけねこ.jpg")),
      &authority, &root_id, &path));
  EXPECT_EQ("cats", authority);
  EXPECT_EQ("root", root_id);
  EXPECT_EQ(FILE_PATH_LITERAL("home/みけねこ.jpg"), path.value());
}

TEST(ArcDocumentsProviderUtilTest, BuildDocumentUrl) {
  EXPECT_EQ("content://authority/document/document_id",
            BuildDocumentUrl("authority", "document_id").spec());
  EXPECT_EQ("content://a%20b/document/a%20b",
            BuildDocumentUrl("a b", "a b").spec());
  EXPECT_EQ("content://a%2Fb/document/a%2Fb",
            BuildDocumentUrl("a/b", "a/b").spec());
}

TEST(ArcDocumentsProviderUtilTest, GetExtensionsForArcMimeType) {
  // MIME types already known to Chromium.
  EXPECT_NE(0u, GetExtensionsForArcMimeType("audio/mp3").size());
  EXPECT_NE(0u, GetExtensionsForArcMimeType("image/jpeg").size());
  EXPECT_NE(0u, GetExtensionsForArcMimeType("text/html").size());
  EXPECT_NE(
      0u, GetExtensionsForArcMimeType("application/x-chrome-extension").size());

  // MIME types known to Android only.
  EXPECT_NE(0u,
            GetExtensionsForArcMimeType("application/x-android-drm-fl").size());
  EXPECT_NE(0u, GetExtensionsForArcMimeType("audio/x-wav").size());

  // Unknown types.
  EXPECT_EQ(0u, GetExtensionsForArcMimeType("abc/xyz").size());
  EXPECT_EQ(
      0u, GetExtensionsForArcMimeType("vnd.android.document/directory").size());

  // Specially handled types.
  EXPECT_EQ(0u, GetExtensionsForArcMimeType("application/octet-stream").size());
}

TEST(ArcDocumentsProviderUtilTest, GetFileNameForDocument) {
  EXPECT_EQ("kitten.png",
            GetFileNameForDocument(MakeDocument("kitten.png", "image/png")));
  EXPECT_EQ("a__b.png",
            GetFileNameForDocument(MakeDocument("a//b.png", "image/png")));
  EXPECT_EQ("_.png", GetFileNameForDocument(MakeDocument("", "image/png")));
  EXPECT_EQ("_.png", GetFileNameForDocument(MakeDocument(".", "image/png")));
  EXPECT_EQ("_.png", GetFileNameForDocument(MakeDocument("..", "image/png")));
  EXPECT_EQ("_.png",
            GetFileNameForDocument(MakeDocument("......", "image/png")));
  EXPECT_EQ("kitten.png",
            GetFileNameForDocument(MakeDocument("kitten", "image/png")));
  EXPECT_EQ("kitten",
            GetFileNameForDocument(MakeDocument("kitten", "abc/xyz")));

  // Check that files with a mime type different than expected appends a
  // possible extension when a different mime type with a different category is
  // found in the Android mime types map.
  EXPECT_EQ("file.txt.3gp",
            GetFileNameForDocument(MakeDocument("file.txt", "video/3gpp")));

  // Check that files with a mime type different than expected don't append
  // an extension when a different mime type with the same category is
  // found in the Android mime types map.
  EXPECT_EQ("file.3gp",
            GetFileNameForDocument(MakeDocument("file.3gp", "video/mp4")));
}

TEST(ArcDocumentsProviderUtilTest, StripMimeSubType) {
  // Check that the category type is returned for a valid mime type.
  EXPECT_EQ("video", StripMimeSubType("video/mp4"));
  // Check that an empty string is returned for malformed mime types.
  EXPECT_EQ("", StripMimeSubType(""));
  EXPECT_EQ("", StripMimeSubType("video/"));
  EXPECT_EQ("", StripMimeSubType("/"));
  EXPECT_EQ("", StripMimeSubType("/abc"));
  EXPECT_EQ("", StripMimeSubType("/abc/xyz"));
}

TEST(ArcDocumentsProviderUtilTest, FindArcMimeTypeFromExtension) {
  // Test that a lone possible extension returns the correct type.
  EXPECT_EQ("application/msword", FindArcMimeTypeFromExtension("doc"));
  // Test that the first extension in the comma delimited list of extensions
  // returns the correct type.
  EXPECT_EQ("video/3gpp", FindArcMimeTypeFromExtension("3gp"));
  // Test that the second extension in the comma delimited list of extensions
  // returns the correct type.
  EXPECT_EQ("audio/mpeg", FindArcMimeTypeFromExtension("mpga"));
  // Test that matching suffixes (m4a) return null without a full match.
  EXPECT_EQ("", FindArcMimeTypeFromExtension("4a"));
  // Test that matching prefixes (imy) return null without a full match.
  EXPECT_EQ("", FindArcMimeTypeFromExtension("im"));
  // Test that ambiguous prefixes (mp4, mpg) return null.
  EXPECT_EQ("", FindArcMimeTypeFromExtension("mp"));
  // Test that invalid mime types return null.
  EXPECT_EQ("", FindArcMimeTypeFromExtension(""));
  EXPECT_EQ("", FindArcMimeTypeFromExtension("invalid"));
}

}  // namespace

}  // namespace arc