Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: net/base/mime_util_unittest.cc

Issue 2665953002: Don't blindly do a prefix match when evaluating net::GetExtensionsForMimeType(). (Closed)
Patch Set: Address Helen's comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/mime_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/mime_util.h"
6
7 #include <algorithm>
8
5 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
6 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
7 #include "build/build_config.h" 11 #include "build/build_config.h"
8 #include "net/base/mime_util.h"
9 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
10 13
11 namespace net { 14 namespace net {
12 15
13 TEST(MimeUtilTest, ExtensionTest) { 16 TEST(MimeUtilTest, ExtensionTest) {
14 // String: png\0css 17 // String: png\0css
15 base::FilePath::StringType containsNullByte; 18 base::FilePath::StringType containsNullByte;
16 containsNullByte.append(FILE_PATH_LITERAL("png")); 19 containsNullByte.append(FILE_PATH_LITERAL("png"));
17 containsNullByte.append(1, FILE_PATH_LITERAL('\0')); 20 containsNullByte.append(1, FILE_PATH_LITERAL('\0'));
18 containsNullByte.append(FILE_PATH_LITERAL("css")); 21 containsNullByte.append(FILE_PATH_LITERAL("css"));
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 EXPECT_TRUE(IsValidTopLevelMimeType("X-video")); 232 EXPECT_TRUE(IsValidTopLevelMimeType("X-video"));
230 233
231 EXPECT_FALSE(IsValidTopLevelMimeType("x-")); 234 EXPECT_FALSE(IsValidTopLevelMimeType("x-"));
232 } 235 }
233 236
234 TEST(MimeUtilTest, TestGetExtensionsForMimeType) { 237 TEST(MimeUtilTest, TestGetExtensionsForMimeType) {
235 const struct { 238 const struct {
236 const char* const mime_type; 239 const char* const mime_type;
237 size_t min_expected_size; 240 size_t min_expected_size;
238 const char* const contained_result; 241 const char* const contained_result;
242 bool no_matches;
239 } tests[] = { 243 } tests[] = {
240 { "text/plain", 2, "txt" }, 244 {"text/plain", 2, "txt"},
241 { "*", 0, NULL }, 245 {"text/pl", 0, NULL, true},
242 { "message/*", 1, "eml" }, 246 {"*", 0, NULL},
243 { "MeSsAge/*", 1, "eml" }, 247 {"", 0, NULL, true},
244 { "image/bmp", 1, "bmp" }, 248 {"message/*", 1, "eml"},
245 { "video/*", 6, "mp4" }, 249 {"MeSsAge/*", 1, "eml"},
250 {"message/", 0, NULL, true},
251 {"image/bmp", 1, "bmp"},
252 {"video/*", 6, "mp4"},
246 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_IOS) 253 #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_IOS)
247 { "video/*", 6, "mpg" }, 254 {"video/*", 6, "mpg"},
248 #else 255 #else
249 { "video/*", 6, "mpeg" }, 256 {"video/*", 6, "mpeg"},
250 #endif 257 #endif
251 { "audio/*", 6, "oga" }, 258 {"audio/*", 6, "oga"},
252 { "aUDIo/*", 6, "wav" }, 259 {"aUDIo/*", 6, "wav"},
253 }; 260 };
254 261
255 for (size_t i = 0; i < arraysize(tests); ++i) { 262 for (const auto& test : tests) {
256 std::vector<base::FilePath::StringType> extensions; 263 std::vector<base::FilePath::StringType> extensions;
257 GetExtensionsForMimeType(tests[i].mime_type, &extensions); 264 GetExtensionsForMimeType(test.mime_type, &extensions);
258 ASSERT_TRUE(tests[i].min_expected_size <= extensions.size()); 265 ASSERT_LE(test.min_expected_size, extensions.size());
259 266
260 if (!tests[i].contained_result) 267 if (test.no_matches)
261 continue; 268 ASSERT_EQ(0u, extensions.size());
262 269
263 bool found = false; 270 if (test.contained_result) {
264 for (size_t j = 0; !found && j < extensions.size(); ++j) { 271 // Convert ASCII to FilePath::StringType.
265 #if defined(OS_WIN) 272 base::FilePath::StringType contained_result(
266 if (extensions[j] == base::UTF8ToWide(tests[i].contained_result)) 273 test.contained_result,
267 found = true; 274 test.contained_result + strlen(test.contained_result));
268 #else 275
269 if (extensions[j] == tests[i].contained_result) 276 bool found = std::find(extensions.begin(), extensions.end(),
270 found = true; 277 contained_result) != extensions.end();
271 #endif 278
279 ASSERT_TRUE(found) << "Must find at least the contained result within "
280 << test.mime_type;
272 } 281 }
273 ASSERT_TRUE(found) << "Must find at least the contained result within "
274 << tests[i].mime_type;
275 } 282 }
276 } 283 }
277 284
278 TEST(MimeUtilTest, TestGenerateMimeMultipartBoundary) { 285 TEST(MimeUtilTest, TestGenerateMimeMultipartBoundary) {
279 std::string boundary1 = GenerateMimeMultipartBoundary(); 286 std::string boundary1 = GenerateMimeMultipartBoundary();
280 std::string boundary2 = GenerateMimeMultipartBoundary(); 287 std::string boundary2 = GenerateMimeMultipartBoundary();
281 288
282 // RFC 1341 says: the boundary parameter [...] consists of 1 to 70 characters. 289 // RFC 1341 says: the boundary parameter [...] consists of 1 to 70 characters.
283 EXPECT_GE(70u, boundary1.size()); 290 EXPECT_GE(70u, boundary1.size());
284 EXPECT_GE(70u, boundary2.size()); 291 EXPECT_GE(70u, boundary2.size());
(...skipping 30 matching lines...) Expand all
315 std::string post_data; 322 std::string post_data;
316 AddMultipartValueForUpload("value name", "value", "boundary", 323 AddMultipartValueForUpload("value name", "value", "boundary",
317 "content type", &post_data); 324 "content type", &post_data);
318 AddMultipartValueForUpload("value name", "value", "boundary", 325 AddMultipartValueForUpload("value name", "value", "boundary",
319 "", &post_data); 326 "", &post_data);
320 AddMultipartFinalDelimiterForUpload("boundary", &post_data); 327 AddMultipartFinalDelimiterForUpload("boundary", &post_data);
321 EXPECT_STREQ(ref_output, post_data.c_str()); 328 EXPECT_STREQ(ref_output, post_data.c_str());
322 } 329 }
323 330
324 } // namespace net 331 } // namespace net
OLDNEW
« no previous file with comments | « net/base/mime_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698