blob: 59ca6edf12a44743ea8640f31d6f1262f9549980 [file] [log] [blame]
Stephen Crane29422bf2020-09-10 17:31:39 -07001/*
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 <aidl/BnBinderRustNdkInteropTest.h>
18#include <aidl/IBinderRustNdkInteropTest.h>
19#include <android/binder_ibinder.h>
20#include <android/binder_manager.h>
21#include <android/binder_process.h>
22#include <binder/Status.h>
23#include <gtest/gtest.h>
24
25using namespace android;
26using ::ndk::ScopedAStatus;
27using ::ndk::SharedRefBase;
28using ::ndk::SpAIBinder;
29
30static const char* kNdkServerName = "NdkServer-BinderRustNdkInteropTest";
31static const char* kRustServerName = "RustServer-BinderRustNdkInteropTest";
32
33extern "C" {
34int rust_call_ndk(const char* service_name);
35int rust_start_service(const char* service_name);
36}
37
38class NdkServer : public aidl::BnBinderRustNdkInteropTest {
39 ScopedAStatus echo(const std::string& in, std::string* out) override {
40 *out = in;
41 return ScopedAStatus::ok();
42 }
43};
44
45TEST(RustNdkInterop, RustCanCallNdk) {
46 ASSERT_EQ(STATUS_OK, rust_call_ndk(kNdkServerName));
47}
48
49TEST(RustNdkInterop, NdkCanCallRust) {
50 ASSERT_EQ(STATUS_OK, rust_start_service(kRustServerName));
51
52 SpAIBinder binder = SpAIBinder(AServiceManager_checkService(kRustServerName));
53 ASSERT_NE(nullptr, binder.get());
54 EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
55
56 auto interface = aidl::IBinderRustNdkInteropTest::fromBinder(binder);
57 // TODO(b/167723746): this test requires that fromBinder allow association
58 // with an already associated local binder by treating it as remote.
59 EXPECT_EQ(interface, nullptr);
60
61 // std::string in("testing");
62 // std::string out;
63 // EXPECT_TRUE(interface->echo(in, &out).isOk());
64 // EXPECT_EQ(in, out);
65}
66
67int main(int argc, char** argv) {
68 ::testing::InitGoogleTest(&argc, argv);
69
70 // so we can host a client and service concurrently
71 ABinderProcess_setThreadPoolMaxThreadCount(1);
72 ABinderProcess_startThreadPool();
73
74 std::shared_ptr<NdkServer> ndkServer = SharedRefBase::make<NdkServer>();
75 EXPECT_EQ(STATUS_OK, AServiceManager_addService(ndkServer->asBinder().get(), kNdkServerName));
76
77 return RUN_ALL_TESTS();
78}