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