You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

364 lines
14 KiB

  1. #include <config.h>
  2. #include <apt-pkg/fileutl.h>
  3. #include <apt-pkg/strutl.h>
  4. #include <string>
  5. #include <vector>
  6. #include <gtest/gtest.h>
  7. #include "file-helpers.h"
  8. TEST(StrUtilTest,DeEscapeString)
  9. {
  10. // nothing special
  11. EXPECT_EQ("", DeEscapeString(""));
  12. EXPECT_EQ("foobar", DeEscapeString("foobar"));
  13. // hex and octal
  14. EXPECT_EQ("foo bar\nbaz", DeEscapeString("foo\\040bar\\x0abaz"));
  15. EXPECT_EQ("foo ", DeEscapeString("foo\\040"));
  16. EXPECT_EQ("\nbaz", DeEscapeString("\\x0abaz"));
  17. EXPECT_EQ("/media/Ubuntu 11.04 amd64", DeEscapeString("/media/Ubuntu\\04011.04\\040amd64"));
  18. // double slashes
  19. EXPECT_EQ("foo\\ x", DeEscapeString("foo\\\\ x"));
  20. EXPECT_EQ("\\foo\\", DeEscapeString("\\\\foo\\\\"));
  21. }
  22. TEST(StrUtilTest,StringStrip)
  23. {
  24. EXPECT_EQ("", APT::String::Strip(""));
  25. EXPECT_EQ("foobar", APT::String::Strip("foobar"));
  26. EXPECT_EQ("foo bar", APT::String::Strip("foo bar"));
  27. EXPECT_EQ("", APT::String::Strip(" "));
  28. EXPECT_EQ("", APT::String::Strip(" \r\n \t "));
  29. EXPECT_EQ("foo bar", APT::String::Strip("foo bar "));
  30. EXPECT_EQ("foo bar", APT::String::Strip("foo bar \r\n \t "));
  31. EXPECT_EQ("foo bar", APT::String::Strip("\r\n \t foo bar"));
  32. EXPECT_EQ("bar foo", APT::String::Strip("\r\n \t bar foo \r\n \t "));
  33. EXPECT_EQ("bar \t\r\n foo", APT::String::Strip("\r\n \t bar \t\r\n foo \r\n \t "));
  34. }
  35. TEST(StrUtilTest,StringSplitBasic)
  36. {
  37. std::vector<std::string> result = StringSplit("", "");
  38. EXPECT_TRUE(result.empty());
  39. result = StringSplit("abc", "");
  40. EXPECT_TRUE(result.empty());
  41. result = StringSplit("", "abc");
  42. EXPECT_EQ(result.size(), 1u);
  43. result = StringSplit("abc", "b");
  44. ASSERT_EQ(result.size(), 2u);
  45. EXPECT_EQ(result[0], "a");
  46. EXPECT_EQ(result[1], "c");
  47. result = StringSplit("abc", "abc");
  48. ASSERT_EQ(result.size(), 2u);
  49. EXPECT_EQ(result[0], "");
  50. EXPECT_EQ(result[1], "");
  51. }
  52. TEST(StrUtilTest,StringSplitDpkgStatus)
  53. {
  54. std::string const input = "status: libnet1:amd64: unpacked";
  55. std::vector<std::string> result = StringSplit(input, "xxx");
  56. ASSERT_EQ(result.size(), 1u);
  57. EXPECT_EQ(result[0], input);
  58. result = StringSplit(input, "");
  59. EXPECT_TRUE(result.empty());
  60. result = StringSplit(input, ": ");
  61. ASSERT_EQ(result.size(), 3u);
  62. EXPECT_EQ(result[0], "status");
  63. EXPECT_EQ(result[1], "libnet1:amd64");
  64. EXPECT_EQ(result[2], "unpacked");
  65. result = StringSplit("x:y:z", ":", 2);
  66. ASSERT_EQ(result.size(), 2u);
  67. EXPECT_EQ(result[0], "x");
  68. EXPECT_EQ(result[1], "y:z");
  69. }
  70. TEST(StrUtilTest,EndsWith)
  71. {
  72. using APT::String::Endswith;
  73. EXPECT_TRUE(Endswith("abcd", "d"));
  74. EXPECT_TRUE(Endswith("abcd", "cd"));
  75. EXPECT_TRUE(Endswith("abcd", "abcd"));
  76. EXPECT_FALSE(Endswith("abcd", "x"));
  77. EXPECT_FALSE(Endswith("abcd", "abcndefg"));
  78. }
  79. TEST(StrUtilTest,StartsWith)
  80. {
  81. using APT::String::Startswith;
  82. EXPECT_TRUE(Startswith("abcd", "a"));
  83. EXPECT_TRUE(Startswith("abcd", "ab"));
  84. EXPECT_TRUE(Startswith("abcd", "abcd"));
  85. EXPECT_FALSE(Startswith("abcd", "x"));
  86. EXPECT_FALSE(Startswith("abcd", "abcndefg"));
  87. }
  88. TEST(StrUtilTest,TimeToStr)
  89. {
  90. EXPECT_EQ("0s", TimeToStr(0));
  91. EXPECT_EQ("42s", TimeToStr(42));
  92. EXPECT_EQ("9min 21s", TimeToStr((9*60) + 21));
  93. EXPECT_EQ("20min 42s", TimeToStr((20*60) + 42));
  94. EXPECT_EQ("10h 42min 21s", TimeToStr((10*3600) + (42*60) + 21));
  95. EXPECT_EQ("10h 42min 21s", TimeToStr((10*3600) + (42*60) + 21));
  96. EXPECT_EQ("1988d 3h 29min 7s", TimeToStr((1988*86400) + (3*3600) + (29*60) + 7));
  97. EXPECT_EQ("59s", TimeToStr(59));
  98. EXPECT_EQ("60s", TimeToStr(60));
  99. EXPECT_EQ("1min 1s", TimeToStr(61));
  100. EXPECT_EQ("59min 59s", TimeToStr(3599));
  101. EXPECT_EQ("60min 0s", TimeToStr(3600));
  102. EXPECT_EQ("1h 0min 1s", TimeToStr(3601));
  103. EXPECT_EQ("1h 1min 0s", TimeToStr(3660));
  104. EXPECT_EQ("23h 59min 59s", TimeToStr(86399));
  105. EXPECT_EQ("24h 0min 0s", TimeToStr(86400));
  106. EXPECT_EQ("1d 0h 0min 1s", TimeToStr(86401));
  107. EXPECT_EQ("1d 0h 1min 0s", TimeToStr(86460));
  108. }
  109. TEST(StrUtilTest,SubstVar)
  110. {
  111. EXPECT_EQ("", SubstVar("", "fails", "passes"));
  112. EXPECT_EQ("test ", SubstVar("test fails", "fails", ""));
  113. EXPECT_EQ("test passes", SubstVar("test passes", "", "fails"));
  114. EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes"));
  115. EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes"));
  116. EXPECT_EQ("starts with", SubstVar("beginnt with", "beginnt", "starts"));
  117. EXPECT_EQ("beginnt with", SubstVar("starts with", "starts", "beginnt"));
  118. EXPECT_EQ("is in middle", SubstVar("is in der middle", "in der", "in"));
  119. EXPECT_EQ("is in der middle", SubstVar("is in middle", "in", "in der"));
  120. EXPECT_EQ("does end", SubstVar("does enden", "enden", "end"));
  121. EXPECT_EQ("does enden", SubstVar("does end", "end", "enden"));
  122. EXPECT_EQ("abc", SubstVar("abc", "d", "a"));
  123. EXPECT_EQ("abc", SubstVar("abd", "d", "c"));
  124. EXPECT_EQ("abc", SubstVar("adc", "d", "b"));
  125. EXPECT_EQ("abc", SubstVar("dbc", "d", "a"));
  126. EXPECT_EQ("b", SubstVar("b", "aa", "a"));
  127. EXPECT_EQ("bb", SubstVar("bb", "aa", "a"));
  128. EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a"));
  129. EXPECT_EQ("aa", SubstVar("aaaa", "aa", "a"));
  130. EXPECT_EQ("aaaa", SubstVar("aa", "a", "aa"));
  131. EXPECT_EQ("aaaa", SubstVar("aaaa", "a", "a"));
  132. EXPECT_EQ("a a a a ", SubstVar("aaaa", "a", "a "));
  133. EXPECT_EQ(" bb bb bb bb ", SubstVar(" a a a a ", "a", "bb"));
  134. EXPECT_EQ(" bb bb bb bb ", SubstVar(" aaa aaa aaa aaa ", "aaa", "bb"));
  135. EXPECT_EQ(" bb a bb a bb a bb ", SubstVar(" aaa a aaa a aaa a aaa ", "aaa", "bb"));
  136. }
  137. TEST(StrUtilTest,Base64Encode)
  138. {
  139. EXPECT_EQ("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", Base64Encode("Aladdin:open sesame"));
  140. EXPECT_EQ("cGxlYXN1cmUu", Base64Encode("pleasure."));
  141. EXPECT_EQ("bGVhc3VyZS4=", Base64Encode("leasure."));
  142. EXPECT_EQ("ZWFzdXJlLg==", Base64Encode("easure."));
  143. EXPECT_EQ("YXN1cmUu", Base64Encode("asure."));
  144. EXPECT_EQ("c3VyZS4=", Base64Encode("sure."));
  145. EXPECT_EQ("dXJlLg==", Base64Encode("ure."));
  146. EXPECT_EQ("cmUu", Base64Encode("re."));
  147. EXPECT_EQ("ZS4=", Base64Encode("e."));
  148. EXPECT_EQ("Lg==", Base64Encode("."));
  149. EXPECT_EQ("", Base64Encode(""));
  150. }
  151. static void ReadMessagesTestWithNewLine(char const * const nl, char const * const ab)
  152. {
  153. SCOPED_TRACE(SubstVar(SubstVar(nl, "\n", "n"), "\r", "r") + " # " + ab);
  154. FileFd fd;
  155. std::string pkgA = "Package: pkgA\n"
  156. "Version: 1\n"
  157. "Size: 100\n"
  158. "Description: aaa\n"
  159. " aaa";
  160. std::string pkgB = "Package: pkgB\n"
  161. "Version: 1\n"
  162. "Flag: no\n"
  163. "Description: bbb";
  164. std::string pkgC = "Package: pkgC\n"
  165. "Version: 2\n"
  166. "Flag: yes\n"
  167. "Description:\n"
  168. " ccc";
  169. createTemporaryFile("readmessage", fd, NULL, (pkgA + nl + pkgB + nl + pkgC + nl).c_str());
  170. std::vector<std::string> list;
  171. EXPECT_TRUE(ReadMessages(fd.Fd(), list));
  172. EXPECT_EQ(3u, list.size());
  173. EXPECT_EQ(pkgA, list[0]);
  174. EXPECT_EQ(pkgB, list[1]);
  175. EXPECT_EQ(pkgC, list[2]);
  176. size_t const msgsize = 63990;
  177. createTemporaryFile("readmessage", fd, NULL, NULL);
  178. for (size_t j = 0; j < msgsize; ++j)
  179. fd.Write(ab, strlen(ab));
  180. for (size_t i = 0; i < 21; ++i)
  181. {
  182. std::string msg;
  183. strprintf(msg, "msgsize=%zu i=%zu", msgsize, i);
  184. SCOPED_TRACE(msg);
  185. fd.Seek((msgsize + (i - 1)) * strlen(ab));
  186. fd.Write(ab, strlen(ab));
  187. fd.Write(nl, strlen(nl));
  188. fd.Seek(0);
  189. list.clear();
  190. EXPECT_TRUE(ReadMessages(fd.Fd(), list));
  191. EXPECT_EQ(1u, list.size());
  192. EXPECT_EQ((msgsize + i) * strlen(ab), list[0].length());
  193. EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab));
  194. }
  195. list.clear();
  196. fd.Write(pkgA.c_str(), pkgA.length());
  197. fd.Write(nl, strlen(nl));
  198. fd.Seek(0);
  199. EXPECT_TRUE(ReadMessages(fd.Fd(), list));
  200. EXPECT_EQ(2u, list.size());
  201. EXPECT_EQ((msgsize + 20) * strlen(ab), list[0].length());
  202. EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab));
  203. EXPECT_EQ(pkgA, list[1]);
  204. fd.Close();
  205. }
  206. TEST(StrUtilTest,ReadMessages)
  207. {
  208. ReadMessagesTestWithNewLine("\n\n", "a");
  209. ReadMessagesTestWithNewLine("\r\n\r\n", "a");
  210. ReadMessagesTestWithNewLine("\n\n", "ab");
  211. ReadMessagesTestWithNewLine("\r\n\r\n", "ab");
  212. }
  213. TEST(StrUtilTest,QuoteString)
  214. {
  215. EXPECT_EQ("", QuoteString("", ""));
  216. EXPECT_EQ("K%c3%b6ln", QuoteString("Köln", ""));
  217. EXPECT_EQ("Köln", DeQuoteString(QuoteString("Köln", "")));
  218. EXPECT_EQ("Köln", DeQuoteString(DeQuoteString(QuoteString(QuoteString("Köln", ""), ""))));
  219. EXPECT_EQ("~-_$#|u%c3%a4%c3%b6%c5%a6%e2%84%a2%e2%85%9e%c2%b1%c3%86%e1%ba%9e%c2%aa%c3%9f", QuoteString("~-_$#|uäöŦ™⅞±Æẞªß", ""));
  220. EXPECT_EQ("~-_$#|uäöŦ™⅞±Æẞªß", DeQuoteString(QuoteString("~-_$#|uäöŦ™⅞±Æẞªß", "")));
  221. EXPECT_EQ("%45ltvill%65%2d%45rbach", QuoteString("Eltville-Erbach", "E-Ae"));
  222. EXPECT_EQ("Eltville-Erbach", DeQuoteString(QuoteString("Eltville-Erbach", "")));
  223. }
  224. TEST(StrUtilTest,RFC1123StrToTime)
  225. {
  226. {
  227. time_t t;
  228. EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 GMT", t));
  229. EXPECT_EQ(784111777, t);
  230. } {
  231. time_t t;
  232. EXPECT_TRUE(RFC1123StrToTime("Sun, 6 Nov 1994 08:49:37 UTC", t));
  233. EXPECT_EQ(784111777, t);
  234. } {
  235. time_t t;
  236. EXPECT_TRUE(RFC1123StrToTime("Sun, 6 Nov 1994 08:49:37 UTC", t));
  237. EXPECT_EQ(784111777, t);
  238. } {
  239. time_t t;
  240. EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 8:49:37 UTC", t));
  241. EXPECT_EQ(784111777, t);
  242. } {
  243. time_t t;
  244. EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 UTC", t));
  245. EXPECT_EQ(784111777, t);
  246. } {
  247. time_t t;
  248. EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 -0000", t));
  249. EXPECT_EQ(784111777, t);
  250. } {
  251. time_t t;
  252. EXPECT_TRUE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 +0000", t));
  253. EXPECT_EQ(784111777, t);
  254. } {
  255. time_t t;
  256. EXPECT_TRUE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 GMT", t));
  257. EXPECT_EQ(784111777, t);
  258. } {
  259. time_t t;
  260. EXPECT_TRUE(RFC1123StrToTime("Sunday, 6-Nov-94 08:49:37 GMT", t));
  261. EXPECT_EQ(784111777, t);
  262. } {
  263. time_t t;
  264. EXPECT_TRUE(RFC1123StrToTime("Sunday, 06-Nov-94 8:49:37 GMT", t));
  265. EXPECT_EQ(784111777, t);
  266. } {
  267. time_t t;
  268. EXPECT_TRUE(RFC1123StrToTime("Sun Nov 6 08:49:37 1994", t));
  269. EXPECT_EQ(784111777, t);
  270. } {
  271. time_t t;
  272. EXPECT_TRUE(RFC1123StrToTime("Sun Nov 06 08:49:37 1994", t));
  273. EXPECT_EQ(784111777, t);
  274. } {
  275. time_t t;
  276. EXPECT_TRUE(RFC1123StrToTime("Sun Nov 6 8:49:37 1994", t));
  277. EXPECT_EQ(784111777, t);
  278. }
  279. time_t t;
  280. EXPECT_FALSE(RFC1123StrToTime("So, 06 Nov 1994 08:49:37 UTC", t));
  281. EXPECT_FALSE(RFC1123StrToTime(", 06 Nov 1994 08:49:37 UTC", t));
  282. EXPECT_FALSE(RFC1123StrToTime("Son, 06 Nov 1994 08:49:37 UTC", t));
  283. EXPECT_FALSE(RFC1123StrToTime("Sun: 06 Nov 1994 08:49:37 UTC", t));
  284. EXPECT_FALSE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37", t));
  285. EXPECT_FALSE(RFC1123StrToTime("Sun, 06 Nov 1994 08:49:37 GMT+1", t));
  286. EXPECT_FALSE(RFC1123StrToTime("Sun, 06 Nov 1994 GMT", t));
  287. EXPECT_FALSE(RFC1123StrToTime("Sunday, 06 Nov 1994 GMT", t));
  288. EXPECT_FALSE(RFC1123StrToTime("Sonntag, 06 Nov 1994 08:49:37 GMT", t));
  289. EXPECT_FALSE(RFC1123StrToTime("domingo Nov 6 08:49:37 1994", t));
  290. EXPECT_FALSE(RFC1123StrToTime("Sunday: 06-Nov-94 08:49:37 GMT", t));
  291. EXPECT_FALSE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 GMT+1", t));
  292. EXPECT_FALSE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 EDT", t));
  293. EXPECT_FALSE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 -0100", t));
  294. EXPECT_FALSE(RFC1123StrToTime("Sunday, 06-Nov-94 08:49:37 -0.1", t));
  295. }
  296. TEST(StrUtilTest, LookupTag)
  297. {
  298. EXPECT_EQ("", LookupTag("", "Field", ""));
  299. EXPECT_EQ("", LookupTag("", "Field", nullptr));
  300. EXPECT_EQ("default", LookupTag("", "Field", "default"));
  301. EXPECT_EQ("default", LookupTag("Field1: yes", "Field", "default"));
  302. EXPECT_EQ("default", LookupTag("Fiel: yes", "Field", "default"));
  303. EXPECT_EQ("default", LookupTag("Fiel d: yes", "Field", "default"));
  304. EXPECT_EQ("foo", LookupTag("Field: foo", "Field", "default"));
  305. EXPECT_EQ("foo", LookupTag("Field: foo\n", "Field", "default"));
  306. EXPECT_EQ("foo", LookupTag("\nField: foo\n", "Field", "default"));
  307. EXPECT_EQ("foo", LookupTag("Field:foo", "Field", "default"));
  308. EXPECT_EQ("foo", LookupTag("Field:foo\n", "Field", "default"));
  309. EXPECT_EQ("foo", LookupTag("\nField:foo\n", "Field", "default"));
  310. EXPECT_EQ("foo", LookupTag("Field:\tfoo\n", "Field", "default"));
  311. EXPECT_EQ("foo", LookupTag("Field: foo \t", "Field", "default"));
  312. EXPECT_EQ("foo", LookupTag("Field: foo \t\n", "Field", "default"));
  313. EXPECT_EQ("Field : yes", LookupTag("Field: Field : yes \t\n", "Field", "default"));
  314. EXPECT_EQ("Field : yes", LookupTag("Field:\n Field : yes \t\n", "Field", "default"));
  315. EXPECT_EQ("Field : yes", LookupTag("Foo: bar\nField: Field : yes \t\n", "Field", "default"));
  316. EXPECT_EQ("line1\nline2", LookupTag("Multi: line1\n line2", "Multi", "default"));
  317. EXPECT_EQ("line1\nline2", LookupTag("Multi: line1\n line2\n", "Multi", "default"));
  318. EXPECT_EQ("line1\nline2", LookupTag("Multi:\n line1\n line2\n", "Multi", "default"));
  319. EXPECT_EQ("line1\n\nline2", LookupTag("Multi:\n line1\n .\n line2\n", "Multi", "default"));
  320. EXPECT_EQ("line1\na\nline2", LookupTag("Multi:\n line1\n a\n line2\n", "Multi", "default"));
  321. EXPECT_EQ("line1\nfoo\nline2", LookupTag("Multi:\n line1\n foo\n line2\n", "Multi", "default"));
  322. EXPECT_EQ("line1\n line2", LookupTag("Multi: line1\n line2", "Multi", "default"));
  323. EXPECT_EQ(" line1\n \t line2", LookupTag("Multi:\t \n line1\n \t line2\n", "Multi", "default"));
  324. EXPECT_EQ(" line1\n\n\n \t line2", LookupTag("Multi:\t \n line1\n .\n . \n \t line2\n", "Multi", "default"));
  325. std::string const msg =
  326. "Field1: Value1\nField2:Value2\nField3:\t Value3\n"
  327. "Multi-Field1: Line1\n Line2\nMulti-Field2:\n Line1\n Line2\n"
  328. "Field4: Value4\nField5:Value5";
  329. EXPECT_EQ("Value1", LookupTag(msg, "Field1", ""));
  330. EXPECT_EQ("Value2", LookupTag(msg, "Field2", ""));
  331. EXPECT_EQ("Value3", LookupTag(msg, "Field3", ""));
  332. EXPECT_EQ("Line1\nLine2", LookupTag(msg, "Multi-Field1", ""));
  333. EXPECT_EQ("Line1\nLine2", LookupTag(msg, "Multi-Field2", ""));
  334. EXPECT_EQ("Value4", LookupTag(msg, "Field4", ""));
  335. EXPECT_EQ("Value5", LookupTag(msg, "Field5", ""));
  336. }