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