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