1 /*
2  * Copyright (C) 2022 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 <aidl/Gtest.h>
18 #include <aidl/Vintf.h>
19 #include <aidl/android/hardware/automotive/can/BusConfig.h>
20 #include <aidl/android/hardware/automotive/can/ICanController.h>
21 #include <aidl/android/hardware/automotive/can/Result.h>
22 #include <aidl/android/hardware/automotive/can/VirtualInterface.h>
23 #include <android-base/logging.h>
24 #include <android/binder_manager.h>
25 #include <gtest/gtest.h>
26 #include <libnetdevice/libnetdevice.h>
27 #include <libnl++/MessageFactory.h>
28 #include <libnl++/Socket.h>
29 #include <libnl++/printer.h>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
32 
33 #include <chrono>
34 #include <thread>
35 
36 using aidl::android::hardware::automotive::can::BusConfig;
37 using aidl::android::hardware::automotive::can::ICanController;
38 using aidl::android::hardware::automotive::can::VirtualInterface;
39 using namespace std::chrono_literals;
40 using namespace std::string_literals;
41 
42 class CanControllerAidlTest : public ::testing::TestWithParam<std::string> {
43   public:
SetUp()44     virtual void SetUp() override {
45         android::base::SetDefaultTag("CAN_HAL_VTS");
46         android::base::SetMinimumLogSeverity(android::base::VERBOSE);
47         const auto instance = ICanController::descriptor + "/default"s;
48         mCanControllerService = ICanController::fromBinder(
49                 ndk::SpAIBinder(AServiceManager_waitForService(instance.c_str())));
50 
51         ASSERT_NE(mCanControllerService, nullptr);
52     }
TearDown()53     virtual void TearDown() override {}
54 
55     static bool mTestCaseInitialized;
56     std::shared_ptr<ICanController> mCanControllerService;
57 };
58 
59 // we can't test a real bus, since we can't make any assumptions about hardware
60 // this checks upBus, getInterfaceName, and downBus
TEST_P(CanControllerAidlTest,ToggleBus)61 TEST_P(CanControllerAidlTest, ToggleBus) {
62     const std::string_view canIface = "vcan50";
63     const std::string busName = "VTS_CAN";
64 
65     std::string upBusReturn;  // should be vcan50
66     BusConfig config = {};
67     VirtualInterface iface = {};
68     iface.ifname = canIface;
69     config.interfaceId.set<BusConfig::InterfaceId::Tag::virtualif>(iface);
70     config.name = busName;
71     auto aidlStatus = mCanControllerService->upBus(config, &upBusReturn);
72     ASSERT_TRUE(aidlStatus.isOk());
73     EXPECT_EQ(upBusReturn, canIface);
74 
75     std::string ifaceName;
76     aidlStatus = mCanControllerService->getInterfaceName(busName, &ifaceName);
77     ASSERT_TRUE(aidlStatus.isOk());
78     EXPECT_EQ(ifaceName, canIface);
79 
80     aidlStatus = mCanControllerService->downBus(busName);
81     ASSERT_TRUE(aidlStatus.isOk());
82 }
83 
TEST_P(CanControllerAidlTest,GetSupported)84 TEST_P(CanControllerAidlTest, GetSupported) {
85     LOG(VERBOSE) << "Get the supported iface types";
86     std::vector<::aidl::android::hardware::automotive::can::InterfaceType> supportedTypes;
87     auto aidlStatus = mCanControllerService->getSupportedInterfaceTypes(&supportedTypes);
88     ASSERT_TRUE(aidlStatus.isOk());
89     EXPECT_FALSE(supportedTypes.empty());
90 }
91 
92 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(CanControllerAidlTest);
93 INSTANTIATE_TEST_SUITE_P(
94         PerInstance, CanControllerAidlTest,
95         testing::ValuesIn(android::getAidlHalInstanceNames(ICanController::descriptor)),
96         android::PrintInstanceNameToString);
97