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

Side by Side Diff: base/file_path_unittest.cc

Issue 11642041: Don't allow '\0' characters in FilePath. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: resize -> reserve in SavePackageTest Created 7 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 | Annotate | Revision Log
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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/file_path.h" 6 #include "base/file_path.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h" 10 #include "testing/platform_test.h"
11 11
12 // This macro helps avoid wrapped lines in the test structs. 12 // This macro helps avoid wrapped lines in the test structs.
13 #define FPL(x) FILE_PATH_LITERAL(x) 13 #define FPL(x) FILE_PATH_LITERAL(x)
14 14
15 // This macro constructs strings which can contain NULs.
16 #define FPS(x) FilePath::StringType(FPL(x), arraysize(FPL(x)) - 1)
17
15 struct UnaryTestData { 18 struct UnaryTestData {
16 const FilePath::CharType* input; 19 const FilePath::CharType* input;
17 const FilePath::CharType* expected; 20 const FilePath::CharType* expected;
18 }; 21 };
19 22
20 struct UnaryBooleanTestData { 23 struct UnaryBooleanTestData {
21 const FilePath::CharType* input; 24 const FilePath::CharType* input;
22 bool expected; 25 bool expected;
23 }; 26 };
24 27
(...skipping 1083 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 << "i: " << i << ", input: " << cases[i].native; 1111 << "i: " << i << ", input: " << cases[i].native;
1109 // Test AsUTF8Unsafe() works. 1112 // Test AsUTF8Unsafe() works.
1110 FilePath from_native = FilePath(cases[i].native); 1113 FilePath from_native = FilePath(cases[i].native);
1111 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe()) 1114 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
1112 << "i: " << i << ", input: " << cases[i].native; 1115 << "i: " << i << ", input: " << cases[i].native;
1113 // Test the two file paths are identical. 1116 // Test the two file paths are identical.
1114 EXPECT_EQ(from_utf8.value(), from_native.value()); 1117 EXPECT_EQ(from_utf8.value(), from_native.value());
1115 } 1118 }
1116 } 1119 }
1117 1120
1121 TEST_F(FilePathTest, ConstructWithNUL) {
1122 // Assert FPS() works.
1123 ASSERT_TRUE(FPS("a\0b").length() == 3);
1124
1125 // Test constructor strips '\0'
1126 FilePath path(FPS("a\0b"));
1127 EXPECT_TRUE(path.value().length() == 1);
1128 EXPECT_EQ(path.value(), FPL("a"));
1129 }
1130
1131 TEST_F(FilePathTest, AppendWithNUL) {
1132 // Assert FPS() works.
1133 ASSERT_TRUE(FPS("b\0b").length() == 3);
1134
1135 // Test Append() strips '\0'
1136 FilePath path(FPL("a"));
1137 path = path.Append(FPS("b\0b"));
1138 EXPECT_TRUE(path.value().length() == 3);
1139 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1140 EXPECT_EQ(path.value(), FPL("a\\b"));
1141 #else
1142 EXPECT_EQ(path.value(), FPL("a/b"));
1143 #endif
1144 }
1145
1146 TEST_F(FilePathTest, ReferencesParentWithNUL) {
1147 // Assert FPS() works.
1148 ASSERT_TRUE(FPS("..\0").length() == 3);
1149
1150 // Test ReferencesParent() doesn't break with "..\0"
1151 FilePath path(FPS("..\0"));
1152 EXPECT_TRUE(path.ReferencesParent());
1153 }
1154
1118 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 1155 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1119 TEST_F(FilePathTest, NormalizePathSeparators) { 1156 TEST_F(FilePathTest, NormalizePathSeparators) {
1120 const struct UnaryTestData cases[] = { 1157 const struct UnaryTestData cases[] = {
1121 { FPL("foo/bar"), FPL("foo\\bar") }, 1158 { FPL("foo/bar"), FPL("foo\\bar") },
1122 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") }, 1159 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1123 { FPL("foo\\bar"), FPL("foo\\bar") }, 1160 { FPL("foo\\bar"), FPL("foo\\bar") },
1124 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") }, 1161 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1125 { FPL("foo"), FPL("foo") }, 1162 { FPL("foo"), FPL("foo") },
1126 // Trailing slashes don't automatically get stripped. That's what 1163 // Trailing slashes don't automatically get stripped. That's what
1127 // StripTrailingSeparators() is for. 1164 // StripTrailingSeparators() is for.
(...skipping 24 matching lines...) Expand all
1152 }; 1189 };
1153 for (size_t i = 0; i < arraysize(cases); ++i) { 1190 for (size_t i = 0; i < arraysize(cases); ++i) {
1154 FilePath input(cases[i].input); 1191 FilePath input(cases[i].input);
1155 FilePath observed = input.NormalizePathSeparators(); 1192 FilePath observed = input.NormalizePathSeparators();
1156 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) << 1193 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1157 "i: " << i << ", input: " << input.value(); 1194 "i: " << i << ", input: " << input.value();
1158 } 1195 }
1159 } 1196 }
1160 1197
1161 #endif 1198 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698