• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2023 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 <string>
17 #include <vector>
18 
19 #include <gtest/gtest.h>
20 
21 #include "common/libs/utils/contains.h"
22 #include "host/commands/cvd/types.h"
23 #include "host/commands/cvd/unittests/server/cmd_runner.h"
24 
25 namespace cuttlefish {
26 namespace {
27 
ContainsAll(const std::string & stream,const std::vector<std::string> & tokens)28 bool ContainsAll(const std::string& stream,
29                  const std::vector<std::string>& tokens) {
30   for (const auto& token : tokens) {
31     if (!Contains(stream, token)) {
32       return false;
33     }
34   }
35   return true;
36 }
37 
38 /*
39  * Sees if this might be cvd --help output.
40  *
41  * Not very accurate.
42  */
MaybeCvdHelp(const CmdResult & result)43 bool MaybeCvdHelp(const CmdResult& result) {
44   const auto& stdout = result.Stdout();
45   return ContainsAll(stdout, {"help", "start", "stop", "fleet"});
46 }
47 
MaybeCvdStop(const CmdResult & result)48 bool MaybeCvdStop(const CmdResult& result) {
49   const auto& stderr = result.Stderr();
50   const auto& stdout = result.Stdout();
51   return Contains(stderr, "cvd_internal_stop") ||
52          Contains(stdout, "cvd_internal_stop") ||
53          Contains(stderr, "stop_cvd") || Contains(stdout, "stop_cvd");
54 }
55 
MaybeCvdStart(const CmdResult & result)56 bool MaybeCvdStart(const CmdResult& result) {
57   const auto& stdout = result.Stdout();
58   return ContainsAll(stdout, {"vhost", "modem", "daemon", "adb"});
59 }
60 
61 }  // namespace
62 
TEST(CvdDriver,CvdHelp)63 TEST(CvdDriver, CvdHelp) {
64   cvd_common::Envs envs;
65   CmdRunner::Run("cvd reset -y", envs);
66 
67   auto cmd_help = CmdRunner::Run("cvd help", envs);
68   auto cmd_dash_help = CmdRunner::Run("cvd --help", envs);
69 
70   ASSERT_TRUE(cmd_help.Success()) << cmd_help.Stderr();
71   ASSERT_TRUE(MaybeCvdHelp(cmd_help));
72   ASSERT_TRUE(cmd_dash_help.Success()) << cmd_dash_help.Stderr();
73   ASSERT_TRUE(MaybeCvdHelp(cmd_dash_help));
74 
75   // clean up for the next test
76   CmdRunner::Run("cvd reset -y", envs);
77 }
78 
TEST(CvdDriver,CvdOnly)79 TEST(CvdDriver, CvdOnly) {
80   cvd_common::Envs envs;
81   CmdRunner::Run("cvd reset -y", envs);
82 
83   auto cmd_help = CmdRunner::Run("cvd help", envs);
84   auto cmd_only = CmdRunner::Run("cvd", envs);
85 
86   ASSERT_TRUE(cmd_help.Success()) << cmd_help.Stderr();
87   ASSERT_TRUE(cmd_only.Success()) << cmd_only.Stderr();
88   ASSERT_EQ(cmd_help.Stdout(), cmd_only.Stdout());
89 
90   // clean up for the next test
91   CmdRunner::Run("cvd reset -y", envs);
92 }
93 
94 // this test is expected to fail. included proactively.
TEST(CvdDriver,CvdHelpWrong)95 TEST(CvdDriver, CvdHelpWrong) {
96   cvd_common::Envs envs;
97   CmdRunner::Run("cvd reset -y", envs);
98 
99   auto cmd_help_ref = CmdRunner::Run("cvd help", envs);
100   auto cmd_help_wrong = CmdRunner::Run("cvd help not_exist", envs);
101 
102   EXPECT_TRUE(cmd_help_ref.Success()) << cmd_help_ref.Stderr();
103   EXPECT_TRUE(cmd_help_wrong.Success()) << cmd_help_wrong.Stderr();
104   EXPECT_EQ(cmd_help_ref.Stdout(), cmd_help_wrong.Stdout());
105 
106   // clean up for the next test
107   CmdRunner::Run("cvd reset -y", envs);
108 }
109 
TEST(CvdSubtool,CvdStopHelp)110 TEST(CvdSubtool, CvdStopHelp) {
111   cvd_common::Envs envs;
112   CmdRunner::Run("cvd reset -y", envs);
113 
114   auto cmd_stop_help = CmdRunner::Run("cvd help stop", envs);
115 
116   ASSERT_TRUE(cmd_stop_help.Success()) << cmd_stop_help.Stderr();
117   ASSERT_TRUE(MaybeCvdStop(cmd_stop_help))
118       << "stderr: " << cmd_stop_help.Stderr()
119       << "stdout: " << cmd_stop_help.Stdout();
120 
121   // clean up for the next test
122   CmdRunner::Run("cvd reset -y", envs);
123 }
124 
TEST(CvdSubtool,CvdStartHelp)125 TEST(CvdSubtool, CvdStartHelp) {
126   cvd_common::Envs envs;
127   CmdRunner::Run("cvd reset -y", envs);
128 
129   auto cmd_start_help = CmdRunner::Run("cvd help start", envs);
130 
131   ASSERT_TRUE(cmd_start_help.Success()) << cmd_start_help.Stderr();
132   ASSERT_TRUE(MaybeCvdStart(cmd_start_help))
133       << "stderr: " << cmd_start_help.Stderr()
134       << "stdout: " << cmd_start_help.Stdout();
135 
136   // clean up for the next test
137   CmdRunner::Run("cvd reset -y", envs);
138 }
139 
140 }  // namespace cuttlefish
141