1 /*
2  * Copyright (C) 2021 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 <fruit/fruit.h>
18 #include <gtest/gtest.h>
19 #include <host/libs/config/adb/adb.h>
20 
21 #include <string>
22 
23 #include "host/libs/config/feature.h"
24 
25 namespace cuttlefish {
26 
27 struct TestData {
INJECTcuttlefish::TestData28   INJECT(TestData(AdbConfig& config, AdbConfigFragment& fragment))
29       : config(config), fragment(fragment) {}
30 
31   AdbConfig& config;
32   AdbConfigFragment& fragment;
33 };
34 
TestComponent()35 fruit::Component<TestData> TestComponent() {
36   return fruit::createComponent()
37       .install(AdbConfigComponent)
38       .install(AdbConfigFlagComponent)
39       .install(AdbConfigFragmentComponent)
40       .install(ConfigFlagPlaceholder);
41 }
42 
TEST(AdbConfigTest,SetFromFlags)43 TEST(AdbConfigTest, SetFromFlags) {
44   fruit::Injector<TestData> injector(TestComponent);
45   TestData& data = injector.get<TestData&>();
46   std::vector<std::string> args = {
47       "--adb_mode=vsock_tunnel,vsock_half_tunnel,native_vsock,unknown",
48       "--run_adb_connector=false",
49   };
50   auto flags = injector.getMultibindings<FlagFeature>();
51   auto processed = FlagFeature::ProcessFlags(flags, args);
52   ASSERT_TRUE(processed.ok()) << processed.error().Trace();
53   ASSERT_TRUE(args.empty());
54 
55   std::set<AdbMode> modes = {AdbMode::VsockTunnel, AdbMode::VsockHalfTunnel,
56                              AdbMode::NativeVsock, AdbMode::Unknown};
57   ASSERT_EQ(data.config.Modes(), modes);
58   ASSERT_FALSE(data.config.RunConnector());
59 }
60 
TEST(AdbConfigTest,SerializeDeserialize)61 TEST(AdbConfigTest, SerializeDeserialize) {
62   fruit::Injector<TestData> injector1(TestComponent);
63   TestData& data1 = injector1.get<TestData&>();
64   ASSERT_TRUE(
65       data1.config.SetModes({AdbMode::VsockTunnel, AdbMode::VsockHalfTunnel,
66                              AdbMode::NativeVsock, AdbMode::Unknown}));
67   ASSERT_TRUE(data1.config.SetRunConnector(false));
68 
69   fruit::Injector<TestData> injector2(TestComponent);
70   TestData& data2 = injector2.get<TestData&>();
71   ASSERT_TRUE(data2.fragment.Deserialize(data1.fragment.Serialize()));
72   ASSERT_EQ(data1.config.Modes(), data2.config.Modes());
73   ASSERT_EQ(data1.config.RunConnector(), data2.config.RunConnector());
74 }
75 
76 }  // namespace cuttlefish
77