blob: 243d23a0f2db62261b23a10cbf6cac6a0d0905bd [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 Kongstad02751232018-04-27 13:16:32 +020041namespace android {
42namespace idmap2 {
43
44TEST(CommandLineOptionsTests, Flag) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010045 bool foo = true;
46 bool bar = false;
Mårten Kongstad02751232018-04-27 13:16:32 +020047 CommandLineOptions opts =
48 CommandLineOptions("test").OptionalFlag("--foo", "", &foo).OptionalFlag("--bar", "", &bar);
49
50 std::ostream fakeStdErr(nullptr);
51 bool success = opts.Parse({"--foo", "--bar"}, fakeStdErr);
52 ASSERT_TRUE(success);
53 ASSERT_TRUE(foo);
54 ASSERT_TRUE(bar);
55
56 foo = bar = false;
57 success = opts.Parse({"--foo"}, fakeStdErr);
58 ASSERT_TRUE(success);
59 ASSERT_TRUE(foo);
60 ASSERT_FALSE(bar);
61}
62
63TEST(CommandLineOptionsTests, MandatoryOption) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010064 std::string foo;
65 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +020066 CommandLineOptions opts = CommandLineOptions("test")
67 .MandatoryOption("--foo", "", &foo)
68 .MandatoryOption("--bar", "", &bar);
69 std::ostream fakeStdErr(nullptr);
70 bool success = opts.Parse({"--foo", "FOO", "--bar", "BAR"}, fakeStdErr);
71 ASSERT_TRUE(success);
72 ASSERT_EQ(foo, "FOO");
73 ASSERT_EQ(bar, "BAR");
74
75 success = opts.Parse({"--foo"}, fakeStdErr);
76 ASSERT_FALSE(success);
77}
78
79TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsButExpectedOnce) {
80 std::string foo;
81 CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &foo);
82 std::ostream fakeStdErr(nullptr);
83 bool success = opts.Parse({"--foo", "FIRST", "--foo", "SECOND"}, fakeStdErr);
84 ASSERT_TRUE(success);
85 ASSERT_EQ(foo, "SECOND");
86}
87
88TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsAndExpectedOnceOrMore) {
89 std::vector<std::string> args;
90 CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &args);
91 std::ostream fakeStdErr(nullptr);
92 bool success = opts.Parse({"--foo", "FOO", "--foo", "BAR"}, fakeStdErr);
93 ASSERT_TRUE(success);
Mårten Kongstadb8779022018-11-29 09:53:17 +010094 ASSERT_EQ(args.size(), 2U);
Mårten Kongstad02751232018-04-27 13:16:32 +020095 ASSERT_EQ(args[0], "FOO");
96 ASSERT_EQ(args[1], "BAR");
97}
98
99TEST(CommandLineOptionsTests, OptionalOption) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100100 std::string foo;
101 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +0200102 CommandLineOptions opts = CommandLineOptions("test")
103 .OptionalOption("--foo", "", &foo)
104 .OptionalOption("--bar", "", &bar);
105 std::ostream fakeStdErr(nullptr);
106 bool success = opts.Parse({"--foo", "FOO", "--bar", "BAR"}, fakeStdErr);
107 ASSERT_TRUE(success);
108 ASSERT_EQ(foo, "FOO");
109 ASSERT_EQ(bar, "BAR");
110
111 success = opts.Parse({"--foo", "BAZ"}, fakeStdErr);
112 ASSERT_TRUE(success);
113 ASSERT_EQ(foo, "BAZ");
114
115 success = opts.Parse({"--foo"}, fakeStdErr);
116 ASSERT_FALSE(success);
117
118 success = opts.Parse({"--foo", "--bar", "BAR"}, fakeStdErr);
119 ASSERT_FALSE(success);
120
121 success = opts.Parse({"--foo", "FOO", "--bar"}, fakeStdErr);
122 ASSERT_FALSE(success);
123}
124
125TEST(CommandLineOptionsTests, CornerCases) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100126 std::string foo;
127 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +0200128 bool baz = false;
129 CommandLineOptions opts = CommandLineOptions("test")
130 .MandatoryOption("--foo", "", &foo)
131 .OptionalFlag("--baz", "", &baz)
132 .OptionalOption("--bar", "", &bar);
133 std::ostream fakeStdErr(nullptr);
134 bool success = opts.Parse({"--unexpected"}, fakeStdErr);
135 ASSERT_FALSE(success);
136
137 success = opts.Parse({"--bar", "BAR"}, fakeStdErr);
138 ASSERT_FALSE(success);
139
140 success = opts.Parse({"--baz", "--foo", "FOO"}, fakeStdErr);
141 ASSERT_TRUE(success);
142 ASSERT_TRUE(baz);
143 ASSERT_EQ(foo, "FOO");
144}
145
146TEST(CommandLineOptionsTests, ConvertArgvToVector) {
147 const char* argv[] = {
148 "program-name",
149 "--foo",
150 "FOO",
151 nullptr,
152 };
153 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(3, argv);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100154 ASSERT_EQ(v->size(), 2UL);
Mårten Kongstad02751232018-04-27 13:16:32 +0200155 ASSERT_EQ((*v)[0], "--foo");
156 ASSERT_EQ((*v)[1], "FOO");
157}
158
159TEST(CommandLineOptionsTests, ConvertArgvToVectorNoArgs) {
160 const char* argv[] = {
161 "program-name",
162 nullptr,
163 };
164 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(1, argv);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100165 ASSERT_EQ(v->size(), 0UL);
Mårten Kongstad02751232018-04-27 13:16:32 +0200166}
167
168TEST(CommandLineOptionsTests, Usage) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100169 std::string arg1;
170 std::string arg2;
171 std::string arg3;
172 std::string arg4;
173 bool arg5 = false;
174 bool arg6 = false;
Mårten Kongstad02751232018-04-27 13:16:32 +0200175 std::vector<std::string> arg7;
176 CommandLineOptions opts = CommandLineOptions("test")
177 .MandatoryOption("--aa", "description-aa", &arg1)
178 .OptionalFlag("--bb", "description-bb", &arg5)
179 .OptionalOption("--cc", "description-cc", &arg2)
180 .OptionalOption("--dd", "description-dd", &arg3)
181 .MandatoryOption("--ee", "description-ee", &arg4)
182 .OptionalFlag("--ff", "description-ff", &arg6)
183 .MandatoryOption("--gg", "description-gg", &arg7);
184 std::stringstream stream;
185 opts.Usage(stream);
186 const std::string s = stream.str();
187 ASSERT_NE(s.find("usage: test --aa arg [--bb] [--cc arg] [--dd arg] --ee arg [--ff] --gg arg "
188 "[--gg arg [..]]"),
189 std::string::npos);
190 ASSERT_NE(s.find("--aa arg description-aa"), std::string::npos);
191 ASSERT_NE(s.find("--ff description-ff"), std::string::npos);
192 ASSERT_NE(s.find("--gg arg description-gg (can be provided multiple times)"),
193 std::string::npos);
194}
195
196} // namespace idmap2
197} // namespace android