binderLibTest.cpp revision 45b07b49ba37ef4843087a2e6c35c8776d1a3c25
106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews/*
206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * Copyright (C) 2014 The Android Open Source Project
306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews *
406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * Licensed under the Apache License, Version 2.0 (the "License");
506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * you may not use this file except in compliance with the License.
606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * You may obtain a copy of the License at
706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews *
806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews *      http://www.apache.org/licenses/LICENSE-2.0
906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews *
1006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * Unless required by applicable law or agreed to in writing, software
1106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * distributed under the License is distributed on an "AS IS" BASIS,
1206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * See the License for the specific language governing permissions and
1406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews * limitations under the License.
1506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews */
1606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
1706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <errno.h>
1806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <fcntl.h>
1906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <poll.h>
2006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <pthread.h>
2106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <stdio.h>
2206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <stdlib.h>
2306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
2406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <gtest/gtest.h>
2506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
2606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <binder/Binder.h>
2706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <binder/IBinder.h>
2806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <binder/IPCThreadState.h>
2906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#include <binder/IServiceManager.h>
3006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
3145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen#include <sys/epoll.h>
3245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
3306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
3406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
3506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsusing namespace android;
3606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
37336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yangstatic ::testing::AssertionResult IsPageAligned(void *buf) {
38336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    if (((unsigned long)buf & ((unsigned long)PAGE_SIZE - 1)) == 0)
39336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        return ::testing::AssertionSuccess();
40336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    else
41336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        return ::testing::AssertionFailure() << buf << " is not page aligned";
42336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang}
43336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
4406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsstatic testing::Environment* binder_env;
4506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsstatic char *binderservername;
4687c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brienstatic char *binderserversuffix;
4706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsstatic char binderserverarg[] = "--binderserver";
4806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
4906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsstatic String16 binderLibTestServiceName = String16("test.binderLib");
5006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
5106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsenum BinderLibTestTranscationCode {
5206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
5306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_REGISTER_SERVER,
5406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_ADD_SERVER,
5545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    BINDER_LIB_TEST_ADD_POLL_SERVER,
5606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_CALL_BACK,
57336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
5845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    BINDER_LIB_TEST_DELAYED_CALL_BACK,
5906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_NOP_CALL_BACK,
607060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    BINDER_LIB_TEST_GET_SELF_TRANSACTION,
6106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_GET_ID_TRANSACTION,
6206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_INDIRECT_TRANSACTION,
6306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
6406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
6506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
6606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
6706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
6806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION,
6906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_EXIT_TRANSACTION,
7006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
7106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
7252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
7306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
7406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
7545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenenpid_t start_server_process(int arg2, bool usePoll = false)
7606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
7706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int ret;
7806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    pid_t pid;
7906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t status;
8006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int pipefd[2];
8106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    char stri[16];
8206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    char strpipefd1[16];
8345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    char usepoll[2];
8406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    char *childargv[] = {
8506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        binderservername,
8606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        binderserverarg,
8706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        stri,
8806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        strpipefd1,
8945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        usepoll,
9087c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien        binderserversuffix,
9106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        NULL
9206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    };
9306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
9406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = pipe(pipefd);
9506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    if (ret < 0)
9606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        return ret;
9706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
9806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    snprintf(stri, sizeof(stri), "%d", arg2);
9906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    snprintf(strpipefd1, sizeof(strpipefd1), "%d", pipefd[1]);
10045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    snprintf(usepoll, sizeof(usepoll), "%d", usePoll ? 1 : 0);
10106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
10206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    pid = fork();
10306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    if (pid == -1)
10406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        return pid;
10506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    if (pid == 0) {
10606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        close(pipefd[0]);
10706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        execv(binderservername, childargv);
10806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        status = -errno;
10906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        write(pipefd[1], &status, sizeof(status));
11006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        fprintf(stderr, "execv failed, %s\n", strerror(errno));
11106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        _exit(EXIT_FAILURE);
11206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
11306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    close(pipefd[1]);
11406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = read(pipefd[0], &status, sizeof(status));
11506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    //printf("pipe read returned %d, status %d\n", ret, status);
11606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    close(pipefd[0]);
11706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    if (ret == sizeof(status)) {
11806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = status;
11906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    } else {
12006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        kill(pid, SIGKILL);
12106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        if (ret >= 0) {
12206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = NO_INIT;
12306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
12406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
12506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    if (ret < 0) {
12606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        wait(NULL);
12706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        return ret;
12806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
12906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    return pid;
13006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
13106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
13206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass BinderLibTestEnv : public ::testing::Environment {
13306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    public:
13406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestEnv() {}
13506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> getServer(void) {
13606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            return m_server;
13706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
13806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
13906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    private:
14006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual void SetUp() {
14106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            m_serverpid = start_server_process(0);
14206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            //printf("m_serverpid %d\n", m_serverpid);
14306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ASSERT_GT(m_serverpid, 0);
14406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
14506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            sp<IServiceManager> sm = defaultServiceManager();
14606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            //printf("%s: pid %d, get service\n", __func__, m_pid);
14706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            m_server = sm->getService(binderLibTestServiceName);
14806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ASSERT_TRUE(m_server != NULL);
14906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            //printf("%s: pid %d, get service done\n", __func__, m_pid);
15006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
15106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual void TearDown() {
15206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            status_t ret;
15306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            Parcel data, reply;
15406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            int exitStatus;
15506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pid_t pid;
15606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
15706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            //printf("%s: pid %d\n", __func__, m_pid);
15806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (m_server != NULL) {
15906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = m_server->transact(BINDER_LIB_TEST_GET_STATUS_TRANSACTION, data, &reply);
16006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                EXPECT_EQ(0, ret);
16106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = m_server->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
16206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                EXPECT_EQ(0, ret);
16306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
16406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (m_serverpid > 0) {
16506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                //printf("wait for %d\n", m_pids[i]);
16606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pid = wait(&exitStatus);
16706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                EXPECT_EQ(m_serverpid, pid);
16806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                EXPECT_TRUE(WIFEXITED(exitStatus));
16906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                EXPECT_EQ(0, WEXITSTATUS(exitStatus));
17006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
17106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
17206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
17306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        pid_t m_serverpid;
17406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> m_server;
17506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
17606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
17706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass BinderLibTest : public ::testing::Test {
17806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    public:
17906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual void SetUp() {
18006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            m_server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
18106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
18206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual void TearDown() {
18306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
18406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    protected:
18545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        sp<IBinder> addServerEtc(int32_t *idPtr, int code)
18606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
18706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            int ret;
18806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            int32_t id;
18906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            Parcel data, reply;
19006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            sp<IBinder> binder;
19106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
19245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            ret = m_server->transact(code, data, &reply);
19306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_EQ(NO_ERROR, ret);
19406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
19506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_FALSE(binder != NULL);
19606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            binder = reply.readStrongBinder();
19706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_TRUE(binder != NULL);
19806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = reply.readInt32(&id);
19906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_EQ(NO_ERROR, ret);
20006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (idPtr)
20106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                *idPtr = id;
20206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            return binder;
20306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
20445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
20545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        sp<IBinder> addServer(int32_t *idPtr = NULL)
20645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        {
20745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_SERVER);
20845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
20945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
21045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        sp<IBinder> addPollServer(int32_t *idPtr = NULL)
21145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        {
21245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            return addServerEtc(idPtr, BINDER_LIB_TEST_ADD_POLL_SERVER);
21345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
21445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
21506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        void waitForReadData(int fd, int timeout_ms) {
21606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            int ret;
21706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pollfd pfd = pollfd();
21806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
21906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pfd.fd = fd;
22006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pfd.events = POLLIN;
22106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = poll(&pfd, 1, timeout_ms);
22206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_EQ(1, ret);
22306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
22406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
22506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> m_server;
22606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
22706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
22806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass BinderLibTestBundle : public Parcel
22906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
23006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    public:
23106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle(void) {}
23206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle(const Parcel *source) : m_isValid(false) {
23306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            int32_t mark;
23406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            int32_t bundleLen;
23506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            size_t pos;
23606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
23706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (source->readInt32(&mark))
23806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return;
23906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (mark != MARK_START)
24006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return;
24106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (source->readInt32(&bundleLen))
24206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return;
24306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pos = source->dataPosition();
24406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (Parcel::appendFrom(source, pos, bundleLen))
24506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return;
24606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            source->setDataPosition(pos + bundleLen);
24706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (source->readInt32(&mark))
24806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return;
24906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (mark != MARK_END)
25006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return;
25106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            m_isValid = true;
25206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            setDataPosition(0);
25306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
25406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        void appendTo(Parcel *dest) {
25506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            dest->writeInt32(MARK_START);
25606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            dest->writeInt32(dataSize());
25706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            dest->appendFrom(this, 0, dataSize());
25806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            dest->writeInt32(MARK_END);
25906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        };
26006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        bool isValid(void) {
26106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            return m_isValid;
26206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
26306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    private:
26406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        enum {
26506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            MARK_START  = B_PACK_CHARS('B','T','B','S'),
26606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            MARK_END    = B_PACK_CHARS('B','T','B','E'),
26706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        };
26806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        bool m_isValid;
26906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
27006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
27106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass BinderLibTestEvent
27206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
27306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    public:
27406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestEvent(void)
27506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            : m_eventTriggered(false)
27606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
27706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_mutex_init(&m_waitMutex, NULL);
27806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_cond_init(&m_waitCond, NULL);
27906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
28006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        int waitEvent(int timeout_s)
28106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
28206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            int ret;
28306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_mutex_lock(&m_waitMutex);
28406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (!m_eventTriggered) {
28506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                struct timespec ts;
28606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                clock_gettime(CLOCK_REALTIME, &ts);
28706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ts.tv_sec += timeout_s;
28806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_cond_timedwait(&m_waitCond, &m_waitMutex, &ts);
28906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
29006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = m_eventTriggered ? NO_ERROR : TIMED_OUT;
29106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_mutex_unlock(&m_waitMutex);
29206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            return ret;
29306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
294f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        pthread_t getTriggeringThread()
295f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        {
296f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen            return m_triggeringThread;
297f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        }
29806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    protected:
29906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        void triggerEvent(void) {
30006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_mutex_lock(&m_waitMutex);
30106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_cond_signal(&m_waitCond);
30206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            m_eventTriggered = true;
303f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen            m_triggeringThread = pthread_self();
30406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_mutex_unlock(&m_waitMutex);
30506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        };
30606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    private:
30706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        pthread_mutex_t m_waitMutex;
30806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        pthread_cond_t m_waitCond;
30906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        bool m_eventTriggered;
310f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        pthread_t m_triggeringThread;
31106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
31206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
31306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass BinderLibTestCallBack : public BBinder, public BinderLibTestEvent
31406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
31506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    public:
31606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestCallBack()
31706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            : m_result(NOT_ENOUGH_DATA)
318336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang            , m_prev_end(NULL)
31906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
32006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
32106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        status_t getResult(void)
32206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
32306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            return m_result;
32406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
32506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
32606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    private:
32706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual status_t onTransact(uint32_t code,
32806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                                    const Parcel& data, Parcel* reply,
32906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                                    uint32_t flags = 0)
33006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
33106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            (void)reply;
33206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            (void)flags;
33306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            switch(code) {
33406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_CALL_BACK:
33506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                m_result = data.readInt32();
33606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                triggerEvent();
33706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
338336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang            case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
339336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                sp<IBinder> server;
340336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                int ret;
341336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                const uint8_t *buf = data.data();
342336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                size_t size = data.dataSize();
343336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                if (m_prev_end) {
344336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    /* 64-bit kernel needs at most 8 bytes to align buffer end */
345336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
346336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                } else {
347336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    EXPECT_TRUE(IsPageAligned((void *)buf));
348336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                }
349336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
350336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
351336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
352336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                if (size > 0) {
353336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
354336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
355336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                                           data, reply);
356336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    EXPECT_EQ(NO_ERROR, ret);
357336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                }
358336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                return NO_ERROR;
359336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang            }
36006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            default:
36106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return UNKNOWN_TRANSACTION;
36206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
36306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
36406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
36506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        status_t m_result;
366336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        const uint8_t *m_prev_end;
36706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
36806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
36906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
37006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
37106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    private:
37206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual void binderDied(const wp<IBinder>& who) {
37306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            (void)who;
37406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            triggerEvent();
37506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        };
37606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
37706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
37806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, NopTransaction) {
37906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
38006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
38106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
38206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
38306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
38406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
38506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, SetError) {
38606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t testValue[] = { 0, -123, 123 };
38706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
38806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        status_t ret;
38906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        Parcel data, reply;
39006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeInt32(testValue[i]);
39106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
39206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(testValue[i], ret);
39306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
39406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
39506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
39606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, GetId) {
39706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
39806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t id;
39906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
40006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
40106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
40206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&id);
40306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
40406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(0, id);
40506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
40606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
40706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, PtrSize) {
40806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
40906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t ptrsize;
41006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
41106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> server = addServer();
41206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(server != NULL);
41306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
41406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
41506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&ptrsize);
41606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
41706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    RecordProperty("TestPtrSize", sizeof(void *));
41806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    RecordProperty("ServerPtrSize", sizeof(void *));
41906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
42006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
42106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, IndirectGetId2)
42206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
42306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
42406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t id;
42506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t count;
42606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
42706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t serverId[3];
42806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
42906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    data.writeInt32(ARRAY_SIZE(serverId));
43006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
43106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> server;
43206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle datai;
43306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
43406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        server = addServer(&serverId[i]);
43506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(server != NULL);
43606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeStrongBinder(server);
43706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
43806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.appendTo(&data);
43906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
44006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
44106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
44206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
44306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
44406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&id);
44506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
44606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(0, id);
44706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
44806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&count);
44906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
4506d5fa9459f3b084ab802f32bb8a490ac0f25f2a4Arve Hjønnevåg    EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
45106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
45206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < (size_t)count; i++) {
45306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle replyi(&reply);
45406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_TRUE(replyi.isValid());
45506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = replyi.readInt32(&id);
45606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
45706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(serverId[i], id);
45806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
45906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
46006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
46106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(reply.dataSize(), reply.dataPosition());
46206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
46306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
46406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, IndirectGetId3)
46506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
46606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
46706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t id;
46806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t count;
46906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
47006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t serverId[3];
47106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
47206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    data.writeInt32(ARRAY_SIZE(serverId));
47306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
47406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> server;
47506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle datai;
47606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle datai2;
47706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
47806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        server = addServer(&serverId[i]);
47906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(server != NULL);
48006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeStrongBinder(server);
48106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
48206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
48306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.writeInt32(1);
48406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.writeStrongBinder(m_server);
48506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
48606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai2.appendTo(&datai);
48706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
48806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.appendTo(&data);
48906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
49006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
49106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
49206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
49306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
49406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&id);
49506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
49606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(0, id);
49706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
49806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&count);
49906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
5006d5fa9459f3b084ab802f32bb8a490ac0f25f2a4Arve Hjønnevåg    EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
50106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
50206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < (size_t)count; i++) {
50306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        int32_t counti;
50406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
50506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle replyi(&reply);
50606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_TRUE(replyi.isValid());
50706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = replyi.readInt32(&id);
50806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
50906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(serverId[i], id);
51006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
51106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = replyi.readInt32(&counti);
51206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_EQ(NO_ERROR, ret);
51306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(1, counti);
51406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
51506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle replyi2(&replyi);
51606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_TRUE(replyi2.isValid());
51706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = replyi2.readInt32(&id);
51806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
51906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(0, id);
52006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
52106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
52206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
52306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
52406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
52506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(reply.dataSize(), reply.dataPosition());
52606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
52706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
52806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, CallBack)
52906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
53006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
53106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
53206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
53306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    data.writeStrongBinder(callBack);
53406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
53506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
53606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = callBack->waitEvent(5);
53706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
53806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = callBack->getResult();
53906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
54006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
54106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
54206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, AddServer)
54306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
54406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> server = addServer();
54506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(server != NULL);
54606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
54706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
54806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, DeathNotificationNoRefs)
54906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
55006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
55106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
55206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
55306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
55406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
55506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> binder = addServer();
55606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(binder != NULL);
55706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = binder->linkToDeath(testDeathRecipient);
55806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
55906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
56006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    IPCThreadState::self()->flushCommands();
56106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = testDeathRecipient->waitEvent(5);
56206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
56306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#if 0 /* Is there an unlink api that does not require a strong reference? */
56406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = binder->unlinkToDeath(testDeathRecipient);
56506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
56606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#endif
56706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
56806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
56906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, DeathNotificationWeakRef)
57006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
57106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
57206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    wp<IBinder> wbinder;
57306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
57406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
57506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
57606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
57706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> binder = addServer();
57806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(binder != NULL);
57906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = binder->linkToDeath(testDeathRecipient);
58006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
58106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        wbinder = binder;
58206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
58306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    IPCThreadState::self()->flushCommands();
58406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = testDeathRecipient->waitEvent(5);
58506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
58606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#if 0 /* Is there an unlink api that does not require a strong reference? */
58706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = binder->unlinkToDeath(testDeathRecipient);
58806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
58906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#endif
59006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
59106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
59206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, DeathNotificationStrongRef)
59306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
59406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
59506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> sbinder;
59606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
59706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
59806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
59906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
60006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> binder = addServer();
60106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(binder != NULL);
60206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = binder->linkToDeath(testDeathRecipient);
60306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
60406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sbinder = binder;
60506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
60606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
60706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        Parcel data, reply;
60806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
60906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(0, ret);
61006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
61106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    IPCThreadState::self()->flushCommands();
61206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = testDeathRecipient->waitEvent(5);
61306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
61406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = sbinder->unlinkToDeath(testDeathRecipient);
61506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(DEAD_OBJECT, ret);
61606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
61706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
61806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, DeathNotificationMultiple)
61906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
62006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
62106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    const int clientcount = 2;
62206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> target;
62306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> linkedclient[clientcount];
62406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<BinderLibTestCallBack> callBack[clientcount];
62506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> passiveclient[clientcount];
62606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
62706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    target = addServer();
62806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(target != NULL);
62906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (int i = 0; i < clientcount; i++) {
63006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
63106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            Parcel data, reply;
63206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
63306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            linkedclient[i] = addServer();
63406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ASSERT_TRUE(linkedclient[i] != NULL);
63506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            callBack[i] = new BinderLibTestCallBack();
63606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeStrongBinder(target);
63706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeStrongBinder(callBack[i]);
63806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
63906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_EQ(NO_ERROR, ret);
64006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
64106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
64206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            Parcel data, reply;
64306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
64406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            passiveclient[i] = addServer();
64506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ASSERT_TRUE(passiveclient[i] != NULL);
64606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeStrongBinder(target);
64706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
64806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_EQ(NO_ERROR, ret);
64906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
65006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
65106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
65206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        Parcel data, reply;
65306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
65406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(0, ret);
65506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
65606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
65706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (int i = 0; i < clientcount; i++) {
65806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = callBack[i]->waitEvent(5);
65906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
66006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = callBack[i]->getResult();
66106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
66206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
66306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
66406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
665f7100e492ff6301dc5f0250e90715e646c96774eMartijn CoenenTEST_F(BinderLibTest, DeathNotificationThread)
666f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen{
667f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    status_t ret;
668f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    sp<BinderLibTestCallBack> callback;
669f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    sp<IBinder> target = addServer();
670f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ASSERT_TRUE(target != NULL);
671f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    sp<IBinder> client = addServer();
672f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ASSERT_TRUE(client != NULL);
673f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
674f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
675f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
676f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ret = target->linkToDeath(testDeathRecipient);
677f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    EXPECT_EQ(NO_ERROR, ret);
678f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
679f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    {
680f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        Parcel data, reply;
681f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
682f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        EXPECT_EQ(0, ret);
683f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    }
684f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
685f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    /* Make sure it's dead */
686f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    testDeathRecipient->waitEvent(5);
687f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
688f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    /* Now, pass the ref to another process and ask that process to
689f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * call linkToDeath() on it, and wait for a response. This tests
690f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * two things:
691f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * 1) You still get death notifications when calling linkToDeath()
692f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *    on a ref that is already dead when it was passed to you.
693f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * 2) That death notifications are not directly pushed to the thread
694f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *    registering them, but to the threadpool (proc workqueue) instead.
695f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *
696f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
697f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * is blocked on a condition variable waiting for the death notification to be
698f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * called; therefore, that thread is not available for handling proc work.
699f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * So, if the death notification was pushed to the thread workqueue, the callback
700f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * would never be called, and the test would timeout and fail.
701f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *
702f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * Note that we can't do this part of the test from this thread itself, because
703f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * the binder driver would only push death notifications to the thread if
704f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * it is a looper thread, which this thread is not.
705f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *
706f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * See b/23525545 for details.
707f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     */
708f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    {
709f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        Parcel data, reply;
710f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
711f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        callback = new BinderLibTestCallBack();
712f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        data.writeStrongBinder(target);
713f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        data.writeStrongBinder(callback);
714f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
715f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        EXPECT_EQ(NO_ERROR, ret);
716f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    }
717f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
718f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ret = callback->waitEvent(5);
719f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    EXPECT_EQ(NO_ERROR, ret);
720f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ret = callback->getResult();
721f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    EXPECT_EQ(NO_ERROR, ret);
722f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen}
723f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
72406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, PassFile) {
72506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int ret;
72606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int pipefd[2];
72706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    uint8_t buf[1] = { 0 };
72806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    uint8_t write_value = 123;
72906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
73006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = pipe2(pipefd, O_NONBLOCK);
73106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(0, ret);
73206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
73306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
73406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        Parcel data, reply;
73506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        uint8_t writebuf[1] = { write_value };
73606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
73706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = data.writeFileDescriptor(pipefd[1], true);
73806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
73906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
74006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = data.writeInt32(sizeof(writebuf));
74106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
74206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
74306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = data.write(writebuf, sizeof(writebuf));
74406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
74506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
74606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
74706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
74806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
74906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
75006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = read(pipefd[0], buf, sizeof(buf));
7516d5fa9459f3b084ab802f32bb8a490ac0f25f2a4Arve Hjønnevåg    EXPECT_EQ(sizeof(buf), (size_t)ret);
75206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(write_value, buf[0]);
75306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
75406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
75506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
75606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = read(pipefd[0], buf, sizeof(buf));
75706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(0, ret);
75806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
75906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    close(pipefd[0]);
76006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
76106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
76206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, PromoteLocal) {
76306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> strong = new BBinder();
76406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    wp<IBinder> weak = strong;
76506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> strong_from_weak = weak.promote();
76606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_TRUE(strong != NULL);
76706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(strong, strong_from_weak);
76806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    strong = NULL;
76906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    strong_from_weak = NULL;
77006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    strong_from_weak = weak.promote();
77106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_TRUE(strong_from_weak == NULL);
77206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
77306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
77406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, PromoteRemote) {
77506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int ret;
77606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
77706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> strong = new BBinder();
77806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> server = addServer();
77906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
78006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(server != NULL);
78106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(strong != NULL);
78206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
78306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = data.writeWeakBinder(strong);
78406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
78506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
78606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply);
78706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_GE(ret, 0);
78806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
78906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
7907060431a6f0eb395bdc32171aecc51c25fbe7dacArve HjønnevågTEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
7917060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    status_t ret;
7927060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    Parcel data, reply;
7937060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg
7947060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
7957060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    EXPECT_EQ(NO_ERROR, ret);
7967060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg
7977060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    const flat_binder_object *fb = reply.readObject(false);
7987060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    ASSERT_TRUE(fb != NULL);
7997060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    EXPECT_EQ(fb->hdr.type, BINDER_TYPE_HANDLE);
8007060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    EXPECT_EQ(ProcessState::self()->getStrongProxyForHandle(fb->handle), m_server);
8017060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    EXPECT_EQ(fb->cookie, (binder_uintptr_t)0);
8027060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    EXPECT_EQ(fb->binder >> 32, (binder_uintptr_t)0);
8037060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg}
8047060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg
80552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'BrienTEST_F(BinderLibTest, FreedBinder) {
80652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    status_t ret;
80752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien
80852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    sp<IBinder> server = addServer();
80952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    ASSERT_TRUE(server != NULL);
81052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien
81152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    __u32 freedHandle;
81252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    wp<IBinder> keepFreedBinder;
81352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    {
81452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        Parcel data, reply;
81552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        data.writeBool(false); /* request weak reference */
81652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
81752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        ASSERT_EQ(NO_ERROR, ret);
81852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
81952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        freedHandle = freed->handle;
82052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        /* Add a weak ref to the freed binder so the driver does not
82152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * delete its reference to it - otherwise the transaction
82252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * fails regardless of whether the driver is fixed.
82352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         */
82452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        keepFreedBinder = reply.readWeakBinder();
82552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    }
82652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    {
82752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        Parcel data, reply;
82852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        data.writeStrongBinder(server);
82952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        /* Replace original handle with handle to the freed binder */
83052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
83152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        __u32 oldHandle = strong->handle;
83252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        strong->handle = freedHandle;
83352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
83452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        /* Returns DEAD_OBJECT (-32) if target crashes and
83552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * FAILED_TRANSACTION if the driver rejects the invalid
83652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * object.
83752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         */
83852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
83952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        /* Restore original handle so parcel destructor does not use
84052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * the wrong handle.
84152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         */
84252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        strong->handle = oldHandle;
84352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    }
84452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien}
84552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien
846336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry YangTEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
847336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    status_t ret;
848336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    Parcel data, reply;
849336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
850336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    for (int i = 0; i < 2; i++) {
851336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        BinderLibTestBundle datai;
852336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        datai.appendFrom(&data, 0, data.dataSize());
853336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
854336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        data.freeData();
855336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        data.writeInt32(1);
856336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        data.writeStrongBinder(callBack);
857336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
858336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
859336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        datai.appendTo(&data);
860336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    }
861336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
862336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    EXPECT_EQ(NO_ERROR, ret);
863336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang}
864336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
86545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn CoenenTEST_F(BinderLibTest, OnewayQueueing)
86645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen{
86745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    status_t ret;
86845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    Parcel data, data2;
86945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
87045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    sp<IBinder> pollServer = addPollServer();
87145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
87245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
87345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    data.writeStrongBinder(callBack);
87445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    data.writeInt32(500000); // delay in us before calling back
87545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
87645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
87745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    data2.writeStrongBinder(callBack2);
87845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    data2.writeInt32(0); // delay in us
87945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
88045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, NULL, TF_ONE_WAY);
88145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
88245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
88345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    // The delay ensures that this second transaction will end up on the async_todo list
88445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    // (for a single-threaded server)
88545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, NULL, TF_ONE_WAY);
88645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
88745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
88845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    // The server will ensure that the two transactions are handled in the expected order;
88945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    // If the ordering is not as expected, an error will be returned through the callbacks.
89045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = callBack->waitEvent(2);
89145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
89245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = callBack->getResult();
89345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
89445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
89545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = callBack2->waitEvent(2);
89645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
89745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = callBack2->getResult();
89845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
89945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen}
90045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
90106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass BinderLibTestService : public BBinder
90206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
90306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    public:
90406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestService(int32_t id)
90506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            : m_id(id)
90606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            , m_nextServerId(id + 1)
90706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            , m_serverStartRequested(false)
90845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            , m_callback(NULL)
90906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
91006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_mutex_init(&m_serverWaitMutex, NULL);
91106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_cond_init(&m_serverWaitCond, NULL);
91206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
91306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ~BinderLibTestService()
91406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
91506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            exit(EXIT_SUCCESS);
91606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
91745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
91845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        void processPendingCall() {
91945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            if (m_callback != NULL) {
92045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                Parcel data;
92145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                data.writeInt32(NO_ERROR);
92245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
92345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                m_callback = NULL;
92445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            }
92545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
92645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
92706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual status_t onTransact(uint32_t code,
92806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                                    const Parcel& data, Parcel* reply,
92906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                                    uint32_t flags = 0) {
93006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            //printf("%s: code %d\n", __func__, code);
93106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            (void)flags;
93206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
93306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
93406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return PERMISSION_DENIED;
93506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
93606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            switch (code) {
93706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_REGISTER_SERVER: {
93806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int32_t id;
93906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> binder;
94006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                id = data.readInt32();
94106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                binder = data.readStrongBinder();
94206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (binder == NULL) {
94306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
94406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
94506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
94606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (m_id != 0)
94706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return INVALID_OPERATION;
94806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
94906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_mutex_lock(&m_serverWaitMutex);
95006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (m_serverStartRequested) {
95106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    m_serverStartRequested = false;
95206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    m_serverStarted = binder;
95306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    pthread_cond_signal(&m_serverWaitCond);
95406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
95506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_mutex_unlock(&m_serverWaitMutex);
95606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
95706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
95845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            case BINDER_LIB_TEST_ADD_POLL_SERVER:
95906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_ADD_SERVER: {
96006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int ret;
96106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                uint8_t buf[1] = { 0 };
96206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int serverid;
96306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
96406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (m_id != 0) {
96506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return INVALID_OPERATION;
96606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
96706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_mutex_lock(&m_serverWaitMutex);
96806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (m_serverStartRequested) {
96906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    ret = -EBUSY;
97006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                } else {
97106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    serverid = m_nextServerId++;
97206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    m_serverStartRequested = true;
97345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
97406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
97506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    pthread_mutex_unlock(&m_serverWaitMutex);
97645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    ret = start_server_process(serverid, usePoll);
97706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    pthread_mutex_lock(&m_serverWaitMutex);
97806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
97906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret > 0) {
98006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    if (m_serverStartRequested) {
98106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        struct timespec ts;
98206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        clock_gettime(CLOCK_REALTIME, &ts);
98306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        ts.tv_sec += 5;
98406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
98506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    }
98606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    if (m_serverStartRequested) {
98706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        m_serverStartRequested = false;
98806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        ret = -ETIMEDOUT;
98906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    } else {
99006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        reply->writeStrongBinder(m_serverStarted);
99106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        reply->writeInt32(serverid);
99206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        m_serverStarted = NULL;
99306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        ret = NO_ERROR;
99406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    }
99506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                } else if (ret >= 0) {
99606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    m_serverStartRequested = false;
99706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    ret = UNKNOWN_ERROR;
99806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
99906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_mutex_unlock(&m_serverWaitMutex);
100006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return ret;
100106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
100206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_NOP_TRANSACTION:
100306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
100445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
100545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                // Note: this transaction is only designed for use with a
100645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                // poll() server. See comments around epoll_wait().
100745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                if (m_callback != NULL) {
100845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    // A callback was already pending; this means that
100945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    // we received a second call while still processing
101045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    // the first one. Fail the test.
101145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    sp<IBinder> callback = data.readStrongBinder();
101245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    Parcel data2;
101345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    data2.writeInt32(UNKNOWN_ERROR);
101445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
101545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, NULL, TF_ONE_WAY);
101645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                } else {
101745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    m_callback = data.readStrongBinder();
101845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    int32_t delayUs = data.readInt32();
101945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    /*
102045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * It's necessary that we sleep here, so the next
102145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * transaction the caller makes will be queued to
102245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * the async queue.
102345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     */
102445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    usleep(delayUs);
102545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
102645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    /*
102745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * Now when we return, libbinder will tell the kernel
102845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * we are done with this transaction, and the kernel
102945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * can move the queued transaction to either the
103045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * thread todo worklist (for kernels without the fix),
103145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * or the proc todo worklist. In case of the former,
103245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * the next outbound call will pick up the pending
103345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * transaction, which leads to undesired reentrant
103445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * behavior. This is caught in the if() branch above.
103545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     */
103645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                }
103745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
103845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                return NO_ERROR;
103945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            }
104006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_NOP_CALL_BACK: {
104106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                Parcel data2, reply2;
104206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> binder;
104306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                binder = data.readStrongBinder();
104406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (binder == NULL) {
104506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
104606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
104706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply2.writeInt32(NO_ERROR);
104806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
104906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
105006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
10517060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg            case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
10527060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg                reply->writeStrongBinder(this);
10537060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg                return NO_ERROR;
105406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_GET_ID_TRANSACTION:
105506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->writeInt32(m_id);
105606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
105706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
105806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int32_t count;
105906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                uint32_t indirect_code;
106006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> binder;
106106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
106206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                count = data.readInt32();
106306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->writeInt32(m_id);
106406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->writeInt32(count);
106506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                for (int i = 0; i < count; i++) {
106606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    binder = data.readStrongBinder();
106706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    if (binder == NULL) {
106806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        return BAD_VALUE;
106906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    }
107006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    indirect_code = data.readInt32();
107106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    BinderLibTestBundle data2(&data);
107206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    if (!data2.isValid()) {
107306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        return BAD_VALUE;
107406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    }
107506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    BinderLibTestBundle reply2;
107606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    binder->transact(indirect_code, data2, &reply2);
107706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    reply2.appendTo(reply);
107806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
107906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
108006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
108106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
108206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->setError(data.readInt32());
108306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
108406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
108506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->writeInt32(sizeof(void *));
108606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
108706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
108806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
108906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
109006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                m_strongRef = data.readStrongBinder();
109106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
109206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
109306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int ret;
109406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                Parcel data2, reply2;
109506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
109606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> target;
109706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> callback;
109806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
109906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                target = data.readStrongBinder();
110006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (target == NULL) {
110106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
110206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
110306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                callback = data.readStrongBinder();
110406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (callback == NULL) {
110506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
110606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
110706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = target->linkToDeath(testDeathRecipient);
110806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret == NO_ERROR)
110906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    ret = testDeathRecipient->waitEvent(5);
111006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                data2.writeInt32(ret);
111106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
111206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
111306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
111406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
111506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int ret;
111606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int32_t size;
111706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                const void *buf;
111806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int fd;
111906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
112006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                fd = data.readFileDescriptor();
112106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (fd < 0) {
112206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
112306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
112406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = data.readInt32(&size);
112506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret != NO_ERROR) {
112606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return ret;
112706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
112806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                buf = data.readInplace(size);
112906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (buf == NULL) {
113006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
113106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
113206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = write(fd, buf, size);
113306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret != size)
113406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return UNKNOWN_ERROR;
113506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
113606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
113706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: {
113806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int ret;
113906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                wp<IBinder> weak;
114006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> strong;
114106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                Parcel data2, reply2;
114206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IServiceManager> sm = defaultServiceManager();
114306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> server = sm->getService(binderLibTestServiceName);
114406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
114506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                weak = data.readWeakBinder();
114606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (weak == NULL) {
114706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
114806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
114906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                strong = weak.promote();
115006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
115106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2);
115206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret != NO_ERROR)
115306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    exit(EXIT_FAILURE);
115406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
115506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (strong == NULL) {
115606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    reply->setError(1);
115706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
115806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
115906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
116006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
116106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                alarm(10);
116206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
116306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_EXIT_TRANSACTION:
116406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                while (wait(NULL) != -1 || errno != ECHILD)
116506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    ;
116606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                exit(EXIT_SUCCESS);
116752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien            case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
116852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                bool strongRef = data.readBool();
116952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                sp<IBinder> binder = new BBinder();
117052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                if (strongRef) {
117152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                    reply->writeStrongBinder(binder);
117252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                } else {
117352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                    reply->writeWeakBinder(binder);
117452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                }
117552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                return NO_ERROR;
117652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien            }
117706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            default:
117806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return UNKNOWN_TRANSACTION;
117906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            };
118006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
118106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    private:
118206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        int32_t m_id;
118306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        int32_t m_nextServerId;
118406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        pthread_mutex_t m_serverWaitMutex;
118506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        pthread_cond_t m_serverWaitCond;
118606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        bool m_serverStartRequested;
118706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> m_serverStarted;
118806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> m_strongRef;
118945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        bool m_callbackPending;
119045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        sp<IBinder> m_callback;
119106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
119206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
119345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenenint run_server(int index, int readypipefd, bool usePoll)
119406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
119587c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    binderLibTestServiceName += String16(binderserversuffix);
119687c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien
119706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
119806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IServiceManager> sm = defaultServiceManager();
119945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    BinderLibTestService* testServicePtr;
120006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
120106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<BinderLibTestService> testService = new BinderLibTestService(index);
120245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        /*
120345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen         * We need this below, but can't hold a sp<> because it prevents the
120445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen         * node from being cleaned up automatically. It's safe in this case
120545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen         * because of how the tests are written.
120645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen         */
120745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        testServicePtr = testService.get();
120845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
120906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        if (index == 0) {
121006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = sm->addService(binderLibTestServiceName, testService);
121106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        } else {
121206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            sp<IBinder> server = sm->getService(binderLibTestServiceName);
121306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            Parcel data, reply;
121406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeInt32(index);
121506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeStrongBinder(testService);
121606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
121706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
121806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
121906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
122006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    write(readypipefd, &ret, sizeof(ret));
122106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    close(readypipefd);
122206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    //printf("%s: ret %d\n", __func__, ret);
122306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    if (ret)
122406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        return 1;
122506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    //printf("%s: joinThreadPool\n", __func__);
122645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    if (usePoll) {
122745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        int fd;
122845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        struct epoll_event ev;
122945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        int epoll_fd;
123045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        IPCThreadState::self()->setupPolling(&fd);
123145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        if (fd < 0) {
123245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            return 1;
123345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
123445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
123545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
123645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        epoll_fd = epoll_create1(0);
123745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        if (epoll_fd == -1) {
123845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            return 1;
123945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
124045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
124145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        ev.events = EPOLLIN;
124245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
124345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            return 1;
124445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
124545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
124645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        while (1) {
124745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             /*
124845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * We simulate a single-threaded process using the binder poll
124945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * interface; besides handling binder commands, it can also
125045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * issue outgoing transactions, by storing a callback in
125145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * m_callback and setting m_callbackPending.
125245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              *
125345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * processPendingCall() will then issue that transaction.
125445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              */
125545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             struct epoll_event events[1];
125645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
125745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             if (numEvents < 0) {
125845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 if (errno == EINTR) {
125945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     continue;
126045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 }
126145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 return 1;
126245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             }
126345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             if (numEvents > 0) {
126445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 IPCThreadState::self()->handlePolledCommands();
126545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
126645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 testServicePtr->processPendingCall();
126745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             }
126845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
126945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    } else {
127045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        ProcessState::self()->startThreadPool();
127145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        IPCThreadState::self()->joinThreadPool();
127245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    }
127306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    //printf("%s: joinThreadPool returned\n", __func__);
127406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    return 1; /* joinThreadPool should not return */
127506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
127606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
127706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsint main(int argc, char **argv) {
127806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int ret;
127906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
128087c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    if (argc == 4 && !strcmp(argv[1], "--servername")) {
128106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        binderservername = argv[2];
128206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    } else {
128306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        binderservername = argv[0];
128406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
128506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
128645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
128745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        binderserversuffix = argv[5];
128845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
128906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
129087c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    binderserversuffix = new char[16];
129187c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    snprintf(binderserversuffix, 16, "%d", getpid());
129287c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    binderLibTestServiceName += String16(binderserversuffix);
129306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
129406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ::testing::InitGoogleTest(&argc, argv);
129506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
129606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ProcessState::self()->startThreadPool();
129706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    return RUN_ALL_TESTS();
129806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
129906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
1300