]> git.saurik.com Git - apt.git/blame - test/libapt/commandline_test.cc
abort connection on '.' target replies in SRV
[apt.git] / test / libapt / commandline_test.cc
CommitLineData
453b82a3
DK
1#include <config.h>
2
ae2be086 3#include <apt-pkg/cmndline.h>
453b82a3 4#include <apt-pkg/configuration.h>
c4b91cbe 5#include <apt-private/private-cmndline.h>
ae2be086 6
f00832cc
DK
7#include <gtest/gtest.h>
8
9class 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
f00832cc
DK
20TEST(CommandLineTest,SaveInConfig)
21{
6079b276
DK
22#define APT_EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }
23 APT_EXPECT_CMD("apt-get install -sf",
f00832cc 24 "apt-get", "install", "-sf");
6079b276 25 APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=Test",
f00832cc 26 "apt-cache", "-s", "apt", "-so", "Debug::test=Test");
6079b276 27 APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"",
f00832cc 28 "apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test");
6079b276 29 APT_EXPECT_CMD("apt-cache -s apt --hallo test=1.0",
f00832cc 30 "apt-cache", "-s", "apt", "--hallo", "test=1.0");
6079b276 31#undef APT_EXPECT_CMD
f00832cc
DK
32}
33TEST(CommandLineTest,Parsing)
ae2be086
DH
34{
35 CommandLine::Args Args[] = {
36 { 't', 0, "Test::Worked", 0 },
bc7a59dd 37 { 'T', "testing", "Test::Worked", CommandLine::HasArg },
ae2be086 38 { 'z', "zero", "Test::Zero", 0 },
bc7a59dd 39 { 'o', "option", 0, CommandLine::ArbItem },
ae2be086
DH
40 {0,0,0,0}
41 };
f00832cc
DK
42 ::Configuration c;
43 CommandLine CmdL(Args, &c);
7a6d9076 44
ae2be086
DH
45 char const * argv[] = { "test", "--zero", "-t" };
46 CmdL.Parse(3 , argv);
f00832cc
DK
47 EXPECT_TRUE(c.FindB("Test::Worked", false));
48 EXPECT_TRUE(c.FindB("Test::Zero", false));
ae2be086 49
f00832cc
DK
50 c.Clear("Test");
51 EXPECT_FALSE(c.FindB("Test::Worked", false));
52 EXPECT_FALSE(c.FindB("Test::Zero", false));
7a6d9076 53
f00832cc
DK
54 c.Set("Test::Zero", true);
55 EXPECT_TRUE(c.FindB("Test::Zero", false));
7a6d9076
DK
56
57 char const * argv2[] = { "test", "--no-zero", "-t" };
58 CmdL.Parse(3 , argv2);
f00832cc
DK
59 EXPECT_TRUE(c.FindB("Test::Worked", false));
60 EXPECT_FALSE(c.FindB("Test::Zero", false));
bc7a59dd
DK
61
62 c.Clear("Test");
63 {
64 char const * argv[] = { "test", "-T", "yes" };
65 CmdL.Parse(3 , argv);
66 EXPECT_TRUE(c.FindB("Test::Worked", false));
67 EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
68 EXPECT_EQ(0, CmdL.FileSize());
69 }
70 c.Clear("Test");
71 {
72 char const * argv[] = { "test", "-T=yes" };
73 CmdL.Parse(2 , argv);
74 EXPECT_TRUE(c.Exists("Test::Worked"));
75 EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
76 EXPECT_EQ(0, CmdL.FileSize());
77 }
78 c.Clear("Test");
79 {
80 char const * argv[] = { "test", "-T=", "yes" };
81 CmdL.Parse(3 , argv);
82 EXPECT_TRUE(c.Exists("Test::Worked"));
83 EXPECT_EQ("no", c.Find("Test::Worked", "no"));
84 EXPECT_EQ(1, CmdL.FileSize());
85 }
86
87 c.Clear("Test");
88 {
89 char const * argv[] = { "test", "--testing", "yes" };
90 CmdL.Parse(3 , argv);
91 EXPECT_TRUE(c.FindB("Test::Worked", false));
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(2 , argv);
99 EXPECT_TRUE(c.Exists("Test::Worked"));
100 EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
101 EXPECT_EQ(0, CmdL.FileSize());
102 }
103 c.Clear("Test");
104 {
105 char const * argv[] = { "test", "--testing=", "yes" };
106 CmdL.Parse(3 , argv);
107 EXPECT_TRUE(c.Exists("Test::Worked"));
108 EXPECT_EQ("no", c.Find("Test::Worked", "no"));
109 EXPECT_EQ(1, CmdL.FileSize());
110 }
111
112 c.Clear("Test");
113 {
114 char const * argv[] = { "test", "-o", "test::worked=yes" };
115 CmdL.Parse(3 , argv);
116 EXPECT_TRUE(c.FindB("Test::Worked", false));
117 EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
118 }
119 c.Clear("Test");
120 {
121 char const * argv[] = { "test", "-o", "test::worked=" };
122 CmdL.Parse(3 , argv);
123 EXPECT_TRUE(c.Exists("Test::Worked"));
124 EXPECT_EQ("no", c.Find("Test::Worked", "no"));
125 }
126 c.Clear("Test");
127 {
128 char const * argv[] = { "test", "-o", "test::worked=", "yes" };
129 CmdL.Parse(4 , argv);
130 EXPECT_TRUE(c.Exists("Test::Worked"));
131 EXPECT_EQ("no", c.Find("Test::Worked", "no"));
132 }
133 c.Clear("Test");
ae2be086 134}
08be0ca3
MV
135
136TEST(CommandLineTest, BoolParsing)
137{
138 CommandLine::Args Args[] = {
139 { 't', 0, "Test::Worked", 0 },
140 {0,0,0,0}
141 };
142 ::Configuration c;
143 CommandLine CmdL(Args, &c);
144
145 // the commandline parser used to use strtol() on the argument
146 // to check if the argument is a boolean expression - that
147 // stopped after the "0". this test ensures that we always check
148 // that the entire string was consumed by strtol
149 {
150 char const * argv[] = { "show", "-t", "0ad" };
151 bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
152 EXPECT_TRUE(res);
153 ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad");
154 }
155
156 {
157 char const * argv[] = { "show", "-t", "0", "ad" };
158 bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
159 EXPECT_TRUE(res);
160 ASSERT_EQ(std::string(CmdL.FileList[0]), "ad");
161 }
162
163}
c4b91cbe 164
d8a57c19 165static bool DoVoid(CommandLine &) { return false; }
c4b91cbe
DK
166
167TEST(CommandLineTest,GetCommand)
168{
169 CommandLine::Dispatch Cmds[] = { {"install",&DoVoid}, {"remove", &DoVoid}, {0,0} };
170 {
171 char const * argv[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
172 char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
173 EXPECT_STREQ("remove", com);
011188e3 174 std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
c4b91cbe
DK
175 ::Configuration c;
176 CommandLine CmdL(Args.data(), &c);
177 ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
178 EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
179 EXPECT_TRUE(c.FindB("APT::Get::Download-Only"));
180 ASSERT_EQ(2, CmdL.FileSize());
181 EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
182 EXPECT_EQ(std::string(CmdL.FileList[1]), "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);
011188e3 188 std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
c4b91cbe
DK
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(3, CmdL.FileSize());
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", "-t", "unstable", "--", "remove", "-d", "foo" };
201 char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
202 EXPECT_STREQ("remove", com);
011188e3 203 std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
c4b91cbe
DK
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(), 3);
210 EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
211 EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
212 EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
213 }
214 {
215 char const * argv[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
216 char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
217 EXPECT_STREQ("install", com);
011188e3 218 std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
c4b91cbe
DK
219 ::Configuration c;
220 CommandLine CmdL(Args.data(), &c);
221 ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
222 EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
223 EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
224 ASSERT_EQ(CmdL.FileSize(), 4);
225 EXPECT_EQ(std::string(CmdL.FileList[0]), "install");
226 EXPECT_EQ(std::string(CmdL.FileList[1]), "remove");
227 EXPECT_EQ(std::string(CmdL.FileList[2]), "-d");
228 EXPECT_EQ(std::string(CmdL.FileList[3]), "foo");
229 }
230}