aboutsummaryrefslogtreecommitdiff
path: root/brillo/flag_helper_test.cc
blob: 29c6429e6facc94b6a9be4e9aadd763a82f32802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cstdint>
#include <cstdio>
#include <sysexits.h>

#include <base/command_line.h>
#include <base/macros.h>
#include <brillo/flag_helper.h>

#include <gtest/gtest.h>

namespace brillo {

class FlagHelperTest : public ::testing::Test {
 public:
  FlagHelperTest() {}
  ~FlagHelperTest() override { brillo::FlagHelper::ResetForTesting(); }
  static void SetUpTestCase() { base::CommandLine::Init(0, nullptr); }
};

// Test that the DEFINE_xxxx macros can create the respective variables
// correctly with the default value.
TEST_F(FlagHelperTest, Defaults) {
  DEFINE_bool(bool1, true, "Test bool flag");
  DEFINE_bool(bool2, false, "Test bool flag");
  DEFINE_int32(int32_1, INT32_MIN, "Test int32 flag");
  DEFINE_int32(int32_2, 0, "Test int32 flag");
  DEFINE_int32(int32_3, INT32_MAX, "Test int32 flag");
  DEFINE_int64(int64_1, INT64_MIN, "Test int64 flag");
  DEFINE_int64(int64_2, 0, "Test int64 flag");
  DEFINE_int64(int64_3, INT64_MAX, "Test int64 flag");
  DEFINE_uint64(uint64_1, 0, "Test uint64 flag");
  DEFINE_uint64(uint64_2, UINT_LEAST64_MAX, "Test uint64 flag");
  DEFINE_double(double_1, -100.5, "Test double flag");
  DEFINE_double(double_2, 0, "Test double flag");
  DEFINE_double(double_3, 100.5, "Test double flag");
  DEFINE_string(string_1, "", "Test string flag");
  DEFINE_string(string_2, "value", "Test string flag");

  const char* argv[] = {"test_program"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);
  brillo::FlagHelper::Init(arraysize(argv), argv, "TestDefaultTrue");

  EXPECT_TRUE(FLAGS_bool1);
  EXPECT_FALSE(FLAGS_bool2);
  EXPECT_EQ(FLAGS_int32_1, INT32_MIN);
  EXPECT_EQ(FLAGS_int32_2, 0);
  EXPECT_EQ(FLAGS_int32_3, INT32_MAX);
  EXPECT_EQ(FLAGS_int64_1, INT64_MIN);
  EXPECT_EQ(FLAGS_int64_2, 0);
  EXPECT_EQ(FLAGS_int64_3, INT64_MAX);
  EXPECT_EQ(FLAGS_uint64_1, 0);
  EXPECT_EQ(FLAGS_uint64_2, UINT_LEAST64_MAX);
  EXPECT_DOUBLE_EQ(FLAGS_double_1, -100.5);
  EXPECT_DOUBLE_EQ(FLAGS_double_2, 0);
  EXPECT_DOUBLE_EQ(FLAGS_double_3, 100.5);
  EXPECT_STREQ(FLAGS_string_1.c_str(), "");
  EXPECT_STREQ(FLAGS_string_2.c_str(), "value");
}

// Test that command line flag values are parsed and update the flag
// variable values correctly when using double '--' flags
TEST_F(FlagHelperTest, SetValueDoubleDash) {
  DEFINE_bool(bool1, false, "Test bool flag");
  DEFINE_bool(bool2, true, "Test bool flag");
  DEFINE_bool(bool3, false, "Test bool flag");
  DEFINE_bool(bool4, true, "Test bool flag");
  DEFINE_int32(int32_1, 1, "Test int32 flag");
  DEFINE_int32(int32_2, 1, "Test int32 flag");
  DEFINE_int32(int32_3, 1, "Test int32 flag");
  DEFINE_int64(int64_1, 1, "Test int64 flag");
  DEFINE_int64(int64_2, 1, "Test int64 flag");
  DEFINE_int64(int64_3, 1, "Test int64 flag");
  DEFINE_uint64(uint64_1, 1, "Test uint64 flag");
  DEFINE_uint64(uint64_2, 1, "Test uint64 flag");
  DEFINE_double(double_1, 1, "Test double flag");
  DEFINE_double(double_2, 1, "Test double flag");
  DEFINE_double(double_3, 1, "Test double flag");
  DEFINE_string(string_1, "default", "Test string flag");
  DEFINE_string(string_2, "default", "Test string flag");

  const char* argv[] = {"test_program",
                        "--bool1",
                        "--nobool2",
                        "--bool3=true",
                        "--bool4=false",
                        "--int32_1=-2147483648",
                        "--int32_2=0",
                        "--int32_3=2147483647",
                        "--int64_1=-9223372036854775808",
                        "--int64_2=0",
                        "--int64_3=9223372036854775807",
                        "--uint64_1=0",
                        "--uint64_2=18446744073709551615",
                        "--double_1=-100.5",
                        "--double_2=0",
                        "--double_3=100.5",
                        "--string_1=",
                        "--string_2=value"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);
  brillo::FlagHelper::Init(arraysize(argv), argv, "TestDefaultTrue");

  EXPECT_TRUE(FLAGS_bool1);
  EXPECT_FALSE(FLAGS_bool2);
  EXPECT_TRUE(FLAGS_bool3);
  EXPECT_FALSE(FLAGS_bool4);
  EXPECT_EQ(FLAGS_int32_1, INT32_MIN);
  EXPECT_EQ(FLAGS_int32_2, 0);
  EXPECT_EQ(FLAGS_int32_3, INT32_MAX);
  EXPECT_EQ(FLAGS_int64_1, INT64_MIN);
  EXPECT_EQ(FLAGS_int64_2, 0);
  EXPECT_EQ(FLAGS_int64_3, INT64_MAX);
  EXPECT_EQ(FLAGS_uint64_1, 0);
  EXPECT_EQ(FLAGS_uint64_2, UINT_LEAST64_MAX);
  EXPECT_DOUBLE_EQ(FLAGS_double_1, -100.5);
  EXPECT_DOUBLE_EQ(FLAGS_double_2, 0);
  EXPECT_DOUBLE_EQ(FLAGS_double_3, 100.5);
  EXPECT_STREQ(FLAGS_string_1.c_str(), "");
  EXPECT_STREQ(FLAGS_string_2.c_str(), "value");
}

// Test that command line flag values are parsed and update the flag
// variable values correctly when using single '-' flags
TEST_F(FlagHelperTest, SetValueSingleDash) {
  DEFINE_bool(bool1, false, "Test bool flag");
  DEFINE_bool(bool2, true, "Test bool flag");
  DEFINE_int32(int32_1, 1, "Test int32 flag");
  DEFINE_int32(int32_2, 1, "Test int32 flag");
  DEFINE_int32(int32_3, 1, "Test int32 flag");
  DEFINE_int64(int64_1, 1, "Test int64 flag");
  DEFINE_int64(int64_2, 1, "Test int64 flag");
  DEFINE_int64(int64_3, 1, "Test int64 flag");
  DEFINE_uint64(uint64_1, 1, "Test uint64 flag");
  DEFINE_uint64(uint64_2, 1, "Test uint64 flag");
  DEFINE_double(double_1, 1, "Test double flag");
  DEFINE_double(double_2, 1, "Test double flag");
  DEFINE_double(double_3, 1, "Test double flag");
  DEFINE_string(string_1, "default", "Test string flag");
  DEFINE_string(string_2, "default", "Test string flag");

  const char* argv[] = {"test_program",
                        "-bool1",
                        "-nobool2",
                        "-int32_1=-2147483648",
                        "-int32_2=0",
                        "-int32_3=2147483647",
                        "-int64_1=-9223372036854775808",
                        "-int64_2=0",
                        "-int64_3=9223372036854775807",
                        "-uint64_1=0",
                        "-uint64_2=18446744073709551615",
                        "-double_1=-100.5",
                        "-double_2=0",
                        "-double_3=100.5",
                        "-string_1=",
                        "-string_2=value"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);
  brillo::FlagHelper::Init(arraysize(argv), argv, "TestDefaultTrue");

  EXPECT_TRUE(FLAGS_bool1);
  EXPECT_FALSE(FLAGS_bool2);
  EXPECT_EQ(FLAGS_int32_1, INT32_MIN);
  EXPECT_EQ(FLAGS_int32_2, 0);
  EXPECT_EQ(FLAGS_int32_3, INT32_MAX);
  EXPECT_EQ(FLAGS_int64_1, INT64_MIN);
  EXPECT_EQ(FLAGS_int64_2, 0);
  EXPECT_EQ(FLAGS_int64_3, INT64_MAX);
  EXPECT_EQ(FLAGS_uint64_1, 0);
  EXPECT_EQ(FLAGS_uint64_2, UINT_LEAST64_MAX);
  EXPECT_DOUBLE_EQ(FLAGS_double_1, -100.5);
  EXPECT_DOUBLE_EQ(FLAGS_double_2, 0);
  EXPECT_DOUBLE_EQ(FLAGS_double_3, 100.5);
  EXPECT_STREQ(FLAGS_string_1.c_str(), "");
  EXPECT_STREQ(FLAGS_string_2.c_str(), "value");
}

// Test that a duplicated flag on the command line picks up the last
// value set.
TEST_F(FlagHelperTest, DuplicateSetValue) {
  DEFINE_int32(int32_1, 0, "Test in32 flag");

  const char* argv[] = {"test_program", "--int32_1=5", "--int32_1=10"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);
  brillo::FlagHelper::Init(arraysize(argv), argv, "TestDuplicateSetvalue");

  EXPECT_EQ(FLAGS_int32_1, 10);
}

// Test that flags set after the -- marker are not parsed as command line flags
TEST_F(FlagHelperTest, FlagTerminator) {
  DEFINE_int32(int32_1, 0, "Test int32 flag");

  const char* argv[] = {"test_program", "--int32_1=5", "--", "--int32_1=10"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);
  brillo::FlagHelper::Init(arraysize(argv), argv, "TestFlagTerminator");

  EXPECT_EQ(FLAGS_int32_1, 5);
}

// Test that help messages are generated correctly when the --help flag
// is passed to the program.
TEST_F(FlagHelperTest, HelpMessage) {
  DEFINE_bool(bool_1, true, "Test bool flag");
  DEFINE_int32(int_1, 0, "Test int flag");
  DEFINE_int64(int64_1, 0, "Test int64 flag");
  DEFINE_uint64(uint64_1, 0, "Test uint64 flag");
  DEFINE_double(double_1, 0, "Test double flag");
  DEFINE_string(string_1, "", "Test string flag");

  const char* argv[] = {"test_program", "--int_1=value", "--help"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);

  FILE* orig = stdout;
  stdout = stderr;

  ASSERT_EXIT(
      brillo::FlagHelper::Init(arraysize(argv), argv, "TestHelpMessage"),
      ::testing::ExitedWithCode(EX_OK),
      "TestHelpMessage\n\n"
      "  --bool_1  \\(Test bool flag\\)  type: bool  default: true\n"
      "  --double_1  \\(Test double flag\\)  type: double  default: 0\n"
      "  --help  \\(Show this help message\\)  type: bool  default: false\n"
      "  --int64_1  \\(Test int64 flag\\)  type: int64  default: 0\n"
      "  --int_1  \\(Test int flag\\)  type: int  default: 0\n"
      "  --string_1  \\(Test string flag\\)  type: string  default: \"\"\n"
      "  --uint64_1  \\(Test uint64 flag\\)  type: uint64  default: 0\n");

  stdout = orig;
}

// Test that passing in unknown command line flags causes the program
// to exit with EX_USAGE error code and corresponding error message.
TEST_F(FlagHelperTest, UnknownFlag) {
  const char* argv[] = {"test_program", "--flag=value"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);

  FILE* orig = stdout;
  stdout = stderr;

  ASSERT_EXIT(brillo::FlagHelper::Init(arraysize(argv), argv, "TestIntExit"),
              ::testing::ExitedWithCode(EX_USAGE),
              "ERROR: unknown command line flag 'flag'");

  stdout = orig;
}

// Test that when passing an incorrect/unparsable type to a command line flag,
// the program exits with code EX_DATAERR and outputs a corresponding message.
TEST_F(FlagHelperTest, BoolParseError) {
  DEFINE_bool(bool_1, 0, "Test bool flag");

  const char* argv[] = {"test_program", "--bool_1=value"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);

  FILE* orig = stdout;
  stdout = stderr;

  ASSERT_EXIT(
      brillo::FlagHelper::Init(arraysize(argv), argv, "TestBoolParseError"),
      ::testing::ExitedWithCode(EX_DATAERR),
      "ERROR: illegal value 'value' specified for bool flag 'bool_1'");

  stdout = orig;
}

// Test that when passing an incorrect/unparsable type to a command line flag,
// the program exits with code EX_DATAERR and outputs a corresponding message.
TEST_F(FlagHelperTest, Int32ParseError) {
  DEFINE_int32(int_1, 0, "Test int flag");

  const char* argv[] = {"test_program", "--int_1=value"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);

  FILE* orig = stdout;
  stdout = stderr;

  ASSERT_EXIT(brillo::FlagHelper::Init(arraysize(argv),
                                       argv,
                                       "TestInt32ParseError"),
              ::testing::ExitedWithCode(EX_DATAERR),
              "ERROR: illegal value 'value' specified for int flag 'int_1'");

  stdout = orig;
}

// Test that when passing an incorrect/unparsable type to a command line flag,
// the program exits with code EX_DATAERR and outputs a corresponding message.
TEST_F(FlagHelperTest, Int64ParseError) {
  DEFINE_int64(int64_1, 0, "Test int64 flag");

  const char* argv[] = {"test_program", "--int64_1=value"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);

  FILE* orig = stdout;
  stdout = stderr;

  ASSERT_EXIT(
      brillo::FlagHelper::Init(arraysize(argv), argv, "TestInt64ParseError"),
      ::testing::ExitedWithCode(EX_DATAERR),
      "ERROR: illegal value 'value' specified for int64 flag "
      "'int64_1'");

  stdout = orig;
}

// Test that when passing an incorrect/unparsable type to a command line flag,
// the program exits with code EX_DATAERR and outputs a corresponding message.
TEST_F(FlagHelperTest, UInt64ParseError) {
  DEFINE_uint64(uint64_1, 0, "Test uint64 flag");

  const char* argv[] = {"test_program", "--uint64_1=value"};
  base::CommandLine command_line(arraysize(argv), argv);

  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
      &command_line);

  FILE* orig = stdout;
  stdout = stderr;

  ASSERT_EXIT(
      brillo::FlagHelper::Init(arraysize(argv), argv, "TestUInt64ParseError"),
      ::testing::ExitedWithCode(EX_DATAERR),
      "ERROR: illegal value 'value' specified for uint64 flag "
      "'uint64_1'");

  stdout = orig;
}

}  // namespace brillo