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