watcher_proxy_unittest.cpp 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 <cstdio>
#include <cstring>
S
sun_fan 已提交
17 18
#include <gmock/gmock.h>
#include <gtest/gtest.h>
19

S
sun_fan 已提交
20
#include "if_system_ability_manager.h"
21
#include "init_unittest.h"
S
sun_fan 已提交
22 23
#include "iservice_registry.h"
#include "iwatcher.h"
24 25
#include "iwatcher_manager.h"
#include "message_parcel.h"
S
sun_fan 已提交
26 27
#include "param_message.h"
#include "param_request.h"
28 29 30
#include "param_utils.h"
#include "parcel.h"
#include "securec.h"
S
sun_fan 已提交
31
#include "system_ability_definition.h"
32 33
#include "watcher.h"
#include "watcher_manager.h"
S
sun_fan 已提交
34
#include "watcher_proxy.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "watcher_utils.h"

using namespace testing::ext;
using namespace std;
using namespace OHOS;
using namespace OHOS::init_param;

class TestWatcher final : public Watcher {
public:
    TestWatcher() {}
    ~TestWatcher() = default;

    void OnParamerterChange(const std::string &name, const std::string &value) override
    {
        printf("TestWatcher::OnParamerterChange name %s %s \n", name.c_str(), value.c_str());
    }
};

using WatcherManagerPtr = WatcherManager *;
4
411148299@qq.com 已提交
54
class WatcherProxyUnitTest : public ::testing::Test {
55
public:
4
411148299@qq.com 已提交
56 57
    WatcherProxyUnitTest() {}
    virtual ~WatcherProxyUnitTest() {}
58 59 60 61 62 63 64 65 66 67 68 69 70

    void SetUp() {}
    void TearDown() {}
    void TestBody() {}

    int TestAddWatcher(const std::string &keyPrefix, uint32_t &watcherId)
    {
        WatcherManagerPtr watcherManager = GetWatcherManager();
        WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to create manager");
        MessageParcel data;
        MessageParcel reply;
        MessageOption option;

X
xionglei6 已提交
71
        data.WriteInterfaceToken(IWatcherManager::GetDescriptor());
72
        data.WriteString(keyPrefix);
S
sun_fan 已提交
73
        sptr<IWatcher> watcher = new TestWatcher();
74 75 76 77 78
        bool ret = data.WriteRemoteObject(watcher->AsObject());
        WATCHER_CHECK(ret, return 0, "Can not get remote");
        watcherManager->OnRemoteRequest(IWatcherManager::ADD_WATCHER, data, reply, option);
        watcherId = reply.ReadUint32();
        EXPECT_NE(watcherId, 0);
4
411148299@qq.com 已提交
79
        EXPECT_EQ(watcherManager->GetWatcherGroup(1000) != NULL, 0); // 1000 test group id
S
sun_fan 已提交
80
        EXPECT_EQ(watcherManager->GetWatcherGroup("TestAddWatcher") != NULL, 0); // test key not exist
81 82 83 84 85 86 87 88 89 90
        return 0;
    }

    int TestDelWatcher(const std::string &keyPrefix, uint32_t &watcherId)
    {
        WatcherManagerPtr watcherManager = GetWatcherManager();
        WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to create manager");
        MessageParcel data;
        MessageParcel reply;
        MessageOption option;
X
xionglei6 已提交
91
        data.WriteInterfaceToken(IWatcherManager::GetDescriptor());
92 93 94 95 96 97 98 99
        data.WriteString(keyPrefix);
        data.WriteUint32(watcherId);
        watcherManager->OnRemoteRequest(IWatcherManager::DEL_WATCHER, data, reply, option);
        EXPECT_EQ(reply.ReadInt32(), 0);
        printf("TestDelWatcher %s watcherId %d %p \n", keyPrefix.c_str(), watcherId, watcherManager);
        return 0;
    }

S
sun_fan 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    int TestProcessWatcherMessage(const std::string &name, uint32_t watcherId)
    {
        WatcherManagerPtr watcherManager = GetWatcherManager();
        WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to create manager");
        const std::string value("test.value");
        uint32_t msgSize = sizeof(ParamMessage) + sizeof(ParamMsgContent) + value.size();
        msgSize = PARAM_ALIGN(msgSize); // align
        std::vector<char> buffer(msgSize, 0);
        ParamMessage *msg = (ParamMessage *)buffer.data();
        WATCHER_CHECK(msg != NULL, return -1, "Invalid msg");
        msg->type = MSG_NOTIFY_PARAM;
        msg->msgSize = msgSize;
        msg->id.watcherId = watcherId;
        int ret = memcpy_s(msg->key, sizeof(msg->key), name.c_str(), name.size());
        WATCHER_CHECK(ret == 0, return -1, "Failed to fill value");
        uint32_t offset = 0;
        ret = FillParamMsgContent(msg, &offset, PARAM_VALUE, value.c_str(), value.size());
        WATCHER_CHECK(ret == 0, return -1, "Failed to fill value");
        watcherManager->ProcessWatcherMessage(buffer, msgSize);
        return 0;
    }

