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     // 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 
main(int argc,char ** argv)67 int 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 }
79