blob: 39f18d3336afb3c79e6bcb0b5cbec68922cea3bf [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sys/mman.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20#include <unistd.h>
21
22#include <fstream>
23#include <memory>
24#include <sstream>
25#include <string>
26#include <vector>
27
28#include "gmock/gmock.h"
29#include "gtest/gtest.h"
30
31#include "android-base/file.h"
32#include "androidfw/ApkAssets.h"
33#include "androidfw/Idmap.h"
34#include "androidfw/LoadedArsc.h"
35
36#include "idmap2/CommandLineOptions.h"
37#include "idmap2/Idmap.h"
38
39#include "TestHelpers.h"
40
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010041namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020042
43TEST(CommandLineOptionsTests, Flag) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010044 bool foo = true;
45 bool bar = false;
Mårten Kongstad02751232018-04-27 13:16:32 +020046 CommandLineOptions opts =
47 CommandLineOptions("test").OptionalFlag("--foo", "", &foo).OptionalFlag("--bar", "", &bar);
48
49 std::ostream fakeStdErr(nullptr);
50 bool success = opts.Parse({"--foo", "--bar"}, fakeStdErr);
51 ASSERT_TRUE(success);
52 ASSERT_TRUE(foo);
53 ASSERT_TRUE(bar);
54
55 foo = bar = false;
56 success = opts.Parse({"--foo"}, fakeStdErr);
57 ASSERT_TRUE(success);
58 ASSERT_TRUE(foo);
59 ASSERT_FALSE(bar);
60}
61
62TEST(CommandLineOptionsTests, MandatoryOption) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010063 std::string foo;
64 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +020065 CommandLineOptions opts = CommandLineOptions("test")
66 .MandatoryOption("--foo", "", &foo)
67 .MandatoryOption("--bar", "", &bar);
68 std::ostream fakeStdErr(nullptr);
69 bool success = opts.Parse({"--foo", "FOO", "--bar", "BAR"}, fakeStdErr);
70 ASSERT_TRUE(success);
71 ASSERT_EQ(foo, "FOO");
72 ASSERT_EQ(bar, "BAR");
73
74 success = opts.Parse({"--foo"}, fakeStdErr);
75 ASSERT_FALSE(success);
76}
77
78TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsButExpectedOnce) {
79 std::string foo;
80 CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &foo);
81 std::ostream fakeStdErr(nullptr);
82 bool success = opts.Parse({"--foo", "FIRST", "--foo", "SECOND"}, fakeStdErr);
83 ASSERT_TRUE(success);
84 ASSERT_EQ(foo, "SECOND");
85}
86
87TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsAndExpectedOnceOrMore) {
88 std::vector<std::string> args;
89 CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &args);
90 std::ostream fakeStdErr(nullptr);
91 bool success = opts.Parse({"--foo", "FOO", "--foo", "BAR"}, fakeStdErr);
92 ASSERT_TRUE(success);
Mårten Kongstadb8779022018-11-29 09:53:17 +010093 ASSERT_EQ(args.size(), 2U);
Mårten Kongstad02751232018-04-27 13:16:32 +020094 ASSERT_EQ(args[0], "FOO");
95 ASSERT_EQ(args[1], "BAR");
96}
97
98TEST(CommandLineOptionsTests, OptionalOption) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010099 std::string foo;
100 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +0200101 CommandLineOptions opts = CommandLineOptions("test")
102 .OptionalOption("--foo", "", &foo)
103 .OptionalOption("--bar", "", &bar);
104 std::ostream fakeStdErr(nullptr);
105 bool success = opts.Parse({"--foo", "FOO", "--bar", "BAR"}, fakeStdErr);
106 ASSERT_TRUE(success);
107 ASSERT_EQ(foo, "FOO");
108 ASSERT_EQ(bar, "BAR");
109
110 success = opts.Parse({"--foo", "BAZ"}, fakeStdErr);
111 ASSERT_TRUE(success);
112 ASSERT_EQ(foo, "BAZ");
113
114 success = opts.Parse({"--foo"}, fakeStdErr);
115 ASSERT_FALSE(success);
116
117 success = opts.Parse({"--foo", "--bar", "BAR"}, fakeStdErr);
118 ASSERT_FALSE(success);
119
120 success = opts.Parse({"--foo", "FOO", "--bar"}, fakeStdErr);
121 ASSERT_FALSE(success);
122}
123
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800124TEST(CommandLineOptionsTests, OptionalOptionList) {
125 std::vector<std::string> foo;
126 std::vector<std::string> bar;
127 CommandLineOptions opts = CommandLineOptions("test")
128 .OptionalOption("--foo", "", &foo)
129 .OptionalOption("--bar", "", &bar);
130 std::ostream fakeStdErr(nullptr);
131 bool success = opts.Parse({"--foo", "FOO", "--bar", "BAR"}, fakeStdErr);
132 ASSERT_TRUE(success);
133 ASSERT_EQ(foo.size(), 1U);
134 ASSERT_EQ(foo[0], "FOO");
135 ASSERT_EQ(bar.size(), 1U);
136 ASSERT_EQ(bar[0], "BAR");
137
138 foo.clear();
139 bar.clear();
140 success = opts.Parse({"--foo", "BAZ"}, fakeStdErr);
141 ASSERT_TRUE(success);
142 ASSERT_EQ(foo.size(), 1U);
143 ASSERT_EQ(foo[0], "BAZ");
144 ASSERT_EQ(bar.size(), 0U);
145
146 foo.clear();
147 bar.clear();
148 success =
149 opts.Parse({"--foo", "BAZ", "--foo", "BIZ", "--bar", "FIZ", "--bar", "FUZZ"}, fakeStdErr);
150 ASSERT_TRUE(success);
151 ASSERT_EQ(foo.size(), 2U);
152 ASSERT_EQ(foo[0], "BAZ");
153 ASSERT_EQ(foo[1], "BIZ");
154 ASSERT_EQ(bar.size(), 2U);
155 ASSERT_EQ(bar[0], "FIZ");
156 ASSERT_EQ(bar[1], "FUZZ");
157
158 foo.clear();
159 bar.clear();
160 success = opts.Parse({"--foo"}, fakeStdErr);
161 ASSERT_FALSE(success);
162
163 foo.clear();
164 bar.clear();
165 success = opts.Parse({"--foo", "--bar", "BAR"}, fakeStdErr);
166 ASSERT_FALSE(success);
167
168 foo.clear();
169 bar.clear();
170 success = opts.Parse({"--foo", "FOO", "--bar"}, fakeStdErr);
171 ASSERT_FALSE(success);
172}
173
Mårten Kongstad02751232018-04-27 13:16:32 +0200174TEST(CommandLineOptionsTests, CornerCases) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100175 std::string foo;
176 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +0200177 bool baz = false;
178 CommandLineOptions opts = CommandLineOptions("test")
179 .MandatoryOption("--foo", "", &foo)
180 .OptionalFlag("--baz", "", &baz)
181 .OptionalOption("--bar", "", &bar);
182 std::ostream fakeStdErr(nullptr);
183 bool success = opts.Parse({"--unexpected"}, fakeStdErr);
184 ASSERT_FALSE(success);
185
186 success = opts.Parse({"--bar", "BAR"}, fakeStdErr);
187 ASSERT_FALSE(success);
188
189 success = opts.Parse({"--baz", "--foo", "FOO"}, fakeStdErr);
190 ASSERT_TRUE(success);
191 ASSERT_TRUE(baz);
192 ASSERT_EQ(foo, "FOO");
193}
194
195TEST(CommandLineOptionsTests, ConvertArgvToVector) {
196 const char* argv[] = {
197 "program-name",
198 "--foo",
199 "FOO",
200 nullptr,
201 };
202 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(3, argv);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100203 ASSERT_EQ(v->size(), 2UL);
Mårten Kongstad02751232018-04-27 13:16:32 +0200204 ASSERT_EQ((*v)[0], "--foo");
205 ASSERT_EQ((*v)[1], "FOO");
206}
207
208TEST(CommandLineOptionsTests, ConvertArgvToVectorNoArgs) {
209 const char* argv[] = {
210 "program-name",
211 nullptr,
212 };
213 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(1, argv);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100214 ASSERT_EQ(v->size(), 0UL);
Mårten Kongstad02751232018-04-27 13:16:32 +0200215}
216
217TEST(CommandLineOptionsTests, Usage) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100218 std::string arg1;
219 std::string arg2;
220 std::string arg3;
221 std::string arg4;
222 bool arg5 = false;
223 bool arg6 = false;
Mårten Kongstad02751232018-04-27 13:16:32 +0200224 std::vector<std::string> arg7;
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800225 std::vector<std::string> arg8;
Mårten Kongstad02751232018-04-27 13:16:32 +0200226 CommandLineOptions opts = CommandLineOptions("test")
227 .MandatoryOption("--aa", "description-aa", &arg1)
228 .OptionalFlag("--bb", "description-bb", &arg5)
229 .OptionalOption("--cc", "description-cc", &arg2)
230 .OptionalOption("--dd", "description-dd", &arg3)
231 .MandatoryOption("--ee", "description-ee", &arg4)
232 .OptionalFlag("--ff", "description-ff", &arg6)
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800233 .MandatoryOption("--gg", "description-gg", &arg7)
234 .OptionalOption("--hh", "description-hh", &arg8);
Mårten Kongstad02751232018-04-27 13:16:32 +0200235 std::stringstream stream;
236 opts.Usage(stream);
237 const std::string s = stream.str();
238 ASSERT_NE(s.find("usage: test --aa arg [--bb] [--cc arg] [--dd arg] --ee arg [--ff] --gg arg "
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800239 "[--gg arg [..]] [--hh arg [..]]"),
Mårten Kongstad02751232018-04-27 13:16:32 +0200240 std::string::npos);
241 ASSERT_NE(s.find("--aa arg description-aa"), std::string::npos);
242 ASSERT_NE(s.find("--ff description-ff"), std::string::npos);
243 ASSERT_NE(s.find("--gg arg description-gg (can be provided multiple times)"),
244 std::string::npos);
245}
246
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100247} // namespace android::idmap2