1 /*
2 * Copyright (C) 2021 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 #define LOG_TAG "cartelemetryd_hal_aidl_test"
17
18 #include <aidl/Gtest.h>
19 #include <aidl/Vintf.h>
20 #include <aidl/android/frameworks/automotive/telemetry/BnCarTelemetryCallback.h>
21 #include <aidl/android/frameworks/automotive/telemetry/CallbackConfig.h>
22 #include <aidl/android/frameworks/automotive/telemetry/CarData.h>
23 #include <aidl/android/frameworks/automotive/telemetry/ICarTelemetry.h>
24 #include <android-base/logging.h>
25 #include <android/binder_auto_utils.h>
26 #include <android/binder_ibinder.h>
27 #include <android/binder_manager.h>
28 #include <android/binder_process.h>
29 #include <android/binder_status.h>
30 #include <gmock/gmock.h>
31 #include <gtest/gtest.h>
32
33 #include <vector>
34
35 namespace android {
36 namespace frameworks {
37 namespace automotive {
38 namespace telemetry {
39
40 using ::aidl::android::frameworks::automotive::telemetry::BnCarTelemetryCallback;
41 using ::aidl::android::frameworks::automotive::telemetry::CallbackConfig;
42 using ::aidl::android::frameworks::automotive::telemetry::CarData;
43 using ::aidl::android::frameworks::automotive::telemetry::ICarTelemetry;
44 using ::ndk::ScopedAStatus;
45
46 class MockCarTelemetryCallback : public BnCarTelemetryCallback {
47 public:
MockCarTelemetryCallback()48 MockCarTelemetryCallback() {}
49
onChange(const std::vector<int32_t> & ids)50 ScopedAStatus onChange([[maybe_unused]] const std::vector<int32_t>& ids) override {
51 return ScopedAStatus::ok();
52 }
53 };
54
buildConfig(const std::vector<int32_t> & ids)55 CallbackConfig buildConfig(const std::vector<int32_t>& ids) {
56 CallbackConfig config;
57 config.carDataIds = ids;
58 return config;
59 }
60
buildCarData(int id,const std::vector<uint8_t> & content)61 CarData buildCarData(int id, const std::vector<uint8_t>& content) {
62 CarData msg;
63 msg.id = id;
64 msg.content = content;
65 return msg;
66 }
67
68 class CarTelemetryTest : public testing::TestWithParam<std::string> {
69 public:
SetUp()70 virtual void SetUp() override {
71 mService = ICarTelemetry::fromBinder(
72 ndk::SpAIBinder(::AServiceManager_waitForService(GetParam().c_str())));
73 ASSERT_NE(nullptr, mService.get()) << "Instance '" << GetParam() << "'' is not available.";
74 }
75
76 std::shared_ptr<ICarTelemetry> mService;
77 };
78
79 // TODO(b/182598466): Add verifying contents after adding internal AIDLs for reading
80 // TODO(b/182598466): Add test for size limit check
TEST_P(CarTelemetryTest,writeReturnsOk)81 TEST_P(CarTelemetryTest, writeReturnsOk) {
82 CarData msg = buildCarData(101, {1, 0, 1, 0});
83
84 auto status = mService->write({msg});
85
86 EXPECT_TRUE(status.isOk()) << status.getMessage();
87 }
88
TEST_P(CarTelemetryTest,AddCallbackReturnsOk)89 TEST_P(CarTelemetryTest, AddCallbackReturnsOk) {
90 CallbackConfig config = buildConfig({101, 102, 103});
91 std::shared_ptr<MockCarTelemetryCallback> callback =
92 ndk::SharedRefBase::make<MockCarTelemetryCallback>();
93
94 auto status = mService->addCallback(config, callback);
95
96 EXPECT_TRUE(status.isOk()) << status.getMessage();
97 }
98
TEST_P(CarTelemetryTest,AddCallbackErrorsWhenDuplicate)99 TEST_P(CarTelemetryTest, AddCallbackErrorsWhenDuplicate) {
100 CallbackConfig config = buildConfig({101, 102, 103});
101 std::shared_ptr<MockCarTelemetryCallback> callback =
102 ndk::SharedRefBase::make<MockCarTelemetryCallback>();
103 mService->addCallback(config, callback);
104
105 auto status = mService->addCallback(config, callback);
106
107 EXPECT_FALSE(status.isOk()) << status.getMessage();
108 }
109
TEST_P(CarTelemetryTest,RemoveCallbackReturnsOk)110 TEST_P(CarTelemetryTest, RemoveCallbackReturnsOk) {
111 CallbackConfig config = buildConfig({101, 102, 103});
112 std::shared_ptr<MockCarTelemetryCallback> callback =
113 ndk::SharedRefBase::make<MockCarTelemetryCallback>();
114 mService->addCallback(config, callback);
115
116 auto status = mService->removeCallback(callback);
117
118 EXPECT_TRUE(status.isOk()) << status.getMessage();
119 }
120
TEST_P(CarTelemetryTest,RemoveCallbackErrorsForNonexistentCallback)121 TEST_P(CarTelemetryTest, RemoveCallbackErrorsForNonexistentCallback) {
122 std::shared_ptr<MockCarTelemetryCallback> callback =
123 ndk::SharedRefBase::make<MockCarTelemetryCallback>();
124
125 auto status = mService->removeCallback(callback);
126
127 EXPECT_FALSE(status.isOk()) << status.getMessage();
128 }
129
130 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(CarTelemetryTest);
131 INSTANTIATE_TEST_SUITE_P(
132 AutomotiveHal, CarTelemetryTest,
133 testing::ValuesIn(::android::getAidlHalInstanceNames(ICarTelemetry::descriptor)),
134 ::android::PrintInstanceNameToString);
135
136 } // namespace telemetry
137 } // namespace automotive
138 } // namespace frameworks
139 } // namespace android
140
main(int argc,char ** argv)141 int main(int argc, char** argv) {
142 ::testing::InitGoogleTest(&argc, argv);
143 ABinderProcess_setThreadPoolMaxThreadCount(1);
144 ABinderProcess_startThreadPool();
145 return RUN_ALL_TESTS();
146 }
147