1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "GRPCVehicleHardware.h"
16 #include "GRPCVehicleProxyServer.h"
17 #include "IVehicleHardware.h"
18 #include "VehicleServer.grpc.pb.h"
19 #include "VehicleServer.pb.h"
20
21 #include <gmock/gmock.h>
22 #include <grpc++/grpc++.h>
23 #include <gtest/gtest.h>
24
25 #include <chrono>
26 #include <memory>
27 #include <string>
28 #include <thread>
29 #include <utility>
30
31 namespace android::hardware::automotive::vehicle::virtualization {
32
33 const std::string kFakeServerAddr = "0.0.0.0:54321";
34
35 class VehicleHardwareForTest : public IVehicleHardware {
36 public:
registerOnPropertyChangeEvent(std::unique_ptr<const PropertyChangeCallback> callback)37 void registerOnPropertyChangeEvent(
38 std::unique_ptr<const PropertyChangeCallback> callback) override {
39 mOnProp = std::move(callback);
40 }
41
onPropertyEvent(std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropValue> values)42 void onPropertyEvent(
43 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropValue> values) {
44 if (mOnProp) {
45 (*mOnProp)(std::move(values));
46 }
47 }
48
49 // Functions that we do not care.
50 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>
getAllPropertyConfigs() const51 getAllPropertyConfigs() const override {
52 return {};
53 }
54
setValues(std::shared_ptr<const SetValuesCallback> callback,const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest> & requests)55 aidl::android::hardware::automotive::vehicle::StatusCode setValues(
56 std::shared_ptr<const SetValuesCallback> callback,
57 const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>&
58 requests) override {
59 return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
60 }
61
getValues(std::shared_ptr<const GetValuesCallback> callback,const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest> & requests) const62 aidl::android::hardware::automotive::vehicle::StatusCode getValues(
63 std::shared_ptr<const GetValuesCallback> callback,
64 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>&
65 requests) const override {
66 return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
67 }
68
dump(const std::vector<std::string> & options)69 DumpResult dump(const std::vector<std::string>& options) override { return {}; }
70
checkHealth()71 aidl::android::hardware::automotive::vehicle::StatusCode checkHealth() override {
72 return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
73 }
74
registerOnPropertySetErrorEvent(std::unique_ptr<const PropertySetErrorCallback> callback)75 void registerOnPropertySetErrorEvent(
76 std::unique_ptr<const PropertySetErrorCallback> callback) override {}
77
78 private:
79 std::unique_ptr<const PropertyChangeCallback> mOnProp;
80 };
81
TEST(GRPCVehicleProxyServerUnitTest,ClientConnectDisconnect)82 TEST(GRPCVehicleProxyServerUnitTest, ClientConnectDisconnect) {
83 auto testHardware = std::make_unique<VehicleHardwareForTest>();
84 // HACK: manipulate the underlying hardware via raw pointer for testing.
85 auto* testHardwareRaw = testHardware.get();
86 auto vehicleServer =
87 std::make_unique<GrpcVehicleProxyServer>(kFakeServerAddr, std::move(testHardware));
88 vehicleServer->Start();
89
90 constexpr auto kWaitForConnectionMaxTime = std::chrono::seconds(5);
91 constexpr auto kWaitForStreamStartTime = std::chrono::seconds(1);
92 constexpr auto kWaitForUpdateDeliveryTime = std::chrono::milliseconds(100);
93
94 auto updateReceived1 = std::make_shared<bool>(false);
95 auto vehicleHardware1 = std::make_unique<GRPCVehicleHardware>(kFakeServerAddr);
96 vehicleHardware1->registerOnPropertyChangeEvent(
97 std::make_unique<const IVehicleHardware::PropertyChangeCallback>(
98 [updateReceived1](const auto&) { *updateReceived1 = true; }));
99 EXPECT_TRUE(vehicleHardware1->waitForConnected(kWaitForConnectionMaxTime));
100 std::this_thread::sleep_for(kWaitForStreamStartTime);
101
102 // Client hardware 1 received update from the server.
103 EXPECT_FALSE(*updateReceived1);
104 testHardwareRaw->onPropertyEvent({});
105 // Wait for the update delivery.
106 std::this_thread::sleep_for(kWaitForUpdateDeliveryTime);
107 EXPECT_TRUE(*updateReceived1);
108
109 // Reset.
110 *updateReceived1 = false;
111
112 auto updateReceived2 = std::make_shared<bool>(false);
113 auto vehicleHardware2 = std::make_unique<GRPCVehicleHardware>(kFakeServerAddr);
114 vehicleHardware2->registerOnPropertyChangeEvent(
115 std::make_unique<const IVehicleHardware::PropertyChangeCallback>(
116 [updateReceived2](const auto&) { *updateReceived2 = true; }));
117 EXPECT_TRUE(vehicleHardware2->waitForConnected(kWaitForConnectionMaxTime));
118 std::this_thread::sleep_for(kWaitForStreamStartTime);
119
120 // Both client hardware 1 and 2 received update from the server.
121 EXPECT_FALSE(*updateReceived1);
122 EXPECT_FALSE(*updateReceived2);
123 testHardwareRaw->onPropertyEvent({});
124 // Wait for the update delivery.
125 std::this_thread::sleep_for(kWaitForUpdateDeliveryTime);
126 EXPECT_TRUE(*updateReceived1);
127 EXPECT_TRUE(*updateReceived2);
128
129 // Reset.
130 *updateReceived1 = false;
131 *updateReceived2 = false;
132
133 vehicleHardware1.reset();
134
135 // Client 1 exited, only client hardware 2 received update from the server.
136 EXPECT_FALSE(*updateReceived1);
137 EXPECT_FALSE(*updateReceived2);
138 testHardwareRaw->onPropertyEvent({});
139 // Wait for the update delivery.
140 std::this_thread::sleep_for(kWaitForUpdateDeliveryTime);
141 EXPECT_FALSE(*updateReceived1);
142 EXPECT_TRUE(*updateReceived2);
143
144 vehicleServer->Shutdown().Wait();
145 }
146
147 } // namespace android::hardware::automotive::vehicle::virtualization
148