• 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 
17 #include "fastboot_driver.h"
18 
19 #include <optional>
20 
21 #include <gtest/gtest.h>
22 #include "mock_transport.h"
23 
24 using namespace ::testing;
25 using namespace fastboot;
26 
27 class DriverTest : public ::testing::Test {
28   protected:
29     InSequence s_;
30 };
31 
TEST_F(DriverTest,GetVar)32 TEST_F(DriverTest, GetVar) {
33     MockTransport transport;
34     FastBootDriver driver(&transport);
35 
36     EXPECT_CALL(transport, Write(_, _))
37             .With(AllArgs(RawData("getvar:version")))
38             .WillOnce(ReturnArg<1>());
39     EXPECT_CALL(transport, Read(_, _)).WillOnce(Invoke(CopyData("OKAY0.4")));
40 
41     std::string output;
42     ASSERT_EQ(driver.GetVar("version", &output), SUCCESS) << driver.Error();
43     ASSERT_EQ(output, "0.4");
44 }
45 
TEST_F(DriverTest,InfoMessage)46 TEST_F(DriverTest, InfoMessage) {
47     MockTransport transport;
48     FastBootDriver driver(&transport);
49 
50     EXPECT_CALL(transport, Write(_, _))
51             .With(AllArgs(RawData("oem dmesg")))
52             .WillOnce(ReturnArg<1>());
53     EXPECT_CALL(transport, Read(_, _)).WillOnce(Invoke(CopyData("INFOthis is an info line")));
54     EXPECT_CALL(transport, Read(_, _)).WillOnce(Invoke(CopyData("OKAY")));
55 
56     std::vector<std::string> info;
57     ASSERT_EQ(driver.RawCommand("oem dmesg", "", nullptr, &info), SUCCESS) << driver.Error();
58     ASSERT_EQ(info.size(), size_t(1));
59     ASSERT_EQ(info[0], "this is an info line");
60 }
61 
TEST_F(DriverTest,TextMessage)62 TEST_F(DriverTest, TextMessage) {
63     MockTransport transport;
64     std::string text;
65 
66     DriverCallbacks callbacks{[](const std::string&) {}, [](int) {}, [](const std::string&) {},
67                               [&text](const std::string& extra_text) { text += extra_text; }};
68 
69     FastBootDriver driver(&transport, callbacks);
70 
71     EXPECT_CALL(transport, Write(_, _))
72             .With(AllArgs(RawData("oem trusty runtest trusty.hwaes.bench")))
73             .WillOnce(ReturnArg<1>());
74     EXPECT_CALL(transport, Read(_, _)).WillOnce(Invoke(CopyData("TEXTthis is a text line")));
75     EXPECT_CALL(transport, Read(_, _))
76             .WillOnce(Invoke(
77                     CopyData("TEXT, albeit very long and split over multiple TEXT messages.")));
78     EXPECT_CALL(transport, Read(_, _))
79             .WillOnce(Invoke(CopyData("TEXT Indeed we can do that now with a TEXT message whenever "
80                                       "we feel like it.")));
81     EXPECT_CALL(transport, Read(_, _))
82             .WillOnce(Invoke(CopyData("TEXT Isn't that truly super cool?")));
83 
84     EXPECT_CALL(transport, Read(_, _)).WillOnce(Invoke(CopyData("OKAY")));
85 
86     std::vector<std::string> info;
87     ASSERT_EQ(driver.RawCommand("oem trusty runtest trusty.hwaes.bench", "", nullptr, &info),
88               SUCCESS)
89             << driver.Error();
90     ASSERT_EQ(text,
91               "this is a text line"
92               ", albeit very long and split over multiple TEXT messages."
93               " Indeed we can do that now with a TEXT message whenever we feel like it."
94               " Isn't that truly super cool?");
95 }
96