Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2318e691 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "libbinder: Add Trusty IPC support to binderRpcTest"

parents f61f48bc 68a55619
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -74,6 +74,9 @@ cc_defaults {
    name: "libbinder_common_defaults",
    host_supported: true,

    // for vndbinder and binderRpcTest
    vendor_available: true,

    srcs: [
        "Binder.cpp",
        "BpBinder.cpp",
@@ -197,6 +200,7 @@ cc_defaults {

cc_library_headers {
    name: "trusty_mock_headers",
    vendor_available: true,
    host_supported: true,

    export_include_dirs: [
@@ -295,8 +299,6 @@ cc_library {

    version_script: "libbinder.map",

    // for vndbinder
    vendor_available: true,
    vndk: {
        enabled: true,
    },
@@ -459,6 +461,7 @@ cc_library_shared {
cc_library_static {
    name: "libbinder_tls_static",
    defaults: ["libbinder_tls_defaults"],
    vendor_available: true,
    visibility: [
        ":__subpackages__",
    ],
+12 −1
Original line number Diff line number Diff line
@@ -138,6 +138,7 @@ cc_test {

aidl_interface {
    name: "binderRpcTestIface",
    vendor_available: true,
    host_supported: true,
    unstable: true,
    srcs: [
@@ -158,6 +159,7 @@ aidl_interface {

cc_library_static {
    name: "libbinder_tls_test_utils",
    vendor_available: true,
    host_supported: true,
    target: {
        darwin: {
@@ -211,6 +213,7 @@ cc_defaults {
    defaults: [
        "binderRpcTest_common_defaults",
    ],
    vendor_available: true,
    gtest: false,
    auto_gen_config: false,
    srcs: [
@@ -221,10 +224,18 @@ cc_defaults {

cc_defaults {
    name: "binderRpcTest_defaults",
    vendor_available: true,
    target: {
        android: {
            test_suites: ["vts"],
        },

        vendor: {
            shared_libs: [
                "libbinder_trusty",
                "libtrusty",
            ],
        },
    },
    defaults: [
        "binderRpcTest_common_defaults",
@@ -267,6 +278,7 @@ cc_defaults {
    name: "binderRpcTest_static_defaults",

    shared_libs: [
        "liblog",
        "libutils",
        // libcrypto_static is not visible to this module
        "libcrypto",
@@ -274,7 +286,6 @@ cc_defaults {
    static_libs: [
        "libbase",
        "libcutils",
        "liblog",
        "libssl",
    ],

+125 −18
Original line number Diff line number Diff line
@@ -28,6 +28,11 @@
#include <sys/prctl.h>
#include <sys/socket.h>

#ifdef __ANDROID_VENDOR__
#include <binder/RpcTransportTipcAndroid.h>
#include <trusty/tipc.h>
#endif // __ANDROID_VENDOR__

#include "binderRpcTestCommon.h"
#include "binderRpcTestFixture.h"

@@ -45,6 +50,10 @@ constexpr bool kEnableSharedLibs = false;
constexpr bool kEnableSharedLibs = true;
#endif

#ifdef __ANDROID_VENDOR__
constexpr char kTrustyIpcDevice[] = "/dev/trusty-ipc-dev0";
#endif

static std::string WaitStatusToString(int wstatus) {
    if (WIFEXITED(wstatus)) {
        return base::StringPrintf("exit status %d", WEXITSTATUS(wstatus));
@@ -270,6 +279,11 @@ std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc(

    auto ret = std::make_unique<LinuxProcessSession>(
            Process([=](android::base::borrowed_fd writeEnd, android::base::borrowed_fd readEnd) {
                if (socketType == SocketType::TIPC) {
                    // Trusty has a single persistent service
                    return;
                }

                auto writeFd = std::to_string(writeEnd.get());
                auto readFd = std::to_string(readEnd.get());
                execl(servicePath.c_str(), servicePath.c_str(), writeFd.c_str(), readFd.c_str(),
@@ -288,15 +302,29 @@ std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc(
        serverConfig.serverSupportedFileDescriptorTransportModes.push_back(
                static_cast<int32_t>(mode));
    }
    if (socketType != SocketType::TIPC) {
        writeToFd(ret->host.writeEnd(), serverConfig);
    }

    std::vector<sp<RpcSession>> sessions;
    auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>();
    for (size_t i = 0; i < options.numSessions; i++) {
        sessions.emplace_back(RpcSession::make(newFactory(rpcSecurity, certVerifier)));
        std::unique_ptr<RpcTransportCtxFactory> factory;
        if (socketType == SocketType::TIPC) {
#ifdef __ANDROID_VENDOR__
            factory = RpcTransportCtxFactoryTipcAndroid::make();
#else
            LOG_ALWAYS_FATAL("TIPC socket type only supported on vendor");
#endif
        } else {
            factory = newFactory(rpcSecurity, certVerifier);
        }
        sessions.emplace_back(RpcSession::make(std::move(factory)));
    }

    auto serverInfo = readFromFd<BinderRpcTestServerInfo>(ret->host.readEnd());
    BinderRpcTestServerInfo serverInfo;
    if (socketType != SocketType::TIPC) {
        serverInfo = readFromFd<BinderRpcTestServerInfo>(ret->host.readEnd());
        BinderRpcTestClientInfo clientInfo;
        for (const auto& session : sessions) {
            auto& parcelableCert = clientInfo.certs.emplace_back();
@@ -312,7 +340,9 @@ std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc(
        if (rpcSecurity == RpcSecurity::TLS) {
            const auto& serverCert = serverInfo.cert.data;
            CHECK_EQ(OK,
                 certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM, serverCert));
                     certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM,
                                                             serverCert));
        }
    }

    status_t status;
@@ -343,6 +373,19 @@ std::unique_ptr<ProcessSession> BinderRpc::createRpcTestSocketServerProcessEtc(
            case SocketType::INET:
                status = session->setupInetClient("127.0.0.1", serverInfo.port);
                break;
            case SocketType::TIPC:
                status = session->setupPreconnectedClient({}, [=]() {
#ifdef __ANDROID_VENDOR__
                    auto port = trustyIpcPort(serverVersion);
                    int tipcFd = tipc_connect(kTrustyIpcDevice, port.c_str());
                    return tipcFd >= 0 ? android::base::unique_fd(tipcFd)
                                       : android::base::unique_fd();
#else
                    LOG_ALWAYS_FATAL("Tried to connect to Trusty outside of vendor");
                    return android::base::unique_fd();
#endif
                });
                break;
            default:
                LOG_ALWAYS_FATAL("Unknown socket type");
        }
@@ -684,6 +727,10 @@ TEST_P(BinderRpc, SingleDeathRecipientOnShutdown) {
}

TEST_P(BinderRpc, DeathRecipientFailsWithoutIncoming) {
    if (socketType() == SocketType::TIPC) {
        // This should work, but Trusty takes too long to restart the service
        GTEST_SKIP() << "Service death test not supported on Trusty";
    }
    class MyDeathRec : public IBinder::DeathRecipient {
    public:
        void binderDied(const wp<IBinder>& /* who */) override {}
@@ -725,6 +772,11 @@ TEST_P(BinderRpc, UnlinkDeathRecipient) {
}

TEST_P(BinderRpc, Die) {
    if (socketType() == SocketType::TIPC) {
        // This should work, but Trusty takes too long to restart the service
        GTEST_SKIP() << "Service death test not supported on Trusty";
    }

    for (bool doDeathCleanup : {true, false}) {
        auto proc = createRpcTestSocketServerProcess({});

@@ -777,6 +829,10 @@ TEST_P(BinderRpc, UseKernelBinderCallingId) {
}

TEST_P(BinderRpc, FileDescriptorTransportRejectNone) {
    if (socketType() == SocketType::TIPC) {
        GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
    }

    auto proc = createRpcTestSocketServerProcess({
            .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::NONE,
            .serverSupportedFileDescriptorTransportModes =
@@ -793,6 +849,10 @@ TEST_P(BinderRpc, FileDescriptorTransportRejectNone) {
}

TEST_P(BinderRpc, FileDescriptorTransportRejectUnix) {
    if (socketType() == SocketType::TIPC) {
        GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
    }

    auto proc = createRpcTestSocketServerProcess({
            .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
            .serverSupportedFileDescriptorTransportModes =
@@ -809,6 +869,10 @@ TEST_P(BinderRpc, FileDescriptorTransportRejectUnix) {
}

TEST_P(BinderRpc, FileDescriptorTransportOptionalUnix) {
    if (socketType() == SocketType::TIPC) {
        GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
    }

    auto proc = createRpcTestSocketServerProcess({
            .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::NONE,
            .serverSupportedFileDescriptorTransportModes =
@@ -822,6 +886,10 @@ TEST_P(BinderRpc, FileDescriptorTransportOptionalUnix) {
}

TEST_P(BinderRpc, ReceiveFile) {
    if (socketType() == SocketType::TIPC) {
        GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
    }

    auto proc = createRpcTestSocketServerProcess({
            .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
            .serverSupportedFileDescriptorTransportModes =
@@ -842,6 +910,10 @@ TEST_P(BinderRpc, ReceiveFile) {
}

TEST_P(BinderRpc, SendFiles) {
    if (socketType() == SocketType::TIPC) {
        GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
    }

    auto proc = createRpcTestSocketServerProcess({
            .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
            .serverSupportedFileDescriptorTransportModes =
@@ -914,6 +986,10 @@ TEST_P(BinderRpc, SendTooManyFiles) {
}

TEST_P(BinderRpc, AppendInvalidFd) {
    if (socketType() == SocketType::TIPC) {
        GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
    }

    auto proc = createRpcTestSocketServerProcess({
            .clientFileDescriptorTransportMode = RpcSession::FileDescriptorTransportMode::UNIX,
            .serverSupportedFileDescriptorTransportModes =
@@ -940,6 +1016,7 @@ TEST_P(BinderRpc, AppendInvalidFd) {
    ASSERT_EQ(-1, pRaw.readFileDescriptor());
}

#ifndef __ANDROID_VENDOR__ // No AIBinder_fromPlatformBinder on vendor
TEST_P(BinderRpc, WorksWithLibbinderNdkPing) {
    if constexpr (!kEnableSharedLibs) {
        GTEST_SKIP() << "Test disabled because Binder was built as a static library";
@@ -971,6 +1048,7 @@ TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) {
    ASSERT_TRUE(status.isOk()) << status.getDescription();
    ASSERT_EQ("aoeuaoeu", out);
}
#endif // __ANDROID_VENDOR__

ssize_t countFds() {
    DIR* dir = opendir("/proc/self/fd/");
@@ -986,6 +1064,9 @@ TEST_P(BinderRpc, Fds) {
    if (serverSingleThreaded()) {
        GTEST_SKIP() << "This test requires multiple threads";
    }
    if (socketType() == SocketType::TIPC) {
        GTEST_SKIP() << "File descriptor tests not supported on Trusty (yet)";
    }

    ssize_t beforeFds = countFds();
    ASSERT_GE(beforeFds, 0);
@@ -1100,6 +1181,21 @@ static std::vector<SocketType> testSocketTypes(bool hasPreconnected = true) {
    return ret;
}

static std::vector<SocketType> testTipcSocketTypes() {
#ifdef __ANDROID_VENDOR__
    auto port = trustyIpcPort(RPC_WIRE_PROTOCOL_VERSION_EXPERIMENTAL);
    int tipcFd = tipc_connect(kTrustyIpcDevice, port.c_str());
    if (tipcFd >= 0) {
        close(tipcFd);
        return {SocketType::TIPC};
    }
#endif // __ANDROID_VENDOR__

    // TIPC is not supported on this device, most likely
    // because /dev/trusty-ipc-dev0 is missing
    return {};
}

INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
                        ::testing::Combine(::testing::ValuesIn(testSocketTypes()),
                                           ::testing::ValuesIn(RpcSecurityValues()),
@@ -1109,6 +1205,14 @@ INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc,
                                           ::testing::Values(false, true)),
                        BinderRpc::PrintParamInfo);

INSTANTIATE_TEST_CASE_P(Trusty, BinderRpc,
                        ::testing::Combine(::testing::ValuesIn(testTipcSocketTypes()),
                                           ::testing::Values(RpcSecurity::RAW),
                                           ::testing::ValuesIn(testVersions()),
                                           ::testing::ValuesIn(testVersions()),
                                           ::testing::Values(true), ::testing::Values(true)),
                        BinderRpc::PrintParamInfo);

class BinderRpcServerRootObject
      : public ::testing::TestWithParam<std::tuple<bool, bool, RpcSecurity>> {};

@@ -1360,7 +1464,10 @@ public:
                              addr, port);
                        return base::unique_fd{};
                    };
                }
                } break;
                case SocketType::TIPC: {
                    LOG_ALWAYS_FATAL("RpcTransportTest should not be enabled for TIPC");
                } break;
            }
            mFd = rpcServer->releaseServer();
            if (!mFd.fd.ok()) return AssertionFailure() << "releaseServer returns invalid fd";
+3 −0
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ enum class SocketType {
    UNIX_RAW,
    VSOCK,
    INET,
    TIPC,
};

static inline std::string PrintToString(SocketType socketType) {
@@ -106,6 +107,8 @@ static inline std::string PrintToString(SocketType socketType) {
            return "vm_socket";
        case SocketType::INET:
            return "inet_socket";
        case SocketType::TIPC:
            return "trusty_ipc";
        default:
            LOG_ALWAYS_FATAL("Unknown socket type");
            return "";
+4 −0
Original line number Diff line number Diff line
@@ -106,6 +106,10 @@ public:

    // Whether the test params support sending FDs in parcels.
    bool supportsFdTransport() const {
        if (socketType() == SocketType::TIPC) {
            // Trusty does not support file descriptors yet
            return false;
        }
        return clientVersion() >= 1 && serverVersion() >= 1 && rpcSecurity() != RpcSecurity::TLS &&
                (socketType() == SocketType::PRECONNECTED || socketType() == SocketType::UNIX ||
                 socketType() == SocketType::UNIX_BOOTSTRAP ||
Loading