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) {
334fb368f762fa17d1456f153bc3d67d4de4c452472Martijn Coenen            case BINDER_LIB_TEST_CALL_BACK: {
335fb368f762fa17d1456f153bc3d67d4de4c452472Martijn Coenen                status_t status = data.readInt32(&m_result);
336fb368f762fa17d1456f153bc3d67d4de4c452472Martijn Coenen                if (status != NO_ERROR) {
337fb368f762fa17d1456f153bc3d67d4de4c452472Martijn Coenen                    m_result = status;
338fb368f762fa17d1456f153bc3d67d4de4c452472Martijn Coenen                }
33906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                triggerEvent();
34006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
341fb368f762fa17d1456f153bc3d67d4de4c452472Martijn Coenen            }
342336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang            case BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF: {
343336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                sp<IBinder> server;
344336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                int ret;
345336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                const uint8_t *buf = data.data();
346336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                size_t size = data.dataSize();
347336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                if (m_prev_end) {
348336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    /* 64-bit kernel needs at most 8 bytes to align buffer end */
349336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    EXPECT_LE((size_t)(buf - m_prev_end), (size_t)8);
350336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                } else {
351336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    EXPECT_TRUE(IsPageAligned((void *)buf));
352336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                }
353336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
354336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                m_prev_end = buf + size + data.objectsCount() * sizeof(binder_size_t);
355336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
356336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                if (size > 0) {
357336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    server = static_cast<BinderLibTestEnv *>(binder_env)->getServer();
358336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    ret = server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION,
359336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                                           data, reply);
360336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                    EXPECT_EQ(NO_ERROR, ret);
361336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                }
362336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang                return NO_ERROR;
363336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang            }
36406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            default:
36506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return UNKNOWN_TRANSACTION;
36606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
36706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
36806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
36906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        status_t m_result;
370336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        const uint8_t *m_prev_end;
37106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
37206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
37306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass TestDeathRecipient : public IBinder::DeathRecipient, public BinderLibTestEvent
37406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
37506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    private:
37606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual void binderDied(const wp<IBinder>& who) {
37706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            (void)who;
37806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            triggerEvent();
37906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        };
38006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
38106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
38206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, NopTransaction) {
38306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
38406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
38506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data, &reply);
38606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
38706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
38806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
38906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, SetError) {
39006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t testValue[] = { 0, -123, 123 };
39106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < ARRAY_SIZE(testValue); i++) {
39206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        status_t ret;
39306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        Parcel data, reply;
39406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeInt32(testValue[i]);
39506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = m_server->transact(BINDER_LIB_TEST_SET_ERROR_TRANSACTION, data, &reply);
39606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(testValue[i], ret);
39706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
39806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
39906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
40006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, GetId) {
40106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
40206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t id;
40306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
40406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_GET_ID_TRANSACTION, data, &reply);
40506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
40606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&id);
40706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
40806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(0, id);
40906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
41006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
41106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, PtrSize) {
41206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
41306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t ptrsize;
41406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
41506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> server = addServer();
41606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(server != NULL);
41706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = server->transact(BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION, data, &reply);
41806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
41906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&ptrsize);
42006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
42106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    RecordProperty("TestPtrSize", sizeof(void *));
42206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    RecordProperty("ServerPtrSize", sizeof(void *));
42306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
42406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
42506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, IndirectGetId2)
42606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
42706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
42806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t id;
42906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t count;
43006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
43106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t serverId[3];
43206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
43306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    data.writeInt32(ARRAY_SIZE(serverId));
43406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
43506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> server;
43606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle datai;
43706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
43806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        server = addServer(&serverId[i]);
43906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(server != NULL);
44006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeStrongBinder(server);
44106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
44206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.appendTo(&data);
44306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
44406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
44506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
44606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
44706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
44806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&id);
44906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
45006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(0, id);
45106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
45206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&count);
45306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
4546d5fa9459f3b084ab802f32bb8a490ac0f25f2a4Arve Hjønnevåg    EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
45506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
45606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < (size_t)count; i++) {
45706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle replyi(&reply);
45806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_TRUE(replyi.isValid());
45906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = replyi.readInt32(&id);
46006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
46106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(serverId[i], id);
46206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
46306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
46406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
46506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(reply.dataSize(), reply.dataPosition());
46606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
46706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
46806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, IndirectGetId3)
46906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
47006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
47106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t id;
47206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t count;
47306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
47406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int32_t serverId[3];
47506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
47606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    data.writeInt32(ARRAY_SIZE(serverId));
47706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < ARRAY_SIZE(serverId); i++) {
47806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> server;
47906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle datai;
48006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle datai2;
48106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
48206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        server = addServer(&serverId[i]);
48306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(server != NULL);
48406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeStrongBinder(server);
48506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        data.writeInt32(BINDER_LIB_TEST_INDIRECT_TRANSACTION);
48606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
48706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.writeInt32(1);
48806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.writeStrongBinder(m_server);
48906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.writeInt32(BINDER_LIB_TEST_GET_ID_TRANSACTION);
49006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai2.appendTo(&datai);
49106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
49206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        datai.appendTo(&data);
49306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
49406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
49506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
49606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
49706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
49806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&id);
49906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
50006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(0, id);
50106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
50206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = reply.readInt32(&count);
50306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(NO_ERROR, ret);
5046d5fa9459f3b084ab802f32bb8a490ac0f25f2a4Arve Hjønnevåg    EXPECT_EQ(ARRAY_SIZE(serverId), (size_t)count);
50506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
50606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (size_t i = 0; i < (size_t)count; i++) {
50706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        int32_t counti;
50806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
50906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle replyi(&reply);
51006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_TRUE(replyi.isValid());
51106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = replyi.readInt32(&id);
51206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
51306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(serverId[i], id);
51406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
51506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = replyi.readInt32(&counti);
51606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_EQ(NO_ERROR, ret);
51706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(1, counti);
51806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
51906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestBundle replyi2(&replyi);
52006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_TRUE(replyi2.isValid());
52106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = replyi2.readInt32(&id);
52206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
52306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(0, id);
52406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(replyi2.dataSize(), replyi2.dataPosition());
52506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
52606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(replyi.dataSize(), replyi.dataPosition());
52706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
52806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
52906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(reply.dataSize(), reply.dataPosition());
53006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
53106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
53206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, CallBack)
53306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
53406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
53506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
53606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
53706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    data.writeStrongBinder(callBack);
53806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply, TF_ONE_WAY);
53906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
54006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = callBack->waitEvent(5);
54106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
54206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = callBack->getResult();
54306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
54406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
54506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
54606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, AddServer)
54706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
54806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> server = addServer();
54906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(server != NULL);
55006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
55106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
55206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, DeathNotificationNoRefs)
55306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
55406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
55506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
55606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
55706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
55806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
55906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> binder = addServer();
56006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(binder != NULL);
56106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = binder->linkToDeath(testDeathRecipient);
56206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
56306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
56406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    IPCThreadState::self()->flushCommands();
56506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = testDeathRecipient->waitEvent(5);
56606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
56706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#if 0 /* Is there an unlink api that does not require a strong reference? */
56806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = binder->unlinkToDeath(testDeathRecipient);
56906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
57006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#endif
57106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
57206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
57306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, DeathNotificationWeakRef)
57406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
57506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
57606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    wp<IBinder> wbinder;
57706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
57806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
57906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
58006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
58106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> binder = addServer();
58206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(binder != NULL);
58306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = binder->linkToDeath(testDeathRecipient);
58406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
58506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        wbinder = binder;
58606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
58706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    IPCThreadState::self()->flushCommands();
58806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = testDeathRecipient->waitEvent(5);
58906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
59006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#if 0 /* Is there an unlink api that does not require a strong reference? */
59106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = binder->unlinkToDeath(testDeathRecipient);
59206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
59306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews#endif
59406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
59506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
59606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, DeathNotificationStrongRef)
59706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
59806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
59906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> sbinder;
60006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
60106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
60206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
60306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
60406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> binder = addServer();
60506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ASSERT_TRUE(binder != NULL);
60606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = binder->linkToDeath(testDeathRecipient);
60706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
60806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sbinder = binder;
60906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
61006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
61106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        Parcel data, reply;
61206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = sbinder->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
61306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(0, ret);
61406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
61506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    IPCThreadState::self()->flushCommands();
61606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = testDeathRecipient->waitEvent(5);
61706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
61806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = sbinder->unlinkToDeath(testDeathRecipient);
61906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(DEAD_OBJECT, ret);
62006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
62106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
62206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, DeathNotificationMultiple)
62306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
62406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
62506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    const int clientcount = 2;
62606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> target;
62706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> linkedclient[clientcount];
62806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<BinderLibTestCallBack> callBack[clientcount];
62906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> passiveclient[clientcount];
63006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
63106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    target = addServer();
63206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(target != NULL);
63306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (int i = 0; i < clientcount; i++) {
63406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
63506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            Parcel data, reply;
63606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
63706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            linkedclient[i] = addServer();
63806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ASSERT_TRUE(linkedclient[i] != NULL);
63906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            callBack[i] = new BinderLibTestCallBack();
64006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeStrongBinder(target);
64106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeStrongBinder(callBack[i]);
64206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = linkedclient[i]->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
64306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_EQ(NO_ERROR, ret);
64406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
64506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
64606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            Parcel data, reply;
64706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
64806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            passiveclient[i] = addServer();
64906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ASSERT_TRUE(passiveclient[i] != NULL);
65006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeStrongBinder(target);
65106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = passiveclient[i]->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply, TF_ONE_WAY);
65206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            EXPECT_EQ(NO_ERROR, ret);
65306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
65406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
65506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
65606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        Parcel data, reply;
65706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
65806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(0, ret);
65906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
66006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
66106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    for (int i = 0; i < clientcount; i++) {
66206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = callBack[i]->waitEvent(5);
66306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
66406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = callBack[i]->getResult();
66506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
66606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
66706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
66806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
669f7100e492ff6301dc5f0250e90715e646c96774eMartijn CoenenTEST_F(BinderLibTest, DeathNotificationThread)
670f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen{
671f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    status_t ret;
672f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    sp<BinderLibTestCallBack> callback;
673f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    sp<IBinder> target = addServer();
674f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ASSERT_TRUE(target != NULL);
675f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    sp<IBinder> client = addServer();
676f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ASSERT_TRUE(client != NULL);
677f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
678f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
679f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
680f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ret = target->linkToDeath(testDeathRecipient);
681f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    EXPECT_EQ(NO_ERROR, ret);
682f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
683f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    {
684f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        Parcel data, reply;
685f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        ret = target->transact(BINDER_LIB_TEST_EXIT_TRANSACTION, data, &reply, TF_ONE_WAY);
686f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        EXPECT_EQ(0, ret);
687f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    }
688f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
689f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    /* Make sure it's dead */
690f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    testDeathRecipient->waitEvent(5);
691f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
692f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    /* Now, pass the ref to another process and ask that process to
693f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * call linkToDeath() on it, and wait for a response. This tests
694f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * two things:
695f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * 1) You still get death notifications when calling linkToDeath()
696f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *    on a ref that is already dead when it was passed to you.
697f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * 2) That death notifications are not directly pushed to the thread
698f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *    registering them, but to the threadpool (proc workqueue) instead.
699f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *
700f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * 2) is tested because the thread handling BINDER_LIB_TEST_DEATH_TRANSACTION
701f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * is blocked on a condition variable waiting for the death notification to be
702f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * called; therefore, that thread is not available for handling proc work.
703f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * So, if the death notification was pushed to the thread workqueue, the callback
704f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * would never be called, and the test would timeout and fail.
705f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *
706f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * Note that we can't do this part of the test from this thread itself, because
707f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * the binder driver would only push death notifications to the thread if
708f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * it is a looper thread, which this thread is not.
709f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     *
710f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     * See b/23525545 for details.
711f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen     */
712f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    {
713f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        Parcel data, reply;
714f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
715f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        callback = new BinderLibTestCallBack();
716f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        data.writeStrongBinder(target);
717f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        data.writeStrongBinder(callback);
718f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        ret = client->transact(BINDER_LIB_TEST_LINK_DEATH_TRANSACTION, data, &reply, TF_ONE_WAY);
719f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen        EXPECT_EQ(NO_ERROR, ret);
720f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    }
721f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
722f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ret = callback->waitEvent(5);
723f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    EXPECT_EQ(NO_ERROR, ret);
724f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    ret = callback->getResult();
725f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen    EXPECT_EQ(NO_ERROR, ret);
726f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen}
727f7100e492ff6301dc5f0250e90715e646c96774eMartijn Coenen
72806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, PassFile) {
72906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int ret;
73006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int pipefd[2];
73106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    uint8_t buf[1] = { 0 };
73206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    uint8_t write_value = 123;
73306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
73406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = pipe2(pipefd, O_NONBLOCK);
73506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_EQ(0, ret);
73606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
73706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
73806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        Parcel data, reply;
73906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        uint8_t writebuf[1] = { write_value };
74006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
74106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = data.writeFileDescriptor(pipefd[1], true);
74206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
74306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
74406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = data.writeInt32(sizeof(writebuf));
74506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
74606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
74706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = data.write(writebuf, sizeof(writebuf));
74806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
74906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
75006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ret = m_server->transact(BINDER_LIB_TEST_WRITE_FILE_TRANSACTION, data, &reply);
75106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        EXPECT_EQ(NO_ERROR, ret);
75206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
75306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
75406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = read(pipefd[0], buf, sizeof(buf));
7556d5fa9459f3b084ab802f32bb8a490ac0f25f2a4Arve Hjønnevåg    EXPECT_EQ(sizeof(buf), (size_t)ret);
75606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(write_value, buf[0]);
75706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
75806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    waitForReadData(pipefd[0], 5000); /* wait for other proccess to close pipe */
75906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
76006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = read(pipefd[0], buf, sizeof(buf));
76106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(0, ret);
76206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
76306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    close(pipefd[0]);
76406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
76506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
76606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, PromoteLocal) {
76706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> strong = new BBinder();
76806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    wp<IBinder> weak = strong;
76906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> strong_from_weak = weak.promote();
77006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_TRUE(strong != NULL);
77106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(strong, strong_from_weak);
77206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    strong = NULL;
77306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    strong_from_weak = NULL;
77406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    strong_from_weak = weak.promote();
77506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_TRUE(strong_from_weak == NULL);
77606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
77706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
77806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley AndrewsTEST_F(BinderLibTest, PromoteRemote) {
77906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int ret;
78006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    Parcel data, reply;
78106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> strong = new BBinder();
78206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IBinder> server = addServer();
78306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
78406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(server != NULL);
78506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ASSERT_TRUE(strong != NULL);
78606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
78706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = data.writeWeakBinder(strong);
78806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_EQ(NO_ERROR, ret);
78906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
79006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ret = server->transact(BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION, data, &reply);
79106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    EXPECT_GE(ret, 0);
79206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
79306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
7947060431a6f0eb395bdc32171aecc51c25fbe7dacArve HjønnevågTEST_F(BinderLibTest, CheckHandleZeroBinderHighBitsZeroCookie) {
7957060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    status_t ret;
7967060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    Parcel data, reply;
7977060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg
7987060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    ret = m_server->transact(BINDER_LIB_TEST_GET_SELF_TRANSACTION, data, &reply);
7997060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    EXPECT_EQ(NO_ERROR, ret);
8007060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg
8017060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    const flat_binder_object *fb = reply.readObject(false);
8027060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg    ASSERT_TRUE(fb != NULL);
803ad6503cf91e1ef0e3c1595e143ab9e11f0da5530Hsin-Yi Chen    EXPECT_EQ(BINDER_TYPE_HANDLE, fb->hdr.type);
804ad6503cf91e1ef0e3c1595e143ab9e11f0da5530Hsin-Yi Chen    EXPECT_EQ(m_server, ProcessState::self()->getStrongProxyForHandle(fb->handle));
805ad6503cf91e1ef0e3c1595e143ab9e11f0da5530Hsin-Yi Chen    EXPECT_EQ((binder_uintptr_t)0, fb->cookie);
806ad6503cf91e1ef0e3c1595e143ab9e11f0da5530Hsin-Yi Chen    EXPECT_EQ((uint64_t)0, (uint64_t)fb->binder >> 32);
8077060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg}
8087060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg
80952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'BrienTEST_F(BinderLibTest, FreedBinder) {
81052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    status_t ret;
81152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien
81252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    sp<IBinder> server = addServer();
81352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    ASSERT_TRUE(server != NULL);
81452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien
81552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    __u32 freedHandle;
81652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    wp<IBinder> keepFreedBinder;
81752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    {
81852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        Parcel data, reply;
81952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        data.writeBool(false); /* request weak reference */
82052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        ret = server->transact(BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION, data, &reply);
82152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        ASSERT_EQ(NO_ERROR, ret);
82252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        struct flat_binder_object *freed = (struct flat_binder_object *)(reply.data());
82352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        freedHandle = freed->handle;
82452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        /* Add a weak ref to the freed binder so the driver does not
82552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * delete its reference to it - otherwise the transaction
82652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * fails regardless of whether the driver is fixed.
82752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         */
82852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        keepFreedBinder = reply.readWeakBinder();
82952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    }
83052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    {
83152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        Parcel data, reply;
83252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        data.writeStrongBinder(server);
83352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        /* Replace original handle with handle to the freed binder */
83452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        struct flat_binder_object *strong = (struct flat_binder_object *)(data.data());
83552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        __u32 oldHandle = strong->handle;
83652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        strong->handle = freedHandle;
83752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        ret = server->transact(BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION, data, &reply);
83852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        /* Returns DEAD_OBJECT (-32) if target crashes and
83952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * FAILED_TRANSACTION if the driver rejects the invalid
84052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * object.
84152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         */
84252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        EXPECT_EQ((status_t)FAILED_TRANSACTION, ret);
84352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        /* Restore original handle so parcel destructor does not use
84452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         * the wrong handle.
84552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien         */
84652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien        strong->handle = oldHandle;
84752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien    }
84852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien}
84952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien
850336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry YangTEST_F(BinderLibTest, CheckNoHeaderMappedInUser) {
851336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    status_t ret;
852336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    Parcel data, reply;
853336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
854336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    for (int i = 0; i < 2; i++) {
855336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        BinderLibTestBundle datai;
856336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        datai.appendFrom(&data, 0, data.dataSize());
857336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
858336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        data.freeData();
859336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        data.writeInt32(1);
860336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        data.writeStrongBinder(callBack);
861336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        data.writeInt32(BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF);
862336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
863336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang        datai.appendTo(&data);
864336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    }
865336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    ret = m_server->transact(BINDER_LIB_TEST_INDIRECT_TRANSACTION, data, &reply);
866336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang    EXPECT_EQ(NO_ERROR, ret);
867336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang}
868336cdd3437a94e8dfcb897959da5d6eeb6379473Sherry Yang
86945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn CoenenTEST_F(BinderLibTest, OnewayQueueing)
87045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen{
87145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    status_t ret;
87245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    Parcel data, data2;
87345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
87445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    sp<IBinder> pollServer = addPollServer();
87545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
87645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
87745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    data.writeStrongBinder(callBack);
87845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    data.writeInt32(500000); // delay in us before calling back
87945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
88045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    sp<BinderLibTestCallBack> callBack2 = new BinderLibTestCallBack();
88145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    data2.writeStrongBinder(callBack2);
88245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    data2.writeInt32(0); // delay in us
88345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
88445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data, NULL, TF_ONE_WAY);
88545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
88645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
88745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    // The delay ensures that this second transaction will end up on the async_todo list
88845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    // (for a single-threaded server)
88945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = pollServer->transact(BINDER_LIB_TEST_DELAYED_CALL_BACK, data2, NULL, TF_ONE_WAY);
89045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
89145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
89245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    // The server will ensure that the two transactions are handled in the expected order;
89345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    // If the ordering is not as expected, an error will be returned through the callbacks.
89445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = callBack->waitEvent(2);
89545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
89645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = callBack->getResult();
89745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
89845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
89945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = callBack2->waitEvent(2);
90045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
90145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    ret = callBack2->getResult();
90245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    EXPECT_EQ(NO_ERROR, ret);
90345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen}
90445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
90506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsclass BinderLibTestService : public BBinder
90606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
90706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    public:
90806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        BinderLibTestService(int32_t id)
90906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            : m_id(id)
91006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            , m_nextServerId(id + 1)
91106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            , m_serverStartRequested(false)
91245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            , m_callback(NULL)
91306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
91406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_mutex_init(&m_serverWaitMutex, NULL);
91506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            pthread_cond_init(&m_serverWaitCond, NULL);
91606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
91706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        ~BinderLibTestService()
91806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        {
91906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            exit(EXIT_SUCCESS);
92006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
92145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
92245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        void processPendingCall() {
92345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            if (m_callback != NULL) {
92445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                Parcel data;
92545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                data.writeInt32(NO_ERROR);
92645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                m_callback->transact(BINDER_LIB_TEST_CALL_BACK, data, nullptr, TF_ONE_WAY);
92745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                m_callback = NULL;
92845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            }
92945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
93045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
93106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        virtual status_t onTransact(uint32_t code,
93206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                                    const Parcel& data, Parcel* reply,
93306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                                    uint32_t flags = 0) {
93406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            //printf("%s: code %d\n", __func__, code);
93506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            (void)flags;
93606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
93706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            if (getuid() != (uid_t)IPCThreadState::self()->getCallingUid()) {
93806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return PERMISSION_DENIED;
93906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
94006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            switch (code) {
94106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_REGISTER_SERVER: {
94206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int32_t id;
94306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> binder;
94406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                id = data.readInt32();
94506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                binder = data.readStrongBinder();
94606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (binder == NULL) {
94706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
94806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
94906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
95006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (m_id != 0)
95106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return INVALID_OPERATION;
95206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
95306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_mutex_lock(&m_serverWaitMutex);
95406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (m_serverStartRequested) {
95506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    m_serverStartRequested = false;
95606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    m_serverStarted = binder;
95706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    pthread_cond_signal(&m_serverWaitCond);
95806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
95906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_mutex_unlock(&m_serverWaitMutex);
96006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
96106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
96245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            case BINDER_LIB_TEST_ADD_POLL_SERVER:
96306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_ADD_SERVER: {
96406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int ret;
96506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                uint8_t buf[1] = { 0 };
96606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int serverid;
96706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
96806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (m_id != 0) {
96906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return INVALID_OPERATION;
97006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
97106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_mutex_lock(&m_serverWaitMutex);
97206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (m_serverStartRequested) {
97306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    ret = -EBUSY;
97406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                } else {
97506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    serverid = m_nextServerId++;
97606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    m_serverStartRequested = true;
97745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    bool usePoll = code == BINDER_LIB_TEST_ADD_POLL_SERVER;
97806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
97906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    pthread_mutex_unlock(&m_serverWaitMutex);
98045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    ret = start_server_process(serverid, usePoll);
98106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    pthread_mutex_lock(&m_serverWaitMutex);
98206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
98306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret > 0) {
98406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    if (m_serverStartRequested) {
98506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        struct timespec ts;
98606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        clock_gettime(CLOCK_REALTIME, &ts);
98706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        ts.tv_sec += 5;
98806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        ret = pthread_cond_timedwait(&m_serverWaitCond, &m_serverWaitMutex, &ts);
98906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    }
99006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    if (m_serverStartRequested) {
99106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        m_serverStartRequested = false;
99206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        ret = -ETIMEDOUT;
99306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    } else {
99406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        reply->writeStrongBinder(m_serverStarted);
99506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        reply->writeInt32(serverid);
99606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        m_serverStarted = NULL;
99706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        ret = NO_ERROR;
99806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    }
99906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                } else if (ret >= 0) {
100006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    m_serverStartRequested = false;
100106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    ret = UNKNOWN_ERROR;
100206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
100306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                pthread_mutex_unlock(&m_serverWaitMutex);
100406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return ret;
100506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
100606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_NOP_TRANSACTION:
100706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
100845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            case BINDER_LIB_TEST_DELAYED_CALL_BACK: {
100945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                // Note: this transaction is only designed for use with a
101045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                // poll() server. See comments around epoll_wait().
101145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                if (m_callback != NULL) {
101245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    // A callback was already pending; this means that
101345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    // we received a second call while still processing
101445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    // the first one. Fail the test.
101545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    sp<IBinder> callback = data.readStrongBinder();
101645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    Parcel data2;
101745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    data2.writeInt32(UNKNOWN_ERROR);
101845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
101945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, NULL, TF_ONE_WAY);
102045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                } else {
102145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    m_callback = data.readStrongBinder();
102245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    int32_t delayUs = data.readInt32();
102345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    /*
102445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * It's necessary that we sleep here, so the next
102545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * transaction the caller makes will be queued to
102645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * the async queue.
102745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     */
102845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    usleep(delayUs);
102945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
103045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                    /*
103145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * Now when we return, libbinder will tell the kernel
103245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * we are done with this transaction, and the kernel
103345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * can move the queued transaction to either the
103445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * thread todo worklist (for kernels without the fix),
103545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * or the proc todo worklist. In case of the former,
103645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * the next outbound call will pick up the pending
103745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * transaction, which leads to undesired reentrant
103845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     * behavior. This is caught in the if() branch above.
103945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     */
104045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                }
104145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
104245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                return NO_ERROR;
104345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            }
104406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_NOP_CALL_BACK: {
104506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                Parcel data2, reply2;
104606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> binder;
104706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                binder = data.readStrongBinder();
104806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (binder == NULL) {
104906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
105006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
1051fb368f762fa17d1456f153bc3d67d4de4c452472Martijn Coenen                data2.writeInt32(NO_ERROR);
105206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
105306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
105406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
10557060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg            case BINDER_LIB_TEST_GET_SELF_TRANSACTION:
10567060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg                reply->writeStrongBinder(this);
10577060431a6f0eb395bdc32171aecc51c25fbe7dacArve Hjønnevåg                return NO_ERROR;
105806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_GET_ID_TRANSACTION:
105906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->writeInt32(m_id);
106006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
106106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_INDIRECT_TRANSACTION: {
106206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int32_t count;
106306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                uint32_t indirect_code;
106406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> binder;
106506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
106606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                count = data.readInt32();
106706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->writeInt32(m_id);
106806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->writeInt32(count);
106906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                for (int i = 0; i < count; i++) {
107006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    binder = data.readStrongBinder();
107106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    if (binder == NULL) {
107206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        return BAD_VALUE;
107306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    }
107406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    indirect_code = data.readInt32();
107506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    BinderLibTestBundle data2(&data);
107606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    if (!data2.isValid()) {
107706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                        return BAD_VALUE;
107806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    }
107906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    BinderLibTestBundle reply2;
108006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    binder->transact(indirect_code, data2, &reply2);
108106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    reply2.appendTo(reply);
108206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
108306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
108406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
108506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_SET_ERROR_TRANSACTION:
108606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->setError(data.readInt32());
108706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
108806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION:
108906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                reply->writeInt32(sizeof(void *));
109006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
109106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_GET_STATUS_TRANSACTION:
109206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
109306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION:
109406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                m_strongRef = data.readStrongBinder();
109506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
109606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_LINK_DEATH_TRANSACTION: {
109706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int ret;
109806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                Parcel data2, reply2;
109906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<TestDeathRecipient> testDeathRecipient = new TestDeathRecipient();
110006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> target;
110106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> callback;
110206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
110306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                target = data.readStrongBinder();
110406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (target == NULL) {
110506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
110606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
110706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                callback = data.readStrongBinder();
110806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (callback == NULL) {
110906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
111006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
111106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = target->linkToDeath(testDeathRecipient);
111206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret == NO_ERROR)
111306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    ret = testDeathRecipient->waitEvent(5);
111406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                data2.writeInt32(ret);
111506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                callback->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
111606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
111706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
111806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_WRITE_FILE_TRANSACTION: {
111906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int ret;
112006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int32_t size;
112106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                const void *buf;
112206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int fd;
112306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
112406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                fd = data.readFileDescriptor();
112506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (fd < 0) {
112606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
112706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
112806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = data.readInt32(&size);
112906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret != NO_ERROR) {
113006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return ret;
113106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
113206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                buf = data.readInplace(size);
113306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (buf == NULL) {
113406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
113506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
113606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = write(fd, buf, size);
113706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret != size)
113806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return UNKNOWN_ERROR;
113906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
114006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
114106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_PROMOTE_WEAK_REF_TRANSACTION: {
114206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                int ret;
114306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                wp<IBinder> weak;
114406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> strong;
114506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                Parcel data2, reply2;
114606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IServiceManager> sm = defaultServiceManager();
114706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                sp<IBinder> server = sm->getService(binderLibTestServiceName);
114806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
114906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                weak = data.readWeakBinder();
115006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (weak == NULL) {
115106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    return BAD_VALUE;
115206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
115306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                strong = weak.promote();
115406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
115506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                ret = server->transact(BINDER_LIB_TEST_NOP_TRANSACTION, data2, &reply2);
115606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (ret != NO_ERROR)
115706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    exit(EXIT_FAILURE);
115806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
115906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                if (strong == NULL) {
116006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    reply->setError(1);
116106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                }
116206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
116306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            }
116406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION:
116506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                alarm(10);
116606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return NO_ERROR;
116706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            case BINDER_LIB_TEST_EXIT_TRANSACTION:
116806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                while (wait(NULL) != -1 || errno != ECHILD)
116906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                    ;
117006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                exit(EXIT_SUCCESS);
117152be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien            case BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION: {
117252be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                bool strongRef = data.readBool();
117352be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                sp<IBinder> binder = new BBinder();
117452be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                if (strongRef) {
117552be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                    reply->writeStrongBinder(binder);
117652be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                } else {
117752be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                    reply->writeWeakBinder(binder);
117852be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                }
117952be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien                return NO_ERROR;
118052be2c9f52676b09a86e0eb7ba8fcd27af5a23caConnor O'Brien            }
118106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            default:
118206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews                return UNKNOWN_TRANSACTION;
118306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            };
118406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
118506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    private:
118606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        int32_t m_id;
118706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        int32_t m_nextServerId;
118806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        pthread_mutex_t m_serverWaitMutex;
118906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        pthread_cond_t m_serverWaitCond;
119006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        bool m_serverStartRequested;
119106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> m_serverStarted;
119206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<IBinder> m_strongRef;
119345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        bool m_callbackPending;
119445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        sp<IBinder> m_callback;
119506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews};
119606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
119745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenenint run_server(int index, int readypipefd, bool usePoll)
119806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews{
119987c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    binderLibTestServiceName += String16(binderserversuffix);
120087c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien
120106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    status_t ret;
120206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    sp<IServiceManager> sm = defaultServiceManager();
120345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    BinderLibTestService* testServicePtr;
120406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    {
120506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        sp<BinderLibTestService> testService = new BinderLibTestService(index);
120645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        /*
120745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen         * We need this below, but can't hold a sp<> because it prevents the
120845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen         * node from being cleaned up automatically. It's safe in this case
120945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen         * because of how the tests are written.
121045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen         */
121145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        testServicePtr = testService.get();
121245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
121306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        if (index == 0) {
121406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = sm->addService(binderLibTestServiceName, testService);
121506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        } else {
121606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            sp<IBinder> server = sm->getService(binderLibTestServiceName);
121706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            Parcel data, reply;
121806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeInt32(index);
121906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            data.writeStrongBinder(testService);
122006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
122106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews            ret = server->transact(BINDER_LIB_TEST_REGISTER_SERVER, data, &reply);
122206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        }
122306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
122406b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    write(readypipefd, &ret, sizeof(ret));
122506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    close(readypipefd);
122606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    //printf("%s: ret %d\n", __func__, ret);
122706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    if (ret)
122806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        return 1;
122906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    //printf("%s: joinThreadPool\n", __func__);
123045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    if (usePoll) {
123145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        int fd;
123245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        struct epoll_event ev;
123345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        int epoll_fd;
123445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        IPCThreadState::self()->setupPolling(&fd);
123545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        if (fd < 0) {
123645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            return 1;
123745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
123845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        IPCThreadState::self()->flushCommands(); // flush BC_ENTER_LOOPER
123945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
124045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        epoll_fd = epoll_create1(0);
124145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        if (epoll_fd == -1) {
124245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            return 1;
124345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
124445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
124545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        ev.events = EPOLLIN;
124645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
124745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen            return 1;
124845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
124945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen
125045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        while (1) {
125145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             /*
125245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * We simulate a single-threaded process using the binder poll
125345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * interface; besides handling binder commands, it can also
125445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * issue outgoing transactions, by storing a callback in
125545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * m_callback and setting m_callbackPending.
125645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              *
125745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              * processPendingCall() will then issue that transaction.
125845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen              */
125945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             struct epoll_event events[1];
126045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             int numEvents = epoll_wait(epoll_fd, events, 1, 1000);
126145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             if (numEvents < 0) {
126245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 if (errno == EINTR) {
126345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                     continue;
126445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 }
126545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 return 1;
126645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             }
126745b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             if (numEvents > 0) {
126845b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 IPCThreadState::self()->handlePolledCommands();
126945b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 IPCThreadState::self()->flushCommands(); // flush BC_FREE_BUFFER
127045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen                 testServicePtr->processPendingCall();
127145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen             }
127245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        }
127345b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    } else {
127445b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        ProcessState::self()->startThreadPool();
127545b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        IPCThreadState::self()->joinThreadPool();
127645b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    }
127706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    //printf("%s: joinThreadPool returned\n", __func__);
127806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    return 1; /* joinThreadPool should not return */
127906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
128006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
128106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrewsint main(int argc, char **argv) {
128206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    int ret;
128306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
128487c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    if (argc == 4 && !strcmp(argv[1], "--servername")) {
128506b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        binderservername = argv[2];
128606b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    } else {
128706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews        binderservername = argv[0];
128806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
128906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
129045b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen    if (argc == 6 && !strcmp(argv[1], binderserverarg)) {
129145b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        binderserversuffix = argv[5];
129245b07b49ba37ef4843087a2e6c35c8776d1a3c25Martijn Coenen        return run_server(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]) == 1);
129306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    }
129487c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    binderserversuffix = new char[16];
129587c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    snprintf(binderserversuffix, 16, "%d", getpid());
129687c03cf9e26f066fe7ff0e6a54b88cae6f1f29fdConnor O'Brien    binderLibTestServiceName += String16(binderserversuffix);
129706b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
129806b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ::testing::InitGoogleTest(&argc, argv);
129906b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    binder_env = AddGlobalTestEnvironment(new BinderLibTestEnv());
130006b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    ProcessState::self()->startThreadPool();
130106b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews    return RUN_ALL_TESTS();
130206b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews}
130306b01adcb882b4dd736b34bbcccf7a45f43d96a6Riley Andrews
1304