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

S
sun_fan 已提交
17
#include "if_system_ability_manager.h"
M
Mupceet 已提交
18
#include "param_stub.h"
S
sun_fan 已提交
19 20
#include "iservice_registry.h"
#include "iwatcher.h"
21 22
#include "iwatcher_manager.h"
#include "message_parcel.h"
S
sun_fan 已提交
23
#include "param_message.h"
M
Mupceet 已提交
24
#include "init_param.h"
25 26 27
#include "param_utils.h"
#include "parcel.h"
#include "securec.h"
S
sun_fan 已提交
28
#include "system_ability_definition.h"
29 30
#include "watcher.h"
#include "watcher_manager.h"
S
sun_fan 已提交
31
#include "watcher_proxy.h"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#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 已提交
51
class WatcherProxyUnitTest : public ::testing::Test {
52
public:
4
411148299@qq.com 已提交
53 54
    WatcherProxyUnitTest() {}
    virtual ~WatcherProxyUnitTest() {}
55 56 57 58 59 60 61 62 63 64 65 66 67

    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 已提交
68
        data.WriteInterfaceToken(IWatcherManager::GetDescriptor());
69
        data.WriteString(keyPrefix);
S
sun_fan 已提交
70
        sptr<IWatcher> watcher = new TestWatcher();
71 72 73 74 75
        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 已提交
76
        EXPECT_EQ(watcherManager->GetWatcherGroup(1000) != NULL, 0); // 1000 test group id
S
sun_fan 已提交
77
        EXPECT_EQ(watcherManager->GetWatcherGroup("TestAddWatcher") != NULL, 0); // test key not exist
78 79 80 81 82 83 84 85 86 87
        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 已提交
88
        data.WriteInterfaceToken(IWatcherManager::GetDescriptor());
89 90 91 92 93 94 95 96
        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 已提交
97 98 99 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
    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 已提交
133 134 135 136 137 138 139
    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 已提交
140 141

        data.WriteInterfaceToken(IWatcherManager::GetDescriptor());
X
add ut  
xionglei6 已提交
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 174 175 176 177 178 179 180 181 182 183
        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;
    }

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

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

4
411148299@qq.com 已提交
206
HWTEST_F(WatcherProxyUnitTest, TestAddWatcher2, TestSize.Level0)
207
{
4
411148299@qq.com 已提交
208
    WatcherProxyUnitTest test;
209 210 211 212 213 214
    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 已提交
215
HWTEST_F(WatcherProxyUnitTest, TestAddWatcher3, TestSize.Level0)
216
{
4
411148299@qq.com 已提交
217
    WatcherProxyUnitTest test;
218 219 220 221
    uint32_t watcherId = 0;
    test.TestAddWatcher("test.permission.watcher.test3", watcherId);
}

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

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

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

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

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();
269
}