commandline_test.cc 7.8 KB

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