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/versioned/tests/BnFooInterface.h>
18 #include <android/aidl/versioned/tests/IFooInterface.h>
19 #include <binder/IServiceManager.h>
20 #include <binder/ProcessState.h>
21 #include <gtest/gtest.h>
22 #include <utils/String16.h>
23 
24 #include "aidl_test_client.h"
25 
26 using android::OK;
27 using android::sp;
28 using android::String16;
29 using android::aidl::versioned::tests::BazUnion;
30 using android::aidl::versioned::tests::Foo;
31 using android::aidl::versioned::tests::IFooInterface;
32 using android::aidl::versioned::tests::IFooInterfaceDelegator;
33 
34 class VersionedInterfaceTest : public AidlTest {
35  public:
SetUp()36   void SetUp() override {
37     android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
38     android::ProcessState::self()->startThreadPool();
39     versioned = android::waitForService<IFooInterface>(IFooInterface::descriptor);
40     ASSERT_NE(nullptr, versioned);
41 
42     AidlTest::SetUp();
43   }
44 
45   sp<IFooInterface> versioned;
46 };
47 
TEST_F(VersionedInterfaceTest,getInterfaceVersion)48 TEST_F(VersionedInterfaceTest, getInterfaceVersion) {
49   EXPECT_EQ(1, versioned->getInterfaceVersion());
50 }
51 
TEST_F(VersionedInterfaceTest,getInterfaceHash)52 TEST_F(VersionedInterfaceTest, getInterfaceHash) {
53   EXPECT_EQ("9e7be1859820c59d9d55dd133e71a3687b5d2e5b", versioned->getInterfaceHash());
54 }
55 
TEST_F(VersionedInterfaceTest,noProblemWhenPassingAUnionWithOldField)56 TEST_F(VersionedInterfaceTest, noProblemWhenPassingAUnionWithOldField) {
57   std::string result;
58   auto status =
59       versioned->acceptUnionAndReturnString(BazUnion::make<BazUnion::intNum>(42), &result);
60   EXPECT_TRUE(status.isOk());
61   EXPECT_EQ("42", result);
62 }
63 
TEST_F(VersionedInterfaceTest,errorWhenPassingAUnionWithNewField)64 TEST_F(VersionedInterfaceTest, errorWhenPassingAUnionWithNewField) {
65   std::string result;
66   auto status =
67       versioned->acceptUnionAndReturnString(BazUnion::make<BazUnion::longNum>(42L), &result);
68   // b/173458620 - Java and C++ return different errors
69   if (backend == BackendType::JAVA) {
70     EXPECT_EQ(::android::binder::Status::EX_ILLEGAL_ARGUMENT, status.exceptionCode()) << status;
71   } else {
72     EXPECT_EQ(::android::BAD_VALUE, status.transactionError()) << status;
73   }
74 }
75 
TEST_F(VersionedInterfaceTest,arrayOfParcelableWithNewParam)76 TEST_F(VersionedInterfaceTest, arrayOfParcelableWithNewParam) {
77   std::vector<Foo> foos(42);
78   int32_t length;
79   auto status = versioned->returnsLengthOfFooArray(foos, &length);
80   EXPECT_TRUE(status.isOk());
81   EXPECT_EQ(42, length);
82 }
83 
TEST_F(VersionedInterfaceTest,readDataCorrectlyAfterParcelableWithNewField)84 TEST_F(VersionedInterfaceTest, readDataCorrectlyAfterParcelableWithNewField) {
85   Foo inFoo, inoutFoo, outFoo;
86   inoutFoo.intDefault42 = 0;
87   outFoo.intDefault42 = 0;
88   int32_t ret;
89   auto status = versioned->ignoreParcelablesAndRepeatInt(inFoo, &inoutFoo, &outFoo, 43, &ret);
90   EXPECT_TRUE(status.isOk());
91   EXPECT_EQ(43, ret);
92   EXPECT_EQ(0, inoutFoo.intDefault42);
93   EXPECT_EQ(0, outFoo.intDefault42);
94 }
95 
TEST_F(VersionedInterfaceTest,newerDelegatorReturnsImplVersion)96 TEST_F(VersionedInterfaceTest, newerDelegatorReturnsImplVersion) {
97   auto delegator = sp<IFooInterfaceDelegator>::make(versioned);
98   EXPECT_EQ(1, delegator->getInterfaceVersion());
99 }
100 
TEST_F(VersionedInterfaceTest,newerDelegatorReturnsImplHash)101 TEST_F(VersionedInterfaceTest, newerDelegatorReturnsImplHash) {
102   auto delegator = sp<IFooInterfaceDelegator>::make(versioned);
103   EXPECT_EQ("9e7be1859820c59d9d55dd133e71a3687b5d2e5b", delegator->getInterfaceHash());
104 }
105 
TEST_F(VersionedInterfaceTest,errorWhenCallingV2Api)106 TEST_F(VersionedInterfaceTest, errorWhenCallingV2Api) {
107   auto status = versioned->newApi();
108   EXPECT_EQ(::android::UNKNOWN_TRANSACTION, status.transactionError()) << status;
109 }
110