    int TestWatchProxy(const std::string &name, const std::string &value)
    {
        sptr<ISystemAbilityManager> systemMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
        EXPECT_NE(systemMgr, nullptr);
        sptr<IRemoteObject> remoteObj = systemMgr->GetSystemAbility(PARAM_WATCHER_DISTRIBUTED_SERVICE_ID);
        EXPECT_NE(remoteObj, nullptr);
        WatcherProxy *watcher = new WatcherProxy(remoteObj);
        if (watcher != nullptr) {
            watcher->OnParamerterChange(name, value);
            delete watcher;
        }
        return 0;
    }

X
add ut  
xionglei6 已提交
136 137 138 139 140 141 142
    int TestWatchAgentDel(const std::string &keyPrefix)
    {
        WatcherManagerPtr watcherManager = GetWatcherManager();
        WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to create manager");
        MessageParcel data;
        MessageParcel reply;
        MessageOption option;
X
xionglei6 已提交
143 144

        data.WriteInterfaceToken(IWatcherManager::GetDescriptor());
X
add ut  
xionglei6 已提交
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 174 175 176 177 178 179 180 181 182 183 184 185 186
        data.WriteString(keyPrefix);
        sptr<IWatcher> watcher = new TestWatcher();
        bool ret = data.WriteRemoteObject(watcher->AsObject());
        WATCHER_CHECK(ret, return 0, "Can not get remote");
        watcherManager->OnRemoteRequest(IWatcherManager::ADD_WATCHER, data, reply, option);
        uint32_t watcherId = reply.ReadUint32();
        EXPECT_NE(watcherId, 0);
        if (watcherManager->GetDeathRecipient() != nullptr) {
            watcherManager->GetDeathRecipient()->OnRemoteDied(watcher->AsObject());
        }
        printf("TestWatchAgentDel %s success \n", keyPrefix.c_str());
        EXPECT_EQ(watcherManager->GetWatcher(watcherId) == nullptr, 1);
        return 0;
    }

    int TestInvalid(const std::string &keyPrefix)
    {
        WatcherManagerPtr watcherManager = GetWatcherManager();
        WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to create manager");
        MessageParcel data;
        MessageParcel reply;
        MessageOption option;
        data.WriteString(keyPrefix);
        sptr<IWatcher> watcher = new TestWatcher();
        bool ret = data.WriteRemoteObject(watcher->AsObject());
        WATCHER_CHECK(ret, return 0, "Can not get remote");
        watcherManager->OnRemoteRequest(IWatcherManager::ADD_WATCHER + 1, data, reply, option);

        if (watcherManager->GetDeathRecipient() != nullptr) {
            watcherManager->GetDeathRecipient()->OnRemoteDied(watcher->AsObject());
        }
        return 0;
    }

    int TestStop()
    {
        WatcherManagerPtr watcherManager = GetWatcherManager();
        WATCHER_CHECK(watcherManager != nullptr, return -1, "Failed to create manager");
        watcherManager->OnStop();
        return 0;
    }

187 188
    WatcherManagerPtr GetWatcherManager()
    {
X
add ut  
xionglei6 已提交
189 190 191 192
        static WatcherManagerPtr watcherManager = nullptr;
        if (watcherManager == nullptr) {
            watcherManager = new WatcherManager(0, true);
            if (watcherManager == nullptr) {
193 194
                return nullptr;
            }
X
add ut  
xionglei6 已提交
195
            watcherManager->OnStart();
196
        }
X
add ut  
xionglei6 已提交
197
        return watcherManager;
198 199 200
    }
};

