1 /*
2  * Copyright (C) 2022 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 <algorithm>
18 #include <fstream>
19 #include <iostream>
20 
21 #include <android-base/file.h>
22 #include <gtest/gtest.h>
23 
24 #include "host/commands/cvd/parser/launch_cvd_parser.h"
25 #include "host/commands/cvd/unittests/parser/test_common.h"
26 namespace cuttlefish {
TEST(FlagsParserTest,ParseInvalidJson)27 TEST(FlagsParserTest, ParseInvalidJson) {
28   const char* test_string = R""""(
29     instances=50;
30   )"""";
31 
32   Json::Value json_configs;
33   std::string json_text(test_string);
34 
35   EXPECT_FALSE(ParseJsonString(json_text, json_configs));
36 }
37 
TEST(FlagsParserTest,ParseJsonWithSpellingError)38 TEST(FlagsParserTest, ParseJsonWithSpellingError) {
39   const char* test_string = R""""(
40 {
41     "Insta" :
42     [
43         {
44         }
45     ]
46 }
47   )"""";
48 
49   Json::Value json_configs;
50   std::string json_text(test_string);
51 
52   EXPECT_TRUE(ParseJsonString(json_text, json_configs))
53       << "Invalid Json string";
54   auto serialized_data = LaunchCvdParserTester(json_configs);
55   EXPECT_FALSE(serialized_data.ok());
56 }
57 
TEST(FlagsParserTest,ParseBasicJsonSingleInstances)58 TEST(FlagsParserTest, ParseBasicJsonSingleInstances) {
59   const char* test_string = R""""(
60 {
61     "instances" :
62     [
63         {
64           "vm": {
65             "crosvm":{
66             }
67           }
68         }
69     ]
70 }
71   )"""";
72 
73   Json::Value json_configs;
74   std::string json_text(test_string);
75 
76   EXPECT_TRUE(ParseJsonString(json_text, json_configs))
77       << "Invalid Json string";
78   auto serialized_data = LaunchCvdParserTester(json_configs);
79   EXPECT_TRUE(serialized_data.ok()) << serialized_data.error().Trace();
80   EXPECT_TRUE(FindConfig(*serialized_data, "--num_instances=1"))
81       << "num_instances flag is missing or wrongly formatted";
82 }
83 
TEST(FlagsParserTest,ParseBasicJsonTwoInstances)84 TEST(FlagsParserTest, ParseBasicJsonTwoInstances) {
85   const char* test_string = R""""(
86 {
87     "instances" :
88     [
89         {
90           "vm": {
91             "crosvm":{
92             }
93           }
94         },
95         {
96           "vm": {
97             "crosvm":{
98             }
99           }
100         }
101     ]
102 }
103   )"""";
104 
105   Json::Value json_configs;
106   std::string json_text(test_string);
107 
108   EXPECT_TRUE(ParseJsonString(json_text, json_configs))
109       << "Invalid Json string";
110   auto serialized_data = LaunchCvdParserTester(json_configs);
111   EXPECT_TRUE(serialized_data.ok()) << serialized_data.error().Trace();
112   EXPECT_TRUE(FindConfig(*serialized_data, "--num_instances=2"))
113       << "num_instances flag is missing or wrongly formatted";
114 }
115 
TEST(BootFlagsParserTest,ParseNetSimFlagEmptyJson)116 TEST(BootFlagsParserTest, ParseNetSimFlagEmptyJson) {
117   const char* test_string = R""""(
118 {
119   "instances" :
120   [
121         {
122           "vm": {
123             "crosvm":{
124             }
125           }
126         }
127   ]
128 }
129   )"""";
130 
131   Json::Value json_configs;
132   std::string json_text(test_string);
133 
134   EXPECT_TRUE(ParseJsonString(json_text, json_configs))
135       << "Invalid Json string";
136   auto serialized_data = LaunchCvdParserTester(json_configs);
137   EXPECT_TRUE(serialized_data.ok()) << serialized_data.error().Trace();
138   EXPECT_TRUE(FindConfig(*serialized_data, R"(--netsim_bt=false)"))
139       << "netsim_bt flag is missing or wrongly formatted";
140 }
141 
TEST(BootFlagsParserTest,ParseNetSimFlagEnabled)142 TEST(BootFlagsParserTest, ParseNetSimFlagEnabled) {
143   const char* test_string = R""""(
144 {
145    "netsim_bt": true,
146      "instances" :
147      [
148         {
149           "vm": {
150             "crosvm":{
151             }
152           }
153         }
154       ]
155 }
156   )"""";
157 
158   Json::Value json_configs;
159   std::string json_text(test_string);
160 
161   EXPECT_TRUE(ParseJsonString(json_text, json_configs))
162       << "Invalid Json string";
163   auto serialized_data = LaunchCvdParserTester(json_configs);
164   EXPECT_TRUE(serialized_data.ok()) << serialized_data.error().Trace();
165   EXPECT_TRUE(FindConfig(*serialized_data, R"(--netsim_bt=true)"))
166       << "netsim_bt flag is missing or wrongly formatted";
167 }
168 
169 }  // namespace cuttlefish
170