commandline_test.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <config.h>
  2. #include <apt-pkg/cmndline.h>
  3. #include <apt-pkg/configuration.h>
  4. #include <apt-private/private-cmndline.h>
  5. #include <gtest/gtest.h>
  6. class CLT: public CommandLine {
  7. public:
  8. std::string static AsString(const char * const * const argv,
  9. unsigned int const argc) {
  10. std::string const static conf = "Commandline::AsString";
  11. _config->Clear(conf);
  12. SaveInConfig(argc, argv);
  13. return _config->Find(conf);
  14. }
  15. };
  16. bool ShowHelp(CommandLine &) {return false;}
  17. std::vector<aptDispatchWithHelp> GetCommands() {return {};}
  18. TEST(CommandLineTest,SaveInConfig)
  19. {
  20. #define APT_EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }
  21. APT_EXPECT_CMD("apt-get install -sf",
  22. "apt-get", "install", "-sf");
  23. APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=Test",
  24. "apt-cache", "-s", "apt", "-so", "Debug::test=Test");
  25. APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"",
  26. "apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test");
  27. APT_EXPECT_CMD("apt-cache -s apt --hallo test=1.0",
  28. "apt-cache", "-s", "apt", "--hallo", "test=1.0");
  29. #undef APT_EXPECT_CMD
  30. }
  31. TEST(CommandLineTest,Parsing)
  32. {
  33. CommandLine::Args Args[] = {
  34. { 't', 0, "Test::Worked", 0 },
  35. { 'T', "testing", "Test::Worked", CommandLine::HasArg },
  36. { 'z', "zero", "Test::Zero", 0 },
  37. { 'o', "option", 0, CommandLine::ArbItem },
  38. {0,0,0,0}
  39. };
  40. ::Configuration c;
  41. CommandLine CmdL(Args, &c);
  42. char const * argv[] = { "test", "--zero", "-t" };
  43. CmdL.Parse(3 , argv);
  44. EXPECT_TRUE(c.FindB("Test::Worked", false));
  45. EXPECT_TRUE(c.FindB("Test::Zero", false));
  46. c.Clear("Test");
  47. EXPECT_FALSE(c.FindB("Test::Worked", false));
  48. EXPECT_FALSE(c.FindB("Test::Zero", false));
  49. c.Set("Test::Zero", true);
  50. EXPECT_TRUE(c.FindB("Test::Zero", false));
  51. char const * argv2[] = { "test", "--no-zero", "-t" };
  52. CmdL.Parse(3 , argv2);
  53. EXPECT_TRUE(c.FindB("Test::Worked", false));
  54. EXPECT_FALSE(c.FindB("Test::Zero", false));
  55. c.Clear("Test");
  56. {
  57. char const * argv[] = { "test", "-T", "yes" };
  58. CmdL.Parse(3 , argv);
  59. EXPECT_TRUE(c.FindB("Test::Worked", false));
  60. EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
  61. EXPECT_EQ(0, CmdL.FileSize());
  62. }
  63. c.Clear("Test");
  64. {
  65. char const * argv[] = { "test", "-T=yes" };
  66. CmdL.Parse(2 , argv);
  67. EXPECT_TRUE(c.Exists("Test::Worked"));
  68. EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
  69. EXPECT_EQ(0, CmdL.FileSize());
  70. }
  71. c.Clear("Test");
  72. {
  73. char const * argv[] = { "test", "-T=", "yes" };
  74. CmdL.Parse(3 , argv);
  75. EXPECT_TRUE(c.Exists("Test::Worked"));
  76. EXPECT_EQ("no", c.Find("Test::Worked", "no"));
  77. EXPECT_EQ(1, CmdL.FileSize());
  78. }
  79. c.Clear("Test");
  80. {
  81. char const * argv[] = { "test", "--testing", "yes" };
  82. CmdL.Parse(3 , argv);
  83. EXPECT_TRUE(c.FindB("Test::Worked", false));
  84. EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
  85. EXPECT_EQ(0, CmdL.FileSize());
  86. }
  87. c.Clear("Test");
  88. {
  89. char const * argv[] = { "test", "--testing=yes" };
  90. CmdL.Parse(2 , argv);
  91. EXPECT_TRUE(c.Exists("Test::Worked"));
  92. EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
  93. EXPECT_EQ(0, CmdL.FileSize());
  94. }
  95. c.Clear("Test");
  96. {
  97. char const * argv[] = { "test", "--testing=", "yes" };
  98. CmdL.Parse(3 , argv);
  99. EXPECT_TRUE(c.Exists("Test::Worked"));
  100. EXPECT_EQ("no", c.Find("Test::Worked", "no"));
  101. EXPECT_EQ(1, CmdL.FileSize());
  102. }
  103. c.Clear("Test");
  104. {
  105. char const * argv[] = { "test", "-o", "test::worked=yes" };
  106. CmdL.Parse(3 , argv);
  107. EXPECT_TRUE(c.FindB("Test::Worked", false));
  108. EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
  109. }
  110. c.Clear("Test");
  111. {
  112. char const * argv[] = { "test", "-o", "test::worked=" };
  113. CmdL.Parse(3 , argv);
  114. EXPECT_TRUE(c.Exists("Test::Worked"));
  115. EXPECT_EQ("no", c.Find("Test::Worked", "no"));
  116. }
  117. c.Clear("Test");
  118. {
  119. char const * argv[] = { "test", "-o", "test::worked=", "yes" };
  120. CmdL.Parse(4 , argv);
  121. EXPECT_TRUE(c.Exists("Test::Worked"));
  122. EXPECT_EQ("no", c.Find("Test::Worked", "no"));
  123. }
  124. c.Clear("Test");
  125. }
  126. TEST(CommandLineTest, BoolParsing)
  127. {
  128. CommandLine::Args Args[] = {
  129. { 't', 0, "Test::Worked", 0 },
  130. {0,0,0,0}
  131. };
  132. ::Configuration c;
  133. CommandLine CmdL(Args, &c);
  134. // the commandline parser used to use strtol() on the argument
  135. // to check if the argument is a boolean expression - that
  136. // stopped after the "0". this test ensures that we always check
  137. // that the entire string was consumed by strtol
  138. {
  139. char const * argv[] = { "show", "-t", "0ad" };
  140. bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
  141. EXPECT_TRUE(res);
  142. ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad");
  143. }
  144. {
  145. char const * argv[] = { "show", "-t", "0", "ad" };
  146. bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
  147. EXPECT_TRUE(res);
  148. ASSERT_EQ(std::string(CmdL.FileList[0]), "ad");
  149. }
  150. }
  151. bool DoVoid(CommandLine &) { return false; }
  152. TEST(CommandLineTest,GetCommand)
  153. {
  154. CommandLine::Dispatch Cmds[] = { {"install",&DoVoid}, {"remove", &DoVoid}, {0,0} };
  155. {
  156. char const * argv[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
  157. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  158. EXPECT_STREQ("remove", com);
  159. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  160. ::Configuration c;
  161. CommandLine CmdL(Args.data(), &c);
  162. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  163. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  164. EXPECT_TRUE(c.FindB("APT::Get::Download-Only"));
  165. ASSERT_EQ(2, CmdL.FileSize());
  166. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  167. EXPECT_EQ(std::string(CmdL.FileList[1]), "foo");
  168. }
  169. {
  170. char const * argv[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" };
  171. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  172. EXPECT_STREQ("remove", com);
  173. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  174. ::Configuration c;
  175. CommandLine CmdL(Args.data(), &c);
  176. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  177. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  178. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  179. ASSERT_EQ(3, CmdL.FileSize());
  180. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  181. EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
  182. EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
  183. }
  184. {
  185. char const * argv[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" };
  186. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  187. EXPECT_STREQ("remove", com);
  188. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  189. ::Configuration c;
  190. CommandLine CmdL(Args.data(), &c);
  191. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  192. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  193. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  194. ASSERT_EQ(CmdL.FileSize(), 3);
  195. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  196. EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
  197. EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
  198. }
  199. {
  200. char const * argv[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
  201. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  202. EXPECT_STREQ("install", com);
  203. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  204. ::Configuration c;
  205. CommandLine CmdL(Args.data(), &c);
  206. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  207. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  208. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  209. ASSERT_EQ(CmdL.FileSize(), 4);
  210. EXPECT_EQ(std::string(CmdL.FileList[0]), "install");
  211. EXPECT_EQ(std::string(CmdL.FileList[1]), "remove");
  212. EXPECT_EQ(std::string(CmdL.FileList[2]), "-d");
  213. EXPECT_EQ(std::string(CmdL.FileList[3]), "foo");
  214. }
  215. }