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 
17 #include <aidl/android/aidl/loggable/ILoggableInterface.h>
18 
19 #include <android/binder_auto_utils.h>
20 #include <android/binder_manager.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 #include <aidl/android/aidl/loggable/BpLoggableInterface.h>
25 #include <aidl/android/aidl/tests/BackendType.h>
26 #include <aidl/android/aidl/tests/ITestService.h>
27 
28 using aidl::android::aidl::loggable::BpLoggableInterface;
29 using aidl::android::aidl::loggable::Data;
30 using aidl::android::aidl::loggable::Enum;
31 using aidl::android::aidl::loggable::ILoggableInterface;
32 using aidl::android::aidl::tests::BackendType;
33 using aidl::android::aidl::tests::ITestService;
34 using std::optional;
35 using std::pair;
36 using std::shared_ptr;
37 using std::string;
38 using std::vector;
39 using testing::Eq;
40 
41 struct AidlTest : testing::Test {
42   template <typename T>
getServiceAidlTest43   std::shared_ptr<T> getService() {
44     ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_getService(T::descriptor));
45     return T::fromBinder(binder);
46   }
47 };
48 
TEST_F(AidlTest,LoggableInterface)49 TEST_F(AidlTest, LoggableInterface) {
50   std::shared_ptr<ITestService> service = getService<ITestService>();
51   ASSERT_NE(nullptr, service.get());
52 
53   BackendType backendType;
54   ndk::ScopedAStatus status = service->getBackendType(&backendType);
55   EXPECT_TRUE(status.isOk()) << status.getDescription();
56   if (backendType != BackendType::CPP) GTEST_SKIP();
57 
58   shared_ptr<ILoggableInterface> loggable = getService<ILoggableInterface>();
59   ASSERT_NE(nullptr, loggable.get());
60 
61   BpLoggableInterface::TransactionLog log;
62   BpLoggableInterface::logFunc = [&](const BpLoggableInterface::TransactionLog& tx) { log = tx; };
63 
64   bool boolValue = true;
65   vector<bool> boolArray{false, true};
66   int8_t byteValue = 41;
67   vector<uint8_t> byteArray{42, 43};
68   char16_t charValue = 'x';
69   vector<char16_t> charArray{'a', 'b', 'c'};
70   int32_t intValue{44};
71   vector<int32_t> intArray{45, 46};
72   int64_t longValue = 47;
73   vector<int64_t> longArray{48, 49};
74   float floatValue{50};
75   vector<float> floatArray{51, 52};
76   double doubleValue{52};
77   vector<double> doubleArray{53, 54};
78   string stringValue("def");
79   vector<string> stringArray{string("ghi"), string("jkl")};
80   vector<string> listValue{string("mno")};
81   Data dataValue;
82   dataValue.num = 42;
83   dataValue.str = "abc";
84   dataValue.nestedUnion = "def";
85   dataValue.nestedEnum = Enum::FOO;
86   ndk::SpAIBinder binderValue;
87   ndk::ScopedFileDescriptor pfdValue;
88   vector<ndk::ScopedFileDescriptor> pfdArray;
89   vector<string> _aidl_return;
90   status = loggable->LogThis(boolValue, &boolArray, byteValue, &byteArray, charValue, &charArray,
91                              intValue, &intArray, longValue, &longArray, floatValue, &floatArray,
92                              doubleValue, &doubleArray, stringValue, &stringArray, &listValue,
93                              dataValue, binderValue, &pfdValue, &pfdArray, &_aidl_return);
94   EXPECT_TRUE(status.isOk());
95   EXPECT_EQ(vector<string>{string("loggable")}, _aidl_return);
96 
97   // check the captured log
98   EXPECT_EQ("[loggable]", log.result);
99   EXPECT_EQ("android.aidl.loggable.ILoggableInterface", log.interface_name);
100   EXPECT_EQ("LogThis", log.method_name);
101   EXPECT_EQ(0, log.exception_code);
102   EXPECT_EQ("", log.exception_message);
103   EXPECT_EQ(0, log.transaction_error);
104   EXPECT_EQ(0, log.service_specific_error_code);
105   EXPECT_THAT(log.input_args,
106               Eq(vector<pair<string, string>>{
107                   {"in_boolValue", "true"},
108                   {"in_boolArray", "[false, true]"},
109                   {"in_byteValue", "41"},
110                   {"in_byteArray", "[42, 43]"},
111                   {"in_charValue", "x"},
112                   {"in_charArray", "[a, b, c]"},
113                   {"in_intValue", "44"},
114                   {"in_intArray", "[45, 46]"},
115                   {"in_longValue", "47"},
116                   {"in_longArray", "[48, 49]"},
117                   {"in_floatValue", "50.000000"},
118                   {"in_floatArray", "[51.000000, 52.000000]"},
119                   {"in_doubleValue", "52.000000"},
120                   {"in_doubleArray", "[53.000000, 54.000000]"},
121                   {"in_stringValue", "def"},
122                   {"in_stringArray", "[ghi, jkl]"},
123                   {"in_listValue", "[mno]"},
124                   {"in_dataValue",
125                    "Data{num: 42, str: abc, nestedUnion: Union{str: def}, nestedEnum: FOO}"},
126                   {"in_binderValue", "(null)"},
127                   {"in_pfdValue", "(null)"},
128                   {"in_pfdArray", "[]"},
129               }));
130   EXPECT_THAT(log.output_args,
131               Eq(vector<pair<string, string>>{{"in_boolArray", "[false, true]"},
132                                               {"in_byteArray", "[42, 43]"},
133                                               {"in_charArray", "[a, b, c]"},
134                                               {"in_intArray", "[45, 46]"},
135                                               {"in_longArray", "[48, 49]"},
136                                               {"in_floatArray", "[51.000000, 52.000000]"},
137                                               {"in_doubleArray", "[53.000000, 54.000000]"},
138                                               {"in_stringArray", "[ghi, jkl]"},
139                                               {"in_listValue", "[mno]"},
140                                               {"in_pfdValue", "(null)"},
141                                               {"in_pfdArray", "[]"}}));
142 }
143