blob: 7d1f56b8b5091037ab51172bf3cbde142e70a873 [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) {
45 bool foo = true, bar = false;
46
47 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) {
64 std::string foo, bar;
65 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);
93 ASSERT_EQ(args.size(), 2u);
94 ASSERT_EQ(args[0], "FOO");
95 ASSERT_EQ(args[1], "BAR");
96}
97
98TEST(CommandLineOptionsTests, OptionalOption) {
99 std::string foo, bar;
100 CommandLineOptions opts = CommandLineOptions("test")
101 .OptionalOption("--foo", "", &foo)
102 .OptionalOption("--bar", "", &bar);
103 std::ostream fakeStdErr(nullptr);
104 bool success = opts.Parse({"--foo", "FOO", "--bar", "BAR"}, fakeStdErr);
105 ASSERT_TRUE(success);
106 ASSERT_EQ(foo, "FOO");
107 ASSERT_EQ(bar, "BAR");
108
109 success = opts.Parse({"--foo", "BAZ"}, fakeStdErr);
110 ASSERT_TRUE(success);
111 ASSERT_EQ(foo, "BAZ");
112
113 success = opts.Parse({"--foo"}, fakeStdErr);
114 ASSERT_FALSE(success);
115
116 success = opts.Parse({"--foo", "--bar", "BAR"}, fakeStdErr);
117 ASSERT_FALSE(success);
118
119 success = opts.Parse({"--foo", "FOO", "--bar"}, fakeStdErr);
120 ASSERT_FALSE(success);
121}
122
123TEST(CommandLineOptionsTests, CornerCases) {
124 std::string foo, bar;
125 bool baz = false;
126 CommandLineOptions opts = CommandLineOptions("test")
127 .MandatoryOption("--foo", "", &foo)
128 .OptionalFlag("--baz", "", &baz)
129 .OptionalOption("--bar", "", &bar);
130 std::ostream fakeStdErr(nullptr);
131 bool success = opts.Parse({"--unexpected"}, fakeStdErr);
132 ASSERT_FALSE(success);
133
134 success = opts.Parse({"--bar", "BAR"}, fakeStdErr);
135 ASSERT_FALSE(success);
136
137 success = opts.Parse({"--baz", "--foo", "FOO"}, fakeStdErr);
138 ASSERT_TRUE(success);
139 ASSERT_TRUE(baz);
140 ASSERT_EQ(foo, "FOO");
141}
142
143TEST(CommandLineOptionsTests, ConvertArgvToVector) {
144 const char* argv[] = {
145 "program-name",
146 "--foo",
147 "FOO",
148 nullptr,
149 };
150 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(3, argv);
151 ASSERT_EQ(v->size(), 2ul);
152 ASSERT_EQ((*v)[0], "--foo");
153 ASSERT_EQ((*v)[1], "FOO");
154}
155
156TEST(CommandLineOptionsTests, ConvertArgvToVectorNoArgs) {
157 const char* argv[] = {
158 "program-name",
159 nullptr,
160 };
161 std::unique_ptr<std::vector<std::string>> v = CommandLineOptions::ConvertArgvToVector(1, argv);
162 ASSERT_EQ(v->size(), 0ul);
163}
164
165TEST(CommandLineOptionsTests, Usage) {
166 std::string arg1, arg2, arg3, arg4;
167 bool arg5 = false, arg6 = false;
168 std::vector<std::string> arg7;
169 CommandLineOptions opts = CommandLineOptions("test")
170 .MandatoryOption("--aa", "description-aa", &arg1)
171 .OptionalFlag("--bb", "description-bb", &arg5)
172 .OptionalOption("--cc", "description-cc", &arg2)
173 .OptionalOption("--dd", "description-dd", &arg3)
174 .MandatoryOption("--ee", "description-ee", &arg4)
175 .OptionalFlag("--ff", "description-ff", &arg6)
176 .MandatoryOption("--gg", "description-gg", &arg7);
177 std::stringstream stream;
178 opts.Usage(stream);
179 const std::string s = stream.str();
180 ASSERT_NE(s.find("usage: test --aa arg [--bb] [--cc arg] [--dd arg] --ee arg [--ff] --gg arg "
181 "[--gg arg [..]]"),
182 std::string::npos);
183 ASSERT_NE(s.find("--aa arg description-aa"), std::string::npos);
184 ASSERT_NE(s.find("--ff description-ff"), std::string::npos);
185 ASSERT_NE(s.find("--gg arg description-gg (can be provided multiple times)"),
186 std::string::npos);
187}
188
189} // namespace idmap2
190} // namespace android