file_path_unittest.cc 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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
  3. // found in the LICENSE file.
  4. #include "butil/basictypes.h"
  5. #include "butil/files/file_path.h"
  6. #include "butil/strings/utf_string_conversions.h"
  7. #include <gtest/gtest.h>
  8. #include <gtest/gtest.h>
  9. // This macro helps avoid wrapped lines in the test structs.
  10. #define FPL(x) FILE_PATH_LITERAL(x)
  11. // This macro constructs strings which can contain NULs.
  12. #define FPS(x) FilePath::StringType(FPL(x), arraysize(FPL(x)) - 1)
  13. namespace butil {
  14. struct UnaryTestData {
  15. const FilePath::CharType* input;
  16. const FilePath::CharType* expected;
  17. };
  18. struct UnaryBooleanTestData {
  19. const FilePath::CharType* input;
  20. bool expected;
  21. };
  22. struct BinaryTestData {
  23. const FilePath::CharType* inputs[2];
  24. const FilePath::CharType* expected;
  25. };
  26. struct BinaryBooleanTestData {
  27. const FilePath::CharType* inputs[2];
  28. bool expected;
  29. };
  30. struct BinaryIntTestData {
  31. const FilePath::CharType* inputs[2];
  32. int expected;
  33. };
  34. struct UTF8TestData {
  35. const FilePath::CharType* native;
  36. const char* utf8;
  37. };
  38. // file_util winds up using autoreleased objects on the Mac, so this needs
  39. // to be a testing::Test
  40. class FilePathTest : public testing::Test {
  41. protected:
  42. virtual void SetUp() OVERRIDE {
  43. testing::Test::SetUp();
  44. }
  45. virtual void TearDown() OVERRIDE {
  46. testing::Test::TearDown();
  47. }
  48. };
  49. TEST_F(FilePathTest, DirName) {
  50. const struct UnaryTestData cases[] = {
  51. { FPL(""), FPL(".") },
  52. { FPL("aa"), FPL(".") },
  53. { FPL("/aa/bb"), FPL("/aa") },
  54. { FPL("/aa/bb/"), FPL("/aa") },
  55. { FPL("/aa/bb//"), FPL("/aa") },
  56. { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
  57. { FPL("/aa"), FPL("/") },
  58. { FPL("/aa/"), FPL("/") },
  59. { FPL("/"), FPL("/") },
  60. { FPL("//"), FPL("//") },
  61. { FPL("///"), FPL("/") },
  62. { FPL("aa/"), FPL(".") },
  63. { FPL("aa/bb"), FPL("aa") },
  64. { FPL("aa/bb/"), FPL("aa") },
  65. { FPL("aa/bb//"), FPL("aa") },
  66. { FPL("aa//bb//"), FPL("aa") },
  67. { FPL("aa//bb/"), FPL("aa") },
  68. { FPL("aa//bb"), FPL("aa") },
  69. { FPL("//aa/bb"), FPL("//aa") },
  70. { FPL("//aa/"), FPL("//") },
  71. { FPL("//aa"), FPL("//") },
  72. { FPL("0:"), FPL(".") },
  73. { FPL("@:"), FPL(".") },
  74. { FPL("[:"), FPL(".") },
  75. { FPL("`:"), FPL(".") },
  76. { FPL("{:"), FPL(".") },
  77. { FPL("\xB3:"), FPL(".") },
  78. { FPL("\xC5:"), FPL(".") },
  79. #if defined(OS_WIN)
  80. { FPL("\x0143:"), FPL(".") },
  81. #endif // OS_WIN
  82. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  83. { FPL("c:"), FPL("c:") },
  84. { FPL("C:"), FPL("C:") },
  85. { FPL("A:"), FPL("A:") },
  86. { FPL("Z:"), FPL("Z:") },
  87. { FPL("a:"), FPL("a:") },
  88. { FPL("z:"), FPL("z:") },
  89. { FPL("c:aa"), FPL("c:") },
  90. { FPL("c:/"), FPL("c:/") },
  91. { FPL("c://"), FPL("c://") },
  92. { FPL("c:///"), FPL("c:/") },
  93. { FPL("c:/aa"), FPL("c:/") },
  94. { FPL("c:/aa/"), FPL("c:/") },
  95. { FPL("c:/aa/bb"), FPL("c:/aa") },
  96. { FPL("c:aa/bb"), FPL("c:aa") },
  97. #endif // FILE_PATH_USES_DRIVE_LETTERS
  98. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  99. { FPL("\\aa\\bb"), FPL("\\aa") },
  100. { FPL("\\aa\\bb\\"), FPL("\\aa") },
  101. { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
  102. { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
  103. { FPL("\\aa"), FPL("\\") },
  104. { FPL("\\aa\\"), FPL("\\") },
  105. { FPL("\\"), FPL("\\") },
  106. { FPL("\\\\"), FPL("\\\\") },
  107. { FPL("\\\\\\"), FPL("\\") },
  108. { FPL("aa\\"), FPL(".") },
  109. { FPL("aa\\bb"), FPL("aa") },
  110. { FPL("aa\\bb\\"), FPL("aa") },
  111. { FPL("aa\\bb\\\\"), FPL("aa") },
  112. { FPL("aa\\\\bb\\\\"), FPL("aa") },
  113. { FPL("aa\\\\bb\\"), FPL("aa") },
  114. { FPL("aa\\\\bb"), FPL("aa") },
  115. { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
  116. { FPL("\\\\aa\\"), FPL("\\\\") },
  117. { FPL("\\\\aa"), FPL("\\\\") },
  118. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  119. { FPL("c:\\"), FPL("c:\\") },
  120. { FPL("c:\\\\"), FPL("c:\\\\") },
  121. { FPL("c:\\\\\\"), FPL("c:\\") },
  122. { FPL("c:\\aa"), FPL("c:\\") },
  123. { FPL("c:\\aa\\"), FPL("c:\\") },
  124. { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
  125. { FPL("c:aa\\bb"), FPL("c:aa") },
  126. #endif // FILE_PATH_USES_DRIVE_LETTERS
  127. #endif // FILE_PATH_USES_WIN_SEPARATORS
  128. };
  129. for (size_t i = 0; i < arraysize(cases); ++i) {
  130. FilePath input(cases[i].input);
  131. FilePath observed = input.DirName();
  132. EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
  133. "i: " << i << ", input: " << input.value();
  134. }
  135. }
  136. TEST_F(FilePathTest, BaseName) {
  137. const struct UnaryTestData cases[] = {
  138. { FPL(""), FPL("") },
  139. { FPL("aa"), FPL("aa") },
  140. { FPL("/aa/bb"), FPL("bb") },
  141. { FPL("/aa/bb/"), FPL("bb") },
  142. { FPL("/aa/bb//"), FPL("bb") },
  143. { FPL("/aa/bb/ccc"), FPL("ccc") },
  144. { FPL("/aa"), FPL("aa") },
  145. { FPL("/"), FPL("/") },
  146. { FPL("//"), FPL("//") },
  147. { FPL("///"), FPL("/") },
  148. { FPL("aa/"), FPL("aa") },
  149. { FPL("aa/bb"), FPL("bb") },
  150. { FPL("aa/bb/"), FPL("bb") },
  151. { FPL("aa/bb//"), FPL("bb") },
  152. { FPL("aa//bb//"), FPL("bb") },
  153. { FPL("aa//bb/"), FPL("bb") },
  154. { FPL("aa//bb"), FPL("bb") },
  155. { FPL("//aa/bb"), FPL("bb") },
  156. { FPL("//aa/"), FPL("aa") },
  157. { FPL("//aa"), FPL("aa") },
  158. { FPL("0:"), FPL("0:") },
  159. { FPL("@:"), FPL("@:") },
  160. { FPL("[:"), FPL("[:") },
  161. { FPL("`:"), FPL("`:") },
  162. { FPL("{:"), FPL("{:") },
  163. { FPL("\xB3:"), FPL("\xB3:") },
  164. { FPL("\xC5:"), FPL("\xC5:") },
  165. #if defined(OS_WIN)
  166. { FPL("\x0143:"), FPL("\x0143:") },
  167. #endif // OS_WIN
  168. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  169. { FPL("c:"), FPL("") },
  170. { FPL("C:"), FPL("") },
  171. { FPL("A:"), FPL("") },
  172. { FPL("Z:"), FPL("") },
  173. { FPL("a:"), FPL("") },
  174. { FPL("z:"), FPL("") },
  175. { FPL("c:aa"), FPL("aa") },
  176. { FPL("c:/"), FPL("/") },
  177. { FPL("c://"), FPL("//") },
  178. { FPL("c:///"), FPL("/") },
  179. { FPL("c:/aa"), FPL("aa") },
  180. { FPL("c:/aa/"), FPL("aa") },
  181. { FPL("c:/aa/bb"), FPL("bb") },
  182. { FPL("c:aa/bb"), FPL("bb") },
  183. #endif // FILE_PATH_USES_DRIVE_LETTERS
  184. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  185. { FPL("\\aa\\bb"), FPL("bb") },
  186. { FPL("\\aa\\bb\\"), FPL("bb") },
  187. { FPL("\\aa\\bb\\\\"), FPL("bb") },
  188. { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
  189. { FPL("\\aa"), FPL("aa") },
  190. { FPL("\\"), FPL("\\") },
  191. { FPL("\\\\"), FPL("\\\\") },
  192. { FPL("\\\\\\"), FPL("\\") },
  193. { FPL("aa\\"), FPL("aa") },
  194. { FPL("aa\\bb"), FPL("bb") },
  195. { FPL("aa\\bb\\"), FPL("bb") },
  196. { FPL("aa\\bb\\\\"), FPL("bb") },
  197. { FPL("aa\\\\bb\\\\"), FPL("bb") },
  198. { FPL("aa\\\\bb\\"), FPL("bb") },
  199. { FPL("aa\\\\bb"), FPL("bb") },
  200. { FPL("\\\\aa\\bb"), FPL("bb") },
  201. { FPL("\\\\aa\\"), FPL("aa") },
  202. { FPL("\\\\aa"), FPL("aa") },
  203. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  204. { FPL("c:\\"), FPL("\\") },
  205. { FPL("c:\\\\"), FPL("\\\\") },
  206. { FPL("c:\\\\\\"), FPL("\\") },
  207. { FPL("c:\\aa"), FPL("aa") },
  208. { FPL("c:\\aa\\"), FPL("aa") },
  209. { FPL("c:\\aa\\bb"), FPL("bb") },
  210. { FPL("c:aa\\bb"), FPL("bb") },
  211. #endif // FILE_PATH_USES_DRIVE_LETTERS
  212. #endif // FILE_PATH_USES_WIN_SEPARATORS
  213. };
  214. for (size_t i = 0; i < arraysize(cases); ++i) {
  215. FilePath input(cases[i].input);
  216. FilePath observed = input.BaseName();
  217. EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
  218. "i: " << i << ", input: " << input.value();
  219. }
  220. }
  221. TEST_F(FilePathTest, Append) {
  222. const struct BinaryTestData cases[] = {
  223. { { FPL(""), FPL("cc") }, FPL("cc") },
  224. { { FPL("."), FPL("ff") }, FPL("ff") },
  225. { { FPL("/"), FPL("cc") }, FPL("/cc") },
  226. { { FPL("/aa"), FPL("") }, FPL("/aa") },
  227. { { FPL("/aa/"), FPL("") }, FPL("/aa") },
  228. { { FPL("//aa"), FPL("") }, FPL("//aa") },
  229. { { FPL("//aa/"), FPL("") }, FPL("//aa") },
  230. { { FPL("//"), FPL("aa") }, FPL("//aa") },
  231. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  232. { { FPL("c:"), FPL("a") }, FPL("c:a") },
  233. { { FPL("c:"), FPL("") }, FPL("c:") },
  234. { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
  235. { { FPL("c://"), FPL("a") }, FPL("c://a") },
  236. { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
  237. #endif // FILE_PATH_USES_DRIVE_LETTERS
  238. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  239. // Append introduces the default separator character, so these test cases
  240. // need to be defined with different expected results on platforms that use
  241. // different default separator characters.
  242. { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
  243. { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
  244. { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
  245. { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
  246. { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
  247. { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
  248. { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
  249. { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
  250. { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
  251. { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
  252. { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
  253. { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
  254. { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
  255. { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
  256. { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
  257. { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
  258. { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
  259. { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
  260. { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
  261. { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
  262. { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
  263. { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
  264. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  265. { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
  266. { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
  267. { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
  268. { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
  269. { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
  270. { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
  271. #endif // FILE_PATH_USES_DRIVE_LETTERS
  272. #else // FILE_PATH_USES_WIN_SEPARATORS
  273. { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
  274. { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
  275. { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
  276. { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
  277. { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
  278. { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
  279. { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
  280. { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
  281. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  282. { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
  283. { { FPL("c:/"), FPL("") }, FPL("c:/") },
  284. { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
  285. { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
  286. #endif // FILE_PATH_USES_DRIVE_LETTERS
  287. #endif // FILE_PATH_USES_WIN_SEPARATORS
  288. };
  289. for (size_t i = 0; i < arraysize(cases); ++i) {
  290. FilePath root(cases[i].inputs[0]);
  291. FilePath::StringType leaf(cases[i].inputs[1]);
  292. FilePath observed_str = root.Append(leaf);
  293. EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
  294. "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
  295. FilePath observed_path = root.Append(FilePath(leaf));
  296. EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
  297. "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
  298. // TODO(erikkay): It would be nice to have a unicode test append value to
  299. // handle the case when AppendASCII is passed UTF8
  300. #if defined(OS_WIN)
  301. std::string ascii = WideToUTF8(leaf);
  302. #elif defined(OS_POSIX)
  303. std::string ascii = leaf;
  304. #endif
  305. observed_str = root.AppendASCII(ascii);
  306. EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
  307. "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
  308. }
  309. }
  310. TEST_F(FilePathTest, StripTrailingSeparators) {
  311. const struct UnaryTestData cases[] = {
  312. { FPL(""), FPL("") },
  313. { FPL("/"), FPL("/") },
  314. { FPL("//"), FPL("//") },
  315. { FPL("///"), FPL("/") },
  316. { FPL("////"), FPL("/") },
  317. { FPL("a/"), FPL("a") },
  318. { FPL("a//"), FPL("a") },
  319. { FPL("a///"), FPL("a") },
  320. { FPL("a////"), FPL("a") },
  321. { FPL("/a"), FPL("/a") },
  322. { FPL("/a/"), FPL("/a") },
  323. { FPL("/a//"), FPL("/a") },
  324. { FPL("/a///"), FPL("/a") },
  325. { FPL("/a////"), FPL("/a") },
  326. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  327. { FPL("c:"), FPL("c:") },
  328. { FPL("c:/"), FPL("c:/") },
  329. { FPL("c://"), FPL("c://") },
  330. { FPL("c:///"), FPL("c:/") },
  331. { FPL("c:////"), FPL("c:/") },
  332. { FPL("c:/a"), FPL("c:/a") },
  333. { FPL("c:/a/"), FPL("c:/a") },
  334. { FPL("c:/a//"), FPL("c:/a") },
  335. { FPL("c:/a///"), FPL("c:/a") },
  336. { FPL("c:/a////"), FPL("c:/a") },
  337. #endif // FILE_PATH_USES_DRIVE_LETTERS
  338. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  339. { FPL("\\"), FPL("\\") },
  340. { FPL("\\\\"), FPL("\\\\") },
  341. { FPL("\\\\\\"), FPL("\\") },
  342. { FPL("\\\\\\\\"), FPL("\\") },
  343. { FPL("a\\"), FPL("a") },
  344. { FPL("a\\\\"), FPL("a") },
  345. { FPL("a\\\\\\"), FPL("a") },
  346. { FPL("a\\\\\\\\"), FPL("a") },
  347. { FPL("\\a"), FPL("\\a") },
  348. { FPL("\\a\\"), FPL("\\a") },
  349. { FPL("\\a\\\\"), FPL("\\a") },
  350. { FPL("\\a\\\\\\"), FPL("\\a") },
  351. { FPL("\\a\\\\\\\\"), FPL("\\a") },
  352. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  353. { FPL("c:\\"), FPL("c:\\") },
  354. { FPL("c:\\\\"), FPL("c:\\\\") },
  355. { FPL("c:\\\\\\"), FPL("c:\\") },
  356. { FPL("c:\\\\\\\\"), FPL("c:\\") },
  357. { FPL("c:\\a"), FPL("c:\\a") },
  358. { FPL("c:\\a\\"), FPL("c:\\a") },
  359. { FPL("c:\\a\\\\"), FPL("c:\\a") },
  360. { FPL("c:\\a\\\\\\"), FPL("c:\\a") },
  361. { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
  362. #endif // FILE_PATH_USES_DRIVE_LETTERS
  363. #endif // FILE_PATH_USES_WIN_SEPARATORS
  364. };
  365. for (size_t i = 0; i < arraysize(cases); ++i) {
  366. FilePath input(cases[i].input);
  367. FilePath observed = input.StripTrailingSeparators();
  368. EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
  369. "i: " << i << ", input: " << input.value();
  370. }
  371. }
  372. TEST_F(FilePathTest, IsAbsolute) {
  373. const struct UnaryBooleanTestData cases[] = {
  374. { FPL(""), false },
  375. { FPL("a"), false },
  376. { FPL("c:"), false },
  377. { FPL("c:a"), false },
  378. { FPL("a/b"), false },
  379. { FPL("//"), true },
  380. { FPL("//a"), true },
  381. { FPL("c:a/b"), false },
  382. { FPL("?:/a"), false },
  383. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  384. { FPL("/"), false },
  385. { FPL("/a"), false },
  386. { FPL("/."), false },
  387. { FPL("/.."), false },
  388. { FPL("c:/"), true },
  389. { FPL("c:/a"), true },
  390. { FPL("c:/."), true },
  391. { FPL("c:/.."), true },
  392. { FPL("C:/a"), true },
  393. { FPL("d:/a"), true },
  394. #else // FILE_PATH_USES_DRIVE_LETTERS
  395. { FPL("/"), true },
  396. { FPL("/a"), true },
  397. { FPL("/."), true },
  398. { FPL("/.."), true },
  399. { FPL("c:/"), false },
  400. #endif // FILE_PATH_USES_DRIVE_LETTERS
  401. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  402. { FPL("a\\b"), false },
  403. { FPL("\\\\"), true },
  404. { FPL("\\\\a"), true },
  405. { FPL("a\\b"), false },
  406. { FPL("\\\\"), true },
  407. { FPL("//a"), true },
  408. { FPL("c:a\\b"), false },
  409. { FPL("?:\\a"), false },
  410. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  411. { FPL("\\"), false },
  412. { FPL("\\a"), false },
  413. { FPL("\\."), false },
  414. { FPL("\\.."), false },
  415. { FPL("c:\\"), true },
  416. { FPL("c:\\"), true },
  417. { FPL("c:\\a"), true },
  418. { FPL("c:\\."), true },
  419. { FPL("c:\\.."), true },
  420. { FPL("C:\\a"), true },
  421. { FPL("d:\\a"), true },
  422. #else // FILE_PATH_USES_DRIVE_LETTERS
  423. { FPL("\\"), true },
  424. { FPL("\\a"), true },
  425. { FPL("\\."), true },
  426. { FPL("\\.."), true },
  427. { FPL("c:\\"), false },
  428. #endif // FILE_PATH_USES_DRIVE_LETTERS
  429. #endif // FILE_PATH_USES_WIN_SEPARATORS
  430. };
  431. for (size_t i = 0; i < arraysize(cases); ++i) {
  432. FilePath input(cases[i].input);
  433. bool observed = input.IsAbsolute();
  434. EXPECT_EQ(cases[i].expected, observed) <<
  435. "i: " << i << ", input: " << input.value();
  436. }
  437. }
  438. TEST_F(FilePathTest, PathComponentsTest) {
  439. const struct UnaryTestData cases[] = {
  440. { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
  441. { FPL("///"), FPL("|/")},
  442. { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
  443. { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
  444. { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
  445. { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
  446. { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
  447. { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
  448. { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
  449. { FPL("/"), FPL("|/")},
  450. { FPL("foo"), FPL("|foo")},
  451. { FPL(""), FPL("")},
  452. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  453. { FPL("e:/foo"), FPL("|e:|/|foo")},
  454. { FPL("e:/"), FPL("|e:|/")},
  455. { FPL("e:"), FPL("|e:")},
  456. #endif // FILE_PATH_USES_DRIVE_LETTERS
  457. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  458. { FPL("../foo"), FPL("|..|foo")},
  459. { FPL("./foo"), FPL("|foo")},
  460. { FPL("../foo/bar/"), FPL("|..|foo|bar") },
  461. { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
  462. { FPL("\\\\\\"), FPL("|\\")},
  463. { FPL("\\foo\\\\bar\\\\baz\\"), FPL("|\\|foo|bar|baz")},
  464. { FPL("\\foo\\bar\\baz\\"), FPL("|\\|foo|bar|baz")},
  465. { FPL("\\foo\\bar\\baz\\\\"), FPL("|\\|foo|bar|baz")},
  466. { FPL("\\foo\\bar\\baz\\\\\\"), FPL("|\\|foo|bar|baz")},
  467. { FPL("\\foo\\bar\\baz"), FPL("|\\|foo|bar|baz")},
  468. { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
  469. { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
  470. { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
  471. { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
  472. { FPL("\\"), FPL("|\\")},
  473. #endif // FILE_PATH_USES_WIN_SEPARATORS
  474. };
  475. for (size_t i = 0; i < arraysize(cases); ++i) {
  476. FilePath input(cases[i].input);
  477. std::vector<FilePath::StringType> comps;
  478. input.GetComponents(&comps);
  479. FilePath::StringType observed;
  480. for (size_t j = 0; j < comps.size(); ++j) {
  481. observed.append(FILE_PATH_LITERAL("|"), 1);
  482. observed.append(comps[j]);
  483. }
  484. EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
  485. "i: " << i << ", input: " << input.value();
  486. }
  487. }
  488. TEST_F(FilePathTest, IsParentTest) {
  489. const struct BinaryBooleanTestData cases[] = {
  490. { { FPL("/"), FPL("/foo/bar/baz") }, true},
  491. { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
  492. { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
  493. { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
  494. { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
  495. { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
  496. { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
  497. { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
  498. { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
  499. { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
  500. { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
  501. { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
  502. { { FPL(""), FPL("foo") }, false},
  503. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  504. { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
  505. { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
  506. { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
  507. { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
  508. { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
  509. { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
  510. { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
  511. { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
  512. { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
  513. { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
  514. { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
  515. { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
  516. { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
  517. { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
  518. #endif // FILE_PATH_USES_DRIVE_LETTERS
  519. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  520. { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
  521. { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
  522. { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
  523. { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
  524. { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
  525. { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
  526. { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
  527. #endif // FILE_PATH_USES_WIN_SEPARATORS
  528. };
  529. for (size_t i = 0; i < arraysize(cases); ++i) {
  530. FilePath parent(cases[i].inputs[0]);
  531. FilePath child(cases[i].inputs[1]);
  532. EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
  533. "i: " << i << ", parent: " << parent.value() << ", child: " <<
  534. child.value();
  535. }
  536. }
  537. TEST_F(FilePathTest, AppendRelativePathTest) {
  538. const struct BinaryTestData cases[] = {
  539. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  540. { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
  541. #else // FILE_PATH_USES_WIN_SEPARATORS
  542. { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
  543. #endif // FILE_PATH_USES_WIN_SEPARATORS
  544. { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
  545. { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
  546. { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
  547. { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
  548. { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
  549. { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
  550. { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
  551. { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
  552. { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
  553. { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
  554. { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
  555. { { FPL(""), FPL("foo") }, FPL("")},
  556. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  557. { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
  558. { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
  559. { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
  560. { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
  561. { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
  562. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  563. { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
  564. // TODO(akalin): Figure out how to handle the corner case in the
  565. // commented-out test case below. Appending to an empty path gives
  566. // /foo\bar\baz but appending to a nonempty path "blah" gives
  567. // blah\foo\bar\baz.
  568. // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
  569. #endif // FILE_PATH_USES_WIN_SEPARATORS
  570. { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
  571. { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, FPL("")},
  572. { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
  573. { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
  574. { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
  575. { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
  576. { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
  577. #endif // FILE_PATH_USES_DRIVE_LETTERS
  578. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  579. { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
  580. { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
  581. { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
  582. { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
  583. { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
  584. { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
  585. { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
  586. #endif // FILE_PATH_USES_WIN_SEPARATORS
  587. };
  588. const FilePath base(FPL("blah"));
  589. for (size_t i = 0; i < arraysize(cases); ++i) {
  590. FilePath parent(cases[i].inputs[0]);
  591. FilePath child(cases[i].inputs[1]);
  592. {
  593. FilePath result;
  594. bool success = parent.AppendRelativePath(child, &result);
  595. EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
  596. "i: " << i << ", parent: " << parent.value() << ", child: " <<
  597. child.value();
  598. EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
  599. "i: " << i << ", parent: " << parent.value() << ", child: " <<
  600. child.value();
  601. }
  602. {
  603. FilePath result(base);
  604. bool success = parent.AppendRelativePath(child, &result);
  605. EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
  606. "i: " << i << ", parent: " << parent.value() << ", child: " <<
  607. child.value();
  608. EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
  609. "i: " << i << ", parent: " << parent.value() << ", child: " <<
  610. child.value();
  611. }
  612. }
  613. }
  614. TEST_F(FilePathTest, EqualityTest) {
  615. const struct BinaryBooleanTestData cases[] = {
  616. { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
  617. { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
  618. { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
  619. { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
  620. { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
  621. { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
  622. { { FPL("foo/bar"), FPL("foo/bar") }, true},
  623. { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
  624. { { FPL(""), FPL("foo") }, false},
  625. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  626. { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
  627. { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
  628. { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
  629. { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
  630. { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
  631. { { FPL("c:/"), FPL("c:/") }, true},
  632. { { FPL("c:"), FPL("c:") }, true},
  633. { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false},
  634. { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false},
  635. { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false},
  636. { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
  637. #endif // FILE_PATH_USES_DRIVE_LETTERS
  638. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  639. { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
  640. { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
  641. { { FPL("\\foo/bar"), FPL("\\foo\\bar") }, false},
  642. { { FPL("\\"), FPL("\\") }, true},
  643. { { FPL("\\"), FPL("/") }, false},
  644. { { FPL(""), FPL("\\") }, false},
  645. { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
  646. { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
  647. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  648. { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
  649. { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
  650. { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
  651. #endif // FILE_PATH_USES_DRIVE_LETTERS
  652. #endif // FILE_PATH_USES_WIN_SEPARATORS
  653. };
  654. for (size_t i = 0; i < arraysize(cases); ++i) {
  655. FilePath a(cases[i].inputs[0]);
  656. FilePath b(cases[i].inputs[1]);
  657. EXPECT_EQ(a == b, cases[i].expected) <<
  658. "equality i: " << i << ", a: " << a.value() << ", b: " <<
  659. b.value();
  660. }
  661. for (size_t i = 0; i < arraysize(cases); ++i) {
  662. FilePath a(cases[i].inputs[0]);
  663. FilePath b(cases[i].inputs[1]);
  664. EXPECT_EQ(a != b, !cases[i].expected) <<
  665. "inequality i: " << i << ", a: " << a.value() << ", b: " <<
  666. b.value();
  667. }
  668. }
  669. TEST_F(FilePathTest, Extension) {
  670. FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
  671. FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
  672. EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
  673. EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.FinalExtension());
  674. FilePath base = jpg.BaseName().RemoveExtension();
  675. EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value());
  676. FilePath path_no_ext = base_dir.Append(base);
  677. EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value());
  678. EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
  679. EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension());
  680. EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.FinalExtension());
  681. }
  682. TEST_F(FilePathTest, Extension2) {
  683. const struct UnaryTestData cases[] = {
  684. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  685. { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
  686. { FPL("C:\\a\\b\\c."), FPL(".") },
  687. { FPL("C:\\a\\b\\c"), FPL("") },
  688. { FPL("C:\\a\\b\\"), FPL("") },
  689. { FPL("C:\\a\\b.\\"), FPL(".") },
  690. { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
  691. { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
  692. { FPL("C:\\foo.bar\\.."), FPL("") },
  693. { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
  694. #endif
  695. { FPL("/foo/bar/baz.ext"), FPL(".ext") },
  696. { FPL("/foo/bar/baz."), FPL(".") },
  697. { FPL("/foo/bar/baz.."), FPL(".") },
  698. { FPL("/foo/bar/baz"), FPL("") },
  699. { FPL("/foo/bar/"), FPL("") },
  700. { FPL("/foo/bar./"), FPL(".") },
  701. { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
  702. { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
  703. { FPL("/foo.12345.gz"), FPL(".gz") },
  704. { FPL("/foo..gz"), FPL(".gz") },
  705. { FPL("."), FPL("") },
  706. { FPL(".."), FPL("") },
  707. { FPL("./foo"), FPL("") },
  708. { FPL("./foo.ext"), FPL(".ext") },
  709. { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
  710. { FPL("/foo.bar////"), FPL(".bar") },
  711. { FPL("/foo.bar/.."), FPL("") },
  712. { FPL("/foo.bar/..////"), FPL("") },
  713. { FPL("/foo.1234.luser.js"), FPL(".js") },
  714. { FPL("/user.js"), FPL(".js") },
  715. };
  716. const struct UnaryTestData double_extension_cases[] = {
  717. { FPL("/foo.tar.gz"), FPL(".tar.gz") },
  718. { FPL("/foo.tar.Z"), FPL(".tar.Z") },
  719. { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
  720. { FPL("/foo.1234.gz"), FPL(".1234.gz") },
  721. { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
  722. { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
  723. { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
  724. { FPL("/foo.1234.user.js"), FPL(".user.js") },
  725. { FPL("foo.user.js"), FPL(".user.js") },
  726. };
  727. for (unsigned int i = 0; i < arraysize(cases); ++i) {
  728. FilePath path(cases[i].input);
  729. FilePath::StringType extension = path.Extension();
  730. FilePath::StringType final_extension = path.FinalExtension();
  731. EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
  732. ", path: " << path.value();
  733. EXPECT_STREQ(cases[i].expected, final_extension.c_str()) << "i: " << i <<
  734. ", path: " << path.value();
  735. }
  736. for (unsigned int i = 0; i < arraysize(double_extension_cases); ++i) {
  737. FilePath path(cases[i].input);
  738. FilePath::StringType extension = path.Extension();
  739. EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
  740. ", path: " << path.value();
  741. }
  742. }
  743. TEST_F(FilePathTest, InsertBeforeExtension) {
  744. const struct BinaryTestData cases[] = {
  745. { { FPL(""), FPL("") }, FPL("") },
  746. { { FPL(""), FPL("txt") }, FPL("") },
  747. { { FPL("."), FPL("txt") }, FPL("") },
  748. { { FPL(".."), FPL("txt") }, FPL("") },
  749. { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
  750. { { FPL("."), FPL("") }, FPL(".") },
  751. { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
  752. { { FPL("foo"), FPL("txt") }, FPL("footxt") },
  753. { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
  754. { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
  755. { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
  756. { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
  757. { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
  758. { { FPL("foo"), FPL("") }, FPL("foo") },
  759. { { FPL("foo"), FPL(".") }, FPL("foo.") },
  760. { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
  761. { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
  762. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  763. { { FPL("\\"), FPL("") }, FPL("\\") },
  764. { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
  765. { { FPL("\\."), FPL("txt") }, FPL("") },
  766. { { FPL("\\.."), FPL("txt") }, FPL("") },
  767. { { FPL("\\."), FPL("") }, FPL("\\.") },
  768. { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
  769. FPL("C:\\bar\\footxt.dll") },
  770. { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
  771. FPL("C:\\bar.baz\\foodlltxt") },
  772. { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
  773. FPL("C:\\bar.baz\\footxt.dll") },
  774. { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
  775. FPL("C:\\bar.baz\\foo.dlltxt.exe") },
  776. { { FPL("C:\\bar.baz\\foo"), FPL("") },
  777. FPL("C:\\bar.baz\\foo") },
  778. { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
  779. FPL("C:\\bar.baz\\foo.exe") },
  780. { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
  781. FPL("C:\\bar.baz\\foo.dll.exe") },
  782. { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
  783. FPL("C:\\bar\\baz\\foo (1).exe") },
  784. { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
  785. { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
  786. #endif
  787. { { FPL("/"), FPL("") }, FPL("/") },
  788. { { FPL("/"), FPL("txt") }, FPL("/txt") },
  789. { { FPL("/."), FPL("txt") }, FPL("") },
  790. { { FPL("/.."), FPL("txt") }, FPL("") },
  791. { { FPL("/."), FPL("") }, FPL("/.") },
  792. { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
  793. { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
  794. { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
  795. { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
  796. FPL("/bar.baz/foo.dlltxt.exe") },
  797. { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
  798. { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
  799. { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
  800. { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
  801. { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
  802. };
  803. for (unsigned int i = 0; i < arraysize(cases); ++i) {
  804. FilePath path(cases[i].inputs[0]);
  805. FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
  806. EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
  807. ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
  808. }
  809. }
  810. TEST_F(FilePathTest, RemoveExtension) {
  811. const struct UnaryTestData cases[] = {
  812. { FPL(""), FPL("") },
  813. { FPL("."), FPL(".") },
  814. { FPL(".."), FPL("..") },
  815. { FPL("foo.dll"), FPL("foo") },
  816. { FPL("./foo.dll"), FPL("./foo") },
  817. { FPL("foo..dll"), FPL("foo.") },
  818. { FPL("foo"), FPL("foo") },
  819. { FPL("foo."), FPL("foo") },
  820. { FPL("foo.."), FPL("foo.") },
  821. { FPL("foo.baz.dll"), FPL("foo.baz") },
  822. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  823. { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
  824. { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
  825. #endif
  826. { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
  827. { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
  828. };
  829. for (unsigned int i = 0; i < arraysize(cases); ++i) {
  830. FilePath path(cases[i].input);
  831. FilePath removed = path.RemoveExtension();
  832. FilePath removed_final = path.RemoveFinalExtension();
  833. EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
  834. ", path: " << path.value();
  835. EXPECT_EQ(cases[i].expected, removed_final.value()) << "i: " << i <<
  836. ", path: " << path.value();
  837. }
  838. {
  839. FilePath path(FPL("foo.tar.gz"));
  840. FilePath removed = path.RemoveExtension();
  841. FilePath removed_final = path.RemoveFinalExtension();
  842. EXPECT_EQ(FPL("foo"), removed.value()) << ", path: " << path.value();
  843. EXPECT_EQ(FPL("foo.tar"), removed_final.value()) << ", path: "
  844. << path.value();
  845. }
  846. }
  847. TEST_F(FilePathTest, ReplaceExtension) {
  848. const struct BinaryTestData cases[] = {
  849. { { FPL(""), FPL("") }, FPL("") },
  850. { { FPL(""), FPL("txt") }, FPL("") },
  851. { { FPL("."), FPL("txt") }, FPL("") },
  852. { { FPL(".."), FPL("txt") }, FPL("") },
  853. { { FPL("."), FPL("") }, FPL("") },
  854. { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
  855. { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
  856. { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
  857. { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
  858. { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
  859. { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
  860. { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
  861. { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
  862. { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
  863. { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
  864. { { FPL("foo.dll"), FPL("") }, FPL("foo") },
  865. { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
  866. { { FPL("foo"), FPL("") }, FPL("foo") },
  867. { { FPL("foo"), FPL(".") }, FPL("foo") },
  868. { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
  869. { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
  870. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  871. { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
  872. { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
  873. #endif
  874. { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
  875. { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
  876. };
  877. for (unsigned int i = 0; i < arraysize(cases); ++i) {
  878. FilePath path(cases[i].inputs[0]);
  879. FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
  880. EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
  881. ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
  882. }
  883. }
  884. TEST_F(FilePathTest, AddExtension) {
  885. const struct BinaryTestData cases[] = {
  886. { { FPL(""), FPL("") }, FPL("") },
  887. { { FPL(""), FPL("txt") }, FPL("") },
  888. { { FPL("."), FPL("txt") }, FPL("") },
  889. { { FPL(".."), FPL("txt") }, FPL("") },
  890. { { FPL("."), FPL("") }, FPL("") },
  891. { { FPL("foo.dll"), FPL("txt") }, FPL("foo.dll.txt") },
  892. { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.dll.txt") },
  893. { { FPL("foo..dll"), FPL("txt") }, FPL("foo..dll.txt") },
  894. { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.dll.txt") },
  895. { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
  896. { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
  897. { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
  898. { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
  899. { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.dll.txt") },
  900. { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.dll.txt") },
  901. { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
  902. { { FPL("foo.dll"), FPL(".") }, FPL("foo.dll") },
  903. { { FPL("foo"), FPL("") }, FPL("foo") },
  904. { { FPL("foo"), FPL(".") }, FPL("foo") },
  905. { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
  906. { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz.dll") },
  907. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  908. { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
  909. { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
  910. #endif
  911. { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
  912. { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
  913. };
  914. for (unsigned int i = 0; i < arraysize(cases); ++i) {
  915. FilePath path(cases[i].inputs[0]);
  916. FilePath added = path.AddExtension(cases[i].inputs[1]);
  917. EXPECT_EQ(cases[i].expected, added.value()) << "i: " << i <<
  918. ", path: " << path.value() << ", add: " << cases[i].inputs[1];
  919. }
  920. }
  921. TEST_F(FilePathTest, MatchesExtension) {
  922. const struct BinaryBooleanTestData cases[] = {
  923. { { FPL("foo"), FPL("") }, true},
  924. { { FPL("foo"), FPL(".") }, false},
  925. { { FPL("foo."), FPL("") }, false},
  926. { { FPL("foo."), FPL(".") }, true},
  927. { { FPL("foo.txt"), FPL(".dll") }, false},
  928. { { FPL("foo.txt"), FPL(".txt") }, true},
  929. { { FPL("foo.txt.dll"), FPL(".txt") }, false},
  930. { { FPL("foo.txt.dll"), FPL(".dll") }, true},
  931. { { FPL("foo.TXT"), FPL(".txt") }, true},
  932. { { FPL("foo.txt"), FPL(".TXT") }, true},
  933. { { FPL("foo.tXt"), FPL(".txt") }, true},
  934. { { FPL("foo.txt"), FPL(".tXt") }, true},
  935. { { FPL("foo.tXt"), FPL(".TXT") }, true},
  936. { { FPL("foo.tXt"), FPL(".tXt") }, true},
  937. #if defined(FILE_PATH_USES_DRIVE_LETTERS)
  938. { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
  939. { { FPL("c:/foo.txt"), FPL(".txt") }, true},
  940. #endif // FILE_PATH_USES_DRIVE_LETTERS
  941. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  942. { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
  943. { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
  944. #endif // FILE_PATH_USES_DRIVE_LETTERS
  945. { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
  946. { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
  947. #if defined(OS_WIN) || defined(OS_MACOSX)
  948. // Umlauts A, O, U: direct comparison, and upper case vs. lower case
  949. { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
  950. { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
  951. // C with circumflex: direct comparison, and upper case vs. lower case
  952. { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
  953. { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
  954. #endif
  955. };
  956. for (size_t i = 0; i < arraysize(cases); ++i) {
  957. FilePath path(cases[i].inputs[0]);
  958. FilePath::StringType ext(cases[i].inputs[1]);
  959. EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
  960. "i: " << i << ", path: " << path.value() << ", ext: " << ext;
  961. }
  962. }
  963. TEST_F(FilePathTest, CompareIgnoreCase) {
  964. const struct BinaryIntTestData cases[] = {
  965. { { FPL("foo"), FPL("foo") }, 0},
  966. { { FPL("FOO"), FPL("foo") }, 0},
  967. { { FPL("foo.ext"), FPL("foo.ext") }, 0},
  968. { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
  969. { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
  970. { { FPL("foO"), FPL("foo") }, 0},
  971. { { FPL("foo"), FPL("foO") }, 0},
  972. { { FPL("fOo"), FPL("foo") }, 0},
  973. { { FPL("foo"), FPL("fOo") }, 0},
  974. { { FPL("bar"), FPL("foo") }, -1},
  975. { { FPL("foo"), FPL("bar") }, 1},
  976. { { FPL("BAR"), FPL("foo") }, -1},
  977. { { FPL("FOO"), FPL("bar") }, 1},
  978. { { FPL("bar"), FPL("FOO") }, -1},
  979. { { FPL("foo"), FPL("BAR") }, 1},
  980. { { FPL("BAR"), FPL("FOO") }, -1},
  981. { { FPL("FOO"), FPL("BAR") }, 1},
  982. // German "Eszett" (lower case and the new-fangled upper case)
  983. // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
  984. // However, neither Windows nor Mac OSX converts these.
  985. // (or even have glyphs for <uppercase eszett>)
  986. { { FPL("\u00DF"), FPL("\u00DF") }, 0},
  987. { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
  988. { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
  989. { { FPL("SS"), FPL("\u00DF") }, -1},
  990. { { FPL("SS"), FPL("\u1E9E") }, -1},
  991. #if defined(OS_WIN) || defined(OS_MACOSX)
  992. // Umlauts A, O, U: direct comparison, and upper case vs. lower case
  993. { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
  994. { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
  995. // C with circumflex: direct comparison, and upper case vs. lower case
  996. { { FPL("\u0109"), FPL("\u0109") }, 0},
  997. { { FPL("\u0108"), FPL("\u0109") }, 0},
  998. // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
  999. { { FPL("\u0428"), FPL("\u0428") }, 0},
  1000. { { FPL("\u0428"), FPL("\u0448") }, 0},
  1001. // Greek letter DELTA: direct comparison, and upper case vs. lower case
  1002. { { FPL("\u0394"), FPL("\u0394") }, 0},
  1003. { { FPL("\u0394"), FPL("\u03B4") }, 0},
  1004. // Japanese full-width A: direct comparison, and upper case vs. lower case
  1005. // Note that full-width and standard characters are considered different.
  1006. { { FPL("\uFF21"), FPL("\uFF21") }, 0},
  1007. { { FPL("\uFF21"), FPL("\uFF41") }, 0},
  1008. { { FPL("A"), FPL("\uFF21") }, -1},
  1009. { { FPL("A"), FPL("\uFF41") }, -1},
  1010. { { FPL("a"), FPL("\uFF21") }, -1},
  1011. { { FPL("a"), FPL("\uFF41") }, -1},
  1012. #endif
  1013. #if defined(OS_MACOSX)
  1014. // Codepoints > 0x1000
  1015. // Georgian letter DON: direct comparison, and upper case vs. lower case
  1016. { { FPL("\u10A3"), FPL("\u10A3") }, 0},
  1017. { { FPL("\u10A3"), FPL("\u10D3") }, 0},
  1018. // Combining characters vs. pre-composed characters, upper and lower case
  1019. { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
  1020. { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
  1021. { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
  1022. { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
  1023. { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
  1024. { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
  1025. { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
  1026. { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
  1027. { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
  1028. #endif
  1029. };
  1030. for (size_t i = 0; i < arraysize(cases); ++i) {
  1031. FilePath::StringType s1(cases[i].inputs[0]);
  1032. FilePath::StringType s2(cases[i].inputs[1]);
  1033. int result = FilePath::CompareIgnoreCase(s1, s2);
  1034. EXPECT_EQ(cases[i].expected, result) <<
  1035. "i: " << i << ", s1: " << s1 << ", s2: " << s2;
  1036. }
  1037. }
  1038. TEST_F(FilePathTest, ReferencesParent) {
  1039. const struct UnaryBooleanTestData cases[] = {
  1040. { FPL("."), false },
  1041. { FPL(".."), true },
  1042. { FPL(".. "), true },
  1043. { FPL(" .."), true },
  1044. { FPL("..."), true },
  1045. { FPL("a.."), false },
  1046. { FPL("..a"), false },
  1047. { FPL("../"), true },
  1048. { FPL("/.."), true },
  1049. { FPL("/../"), true },
  1050. { FPL("/a../"), false },
  1051. { FPL("/..a/"), false },
  1052. { FPL("//.."), true },
  1053. { FPL("..//"), true },
  1054. { FPL("//..//"), true },
  1055. { FPL("a//..//c"), true },
  1056. { FPL("../b/c"), true },
  1057. { FPL("/../b/c"), true },
  1058. { FPL("a/b/.."), true },
  1059. { FPL("a/b/../"), true },
  1060. { FPL("a/../c"), true },
  1061. { FPL("a/b/c"), false },
  1062. };
  1063. for (size_t i = 0; i < arraysize(cases); ++i) {
  1064. FilePath input(cases[i].input);
  1065. bool observed = input.ReferencesParent();
  1066. EXPECT_EQ(cases[i].expected, observed) <<
  1067. "i: " << i << ", input: " << input.value();
  1068. }
  1069. }
  1070. // FIXME(gejun): locale-related code does not work yet(on my testing machine)
  1071. /*
  1072. TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
  1073. const struct UTF8TestData cases[] = {
  1074. { FPL("foo.txt"), "foo.txt" },
  1075. // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
  1076. { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
  1077. // Full-width "ABC".
  1078. { FPL("\uFF21\uFF22\uFF23.txt"),
  1079. "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
  1080. };
  1081. for (size_t i = 0; i < arraysize(cases); ++i) {
  1082. // Test FromUTF8Unsafe() works.
  1083. FilePath from_utf8 = FilePath::FromUTF8Unsafe(cases[i].utf8);
  1084. EXPECT_EQ(cases[i].native, from_utf8.value())
  1085. << "i: " << i << ", input: " << cases[i].native;
  1086. // Test AsUTF8Unsafe() works.
  1087. FilePath from_native = FilePath(cases[i].native);
  1088. EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
  1089. << "i: " << i << ", input: " << cases[i].native;
  1090. // Test the two file paths are identical.
  1091. EXPECT_EQ(from_utf8.value(), from_native.value());
  1092. }
  1093. }
  1094. */
  1095. TEST_F(FilePathTest, ConstructWithNUL) {
  1096. // Assert FPS() works.
  1097. ASSERT_EQ(3U, FPS("a\0b").length());
  1098. // Test constructor strips '\0'
  1099. FilePath path(FPS("a\0b"));
  1100. EXPECT_EQ(1U, path.value().length());
  1101. EXPECT_EQ(FPL("a"), path.value());
  1102. }
  1103. TEST_F(FilePathTest, AppendWithNUL) {
  1104. // Assert FPS() works.
  1105. ASSERT_EQ(3U, FPS("b\0b").length());
  1106. // Test Append() strips '\0'
  1107. FilePath path(FPL("a"));
  1108. path = path.Append(FPS("b\0b"));
  1109. EXPECT_EQ(3U, path.value().length());
  1110. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  1111. EXPECT_EQ(FPL("a\\b"), path.value());
  1112. #else
  1113. EXPECT_EQ(FPL("a/b"), path.value());
  1114. #endif
  1115. }
  1116. TEST_F(FilePathTest, ReferencesParentWithNUL) {
  1117. // Assert FPS() works.
  1118. ASSERT_EQ(3U, FPS("..\0").length());
  1119. // Test ReferencesParent() doesn't break with "..\0"
  1120. FilePath path(FPS("..\0"));
  1121. EXPECT_TRUE(path.ReferencesParent());
  1122. }
  1123. #if defined(FILE_PATH_USES_WIN_SEPARATORS)
  1124. TEST_F(FilePathTest, NormalizePathSeparators) {
  1125. const struct UnaryTestData cases[] = {
  1126. { FPL("foo/bar"), FPL("foo\\bar") },
  1127. { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
  1128. { FPL("foo\\bar"), FPL("foo\\bar") },
  1129. { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
  1130. { FPL("foo"), FPL("foo") },
  1131. // Trailing slashes don't automatically get stripped. That's what
  1132. // StripTrailingSeparators() is for.
  1133. { FPL("foo\\"), FPL("foo\\") },
  1134. { FPL("foo/"), FPL("foo\\") },
  1135. { FPL("foo/bar\\"), FPL("foo\\bar\\") },
  1136. { FPL("foo\\bar/"), FPL("foo\\bar\\") },
  1137. { FPL("foo/bar/"), FPL("foo\\bar\\") },
  1138. { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
  1139. { FPL("\\foo/bar"), FPL("\\foo\\bar") },
  1140. { FPL("/foo\\bar"), FPL("\\foo\\bar") },
  1141. { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
  1142. { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
  1143. { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
  1144. { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
  1145. { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
  1146. { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
  1147. { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
  1148. // This method does not normalize the number of path separators.
  1149. { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
  1150. { FPL("foo//bar"), FPL("foo\\\\bar") },
  1151. { FPL("foo/\\bar"), FPL("foo\\\\bar") },
  1152. { FPL("foo\\/bar"), FPL("foo\\\\bar") },
  1153. { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
  1154. { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
  1155. { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
  1156. { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
  1157. };
  1158. for (size_t i = 0; i < arraysize(cases); ++i) {
  1159. FilePath input(cases[i].input);
  1160. FilePath observed = input.NormalizePathSeparators();
  1161. EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
  1162. "i: " << i << ", input: " << input.value();
  1163. }
  1164. }
  1165. #endif
  1166. TEST_F(FilePathTest, EndsWithSeparator) {
  1167. const UnaryBooleanTestData cases[] = {
  1168. { FPL(""), false },
  1169. { FPL("/"), true },
  1170. { FPL("foo/"), true },
  1171. { FPL("bar"), false },
  1172. { FPL("/foo/bar"), false },
  1173. };
  1174. for (size_t i = 0; i < arraysize(cases); ++i) {
  1175. FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
  1176. EXPECT_EQ(cases[i].expected, input.EndsWithSeparator());
  1177. }
  1178. }
  1179. TEST_F(FilePathTest, AsEndingWithSeparator) {
  1180. const UnaryTestData cases[] = {
  1181. { FPL(""), FPL("") },
  1182. { FPL("/"), FPL("/") },
  1183. { FPL("foo"), FPL("foo/") },
  1184. { FPL("foo/"), FPL("foo/") }
  1185. };
  1186. for (size_t i = 0; i < arraysize(cases); ++i) {
  1187. FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
  1188. FilePath expected = FilePath(cases[i].expected).NormalizePathSeparators();
  1189. EXPECT_EQ(expected.value(), input.AsEndingWithSeparator().value());
  1190. }
  1191. }
  1192. #if defined(OS_ANDROID)
  1193. TEST_F(FilePathTest, ContentUriTest) {
  1194. const struct UnaryBooleanTestData cases[] = {
  1195. { FPL("content://foo.bar"), true },
  1196. { FPL("content://foo.bar/"), true },
  1197. { FPL("content://foo/bar"), true },
  1198. { FPL("CoNTenT://foo.bar"), true },
  1199. { FPL("content://"), true },
  1200. { FPL("content:///foo.bar"), true },
  1201. { FPL("content://3foo/bar"), true },
  1202. { FPL("content://_foo/bar"), true },
  1203. { FPL(".. "), false },
  1204. { FPL("foo.bar"), false },
  1205. { FPL("content:foo.bar"), false },
  1206. { FPL("content:/foo.ba"), false },
  1207. { FPL("content:/dir/foo.bar"), false },
  1208. { FPL("content: //foo.bar"), false },
  1209. { FPL("content%2a%2f%2f"), false },
  1210. };
  1211. for (size_t i = 0; i < arraysize(cases); ++i) {
  1212. FilePath input(cases[i].input);
  1213. bool observed = input.IsContentUri();
  1214. EXPECT_EQ(cases[i].expected, observed) <<
  1215. "i: " << i << ", input: " << input.value();
  1216. }
  1217. }
  1218. #endif
  1219. } // namespace butil