4
411148299@qq.com 已提交
201
HWTEST_F(WatcherProxyUnitTest, TestAddWatcher, TestSize.Level0)
202
{
4
411148299@qq.com 已提交
203
    WatcherProxyUnitTest test;
204 205
    uint32_t watcherId = 0;
    test.TestAddWatcher("test.permission.watcher.test1", watcherId);
S
sun_fan 已提交
206
    test.TestProcessWatcherMessage("test.permission.watcher.test1", watcherId);
207 208
}

4
411148299@qq.com 已提交
209
HWTEST_F(WatcherProxyUnitTest, TestAddWatcher2, TestSize.Level0)
210
{
4
411148299@qq.com 已提交
211
    WatcherProxyUnitTest test;
212 213 214 215 216 217
    uint32_t watcherId = 0;
    test.TestAddWatcher("test.permission.watcher.test2", watcherId);
    test.TestAddWatcher("test.permission.watcher.test2", watcherId);
    test.TestAddWatcher("test.permission.watcher.test2", watcherId);
}

4
411148299@qq.com 已提交
218
HWTEST_F(WatcherProxyUnitTest, TestAddWatcher3, TestSize.Level0)
219
{
4
411148299@qq.com 已提交
220
    WatcherProxyUnitTest test;
221 222 223 224
    uint32_t watcherId = 0;
    test.TestAddWatcher("test.permission.watcher.test3", watcherId);
}

4
411148299@qq.com 已提交
225
HWTEST_F(WatcherProxyUnitTest, TestAddWatcher4, TestSize.Level0)
226
{
4
411148299@qq.com 已提交
227
    WatcherProxyUnitTest test;
228
    uint32_t watcherId = 0;
S
sun_fan 已提交
229 230 231
    SystemSetParameter("test.watcher.test4", "1101");
    SystemSetParameter("test.watcher.test4.test", "1102");
    test.TestAddWatcher("test.watcher.test4*", watcherId);
232 233
}

4
411148299@qq.com 已提交
234
HWTEST_F(WatcherProxyUnitTest, TestAddWatcher5, TestSize.Level0)
235
{
4
411148299@qq.com 已提交
236
    WatcherProxyUnitTest test;
237 238 239 240 241
    uint32_t watcherId = 0;
    test.TestAddWatcher("test.permission.watcher.test5", watcherId);
    SystemSetParameter("test.permission.watcher.test5", "1101");
}

4
411148299@qq.com 已提交
242
HWTEST_F(WatcherProxyUnitTest, TestDelWatcher, TestSize.Level0)
243
{
4
411148299@qq.com 已提交
244
    WatcherProxyUnitTest test;
245 246 247
    uint32_t watcherId = 0;
    test.TestAddWatcher("test.permission.watcher.testDel", watcherId);
    test.TestDelWatcher("test.permission.watcher.testDel", watcherId);
S
sun_fan 已提交
248 249
}

4
411148299@qq.com 已提交
250
HWTEST_F(WatcherProxyUnitTest, TestWatchProxy, TestSize.Level0)
S
sun_fan 已提交
251
{
4
411148299@qq.com 已提交
252
    WatcherProxyUnitTest test;
S
sun_fan 已提交
253
    test.TestWatchProxy("test.permission.watcher.test1", "watcherId");
X
add ut  
xionglei6 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}

HWTEST_F(WatcherProxyUnitTest, TestWatchAgentDel, TestSize.Level0)
{
    WatcherProxyUnitTest test;
    test.TestWatchAgentDel("test.permission.watcher.test1");
}

HWTEST_F(WatcherProxyUnitTest, TestInvalid, TestSize.Level0)
{
    WatcherProxyUnitTest test;
    test.TestInvalid("test.permission.watcher.test1");
}

HWTEST_F(WatcherProxyUnitTest, TestStop, TestSize.Level0)
{
    WatcherProxyUnitTest test;
    test.TestStop();
272
}