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 <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
25 using namespace android;
26 using ::ndk::ScopedAStatus;
27 using ::ndk::SharedRefBase;
28 using ::ndk::SpAIBinder;
29
30 static const char* kNdkServerName = "NdkServer-BinderRustNdkInteropTest";
31 static const char* kRustServerName = "RustServer-BinderRustNdkInteropTest";
32
33 extern "C" {
34 int rust_call_ndk(const char* service_name);
35 int rust_start_service(const char* service_name);
36 }
37
38 class NdkServer : public aidl::BnBinderRustNdkInteropTest {
echo(const std::string & in,std::string * out)39 ScopedAStatus echo(const std::string& in, std::string* out) override {
40 *out = in;
41 return ScopedAStatus::ok();
42 }
43 };
44
TEST(RustNdkInterop,RustCanCallNdk)45 TEST(RustNdkInterop, RustCanCallNdk) {
46 ASSERT_EQ(STATUS_OK, rust_call_ndk(kNdkServerName));
47 }
48
TEST(RustNdkInterop,NdkCanCallRust)49 TEST(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 EXPECT_NE(interface, nullptr);
58
59 std::string in("testing");
60 std::string out;
61 EXPECT_TRUE(interface->echo(in, &out).isOk());
62 EXPECT_EQ(in, out);
63 }
64
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66 ::testing::InitGoogleTest(&argc, argv);
67
68 // so we can host a client and service concurrently
69 ABinderProcess_setThreadPoolMaxThreadCount(1);
70 ABinderProcess_startThreadPool();
71
72 std::shared_ptr<NdkServer> ndkServer = SharedRefBase::make<NdkServer>();
73 EXPECT_EQ(STATUS_OK, AServiceManager_addService(ndkServer->asBinder().get(), kNdkServerName));
74
75 return RUN_ALL_TESTS();
76 }
77