blob: c27d27a16b94a6841499c71ff1a787d0339f6e7f [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
124TEST(CommandLineOptionsTests, CornerCases) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100125 std::string foo;
126 std::string bar;
Mårten Kongstad02751232018-04-27 13:16:32 +0200127 bool baz = false;
128 CommandLineOptions opts = CommandLineOptions("test")
129 .MandatoryOption("--foo", "", &foo)
130 .OptionalFlag("--baz", "", &baz)
131 .OptionalOption("--bar", "", &bar);
132 std::ostream fakeStdErr(nullptr);
133 bool success = opts.Parse({"--unexpected"}, fakeStdErr);
134 ASSERT_FALSE(success);
135
136 success = opts.Parse({"--bar", "BAR"}, fakeStdErr);
137 ASSERT_FALSE(success);
138
139 success = opts.Parse({"--baz", "--foo", "FOO"}, fakeStdErr);
140 ASSERT_TRUE(success);
141 ASSERT_TRUE(baz);
142 ASSERT_EQ(foo, "FOO");
143}
144
145TEST(CommandLineOptionsTests, ConvertArgvToVector) {
146 const char* argv[] = {
147 "program-name",
148 "--foo",
149 "FOO",
150 nullptr,
151 };
152 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(3, argv);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100153 ASSERT_EQ(v->size(), 2UL);
Mårten Kongstad02751232018-04-27 13:16:32 +0200154 ASSERT_EQ((*v)[0], "--foo");
155 ASSERT_EQ((*v)[1], "FOO");
156}
157
158TEST(CommandLineOptionsTests, ConvertArgvToVectorNoArgs) {
159 const char* argv[] = {
160 "program-name",
161 nullptr,
162 };
163 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(1, argv);
Mårten Kongstadb8779022018-11-29 09:53:17 +0100164 ASSERT_EQ(v->size(), 0UL);
Mårten Kongstad02751232018-04-27 13:16:32 +0200165}
166
167TEST(CommandLineOptionsTests, Usage) {
Mårten Kongstadb8779022018-11-29 09:53:17 +0100168 std::string arg1;
169 std::string arg2;
170 std::string arg3;
171 std::string arg4;
172 bool arg5 = false;
173 bool arg6 = false;
Mårten Kongstad02751232018-04-27 13:16:32 +0200174 std::vector<std::string> arg7;
175 CommandLineOptions opts = CommandLineOptions("test")
176 .MandatoryOption("--aa", "description-aa", &arg1)
177 .OptionalFlag("--bb", "description-bb", &arg5)
178 .OptionalOption("--cc", "description-cc", &arg2)
179 .OptionalOption("--dd", "description-dd", &arg3)
180 .MandatoryOption("--ee", "description-ee", &arg4)
181 .OptionalFlag("--ff", "description-ff", &arg6)
182 .MandatoryOption("--gg", "description-gg", &arg7);
183 std::stringstream stream;
184 opts.Usage(stream);
185 const std::string s = stream.str();
186 ASSERT_NE(s.find("usage: test --aa arg [--bb] [--cc arg] [--dd arg] --ee arg [--ff] --gg arg "
187 "[--gg arg [..]]"),
188 std::string::npos);
189 ASSERT_NE(s.find("--aa arg description-aa"), std::string::npos);
190 ASSERT_NE(s.find("--ff description-ff"), std::string::npos);
191 ASSERT_NE(s.find("--gg arg description-gg (can be provided multiple times)"),
192 std::string::npos);
193}
194
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100195} // namespace android::idmap2