1e1a9abe01b16b21bbec1068a499d776f4c867567Steve Fung// Copyright 2014 The Chromium OS Authors. All rights reserved.
248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Use of this source code is governed by a BSD-style license that can be
348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// found in the LICENSE file.
448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung#include <cstdint>
648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung#include <cstdio>
748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung#include <sysexits.h>
848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung#include <base/command_line.h>
1048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung#include <base/macros.h>
119ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko#include <brillo/flag_helper.h>
1248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
1348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung#include <gtest/gtest.h>
1448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
159ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenkonamespace brillo {
1648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
1748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fungclass FlagHelperTest : public ::testing::Test {
1848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung public:
1905d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  FlagHelperTest() {}
209ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  ~FlagHelperTest() override { brillo::FlagHelper::ResetForTesting(); }
219ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  static void SetUpTestCase() { base::CommandLine::Init(0, nullptr); }
2248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung};
2348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
2448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that the DEFINE_xxxx macros can create the respective variables
2548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// correctly with the default value.
2648b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, Defaults) {
2748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_bool(bool1, true, "Test bool flag");
2848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_bool(bool2, false, "Test bool flag");
2948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_1, INT32_MIN, "Test int32 flag");
3048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_2, 0, "Test int32 flag");
3148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_3, INT32_MAX, "Test int32 flag");
3248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_1, INT64_MIN, "Test int64 flag");
3348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_2, 0, "Test int64 flag");
3448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_3, INT64_MAX, "Test int64 flag");
3548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_uint64(uint64_1, 0, "Test uint64 flag");
3648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_uint64(uint64_2, UINT_LEAST64_MAX, "Test uint64 flag");
3748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_1, -100.5, "Test double flag");
3848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_2, 0, "Test double flag");
3948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_3, 100.5, "Test double flag");
4048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_string(string_1, "", "Test string flag");
4148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_string(string_2, "value", "Test string flag");
4248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
4348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  const char* argv[] = {"test_program"};
44491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
4548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
469ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
4748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
489ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::Init(arraysize(argv), argv, "TestDefaultTrue");
4948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
5048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_TRUE(FLAGS_bool1);
5148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_FALSE(FLAGS_bool2);
5248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_1, INT32_MIN);
5348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_2, 0);
5448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_3, INT32_MAX);
5548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_1, INT64_MIN);
5648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_2, 0);
5748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_3, INT64_MAX);
5848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_uint64_1, 0);
5948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_uint64_2, UINT_LEAST64_MAX);
6048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_1, -100.5);
6148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_2, 0);
6248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_3, 100.5);
6348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_STREQ(FLAGS_string_1.c_str(), "");
6448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_STREQ(FLAGS_string_2.c_str(), "value");
6548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
6648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
6748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that command line flag values are parsed and update the flag
6848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// variable values correctly when using double '--' flags
6948b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, SetValueDoubleDash) {
7048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_bool(bool1, false, "Test bool flag");
7148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_bool(bool2, true, "Test bool flag");
726aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung  DEFINE_bool(bool3, false, "Test bool flag");
736aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung  DEFINE_bool(bool4, true, "Test bool flag");
7448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_1, 1, "Test int32 flag");
7548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_2, 1, "Test int32 flag");
7648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_3, 1, "Test int32 flag");
7748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_1, 1, "Test int64 flag");
7848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_2, 1, "Test int64 flag");
7948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_3, 1, "Test int64 flag");
8048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_uint64(uint64_1, 1, "Test uint64 flag");
8148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_uint64(uint64_2, 1, "Test uint64 flag");
8248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_1, 1, "Test double flag");
8348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_2, 1, "Test double flag");
8448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_3, 1, "Test double flag");
8548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_string(string_1, "default", "Test string flag");
8648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_string(string_2, "default", "Test string flag");
8748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
8805d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  const char* argv[] = {"test_program",
8905d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--bool1",
9005d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--nobool2",
9105d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--bool3=true",
9205d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--bool4=false",
9305d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--int32_1=-2147483648",
9405d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--int32_2=0",
9505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--int32_3=2147483647",
9605d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--int64_1=-9223372036854775808",
9705d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--int64_2=0",
9805d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--int64_3=9223372036854775807",
9905d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--uint64_1=0",
10005d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--uint64_2=18446744073709551615",
10105d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--double_1=-100.5",
10205d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--double_2=0",
10305d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--double_3=100.5",
10405d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--string_1=",
10505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "--string_2=value"};
106491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
10748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
1089ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
10948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
1109ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::Init(arraysize(argv), argv, "TestDefaultTrue");
11148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
11248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_TRUE(FLAGS_bool1);
11348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_FALSE(FLAGS_bool2);
1146aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung  EXPECT_TRUE(FLAGS_bool3);
1156aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung  EXPECT_FALSE(FLAGS_bool4);
11648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_1, INT32_MIN);
11748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_2, 0);
11848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_3, INT32_MAX);
11948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_1, INT64_MIN);
12048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_2, 0);
12148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_3, INT64_MAX);
12248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_uint64_1, 0);
12348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_uint64_2, UINT_LEAST64_MAX);
12448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_1, -100.5);
12548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_2, 0);
12648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_3, 100.5);
12748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_STREQ(FLAGS_string_1.c_str(), "");
12848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_STREQ(FLAGS_string_2.c_str(), "value");
12948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
13048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
13148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that command line flag values are parsed and update the flag
13248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// variable values correctly when using single '-' flags
13348b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, SetValueSingleDash) {
13448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_bool(bool1, false, "Test bool flag");
13548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_bool(bool2, true, "Test bool flag");
13648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_1, 1, "Test int32 flag");
13748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_2, 1, "Test int32 flag");
13848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_3, 1, "Test int32 flag");
13948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_1, 1, "Test int64 flag");
14048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_2, 1, "Test int64 flag");
14148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_3, 1, "Test int64 flag");
14248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_uint64(uint64_1, 1, "Test uint64 flag");
14348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_uint64(uint64_2, 1, "Test uint64 flag");
14448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_1, 1, "Test double flag");
14548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_2, 1, "Test double flag");
14648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_3, 1, "Test double flag");
14748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_string(string_1, "default", "Test string flag");
14848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_string(string_2, "default", "Test string flag");
14948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
15005d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  const char* argv[] = {"test_program",
15105d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-bool1",
15205d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-nobool2",
15305d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-int32_1=-2147483648",
15405d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-int32_2=0",
15505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-int32_3=2147483647",
15605d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-int64_1=-9223372036854775808",
15705d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-int64_2=0",
15805d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-int64_3=9223372036854775807",
15905d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-uint64_1=0",
16005d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-uint64_2=18446744073709551615",
16105d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-double_1=-100.5",
16205d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-double_2=0",
16305d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-double_3=100.5",
16405d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-string_1=",
16505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko                        "-string_2=value"};
166491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
16748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
1689ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
16948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
1709ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::Init(arraysize(argv), argv, "TestDefaultTrue");
17148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
17248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_TRUE(FLAGS_bool1);
17348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_FALSE(FLAGS_bool2);
17448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_1, INT32_MIN);
17548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_2, 0);
17648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_3, INT32_MAX);
17748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_1, INT64_MIN);
17848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_2, 0);
17948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int64_3, INT64_MAX);
18048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_uint64_1, 0);
18148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_uint64_2, UINT_LEAST64_MAX);
18248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_1, -100.5);
18348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_2, 0);
18448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_DOUBLE_EQ(FLAGS_double_3, 100.5);
18548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_STREQ(FLAGS_string_1.c_str(), "");
18648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_STREQ(FLAGS_string_2.c_str(), "value");
18748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
18848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
18948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that a duplicated flag on the command line picks up the last
19048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// value set.
19148b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, DuplicateSetValue) {
19248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_1, 0, "Test in32 flag");
19348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
19448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  const char* argv[] = {"test_program", "--int32_1=5", "--int32_1=10"};
195491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
19648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
1979ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
19848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
1999ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::Init(arraysize(argv), argv, "TestDuplicateSetvalue");
20048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
20148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_1, 10);
20248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
20348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
20448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that flags set after the -- marker are not parsed as command line flags
20548b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, FlagTerminator) {
20648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int32_1, 0, "Test int32 flag");
20748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
20848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  const char* argv[] = {"test_program", "--int32_1=5", "--", "--int32_1=10"};
209491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
21048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
2119ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
21248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
2139ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::Init(arraysize(argv), argv, "TestFlagTerminator");
21448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
21548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  EXPECT_EQ(FLAGS_int32_1, 5);
21648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
21748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
21848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that help messages are generated correctly when the --help flag
21948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// is passed to the program.
22048b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, HelpMessage) {
22148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_bool(bool_1, true, "Test bool flag");
22248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int_1, 0, "Test int flag");
22348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_1, 0, "Test int64 flag");
22448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_uint64(uint64_1, 0, "Test uint64 flag");
22548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_double(double_1, 0, "Test double flag");
22648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_string(string_1, "", "Test string flag");
22748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
22848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  const char* argv[] = {"test_program", "--int_1=value", "--help"};
229491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
23048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
2319ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
23248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
23348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
23448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  FILE* orig = stdout;
23548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = stderr;
23648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
23705d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  ASSERT_EXIT(
2389ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko      brillo::FlagHelper::Init(arraysize(argv), argv, "TestHelpMessage"),
23905d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      ::testing::ExitedWithCode(EX_OK),
24005d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "TestHelpMessage\n\n"
24105d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "  --bool_1  \\(Test bool flag\\)  type: bool  default: true\n"
24205d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "  --double_1  \\(Test double flag\\)  type: double  default: 0\n"
24305d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "  --help  \\(Show this help message\\)  type: bool  default: false\n"
24405d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "  --int64_1  \\(Test int64 flag\\)  type: int64  default: 0\n"
24505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "  --int_1  \\(Test int flag\\)  type: int  default: 0\n"
24605d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "  --string_1  \\(Test string flag\\)  type: string  default: \"\"\n"
24705d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "  --uint64_1  \\(Test uint64 flag\\)  type: uint64  default: 0\n");
24848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
24948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = orig;
25048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
25148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
25248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that passing in unknown command line flags causes the program
25348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// to exit with EX_USAGE error code and corresponding error message.
25448b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, UnknownFlag) {
25505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  const char* argv[] = {"test_program", "--flag=value"};
256491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
25748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
2589ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
25948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
26048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
26148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  FILE* orig = stdout;
26248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = stderr;
26348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
2649ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  ASSERT_EXIT(brillo::FlagHelper::Init(arraysize(argv), argv, "TestIntExit"),
26548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung              ::testing::ExitedWithCode(EX_USAGE),
26648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung              "ERROR: unknown command line flag 'flag'");
26748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
26848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = orig;
26948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
27048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
27148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that when passing an incorrect/unparsable type to a command line flag,
27248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// the program exits with code EX_DATAERR and outputs a corresponding message.
2736aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve FungTEST_F(FlagHelperTest, BoolParseError) {
2746aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung  DEFINE_bool(bool_1, 0, "Test bool flag");
2756aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung
27605d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  const char* argv[] = {"test_program", "--bool_1=value"};
277491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
2786aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung
2799ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
2806aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung      &command_line);
2816aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung
2826aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung  FILE* orig = stdout;
2836aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung  stdout = stderr;
2846aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung
28505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  ASSERT_EXIT(
2869ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko      brillo::FlagHelper::Init(arraysize(argv), argv, "TestBoolParseError"),
28705d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      ::testing::ExitedWithCode(EX_DATAERR),
28805d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "ERROR: illegal value 'value' specified for bool flag 'bool_1'");
2896aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung
2906aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung  stdout = orig;
2916aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung}
2926aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung
2936aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung// Test that when passing an incorrect/unparsable type to a command line flag,
2946aed560e21ca33f40d0fe0ec770b0d67a1cf2704Steve Fung// the program exits with code EX_DATAERR and outputs a corresponding message.
29548b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, Int32ParseError) {
29648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int32(int_1, 0, "Test int flag");
29748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
29805d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  const char* argv[] = {"test_program", "--int_1=value"};
299491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
30048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
3019ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
30248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
30348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
30448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  FILE* orig = stdout;
30548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = stderr;
30648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
3079ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  ASSERT_EXIT(brillo::FlagHelper::Init(arraysize(argv),
3089ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko                                       argv,
3099ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko                                       "TestInt32ParseError"),
31048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung              ::testing::ExitedWithCode(EX_DATAERR),
31148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung              "ERROR: illegal value 'value' specified for int flag 'int_1'");
31248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
31348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = orig;
31448b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
31548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
31648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that when passing an incorrect/unparsable type to a command line flag,
31748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// the program exits with code EX_DATAERR and outputs a corresponding message.
31848b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, Int64ParseError) {
31948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_int64(int64_1, 0, "Test int64 flag");
32048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
3219ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  const char* argv[] = {"test_program", "--int64_1=value"};
322491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
32348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
3249ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
32548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
32648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
32748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  FILE* orig = stdout;
32848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = stderr;
32948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
33005d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  ASSERT_EXIT(
3319ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko      brillo::FlagHelper::Init(arraysize(argv), argv, "TestInt64ParseError"),
33205d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      ::testing::ExitedWithCode(EX_DATAERR),
33305d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "ERROR: illegal value 'value' specified for int64 flag "
33405d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "'int64_1'");
33548b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
33648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = orig;
33748b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
33848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
33948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// Test that when passing an incorrect/unparsable type to a command line flag,
34048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung// the program exits with code EX_DATAERR and outputs a corresponding message.
34148b3d5fe045b757963b12e8e32f4c689c4808604Steve FungTEST_F(FlagHelperTest, UInt64ParseError) {
34248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  DEFINE_uint64(uint64_1, 0, "Test uint64 flag");
34348b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
34405d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  const char* argv[] = {"test_program", "--uint64_1=value"};
345491405a7ca1f7e0dca63c15931c16fa70c83a470Alex Vakulenko  base::CommandLine command_line(arraysize(argv), argv);
34648b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
3479ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko  brillo::FlagHelper::GetInstance()->set_command_line_for_testing(
34848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung      &command_line);
34948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
35048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  FILE* orig = stdout;
35148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = stderr;
35248b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
35305d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko  ASSERT_EXIT(
3549ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko      brillo::FlagHelper::Init(arraysize(argv), argv, "TestUInt64ParseError"),
35505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      ::testing::ExitedWithCode(EX_DATAERR),
35605d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "ERROR: illegal value 'value' specified for uint64 flag "
35705d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko      "'uint64_1'");
35848b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
35948b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung  stdout = orig;
36048b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung}
36148b3d5fe045b757963b12e8e32f4c689c4808604Steve Fung
3629ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko}  // namespace brillo
363