• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <sys/types.h>
17 #include <unistd.h>
18 
19 #include "host/commands/cvd/unittests/selector/creation_analyzer_helper.h"
20 
21 #include "common/libs/utils/environment.h"
22 #include "common/libs/utils/users.h"
23 #include "host/commands/cvd/selector/instance_database_utils.h"
24 #include "host/commands/cvd/selector/selector_constants.h"
25 #include "host/commands/cvd/types.h"
26 
27 namespace cuttlefish {
28 namespace selector {
29 namespace {
30 
TestUserHome()31 std::string TestUserHome() {
32   static const std::string home = StringFromEnv("HOME", "");
33   if (!home.empty()) {
34     return home;
35   }
36   auto result = SystemWideUserHome(getuid());
37   return (result.ok() ? *result : "");
38 }
39 
AutoGeneratedHome(const std::string & subdir)40 std::string AutoGeneratedHome(const std::string& subdir) {
41   auto parent_result = ParentOfAutogeneratedHomes(getuid(), getgid());
42   if (!parent_result.ok()) {
43     return "";
44   }
45   std::string parent(*parent_result);
46   return parent + "/" + std::to_string(getuid()) + "/" + subdir;
47 }
48 
49 }  // namespace
50 
51 static auto home_test_inputs = testing::Values(
52     InputOutput{
53         .cmd_args = "cvd start --daemon",
54         .selector_args = "--group_name=cf --instance_name=1",
55         .android_host_out = "/home/user/download",
56         .home = "/usr/local/home/_fake_user",
57         .expected_output =
58             Expected{.output = OutputInfo{.home = "/usr/local/home/_fake_user",
59                                           .host_artifacts_path =
60                                               "/home/user/download"},
61                      .is_success = true}},
62     InputOutput{.cmd_args = "cvd start --daemon",
63                 /* no selector_args */
64                 .android_host_out = "/home/user/download",
65                 .home = TestUserHome(),
66                 .expected_output =
67                     Expected{.output = OutputInfo{.home = TestUserHome(),
68                                                   .host_artifacts_path =
69                                                       "/home/user/download"},
70                              .is_success = true}},
71     InputOutput{
72         .cmd_args = "cvd start --daemon",
73         /* no selector_args */
74         .android_host_out = "/home/user/download",
75         /* undefined HOME */
76         .expected_output = Expected{
77             .output = OutputInfo{.home = TestUserHome(),
78                                  .host_artifacts_path = "/home/user/download"},
79             .is_success = true}});
80 
TEST_P(HomeTest,HomeTest)81 TEST_P(HomeTest, HomeTest) {
82   if (TestUserHome().empty()) {
83     /*
84      * If $HOME is the same as the real home directory (i.e. HOME is not
85      * overridden), cvd uses an automatically generated path in place of
86      * HOME when the operation is "start".
87      *
88      * Otherwise, for backward compatibility, cvd respects the overridden
89      * HOME.
90      *
91      * In testing that feature, if we cannot get the real home directory,
92      * the testing is not possible.
93      */
94     GTEST_SKIP() << "$HOME should be available for this set of tests.";
95   }
96   auto param = CreationAnalyzer::CreationAnalyzerParam{
97       .cmd_args = cmd_args_, .envs = envs_, .selector_args = selector_args_};
98 
99   auto result = CreationAnalyzer::Analyze(
100       sub_cmd_, param, credential_, instance_db_, instance_lock_file_manager_);
101 
102   ASSERT_EQ(result.ok(), expected_success_) << result.error().Trace();
103   if (!expected_success_) {
104     return;
105   }
106   ASSERT_EQ(result->home, expected_output_.home);
107 }
108 
109 INSTANTIATE_TEST_SUITE_P(CvdCreationInfo, HomeTest, home_test_inputs);
110 
111 static auto host_out_test_inputs = testing::Values(
112     InputOutput{.cmd_args = "cvd start --daemon",
113                 .selector_args = "--group_name=cf --instance_name=1",
114                 .android_host_out = "/home/user/download",
115                 .home = "/home/fake_user",
116                 .expected_output =
117                     Expected{.output = OutputInfo{.home = "/home/fake_user",
118                                                   .host_artifacts_path =
119                                                       "/home/user/download"},
120                              .is_success = true}},
121     InputOutput{.cmd_args = "cvd start --daemon",
122                 .selector_args = "--group_name=cf --instance_name=1",
123                 /* missing ANDROID_HOST_OUT */
124                 .home = "/home/fake_user",
125                 .expected_output =
126                     Expected{.output = OutputInfo{.home = "/home/fake_user"},
127                              .is_success = false}});
128 
TEST_P(HostArtifactsTest,HostArtifactsTest)129 TEST_P(HostArtifactsTest, HostArtifactsTest) {
130   auto param = CreationAnalyzer::CreationAnalyzerParam{
131       .cmd_args = cmd_args_, .envs = envs_, .selector_args = selector_args_};
132 
133   auto result = CreationAnalyzer::Analyze(
134       sub_cmd_, param, credential_, instance_db_, instance_lock_file_manager_);
135 
136   ASSERT_EQ(result.ok(), expected_success_) << result.error().Trace();
137   if (!expected_success_) {
138     return;
139   }
140   ASSERT_EQ(result->host_artifacts_path, expected_output_.host_artifacts_path);
141 }
142 
143 INSTANTIATE_TEST_SUITE_P(CvdCreationInfo, HostArtifactsTest,
144                          host_out_test_inputs);
145 
146 static auto invalid_sub_cmd_test_inputs =
147     testing::Values(InputOutput{.cmd_args = "cvd stop --daemon",
148                                 .android_host_out = "/home/user/download",
149                                 .home = "/home/fake_user"},
150                     InputOutput{.cmd_args = "cvd",
151                                 .android_host_out = "/home/user/download",
152                                 .home = "/home/fake_user"},
153                     InputOutput{.cmd_args = "cvd help --daemon",
154                                 .android_host_out = "/home/user/download",
155                                 .home = "/home/fake_user"});
156 
TEST_P(InvalidSubCmdTest,InvalidSubCmdTest)157 TEST_P(InvalidSubCmdTest, InvalidSubCmdTest) {
158   auto param = CreationAnalyzer::CreationAnalyzerParam{
159       .cmd_args = cmd_args_, .envs = envs_, .selector_args = selector_args_};
160 
161   auto result = CreationAnalyzer::Analyze(
162       sub_cmd_, param, credential_, instance_db_, instance_lock_file_manager_);
163 
164   ASSERT_FALSE(result.ok())
165       << "Analyze() had to fail with the subcmd in " << GetParam().cmd_args;
166 }
167 
168 INSTANTIATE_TEST_SUITE_P(CvdCreationInfo, InvalidSubCmdTest,
169                          invalid_sub_cmd_test_inputs);
170 
171 static auto& valid_sub_cmd_test_inputs = home_test_inputs;
172 
TEST_P(ValidSubCmdTest,ValidSubCmdTest)173 TEST_P(ValidSubCmdTest, ValidSubCmdTest) {
174   auto param = CreationAnalyzer::CreationAnalyzerParam{
175       .cmd_args = cmd_args_, .envs = envs_, .selector_args = selector_args_};
176 
177   auto result = CreationAnalyzer::Analyze(
178       sub_cmd_, param, credential_, instance_db_, instance_lock_file_manager_);
179 
180   ASSERT_TRUE(result.ok()) << result.error().Trace();
181 }
182 
183 INSTANTIATE_TEST_SUITE_P(CvdCreationInfo, ValidSubCmdTest,
184                          valid_sub_cmd_test_inputs);
185 
186 /*
187  * Tries to run Cuttlefish with default group two times, so the second
188  * run should fail as the default group_name is registed in the Instance-
189  * Database.
190  */
TEST(AutoHomeTest,DefaultFailAtSecondTrialTest)191 TEST(AutoHomeTest, DefaultFailAtSecondTrialTest) {
192   auto android_host_out = StringFromEnv("ANDROID_HOST_OUT", ".");
193   if (android_host_out.empty()) {
194     GTEST_SKIP() << "This test requires ANDROID_HOST_OUT to be set";
195   }
196   auto credential = ucred{.pid = getpid(), .uid = getuid(), .gid = getgid()};
197   InstanceLockFileManager lock_manager;
198   InstanceDatabase instance_db;
199   cvd_common::Envs envs = {{"ANDROID_HOST_OUT", android_host_out}};
200   cvd_common::Args empty_args;
201   std::vector<cvd_common::Args> cmd_args_list{
202       cvd_common::Args{"--daemon", "--instance_nums=7"},
203       cvd_common::Args{"--daemon", "--instance_nums=3"}};
204   auto param0 = CreationAnalyzer::CreationAnalyzerParam{
205       .cmd_args = cmd_args_list[0], .envs = envs, .selector_args = empty_args};
206   auto param1 = CreationAnalyzer::CreationAnalyzerParam{
207       .cmd_args = cmd_args_list[1], .envs = envs, .selector_args = empty_args};
208 
209   auto result_1st_exec = CreationAnalyzer::Analyze("start", param0, credential,
210                                                    instance_db, lock_manager);
211   auto result_db_addition =
212       instance_db.AddInstanceGroup({.group_name = "cvd",
213                                     .home_dir = TestUserHome(),
214                                     .host_artifacts_path = android_host_out,
215                                     .product_out_path = android_host_out});
216   if (!result_db_addition.ok()) {
217     GTEST_SKIP() << "This test requires mock group addition to work.";
218   }
219   auto result_2nd_exec = CreationAnalyzer::Analyze("start", param1, credential,
220                                                    instance_db, lock_manager);
221 
222   ASSERT_TRUE(result_1st_exec.ok()) << result_1st_exec.error().Trace();
223   ASSERT_EQ(result_1st_exec->home, TestUserHome());
224   ASSERT_FALSE(result_2nd_exec.ok())
225       << "Meant to be fail but returned home : " << result_2nd_exec->home;
226 }
227 
TEST(AutoHomeTest,DefaultFollowedByNonDefaultTest)228 TEST(AutoHomeTest, DefaultFollowedByNonDefaultTest) {
229   auto android_host_out = StringFromEnv("ANDROID_HOST_OUT", ".");
230   if (android_host_out.empty()) {
231     GTEST_SKIP() << "This test requires ANDROID_HOST_OUT to be set";
232   }
233   if (AutoGeneratedHome("goog").empty()) {
234     GTEST_SKIP() << "This test requires read-writable temp directory";
235   }
236   auto credential = ucred{.pid = getpid(), .uid = getuid(), .gid = getgid()};
237   InstanceLockFileManager lock_manager;
238   InstanceDatabase instance_db;
239   cvd_common::Envs envs = {{"ANDROID_HOST_OUT", android_host_out}};
240   // needs this as the CreationAnalyzerParam takes references, not copies.
241   // i.e. .cmd_args = cvd_common::Args{} won't work.
242   cvd_common::Args empty_args;
243   cvd_common::Args cmd_arg_for_default{"--daemon", "--instance_nums=7"};
244   cvd_common::Args cmd_args_1st_non_default{"--daemon", "--instance_nums=3"};
245   cvd_common::Args cmd_args_2nd_non_default{"--daemon", "--instance_nums=5"};
246   cvd_common::Args selector_device_name_args{"--group_name=goog",
247                                              "--instance_name=tv"};
248   auto param_default =
249       CreationAnalyzer::CreationAnalyzerParam{.cmd_args = cmd_arg_for_default,
250                                               .envs = envs,
251                                               .selector_args = empty_args};
252   auto param_1st_non_default = CreationAnalyzer::CreationAnalyzerParam{
253       .cmd_args = cmd_args_1st_non_default,
254       .envs = envs,
255       .selector_args = selector_device_name_args};
256   // To make second non-default run non-default.
257   cvd_common::Envs envs_with_home{envs};
258   envs_with_home["HOME"] = "/home/mocktester";
259   auto param_2nd_non_default = CreationAnalyzer::CreationAnalyzerParam{
260       .cmd_args = cmd_args_2nd_non_default,
261       .selector_args = empty_args,
262       .envs = envs_with_home};
263 
264   auto result_default = CreationAnalyzer::Analyze(
265       "start", param_default, credential, instance_db, lock_manager);
266   auto result_db_addition =
267       instance_db.AddInstanceGroup({.group_name = "cvd",
268                                     .home_dir = TestUserHome(),
269                                     .host_artifacts_path = android_host_out,
270                                     .product_out_path = android_host_out});
271   if (!result_db_addition.ok()) {
272     GTEST_SKIP() << "This test requires mock group addition to work.";
273   }
274   auto result_1st_non_default = CreationAnalyzer::Analyze(
275       "start", param_1st_non_default, credential, instance_db, lock_manager);
276   auto result_2nd_non_default = CreationAnalyzer::Analyze(
277       "start", param_2nd_non_default, credential, instance_db, lock_manager);
278 
279   ASSERT_TRUE(result_default.ok()) << result_default.error().Trace();
280   ASSERT_EQ(result_default->home, TestUserHome());
281   ASSERT_TRUE(result_1st_non_default.ok())
282       << result_1st_non_default.error().Trace();
283   ASSERT_EQ(result_1st_non_default->home, AutoGeneratedHome("goog"));
284   ASSERT_TRUE(result_2nd_non_default.ok())
285       << result_2nd_non_default.error().Trace();
286 }
287 
288 }  // namespace selector
289 }  // namespace cuttlefish
290