commandline_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. { 'z', "zero", "Test::Zero", 0 },
  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. }
  54. TEST(CommandLineTest, BoolParsing)
  55. {
  56. CommandLine::Args Args[] = {
  57. { 't', 0, "Test::Worked", 0 },
  58. {0,0,0,0}
  59. };
  60. ::Configuration c;
  61. CommandLine CmdL(Args, &c);
  62. // the commandline parser used to use strtol() on the argument
  63. // to check if the argument is a boolean expression - that
  64. // stopped after the "0". this test ensures that we always check
  65. // that the entire string was consumed by strtol
  66. {
  67. char const * argv[] = { "show", "-t", "0ad" };
  68. bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
  69. EXPECT_TRUE(res);
  70. ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad");
  71. }
  72. {
  73. char const * argv[] = { "show", "-t", "0", "ad" };
  74. bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
  75. EXPECT_TRUE(res);
  76. ASSERT_EQ(std::string(CmdL.FileList[0]), "ad");
  77. }
  78. }
  79. bool DoVoid(CommandLine &) { return false; }
  80. TEST(CommandLineTest,GetCommand)
  81. {
  82. CommandLine::Dispatch Cmds[] = { {"install",&DoVoid}, {"remove", &DoVoid}, {0,0} };
  83. {
  84. char const * argv[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
  85. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  86. EXPECT_STREQ("remove", com);
  87. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  88. ::Configuration c;
  89. CommandLine CmdL(Args.data(), &c);
  90. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  91. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  92. EXPECT_TRUE(c.FindB("APT::Get::Download-Only"));
  93. ASSERT_EQ(2, CmdL.FileSize());
  94. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  95. EXPECT_EQ(std::string(CmdL.FileList[1]), "foo");
  96. }
  97. {
  98. char const * argv[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" };
  99. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  100. EXPECT_STREQ("remove", com);
  101. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  102. ::Configuration c;
  103. CommandLine CmdL(Args.data(), &c);
  104. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  105. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  106. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  107. ASSERT_EQ(3, CmdL.FileSize());
  108. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  109. EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
  110. EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
  111. }
  112. {
  113. char const * argv[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" };
  114. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  115. EXPECT_STREQ("remove", com);
  116. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  117. ::Configuration c;
  118. CommandLine CmdL(Args.data(), &c);
  119. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  120. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  121. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  122. ASSERT_EQ(CmdL.FileSize(), 3);
  123. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  124. EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
  125. EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
  126. }
  127. {
  128. char const * argv[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
  129. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  130. EXPECT_STREQ("install", com);
  131. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  132. ::Configuration c;
  133. CommandLine CmdL(Args.data(), &c);
  134. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  135. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  136. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  137. ASSERT_EQ(CmdL.FileSize(), 4);
  138. EXPECT_EQ(std::string(CmdL.FileList[0]), "install");
  139. EXPECT_EQ(std::string(CmdL.FileList[1]), "remove");
  140. EXPECT_EQ(std::string(CmdL.FileList[2]), "-d");
  141. EXPECT_EQ(std::string(CmdL.FileList[3]), "foo");
  142. }
  143. }