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.
 
 
 
 
 
 

286 lines
9.0 KiB

  1. #include <config.h>
  2. #include <apt-pkg/fileutl.h>
  3. #include <apt-pkg/tagfile.h>
  4. #include <sstream>
  5. #include <string>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <gtest/gtest.h>
  10. #include "file-helpers.h"
  11. TEST(TagFileTest,SingleField)
  12. {
  13. FileFd fd;
  14. createTemporaryFile("singlefield", fd, NULL, "FieldA-12345678: the value of the field");
  15. pkgTagFile tfile(&fd);
  16. pkgTagSection section;
  17. ASSERT_TRUE(tfile.Step(section));
  18. // It has one field
  19. EXPECT_EQ(1u, section.Count());
  20. // ... and it is called FieldA-12345678
  21. EXPECT_TRUE(section.Exists("FieldA-12345678"));
  22. // its value is correct
  23. EXPECT_EQ("the value of the field", section.FindS("FieldA-12345678"));
  24. // A non-existent field has an empty string as value
  25. EXPECT_EQ("", section.FindS("FieldB-12345678"));
  26. // ... and Exists does not lie about missing fields...
  27. EXPECT_FALSE(section.Exists("FieldB-12345678"));
  28. // There is only one section in this tag file
  29. EXPECT_FALSE(tfile.Step(section));
  30. // Now we scan an empty section to test reset
  31. ASSERT_TRUE(section.Scan("\n\n", 2, true));
  32. EXPECT_EQ(0u, section.Count());
  33. EXPECT_FALSE(section.Exists("FieldA-12345678"));
  34. EXPECT_FALSE(section.Exists("FieldB-12345678"));
  35. createTemporaryFile("emptyfile", fd, NULL, NULL);
  36. ASSERT_FALSE(tfile.Step(section));
  37. EXPECT_EQ(0u, section.Count());
  38. }
  39. TEST(TagFileTest,MultipleSections)
  40. {
  41. FileFd fd;
  42. createTemporaryFile("bigsection", fd, NULL, "Package: pkgA\n"
  43. "Version: 1\n"
  44. "Size: 100\n"
  45. "Description: aaa\n"
  46. " aaa\n"
  47. "\n"
  48. "Package: pkgB\n"
  49. "Version: 1\n"
  50. "Flag: no\n"
  51. "Description: bbb\n"
  52. "\n"
  53. "Package: pkgC\n"
  54. "Version: 2\n"
  55. "Flag: yes\n"
  56. "Description:\n"
  57. " ccc\n"
  58. );
  59. pkgTagFile tfile(&fd);
  60. pkgTagSection section;
  61. EXPECT_FALSE(section.Exists("Version"));
  62. EXPECT_TRUE(tfile.Step(section));
  63. EXPECT_EQ(4u, section.Count());
  64. EXPECT_TRUE(section.Exists("Version"));
  65. EXPECT_TRUE(section.Exists("Package"));
  66. EXPECT_TRUE(section.Exists("Size"));
  67. EXPECT_FALSE(section.Exists("Flag"));
  68. EXPECT_TRUE(section.Exists("Description"));
  69. EXPECT_EQ("pkgA", section.FindS("Package"));
  70. EXPECT_EQ("1", section.FindS("Version"));
  71. EXPECT_EQ(1u, section.FindULL("Version"));
  72. EXPECT_EQ(100u, section.FindULL("Size"));
  73. unsigned long Flags = 1;
  74. EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
  75. EXPECT_EQ(1u, Flags);
  76. Flags = 0;
  77. EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
  78. EXPECT_EQ(0u, Flags);
  79. EXPECT_EQ("aaa\n aaa", section.FindS("Description"));
  80. EXPECT_TRUE(tfile.Step(section));
  81. EXPECT_EQ(4u, section.Count());
  82. EXPECT_TRUE(section.Exists("Version"));
  83. EXPECT_TRUE(section.Exists("Package"));
  84. EXPECT_FALSE(section.Exists("Size"));
  85. EXPECT_TRUE(section.Exists("Flag"));
  86. EXPECT_TRUE(section.Exists("Description"));
  87. EXPECT_EQ("pkgB", section.FindS("Package"));
  88. EXPECT_EQ("1", section.FindS("Version"));
  89. EXPECT_EQ(1u, section.FindULL("Version"));
  90. EXPECT_EQ(0u, section.FindULL("Size"));
  91. Flags = 1;
  92. EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
  93. EXPECT_EQ(0u, Flags);
  94. Flags = 0;
  95. EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
  96. EXPECT_EQ(0u, Flags);
  97. EXPECT_EQ("bbb", section.FindS("Description"));
  98. EXPECT_TRUE(tfile.Step(section));
  99. EXPECT_EQ(4u, section.Count());
  100. EXPECT_TRUE(section.Exists("Version"));
  101. EXPECT_TRUE(section.Exists("Package"));
  102. EXPECT_FALSE(section.Exists("Size"));
  103. EXPECT_TRUE(section.Exists("Flag"));
  104. EXPECT_TRUE(section.Exists("Description"));
  105. EXPECT_EQ("pkgC", section.FindS("Package"));
  106. EXPECT_EQ("2", section.FindS("Version"));
  107. EXPECT_EQ(2u, section.FindULL("Version"));
  108. Flags = 0;
  109. EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
  110. EXPECT_EQ(1u, Flags);
  111. Flags = 1;
  112. EXPECT_TRUE(section.FindFlag("Flag", Flags, 1));
  113. EXPECT_EQ(1u, Flags);
  114. EXPECT_EQ("ccc", section.FindS("Description"));
  115. // There is no section left in this tag file
  116. EXPECT_FALSE(tfile.Step(section));
  117. }
  118. TEST(TagFileTest,BigSection)
  119. {
  120. size_t const count = 500;
  121. std::stringstream content;
  122. for (size_t i = 0; i < count; ++i)
  123. content << "Field-" << i << ": " << (2000 + i) << std::endl;
  124. FileFd fd;
  125. createTemporaryFile("bigsection", fd, NULL, content.str().c_str());
  126. pkgTagFile tfile(&fd);
  127. pkgTagSection section;
  128. EXPECT_TRUE(tfile.Step(section));
  129. EXPECT_EQ(count, section.Count());
  130. for (size_t i = 0; i < count; ++i)
  131. {
  132. std::stringstream name;
  133. name << "Field-" << i;
  134. EXPECT_TRUE(section.Exists(name.str().c_str())) << name.str() << " does not exist";
  135. EXPECT_EQ((i + 2000), section.FindULL(name.str().c_str()));
  136. }
  137. // There is only one section in this tag file
  138. EXPECT_FALSE(tfile.Step(section));
  139. }
  140. TEST(TagFileTest, PickedUpFromPreviousCall)
  141. {
  142. size_t const count = 500;
  143. std::stringstream contentstream;
  144. for (size_t i = 0; i < count; ++i)
  145. contentstream << "Field-" << i << ": " << (2000 + i) << std::endl;
  146. contentstream << std::endl << std::endl;
  147. std::string content = contentstream.str();
  148. pkgTagSection section;
  149. EXPECT_FALSE(section.Scan(content.c_str(), content.size()/2));
  150. EXPECT_NE(0u, section.Count());
  151. EXPECT_NE(count, section.Count());
  152. EXPECT_TRUE(section.Scan(content.c_str(), content.size(), false));
  153. EXPECT_EQ(count, section.Count());
  154. for (size_t i = 0; i < count; ++i)
  155. {
  156. std::stringstream name;
  157. name << "Field-" << i;
  158. EXPECT_TRUE(section.Exists(name.str().c_str())) << name.str() << " does not exist";
  159. EXPECT_EQ((i + 2000), section.FindULL(name.str().c_str()));
  160. }
  161. }
  162. TEST(TagFileTest, SpacesEverywhere)
  163. {
  164. std::string content =
  165. "Package: pkgA\n"
  166. "Package: pkgB\n"
  167. "NoSpaces:yes\n"
  168. "NoValue:\n"
  169. "TagSpaces\t :yes\n"
  170. "ValueSpaces: \tyes\n"
  171. "BothSpaces \t:\t yes\n"
  172. "TrailingSpaces: yes\t \n"
  173. "Naming Space: yes\n"
  174. "Naming Spaces: yes\n"
  175. "Package : pkgC \n"
  176. "Multi-Colon::yes:\n"
  177. "\n\n";
  178. pkgTagSection section;
  179. EXPECT_TRUE(section.Scan(content.c_str(), content.size()));
  180. EXPECT_TRUE(section.Exists("Package"));
  181. EXPECT_TRUE(section.Exists("NoSpaces"));
  182. EXPECT_TRUE(section.Exists("NoValue"));
  183. EXPECT_TRUE(section.Exists("TagSpaces"));
  184. EXPECT_TRUE(section.Exists("ValueSpaces"));
  185. EXPECT_TRUE(section.Exists("BothSpaces"));
  186. EXPECT_TRUE(section.Exists("TrailingSpaces"));
  187. EXPECT_TRUE(section.Exists("Naming Space"));
  188. EXPECT_TRUE(section.Exists("Naming Spaces"));
  189. EXPECT_TRUE(section.Exists("Multi-Colon"));
  190. EXPECT_EQ("pkgC", section.FindS("Package"));
  191. EXPECT_EQ("yes", section.FindS("NoSpaces"));
  192. EXPECT_EQ("", section.FindS("NoValue"));
  193. EXPECT_EQ("yes", section.FindS("TagSpaces"));
  194. EXPECT_EQ("yes", section.FindS("ValueSpaces"));
  195. EXPECT_EQ("yes", section.FindS("BothSpaces"));
  196. EXPECT_EQ("yes", section.FindS("TrailingSpaces"));
  197. EXPECT_EQ("yes", section.FindS("Naming Space"));
  198. EXPECT_EQ("yes", section.FindS("Naming Spaces"));
  199. EXPECT_EQ(":yes:", section.FindS("Multi-Colon"));
  200. // overridden values are still present, but not really accessible
  201. EXPECT_EQ(12u, section.Count());
  202. }
  203. TEST(TagFileTest, Comments)
  204. {
  205. FileFd fd;
  206. createTemporaryFile("commentfile", fd, NULL, "# Leading comments should be ignored.\n"
  207. "\n"
  208. "Source: foo\n"
  209. "#Package: foo\n"
  210. "Section: bar\n"
  211. "#Section: overridden\n"
  212. "Priority: optional\n"
  213. "Build-Depends: debhelper,\n"
  214. "# apt-utils, (temporarily disabled)\n"
  215. " apt\n"
  216. "\n"
  217. "# Comments in the middle shouldn't result in extra blank paragraphs either.\n"
  218. "\n"
  219. "# Ditto.\n"
  220. "\n"
  221. "# A comment at the top of a paragraph should be ignored.\n"
  222. "Package: foo\n"
  223. "Architecture: any\n"
  224. "Description: An awesome package\n"
  225. " # This should still appear in the result.\n"
  226. "# this one shouldn't\n"
  227. " Blah, blah, blah. # but this again.\n"
  228. "# A comment at the end of a paragraph should be ignored.\n"
  229. "\n"
  230. "# Trailing comments shouldn't cause extra blank paragraphs."
  231. );
  232. pkgTagFile tfile(&fd, pkgTagFile::SUPPORT_COMMENTS, 1);
  233. pkgTagSection section;
  234. EXPECT_TRUE(tfile.Step(section));
  235. EXPECT_FALSE(section.Exists("Package"));
  236. EXPECT_TRUE(section.Exists("Source"));
  237. EXPECT_EQ("foo", section.FindS("Source"));
  238. EXPECT_TRUE(section.Exists("Section"));
  239. EXPECT_EQ("bar", section.FindS("Section"));
  240. EXPECT_TRUE(section.Exists("Priority"));
  241. EXPECT_EQ("optional", section.FindS("Priority"));
  242. EXPECT_TRUE(section.Exists("Build-Depends"));
  243. EXPECT_EQ("debhelper,\n apt", section.FindS("Build-Depends"));
  244. EXPECT_TRUE(tfile.Step(section));
  245. EXPECT_FALSE(section.Exists("Source"));
  246. EXPECT_TRUE(section.Exists("Package"));
  247. EXPECT_EQ("foo", section.FindS("Package"));
  248. EXPECT_FALSE(section.Exists("Section"));
  249. EXPECT_TRUE(section.Exists("Architecture"));
  250. EXPECT_EQ("any", section.FindS("Architecture"));
  251. EXPECT_FALSE(section.Exists("Build-Depends"));
  252. EXPECT_TRUE(section.Exists("Description"));
  253. EXPECT_EQ("An awesome package\n # This should still appear in the result.\n Blah, blah, blah. # but this again.", section.FindS("Description"));
  254. EXPECT_FALSE(tfile.Step(section));
  255. }