From f1fed6b19c640f4328015e5b54d433e668811478 Mon Sep 17 00:00:00 2001 From: xia-bubai Date: Thu, 22 May 2025 22:11:04 +0800 Subject: [PATCH] fix RemoveNodeFromList Signed-off-by: xia-bubai --- .../innerkits/nativetoken/src/nativetoken.c | 2 +- .../unittest/mock/nativetoken_oper_test.cpp | 41 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/interfaces/innerkits/nativetoken/src/nativetoken.c b/interfaces/innerkits/nativetoken/src/nativetoken.c index b1db39183..093694088 100644 --- a/interfaces/innerkits/nativetoken/src/nativetoken.c +++ b/interfaces/innerkits/nativetoken/src/nativetoken.c @@ -153,7 +153,7 @@ static void RemoveNodeFromList(NativeTokenList **node) if (node == NULL || *node == NULL || g_tokenListHead == NULL) { return; } - NativeTokenList *tmp = g_tokenListHead->next; + NativeTokenList *tmp = g_tokenListHead; while (tmp != NULL) { if (tmp->next == *node) { tmp->next = (*node)->next; diff --git a/interfaces/innerkits/nativetoken/test/unittest/mock/nativetoken_oper_test.cpp b/interfaces/innerkits/nativetoken/test/unittest/mock/nativetoken_oper_test.cpp index b88a09059..61689e193 100644 --- a/interfaces/innerkits/nativetoken/test/unittest/mock/nativetoken_oper_test.cpp +++ b/interfaces/innerkits/nativetoken/test/unittest/mock/nativetoken_oper_test.cpp @@ -468,7 +468,7 @@ HWTEST_F(TokenOperTest, GetNativeTokenFromJson001, TestSize.Level0) std::remove(TOKEN_ID_CFG_FILE_COPY_PATH); } -static int32_t Start(const char *processName) +static int32_t Start(const char *processName, bool isChange = false) { const char **dcapList = new (std::nothrow) const char *[2]; if (dcapList == nullptr) { @@ -483,6 +483,9 @@ static int32_t Start(const char *processName) } permList[0] = "ohos.permission.test1"; permList[1] = "ohos.permission.test2"; + if (isChange) { + permList[1] = "ohos.permission.test3"; + } const char **acls = new (std::nothrow) const char *[1]; if (acls == nullptr) { return 0; @@ -543,12 +546,13 @@ HWTEST_F(TokenOperTest, GetInfoArrFromJson001, TestSize.Level0) /** * @tc.name: RemoveNodeFromList001 - * @tc.desc: GetInfoArrFromJson successfully. + * @tc.desc: remove foundation node of token list. * @tc.type: FUNC * @tc.require: */ HWTEST_F(TokenOperTest, RemoveNodeFromList001, TestSize.Level0) { + SetTimes(); CopyNativeTokenJson(TOKEN_ID_CFG_FILE_PATH, TOKEN_ID_CFG_FILE_COPY_PATH); AtlibInit(); EXPECT_NE(g_tokenListHead, nullptr); @@ -568,3 +572,36 @@ HWTEST_F(TokenOperTest, RemoveNodeFromList001, TestSize.Level0) CopyNativeTokenJson(TOKEN_ID_CFG_FILE_COPY_PATH, TOKEN_ID_CFG_FILE_PATH); std::remove(TOKEN_ID_CFG_FILE_COPY_PATH); } + +/** + * @tc.name: RemoveNodeFromList002 + * @tc.desc: rmeove the second node of token list. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(TokenOperTest, RemoveNodeFromList002, TestSize.Level0) +{ + SetTimes(); + CopyNativeTokenJson(TOKEN_ID_CFG_FILE_PATH, TOKEN_ID_CFG_FILE_COPY_PATH); + AtlibInit(); + EXPECT_NE(g_tokenListHead, nullptr); + // add the node + EXPECT_NE(Start("process3"), 0); + g_strcpyTime = 0; + + // remove the node + EXPECT_EQ(Start("process3", true), 0); + + // check the node whether exsits in the list + NativeTokenList *node = g_tokenListHead->next; + while (node != nullptr) { + if (strcmp(node->processName, "process3") == 0) { + break; + } + node = node->next; + } + EXPECT_EQ(node, nullptr); + + CopyNativeTokenJson(TOKEN_ID_CFG_FILE_COPY_PATH, TOKEN_ID_CFG_FILE_PATH); + std::remove(TOKEN_ID_CFG_FILE_COPY_PATH); +} -- Gitee