1 /*
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 "TestHelpers.h"
29 #include "android-base/file.h"
30 #include "androidfw/ApkAssets.h"
31 #include "androidfw/Idmap.h"
32 #include "androidfw/LoadedArsc.h"
33 #include "gmock/gmock.h"
34 #include "gtest/gtest.h"
35 #include "idmap2/CommandLineOptions.h"
36 #include "idmap2/Idmap.h"
37 
38 namespace android::idmap2 {
39 
TEST(CommandLineOptionsTests,Flag)40 TEST(CommandLineOptionsTests, Flag) {
41   bool foo = true;
42   bool bar = false;
43   CommandLineOptions opts =
44       CommandLineOptions("test").OptionalFlag("--foo", "", &foo).OptionalFlag("--bar", "", &bar);
45 
46   auto success = opts.Parse({"--foo", "--bar"});
47   ASSERT_TRUE(success);
48   ASSERT_TRUE(foo);
49   ASSERT_TRUE(bar);
50 
51   foo = bar = false;
52   success = opts.Parse({"--foo"});
53   ASSERT_TRUE(success);
54   ASSERT_TRUE(foo);
55   ASSERT_FALSE(bar);
56 }
57 
TEST(CommandLineOptionsTests,MandatoryOption)58 TEST(CommandLineOptionsTests, MandatoryOption) {
59   std::string foo;
60   std::string bar;
61   CommandLineOptions opts = CommandLineOptions("test")
62                                 .MandatoryOption("--foo", "", &foo)
63                                 .MandatoryOption("--bar", "", &bar);
64   auto success = opts.Parse({"--foo", "FOO", "--bar", "BAR"});
65   ASSERT_TRUE(success);
66   ASSERT_EQ(foo, "FOO");
67   ASSERT_EQ(bar, "BAR");
68 
69   success = opts.Parse({"--foo"});
70   ASSERT_FALSE(success);
71 }
72 
TEST(CommandLineOptionsTests,MandatoryOptionMultipleArgsButExpectedOnce)73 TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsButExpectedOnce) {
74   std::string foo;
75   CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &foo);
76   auto success = opts.Parse({"--foo", "FIRST", "--foo", "SECOND"});
77   ASSERT_TRUE(success);
78   ASSERT_EQ(foo, "SECOND");
79 }
80 
TEST(CommandLineOptionsTests,MandatoryOptionMultipleArgsAndExpectedOnceOrMore)81 TEST(CommandLineOptionsTests, MandatoryOptionMultipleArgsAndExpectedOnceOrMore) {
82   std::vector<std::string> args;
83   CommandLineOptions opts = CommandLineOptions("test").MandatoryOption("--foo", "", &args);
84   auto success = opts.Parse({"--foo", "FOO", "--foo", "BAR"});
85   ASSERT_TRUE(success);
86   ASSERT_EQ(args.size(), 2U);
87   ASSERT_EQ(args[0], "FOO");
88   ASSERT_EQ(args[1], "BAR");
89 }
90 
TEST(CommandLineOptionsTests,OptionalOption)91 TEST(CommandLineOptionsTests, OptionalOption) {
92   std::string foo;
93   std::string bar;
94   CommandLineOptions opts = CommandLineOptions("test")
95                                 .OptionalOption("--foo", "", &foo)
96                                 .OptionalOption("--bar", "", &bar);
97   auto success = opts.Parse({"--foo", "FOO", "--bar", "BAR"});
98   ASSERT_TRUE(success);
99   ASSERT_EQ(foo, "FOO");
100   ASSERT_EQ(bar, "BAR");
101 
102   success = opts.Parse({"--foo", "BAZ"});
103   ASSERT_TRUE(success);
104   ASSERT_EQ(foo, "BAZ");
105 
106   success = opts.Parse({"--foo"});
107   ASSERT_FALSE(success);
108 
109   success = opts.Parse({"--foo", "--bar", "BAR"});
110   ASSERT_FALSE(success);
111 
112   success = opts.Parse({"--foo", "FOO", "--bar"});
113   ASSERT_FALSE(success);
114 }
115 
TEST(CommandLineOptionsTests,OptionalOptionList)116 TEST(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);
122   auto success = opts.Parse({"--foo", "FOO", "--bar", "BAR"});
123   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();
131   success = opts.Parse({"--foo", "BAZ"});
132   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();
139   success = opts.Parse({"--foo", "BAZ", "--foo", "BIZ", "--bar", "FIZ", "--bar", "FUZZ"});
140   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();
150   success = opts.Parse({"--foo"});
151   ASSERT_FALSE(success);
152 
153   foo.clear();
154   bar.clear();
155   success = opts.Parse({"--foo", "--bar", "BAR"});
156   ASSERT_FALSE(success);
157 
158   foo.clear();
159   bar.clear();
160   success = opts.Parse({"--foo", "FOO", "--bar"});
161   ASSERT_FALSE(success);
162 }
163 
TEST(CommandLineOptionsTests,CornerCases)164 TEST(CommandLineOptionsTests, CornerCases) {
165   std::string foo;
166   std::string bar;
167   bool baz = false;
168   CommandLineOptions opts = CommandLineOptions("test")
169                                 .MandatoryOption("--foo", "", &foo)
170                                 .OptionalFlag("--baz", "", &baz)
171                                 .OptionalOption("--bar", "", &bar);
172   auto success = opts.Parse({"--unexpected"});
173   ASSERT_FALSE(success);
174 
175   success = opts.Parse({"--bar", "BAR"});
176   ASSERT_FALSE(success);
177 
178   success = opts.Parse({"--baz", "--foo", "FOO"});
179   ASSERT_TRUE(success);
180   ASSERT_TRUE(baz);
181   ASSERT_EQ(foo, "FOO");
182 }
183 
TEST(CommandLineOptionsTests,ConvertArgvToVector)184 TEST(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);
192   ASSERT_EQ(v->size(), 2UL);
193   ASSERT_EQ((*v)[0], "--foo");
194   ASSERT_EQ((*v)[1], "FOO");
195 }
196 
TEST(CommandLineOptionsTests,ConvertArgvToVectorNoArgs)197 TEST(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);
203   ASSERT_EQ(v->size(), 0UL);
204 }
205 
TEST(CommandLineOptionsTests,Usage)206 TEST(CommandLineOptionsTests, Usage) {
207   std::string arg1;
208   std::string arg2;
209   std::string arg3;
210   std::string arg4;
211   bool arg5 = false;
212   bool arg6 = false;
213   std::vector<std::string> arg7;
214   std::vector<std::string> arg8;
215   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)
222                                 .MandatoryOption("--gg", "description-gg", &arg7)
223                                 .OptionalOption("--hh", "description-hh", &arg8);
224   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 "
228                    "[--gg arg [..]] [--hh arg [..]]"),
229             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 
236 }  // namespace android::idmap2
237