test_service.cpp 5.4 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "test_service.h"
#include <unistd.h>
#include <fcntl.h>
#include "ipc_skeleton.h"
#include "ipc_debug.h"
#include "string_ex.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"

namespace OHOS {
using namespace OHOS::HiviewDFX;

static int Reverse(int x)
{
    int result = 0;
    int decimal = 10; // decimal value.

    while (x != 0) {
        result = result * decimal + x % decimal;
        x = x / decimal;
    }

    return result;
}

int TestService::Instantiate()
{
    ZLOGI(LABEL, "%{public}s call in", __func__);
    auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    if (saMgr == nullptr) {
        ZLOGE(LABEL, "%{public}s:fail to get Registry", __func__);
        return -ENODEV;
    }

    sptr<IRemoteObject> newInstance = new TestService();
#ifdef IPCSERVERTESTEXTRA
    int result = saMgr->AddSystemAbility(IPC_EXTRA_TEST_SERVICE, newInstance);
    ZLOGI(LABEL, "%{public}s: IPC_EXTRA_TEST_SERVICE result = %{public}d", __func__, result);
#else
    int result = saMgr->AddSystemAbility(IPC_TEST_SERVICE, newInstance);
    ZLOGI(LABEL, "%{public}s: IPC_TEST_SERVICE result = %{public}d", __func__, result);
#endif

    ZLOGI(LABEL, "TestService: strong = %d", newInstance->GetSptrRefCount());
    return result;
}

TestService::TestService() : testFd_(INVALID_FD)
{
}

TestService::~TestService()
{
    if (testFd_ != INVALID_FD) {
        close(testFd_);
    }
}

int TestService::TestSyncTransaction(int data, int &rep, int delayTime)
{
    rep = Reverse(data);

    if (delayTime > 0) {
Z
zhangxiujian 已提交
80
        sleep((uint32_t)delayTime);
M
mamingshuai 已提交
81 82 83 84 85 86 87 88 89 90 91
    }

    ZLOGE(LABEL, "TestServiceStub:read from client data = %{public}d", data);
    return ERR_NONE;
}

int TestService::TestAsyncTransaction(int data, int timeout)
{
    ZLOGE(LABEL, "TestServiceStub:read from client data = %{public}d", data);

    if (timeout > 0) {
Z
zhangxiujian 已提交
92
        sleep((uint32_t)timeout);
M
mamingshuai 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }

    return Reverse(data);
}

int TestService::TestAsyncCallbackTrans(int data, int &reply, int timeout)
{
    if (timeout > 0) {
        timeout = 0;
    }

    return Reverse(data);
}

int TestService::TestZtraceTransaction(std::string &send, std::string &receive, int len)
{
    receive = send;
    transform(receive.begin(), receive.end(), receive.begin(), ::tolower);
    return 0;
}
113

M
mamingshuai 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
int TestService::TestPingService(const std::u16string &serviceName)
{
    std::u16string localServiceName = GetObjectDescriptor();
    if (localServiceName.compare(serviceName) != 0) {
        ZLOGE(LABEL, "local name is ""%s, passing is %s",
            Str16ToStr8(localServiceName).c_str(), Str16ToStr8(serviceName).c_str());
        return -1;
    }

    return 0;
}

sptr<IFoo> TestService::TestGetFooService()
{
    return new FooStub();
}

int TestService::TestGetFileDescriptor()
{
    if (testFd_ != INVALID_FD) {
        close(testFd_);
    }

Z
zhou-liting125 已提交
137
    testFd_ = open("/data/test.txt",
M
mamingshuai 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);

    if (testFd_ == INVALID_FD) {
        ZLOGE(LABEL, "%s(%d):open failed.", __func__, __LINE__);
        return !INVALID_FD;
    }

    ssize_t writeLen = write(testFd_, "Sever write!\n", strlen("Sever write!\n"));
    if (writeLen < 0) {
        ZLOGE(LABEL, "%s(%d): server write file failed.", __func__, __LINE__);
        close(testFd_);
        return INVALID_FD;
    } else {
        ZLOGI(LABEL, "%s(%d): server write file success.", __func__, __LINE__);
    }

    return testFd_;
}

int TestService::TestStringTransaction(const std::string &data)
{
    return data.size();
}

void TestService::TestDumpService()
{
    // use for proxy only.
}

void TestService::TestAsyncDumpService()
{
    // use for proxy only.
}

int TestService::TestRawDataTransaction(int length, int &reply)
{
174 175
    (void)length;
    (void)reply;
M
mamingshuai 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188
    return 0;
}

int TestService::TestRawDataReply(int length)
{
    return 0;
}

int TestService::TestCallingUidPid()
{
    return 0;
}

X
Xi_Yuhao 已提交
189 190
int TestService::TestAccessTokenID(int32_t ftoken_expected)
{
191
    (void)ftoken_expected;
X
Xi_Yuhao 已提交
192 193 194
    return 0;
}

M
mamingshuai 已提交
195 196 197 198 199 200 201
int TestService::TestFlushAsyncCalls(int count, int length)
{
    return 0;
}

int TestService::TestMultipleProcesses(int data, int &rep, int delayTime)
{
202 203 204
    (void)data;
    (void)rep;
    (void)delayTime;
M
mamingshuai 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    return 0;
}

int TestService::Dump(int fd, const std::vector<std::u16string> &args)
{
    ssize_t writeCount = 0;
    if (fd > 0) {
        std::u16string argsParam = args.front();
        std::string context;
        context.append(Str16ToStr8(argsParam));
        context.append(1, '\r');
        writeCount = write(fd, context.data(), context.size());
    }

    return writeCount > 0 ? ERR_NONE : ERR_TRANSACTION_FAILED;
}

std::u16string TestService::TestAshmem(sptr<Ashmem> ashmem, int32_t contentSize)
{
224
    (void)contentSize;
M
mamingshuai 已提交
225 226 227 228 229
    return u"";
}

int TestService::TestNestingSend(int sendCode, int &replyCode)
{
230
    (void)sendCode;
M
mamingshuai 已提交
231 232 233
    return 0;
}
} // namespace OHOS