blob: b04b25660ee4330af4231097a1660af494b7dde7 [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
41using ::testing::NotNull;
42
43namespace android {
44namespace idmap2 {
45
46TEST(CommandLineOptionsTests, Flag) {
47 bool foo = true, bar = false;
48
49 CommandLineOptions opts =
50 CommandLineOptions("test").OptionalFlag("--foo", "", &foo).OptionalFlag("--bar", "", &bar);
51
52 std::ostream fakeStdErr(nullptr);
53 bool success = opts.Parse({"--foo", "--bar"}, fakeStdErr);
54 ASSERT_TRUE(success);
55 ASSERT_TRUE(foo);
56 ASSERT_TRUE(bar);
57
58 foo = bar = false;
59 success = opts.Parse({"--foo"}, fakeStdErr);
60 ASSERT_TRUE(success);
61 ASSERT_TRUE(foo);
62 ASSERT_FALSE(bar);
63}
64
65TEST(CommandLineOptionsTests, MandatoryOption) {
66 std::string foo, bar;
67 CommandLineOptions opts = CommandLineOptions("test")
68 .MandatoryOption("--foo", "", &foo)
69 .MandatoryOption("--bar", "", &bar);
70 std::ostream fakeStdErr(nullptr);
71 bool success = opts.Parse({"--foo", "FOO", "--bar", "BAR"}, fakeStdErr);
72 ASSERT_TRUE(success);
73 ASSERT_EQ(foo, "FOO");
74 ASSERT_EQ(bar, "BAR");
75
76 success = opts.Parse({"--foo"}, fakeStdErr);
77 ASSERT_FALSE(success);
78}
79
80TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsButExpectedOnce) {
81 std::string foo;
82 CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &foo);
83 std::ostream fakeStdErr(nullptr);
84 bool success = opts.Parse({"--foo", "FIRST", "--foo", "SECOND"}, fakeStdErr);
85 ASSERT_TRUE(success);
86 ASSERT_EQ(foo, "SECOND");
87}
88
89TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsAndExpectedOnceOrMore) {
90 std::vector<std::string> args;
91 CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &args);
92 std::ostream fakeStdErr(nullptr);
93 bool success = opts.Parse({"--foo", "FOO", "--foo", "BAR"}, fakeStdErr);
94 ASSERT_TRUE(success);
95 ASSERT_EQ(args.size(), 2u);
96 ASSERT_EQ(args[0], "FOO");
97 ASSERT_EQ(args[1], "BAR");
98}
99
100TEST(CommandLineOptionsTests, OptionalOption) {
101 std::string foo, bar;
102 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) {
126 std::string foo, bar;
127 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);
153 ASSERT_EQ(v->size(), 2ul);
154 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);
164 ASSERT_EQ(v->size(), 0ul);
165}
166
167TEST(CommandLineOptionsTests, Usage) {
168 std::string arg1, arg2, arg3, arg4;
169 bool arg5 = false, arg6 = false;
170 std::vector<std::string> arg7;
171 CommandLineOptions opts = CommandLineOptions("test")
172 .MandatoryOption("--aa", "description-aa", &arg1)
173 .OptionalFlag("--bb", "description-bb", &arg5)
174 .OptionalOption("--cc", "description-cc", &arg2)
175 .OptionalOption("--dd", "description-dd", &arg3)
176 .MandatoryOption("--ee", "description-ee", &arg4)
177 .OptionalFlag("--ff", "description-ff", &arg6)
178 .MandatoryOption("--gg", "description-gg", &arg7);
179 std::stringstream stream;
180 opts.Usage(stream);
181 const std::string s = stream.str();
182 ASSERT_NE(s.find("usage: test --aa arg [--bb] [--cc arg] [--dd arg] --ee arg [--ff] --gg arg "
183 "[--gg arg [..]]"),
184 std::string::npos);
185 ASSERT_NE(s.find("--aa arg description-aa"), std::string::npos);
186 ASSERT_NE(s.find("--ff description-ff"), std::string::npos);
187 ASSERT_NE(s.find("--gg arg description-gg (can be provided multiple times)"),
188 std::string::npos);
189}
190
191} // namespace idmap2
192} // namespace android