blob: 6e83fc9abdb12d64776bb1bc2b88e43759bc6473 [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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070028#include "TestHelpers.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020029#include "android-base/file.h"
30#include "androidfw/ApkAssets.h"
31#include "androidfw/Idmap.h"
32#include "androidfw/LoadedArsc.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070033#include "gmock/gmock.h"
34#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020035#include "idmap2/CommandLineOptions.h"
36#include "idmap2/Idmap.h"
37
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010038namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020039
40TEST(CommandLineOptionsTests, Flag) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010041 bool foo = true;
42 bool bar = false;
Mårten Kongstad02751232018-04-27 13:16:32 +020043 CommandLineOptions opts =
44 CommandLineOptions("test").OptionalFlag("--foo", "", &foo).OptionalFlag("--bar", "", &bar);
45
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +010046 auto success = opts.Parse({"--foo", "--bar"});
Mårten Kongstad02751232018-04-27 13:16:32 +020047 ASSERT_TRUE(success);
48 ASSERT_TRUE(foo);
49 ASSERT_TRUE(bar);
50
51 foo = bar = false;
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +010052 success = opts.Parse({"--foo"});
Mårten Kongstad02751232018-04-27 13:16:32 +020053 ASSERT_TRUE(success);
54 ASSERT_TRUE(foo);
55 ASSERT_FALSE(bar);
56}
57
58TEST(CommandLineOptionsTests, MandatoryOption) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010059 std::string foo;
60 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +020061 CommandLineOptions opts = CommandLineOptions("test")
62 .MandatoryOption("--foo", "", &foo)
63 .MandatoryOption("--bar", "", &bar);
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +010064 auto success = opts.Parse({"--foo", "FOO", "--bar", "BAR"});
Mårten Kongstad02751232018-04-27 13:16:32 +020065 ASSERT_TRUE(success);
66 ASSERT_EQ(foo, "FOO");
67 ASSERT_EQ(bar, "BAR");
68
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +010069 success = opts.Parse({"--foo"});
Mårten Kongstad02751232018-04-27 13:16:32 +020070 ASSERT_FALSE(success);
71}
72
73TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsButExpectedOnce) {
74 std::string foo;
75 CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &foo);
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +010076 auto success = opts.Parse({"--foo", "FIRST", "--foo", "SECOND"});
Mårten Kongstad02751232018-04-27 13:16:32 +020077 ASSERT_TRUE(success);
78 ASSERT_EQ(foo, "SECOND");
79}
80
81TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsAndExpectedOnceOrMore) {
82 std::vector<std::string> args;
83 CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &args);
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +010084 auto success = opts.Parse({"--foo", "FOO", "--foo", "BAR"});
Mårten Kongstad02751232018-04-27 13:16:32 +020085 ASSERT_TRUE(success);
Mårten Kongstadb8779022018-11-29 09:53:17 +010086 ASSERT_EQ(args.size(), 2U);
Mårten Kongstad02751232018-04-27 13:16:32 +020087 ASSERT_EQ(args[0], "FOO");
88 ASSERT_EQ(args[1], "BAR");
89}
90
91TEST(CommandLineOptionsTests, OptionalOption) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010092 std::string foo;
93 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +020094 CommandLineOptions opts = CommandLineOptions("test")
95 .OptionalOption("--foo", "", &foo)
96 .OptionalOption("--bar", "", &bar);
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +010097 auto success = opts.Parse({"--foo", "FOO", "--bar", "BAR"});
Mårten Kongstad02751232018-04-27 13:16:32 +020098 ASSERT_TRUE(success);
99 ASSERT_EQ(foo, "FOO");
100 ASSERT_EQ(bar, "BAR");
101
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100102 success = opts.Parse({"--foo", "BAZ"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200103 ASSERT_TRUE(success);
104 ASSERT_EQ(foo, "BAZ");
105
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100106 success = opts.Parse({"--foo"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200107 ASSERT_FALSE(success);
108
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100109 success = opts.Parse({"--foo", "--bar", "BAR"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200110 ASSERT_FALSE(success);
111
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100112 success = opts.Parse({"--foo", "FOO", "--bar"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200113 ASSERT_FALSE(success);
114}
115
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800116TEST(CommandLineOptionsTests, OptionalOptionList) {
117 std::vector<std::string> foo;
118 std::vector<std::string> bar;
119 CommandLineOptions opts = CommandLineOptions("test")
120 .OptionalOption("--foo", "", &foo)
121 .OptionalOption("--bar", "", &bar);
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100122 auto success = opts.Parse({"--foo", "FOO", "--bar", "BAR"});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800123 ASSERT_TRUE(success);
124 ASSERT_EQ(foo.size(), 1U);
125 ASSERT_EQ(foo[0], "FOO");
126 ASSERT_EQ(bar.size(), 1U);
127 ASSERT_EQ(bar[0], "BAR");
128
129 foo.clear();
130 bar.clear();
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100131 success = opts.Parse({"--foo", "BAZ"});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800132 ASSERT_TRUE(success);
133 ASSERT_EQ(foo.size(), 1U);
134 ASSERT_EQ(foo[0], "BAZ");
135 ASSERT_EQ(bar.size(), 0U);
136
137 foo.clear();
138 bar.clear();
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100139 success = opts.Parse({"--foo", "BAZ", "--foo", "BIZ", "--bar", "FIZ", "--bar", "FUZZ"});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800140 ASSERT_TRUE(success);
141 ASSERT_EQ(foo.size(), 2U);
142 ASSERT_EQ(foo[0], "BAZ");
143 ASSERT_EQ(foo[1], "BIZ");
144 ASSERT_EQ(bar.size(), 2U);
145 ASSERT_EQ(bar[0], "FIZ");
146 ASSERT_EQ(bar[1], "FUZZ");
147
148 foo.clear();
149 bar.clear();
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100150 success = opts.Parse({"--foo"});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800151 ASSERT_FALSE(success);
152
153 foo.clear();
154 bar.clear();
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100155 success = opts.Parse({"--foo", "--bar", "BAR"});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800156 ASSERT_FALSE(success);
157
158 foo.clear();
159 bar.clear();
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100160 success = opts.Parse({"--foo", "FOO", "--bar"});
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800161 ASSERT_FALSE(success);
162}
163
Mårten Kongstad02751232018-04-27 13:16:32 +0200164TEST(CommandLineOptionsTests, CornerCases) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100165 std::string foo;
166 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +0200167 bool baz = false;
168 CommandLineOptions opts = CommandLineOptions("test")
169 .MandatoryOption("--foo", "", &foo)
170 .OptionalFlag("--baz", "", &baz)
171 .OptionalOption("--bar", "", &bar);
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100172 auto success = opts.Parse({"--unexpected"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200173 ASSERT_FALSE(success);
174
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100175 success = opts.Parse({"--bar", "BAR"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200176 ASSERT_FALSE(success);
177
Mårten Kongstad0c6ff1d2019-02-07 02:21:56 +0100178 success = opts.Parse({"--baz", "--foo", "FOO"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200179 ASSERT_TRUE(success);
180 ASSERT_TRUE(baz);
181 ASSERT_EQ(foo, "FOO");
182}
183
184TEST(CommandLineOptionsTests, ConvertArgvToVector) {
185 const char* argv[] = {
186 "program-name",
187 "--foo",
188 "FOO",
189 nullptr,
190 };
191 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(3, argv);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100192 ASSERT_EQ(v->size(), 2UL);
Mårten Kongstad02751232018-04-27 13:16:32 +0200193 ASSERT_EQ((*v)[0], "--foo");
194 ASSERT_EQ((*v)[1], "FOO");
195}
196
197TEST(CommandLineOptionsTests, ConvertArgvToVectorNoArgs) {
198 const char* argv[] = {
199 "program-name",
200 nullptr,
201 };
202 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(1, argv);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100203 ASSERT_EQ(v->size(), 0UL);
Mårten Kongstad02751232018-04-27 13:16:32 +0200204}
205
206TEST(CommandLineOptionsTests, Usage) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100207 std::string arg1;
208 std::string arg2;
209 std::string arg3;
210 std::string arg4;
211 bool arg5 = false;
212 bool arg6 = false;
Mårten Kongstad02751232018-04-27 13:16:32 +0200213 std::vector<std::string> arg7;
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800214 std::vector<std::string> arg8;
Mårten Kongstad02751232018-04-27 13:16:32 +0200215 CommandLineOptions opts = CommandLineOptions("test")
216 .MandatoryOption("--aa", "description-aa", &arg1)
217 .OptionalFlag("--bb", "description-bb", &arg5)
218 .OptionalOption("--cc", "description-cc", &arg2)
219 .OptionalOption("--dd", "description-dd", &arg3)
220 .MandatoryOption("--ee", "description-ee", &arg4)
221 .OptionalFlag("--ff", "description-ff", &arg6)
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800222 .MandatoryOption("--gg", "description-gg", &arg7)
223 .OptionalOption("--hh", "description-hh", &arg8);
Mårten Kongstad02751232018-04-27 13:16:32 +0200224 std::stringstream stream;
225 opts.Usage(stream);
226 const std::string s = stream.str();
227 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 -0800228 "[--gg arg [..]] [--hh arg [..]]"),
Mårten Kongstad02751232018-04-27 13:16:32 +0200229 std::string::npos);
230 ASSERT_NE(s.find("--aa arg description-aa"), std::string::npos);
231 ASSERT_NE(s.find("--ff description-ff"), std::string::npos);
232 ASSERT_NE(s.find("--gg arg description-gg (can be provided multiple times)"),
233 std::string::npos);
234}
235
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100236} // namespace android::idmap2