1//
2//  Copyright (C) 2015 Google, Inc.
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 <base/at_exit.h>
18#include <base/command_line.h>
19#include <base/macros.h>
20#include <gtest/gtest.h>
21
22#include "service/settings.h"
23#include "service/switches.h"
24
25using bluetooth::Settings;
26using namespace bluetooth::switches;
27
28namespace {
29
30class SettingsTest : public ::testing::Test {
31 public:
32  SettingsTest() = default;
33
34  void SetUp() override { base::CommandLine::Reset(); }
35
36  void TearDown() override { base::CommandLine::Reset(); }
37
38 protected:
39  base::AtExitManager exit_manager_;
40  Settings settings_;
41
42 private:
43  DISALLOW_COPY_AND_ASSIGN(SettingsTest);
44};
45
46TEST_F(SettingsTest, EmptyCommandLine) {
47  const base::CommandLine::CharType* argv[] = {"program"};
48  EXPECT_TRUE(base::CommandLine::Init(arraysize(argv), argv));
49  EXPECT_TRUE(settings_.Init());
50}
51
52TEST_F(SettingsTest, UnexpectedSwitches1) {
53  const base::CommandLine::CharType* argv[] = {
54      "program", "--create-ipc-socket=foobar", "--foobarbaz"};
55  EXPECT_TRUE(base::CommandLine::Init(arraysize(argv), argv));
56  EXPECT_FALSE(settings_.Init());
57}
58
59TEST_F(SettingsTest, UnexpectedSwitches2) {
60  const base::CommandLine::CharType* argv[] = {"program", "--foobarbaz"};
61  EXPECT_TRUE(base::CommandLine::Init(arraysize(argv), argv));
62  EXPECT_FALSE(settings_.Init());
63}
64
65TEST_F(SettingsTest, UnexpectedArguments1) {
66  const base::CommandLine::CharType* argv[] = {"program", "foobarbaz"};
67  EXPECT_TRUE(base::CommandLine::Init(arraysize(argv), argv));
68  EXPECT_FALSE(settings_.Init());
69}
70
71TEST_F(SettingsTest, UnexpectedArguments2) {
72  const base::CommandLine::CharType* argv[] = {
73      "program", "--create-ipc-socket=foobar", "foobarbaz"};
74  EXPECT_TRUE(base::CommandLine::Init(arraysize(argv), argv));
75  EXPECT_FALSE(settings_.Init());
76}
77
78TEST_F(SettingsTest, TooManyIpcOptions) {
79  const base::CommandLine::CharType* argv[] = {
80      "program", "--create-ipc-socket=foobar",
81      "--android-ipc-socket-suffix=foobar"};
82  EXPECT_TRUE(base::CommandLine::Init(arraysize(argv), argv));
83  EXPECT_FALSE(settings_.Init());
84}
85
86TEST_F(SettingsTest, GoodArgumentsCreateIpc) {
87  const base::CommandLine::CharType* argv[] = {"program",
88                                               "--create-ipc-socket=foobar"};
89  EXPECT_TRUE(base::CommandLine::Init(arraysize(argv), argv));
90  EXPECT_TRUE(settings_.Init());
91}
92
93TEST_F(SettingsTest, GoodArgumentsAndroidIpc) {
94  const base::CommandLine::CharType* argv[] = {
95      "program", "--android-ipc-socket-suffix=foobar"};
96  EXPECT_TRUE(base::CommandLine::Init(arraysize(argv), argv));
97  EXPECT_TRUE(settings_.Init());
98}
99
100}  // namespace
101