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.
 
 
 
 
 
 

156 lines
5.6 KiB

  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. #define EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }
  17. TEST(CommandLineTest,SaveInConfig)
  18. {
  19. EXPECT_CMD("apt-get install -sf",
  20. "apt-get", "install", "-sf");
  21. EXPECT_CMD("apt-cache -s apt -so Debug::test=Test",
  22. "apt-cache", "-s", "apt", "-so", "Debug::test=Test");
  23. 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. EXPECT_CMD("apt-cache -s apt --hallo test=1.0",
  26. "apt-cache", "-s", "apt", "--hallo", "test=1.0");
  27. }
  28. TEST(CommandLineTest,Parsing)
  29. {
  30. CommandLine::Args Args[] = {
  31. { 't', 0, "Test::Worked", 0 },
  32. { 'z', "zero", "Test::Zero", 0 },
  33. {0,0,0,0}
  34. };
  35. ::Configuration c;
  36. CommandLine CmdL(Args, &c);
  37. char const * argv[] = { "test", "--zero", "-t" };
  38. CmdL.Parse(3 , argv);
  39. EXPECT_TRUE(c.FindB("Test::Worked", false));
  40. EXPECT_TRUE(c.FindB("Test::Zero", false));
  41. c.Clear("Test");
  42. EXPECT_FALSE(c.FindB("Test::Worked", false));
  43. EXPECT_FALSE(c.FindB("Test::Zero", false));
  44. c.Set("Test::Zero", true);
  45. EXPECT_TRUE(c.FindB("Test::Zero", false));
  46. char const * argv2[] = { "test", "--no-zero", "-t" };
  47. CmdL.Parse(3 , argv2);
  48. EXPECT_TRUE(c.FindB("Test::Worked", false));
  49. EXPECT_FALSE(c.FindB("Test::Zero", false));
  50. }
  51. TEST(CommandLineTest, BoolParsing)
  52. {
  53. CommandLine::Args Args[] = {
  54. { 't', 0, "Test::Worked", 0 },
  55. {0,0,0,0}
  56. };
  57. ::Configuration c;
  58. CommandLine CmdL(Args, &c);
  59. // the commandline parser used to use strtol() on the argument
  60. // to check if the argument is a boolean expression - that
  61. // stopped after the "0". this test ensures that we always check
  62. // that the entire string was consumed by strtol
  63. {
  64. char const * argv[] = { "show", "-t", "0ad" };
  65. bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
  66. EXPECT_TRUE(res);
  67. ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad");
  68. }
  69. {
  70. char const * argv[] = { "show", "-t", "0", "ad" };
  71. bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
  72. EXPECT_TRUE(res);
  73. ASSERT_EQ(std::string(CmdL.FileList[0]), "ad");
  74. }
  75. }
  76. bool DoVoid(CommandLine &) { return false; }
  77. TEST(CommandLineTest,GetCommand)
  78. {
  79. CommandLine::Dispatch Cmds[] = { {"install",&DoVoid}, {"remove", &DoVoid}, {0,0} };
  80. {
  81. char const * argv[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
  82. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  83. EXPECT_STREQ("remove", com);
  84. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  85. ::Configuration c;
  86. CommandLine CmdL(Args.data(), &c);
  87. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  88. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  89. EXPECT_TRUE(c.FindB("APT::Get::Download-Only"));
  90. ASSERT_EQ(2, CmdL.FileSize());
  91. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  92. EXPECT_EQ(std::string(CmdL.FileList[1]), "foo");
  93. }
  94. {
  95. char const * argv[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" };
  96. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  97. EXPECT_STREQ("remove", com);
  98. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  99. ::Configuration c;
  100. CommandLine CmdL(Args.data(), &c);
  101. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  102. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  103. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  104. ASSERT_EQ(3, CmdL.FileSize());
  105. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  106. EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
  107. EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
  108. }
  109. {
  110. char const * argv[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" };
  111. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  112. EXPECT_STREQ("remove", com);
  113. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  114. ::Configuration c;
  115. CommandLine CmdL(Args.data(), &c);
  116. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  117. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  118. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  119. ASSERT_EQ(CmdL.FileSize(), 3);
  120. EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
  121. EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
  122. EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
  123. }
  124. {
  125. char const * argv[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
  126. char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
  127. EXPECT_STREQ("install", com);
  128. std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
  129. ::Configuration c;
  130. CommandLine CmdL(Args.data(), &c);
  131. ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
  132. EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
  133. EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
  134. ASSERT_EQ(CmdL.FileSize(), 4);
  135. EXPECT_EQ(std::string(CmdL.FileList[0]), "install");
  136. EXPECT_EQ(std::string(CmdL.FileList[1]), "remove");
  137. EXPECT_EQ(std::string(CmdL.FileList[2]), "-d");
  138. EXPECT_EQ(std::string(CmdL.FileList[3]), "foo");
  139. }
  140. }