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/tests/BackendType.h>
18 #include <aidl/android/aidl/tests/ITestService.h>
19 #include <aidl/android/aidl/versioned/tests/IFooInterface.h>
20
21 #include <android/binder_auto_utils.h>
22 #include <android/binder_manager.h>
23 #include <binder/ProcessState.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26
27 using aidl::android::aidl::tests::BackendType;
28 using aidl::android::aidl::tests::ITestService;
29 using aidl::android::aidl::versioned::tests::BazUnion;
30 using aidl::android::aidl::versioned::tests::Foo;
31 using aidl::android::aidl::versioned::tests::IFooInterface;
32 using std::optional;
33 using std::pair;
34 using std::shared_ptr;
35 using std::string;
36 using std::vector;
37 using testing::Eq;
38
39 struct VersionedInterfaceTest : ::testing::Test {
SetUpVersionedInterfaceTest40 void SetUp() override {
41 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
42 android::ProcessState::self()->startThreadPool();
43 ndk::SpAIBinder binder =
44 ndk::SpAIBinder(AServiceManager_waitForService(IFooInterface::descriptor));
45 versioned = IFooInterface::fromBinder(binder);
46 ASSERT_NE(nullptr, versioned);
47
48 ndk::SpAIBinder testServiceBinder =
49 ndk::SpAIBinder(AServiceManager_waitForService(ITestService::descriptor));
50 auto service = ITestService::fromBinder(testServiceBinder);
51 auto status = service->getBackendType(&backend);
52 EXPECT_TRUE(status.isOk()) << status.getDescription();
53 }
54 shared_ptr<IFooInterface> versioned;
55 BackendType backend;
56 };
57
TEST_F(VersionedInterfaceTest,getInterfaceVersion)58 TEST_F(VersionedInterfaceTest, getInterfaceVersion) {
59 int32_t version;
60 auto status = versioned->getInterfaceVersion(&version);
61 EXPECT_TRUE(status.isOk()) << status.getDescription();
62 EXPECT_EQ(1, version);
63 }
64
TEST_F(VersionedInterfaceTest,getInterfaceHash)65 TEST_F(VersionedInterfaceTest, getInterfaceHash) {
66 string hash;
67 auto status = versioned->getInterfaceHash(&hash);
68 EXPECT_TRUE(status.isOk()) << status.getDescription();
69 EXPECT_EQ("9e7be1859820c59d9d55dd133e71a3687b5d2e5b", hash);
70 }
71
TEST_F(VersionedInterfaceTest,noProblemWhenPassingAUnionWithOldField)72 TEST_F(VersionedInterfaceTest, noProblemWhenPassingAUnionWithOldField) {
73 std::string result;
74 auto status =
75 versioned->acceptUnionAndReturnString(BazUnion::make<BazUnion::intNum>(42), &result);
76 EXPECT_TRUE(status.isOk()) << status.getDescription();
77 EXPECT_EQ("42", result);
78 }
79
TEST_F(VersionedInterfaceTest,errorWhenPassingAUnionWithNewField)80 TEST_F(VersionedInterfaceTest, errorWhenPassingAUnionWithNewField) {
81 std::string result;
82 auto status =
83 versioned->acceptUnionAndReturnString(BazUnion::make<BazUnion::longNum>(42L), &result);
84 // b/173458620 - Java and C++ return different errors
85 if (backend == BackendType::JAVA) {
86 EXPECT_EQ(EX_ILLEGAL_ARGUMENT, status.getExceptionCode());
87 } else {
88 EXPECT_EQ(STATUS_BAD_VALUE, status.getStatus());
89 }
90 }
91
TEST_F(VersionedInterfaceTest,arrayOfParcelableWithNewField)92 TEST_F(VersionedInterfaceTest, arrayOfParcelableWithNewField) {
93 vector<Foo> foos(42);
94 int32_t length;
95 auto status = versioned->returnsLengthOfFooArray(foos, &length);
96 EXPECT_TRUE(status.isOk()) << status.getDescription();
97 EXPECT_EQ(42, length);
98 }
99
TEST_F(VersionedInterfaceTest,readDataCorrectlyAfterParcelableWithNewField)100 TEST_F(VersionedInterfaceTest, readDataCorrectlyAfterParcelableWithNewField) {
101 Foo inFoo, inoutFoo, outFoo;
102 inoutFoo.intDefault42 = 0;
103 outFoo.intDefault42 = 0;
104 int32_t ret;
105 auto status = versioned->ignoreParcelablesAndRepeatInt(inFoo, &inoutFoo, &outFoo, 43, &ret);
106 EXPECT_TRUE(status.isOk()) << status.getDescription();
107 EXPECT_EQ(43, ret);
108 EXPECT_EQ(0, inoutFoo.intDefault42);
109 EXPECT_EQ(0, outFoo.intDefault42);
110 }
111
TEST_F(VersionedInterfaceTest,errorWhenCallingV2Api)112 TEST_F(VersionedInterfaceTest, errorWhenCallingV2Api) {
113 auto status = versioned->newApi();
114 EXPECT_EQ(STATUS_UNKNOWN_TRANSACTION, status.getStatus());
115 }
116