diff --git a/test/unittest/kits_ndk/BUILD.gn b/test/unittest/kits_ndk/BUILD.gn index f530402..7abbdad 100755 --- a/test/unittest/kits_ndk/BUILD.gn +++ b/test/unittest/kits_ndk/BUILD.gn @@ -28,6 +28,7 @@ ohos_unittest("asset_ndk_test") { ] sources = [ "src/asset_add_test.cpp", + "src/asset_auth_query_test.cpp", "src/asset_query_test.cpp", "src/asset_remove_test.cpp", "src/asset_update_test.cpp", diff --git a/test/unittest/kits_ndk/inc/asset_auth_query_test.h b/test/unittest/kits_ndk/inc/asset_auth_query_test.h new file mode 100644 index 0000000..8d9be39 --- /dev/null +++ b/test/unittest/kits_ndk/inc/asset_auth_query_test.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 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. + */ + +#ifndef ASSET_AUTH_QUERY_TEST_H +#define ASSET_AUTH_QUERY_TEST_H + +namespace UnitTest::AssetAuthQueryTest { +int AssetAuthQueryTest001(void); +int AssetAuthQueryTest002(void); +int AssetAuthQueryTest003(void); +int AssetAuthQueryTest004(void); +} + +#endif // ASSET_AUTH_QUERY_TEST_H diff --git a/test/unittest/kits_ndk/src/asset_auth_query_test.cpp b/test/unittest/kits_ndk/src/asset_auth_query_test.cpp new file mode 100644 index 0000000..616afb0 --- /dev/null +++ b/test/unittest/kits_ndk/src/asset_auth_query_test.cpp @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2023 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 "asset_add_test.h" +#include "asset_auth_query_test.h" + +#include +#include + +#include "asset_api.h" +#include "asset_test_common.h" + +using namespace testing::ext; +namespace UnitTest::AssetAuthQueryTest { +class AssetAuthQueryTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(void); + + void TearDown(void); +}; + +void AssetAuthQueryTest::SetUpTestCase(void) +{ +} + +void AssetAuthQueryTest::TearDownTestCase(void) +{ +} + +void AssetAuthQueryTest::SetUp(void) +{ +} + +void AssetAuthQueryTest::TearDown(void) +{ +} + +/** + * @tc.name: AssetAuthQueryTest.AssetAuthQueryTest001 + * @tc.desc: Add auth asset, pre-query, query added auth asset with wrong auth token, post-query, + * expect ASSET_INVALID_ARGUMENT. + * @tc.type: FUNC + * @tc.result:0 + */ +HWTEST_F(AssetAuthQueryTest, AssetAuthQueryTest001, TestSize.Level0) +{ + Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast(const_cast(__func__)) }; + Asset_Attr addAttr[] = { + { .tag = ASSET_TAG_ALIAS, .value.blob = funcName }, + { .tag = ASSET_TAG_SECRET, .value.blob = funcName }, + { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON }, + { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_ANY }, + }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(addAttr, ARRAY_SIZE(addAttr))); + + Asset_Attr preQueryAttr[] = {}; + Asset_Blob challenge = { 0 }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PreQuery(preQueryAttr, ARRAY_SIZE(preQueryAttr), &challenge)); + + Asset_ResultSet queryResultSet = { 0 }; + Asset_Attr queryAttr[] = { + { .tag = ASSET_TAG_ALIAS, .value.blob = funcName }, + { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, + { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge }, + { .tag = ASSET_TAG_AUTH_TOKEN, .value.blob = funcName }, + }; + ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Query(queryAttr, ARRAY_SIZE(queryAttr), &queryResultSet)); + + Asset_Attr postQueryAttr[] = { + { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge }, + }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PostQuery(postQueryAttr, ARRAY_SIZE(postQueryAttr))); + + Asset_Blob blob = { .size = 0, .data = nullptr }; + OH_Asset_FreeBlob(&blob); + OH_Asset_FreeBlob(nullptr); + Asset_ResultSet resultSet = { .count = 0, .results = nullptr }; + OH_Asset_FreeResultSet(&queryResultSet); + OH_Asset_FreeResultSet(&resultSet); + OH_Asset_FreeResultSet(nullptr); + ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__)); +} + +/** + * @tc.name: AssetAuthQueryTest.AssetAuthQueryTest002 + * @tc.desc: Add auth asset, pre-query, query added auth asset without auth token, post-query, + * expect ASSET_INVALID_ARGUMENT. + * @tc.type: FUNC + * @tc.result:0 + */ +HWTEST_F(AssetAuthQueryTest, AssetAuthQueryTest002, TestSize.Level0) +{ + Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast(const_cast(__func__)) }; + Asset_Attr addAttr[] = { + { .tag = ASSET_TAG_ALIAS, .value.blob = funcName }, + { .tag = ASSET_TAG_SECRET, .value.blob = funcName }, + { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON }, + { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_ANY }, + }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(addAttr, ARRAY_SIZE(addAttr))); + + Asset_Attr preQueryAttr[] = {}; + Asset_Blob challenge = { 0 }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PreQuery(preQueryAttr, ARRAY_SIZE(preQueryAttr), &challenge)); + + Asset_ResultSet queryResultSet = { 0 }; + Asset_Attr queryAttr[] = { + { .tag = ASSET_TAG_ALIAS, .value.blob = funcName }, + { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, + { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge }, + }; + ASSERT_EQ(ASSET_INVALID_ARGUMENT, OH_Asset_Query(queryAttr, ARRAY_SIZE(queryAttr), &queryResultSet)); + + Asset_Attr postQueryAttr[] = { + { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge }, + }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PostQuery(postQueryAttr, ARRAY_SIZE(postQueryAttr))); + + Asset_Blob blob = { .size = 0, .data = nullptr }; + OH_Asset_FreeBlob(&blob); + OH_Asset_FreeBlob(nullptr); + Asset_ResultSet resultSet = { .count = 0, .results = nullptr }; + OH_Asset_FreeResultSet(&queryResultSet); + OH_Asset_FreeResultSet(&resultSet); + OH_Asset_FreeResultSet(nullptr); + ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__)); +} + +/** + * @tc.name: AssetAuthQueryTest.AssetAuthQueryTest003 + * @tc.desc: Pre-query, expect ASSET_NOT_FOUND. + * @tc.type: FUNC + * @tc.result:0 + */ +HWTEST_F(AssetAuthQueryTest, AssetAuthQueryTest003, TestSize.Level0) +{ + Asset_Attr preQueryAttr[] = {}; + Asset_Blob challenge = { 0 }; + ASSERT_EQ(ASSET_NOT_FOUND, OH_Asset_PreQuery(preQueryAttr, ARRAY_SIZE(preQueryAttr), &challenge)); + + Asset_Blob blob = { .size = 0, .data = nullptr }; + OH_Asset_FreeBlob(&blob); + OH_Asset_FreeBlob(nullptr); + Asset_ResultSet resultSet = { .count = 0, .results = nullptr }; + OH_Asset_FreeResultSet(&resultSet); + OH_Asset_FreeResultSet(nullptr); +} + +/** + * @tc.name: AssetAuthQueryTest.AssetAuthQueryTest004 + * @tc.desc: Add auth asset, pre-query, post-query, expect ASSET_SUCCESS. + * @tc.type: FUNC + * @tc.result:0 + */ +HWTEST_F(AssetAuthQueryTest, AssetAuthQueryTest004, TestSize.Level0) +{ + Asset_Blob funcName = { .size = strlen(__func__), .data = reinterpret_cast(const_cast(__func__)) }; + Asset_Attr addAttr[] = { + { .tag = ASSET_TAG_ALIAS, .value.blob = funcName }, + { .tag = ASSET_TAG_SECRET, .value.blob = funcName }, + { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_POWERED_ON }, + { .tag = ASSET_TAG_AUTH_TYPE, .value.u32 = ASSET_AUTH_TYPE_ANY }, + }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_Add(addAttr, ARRAY_SIZE(addAttr))); + + Asset_Attr preQueryAttr[] = {}; + Asset_Blob challenge = { 0 }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PreQuery(preQueryAttr, ARRAY_SIZE(preQueryAttr), &challenge)); + + Asset_Attr postQueryAttr[] = { + { .tag = ASSET_TAG_AUTH_CHALLENGE, .value.blob = challenge }, + }; + ASSERT_EQ(ASSET_SUCCESS, OH_Asset_PostQuery(postQueryAttr, ARRAY_SIZE(postQueryAttr))); + + + Asset_Blob blob = { .size = 0, .data = nullptr }; + OH_Asset_FreeBlob(&blob); + OH_Asset_FreeBlob(nullptr); + Asset_ResultSet resultSet = { .count = 0, .results = nullptr }; + OH_Asset_FreeResultSet(&resultSet); + OH_Asset_FreeResultSet(nullptr); + ASSERT_EQ(ASSET_SUCCESS, RemoveByAliasNdk(__func__)); +} +} \ No newline at end of file diff --git a/test/unittest/kits_ndk/src/asset_operation_test.cpp b/test/unittest/kits_ndk/src/asset_operation_test.cpp deleted file mode 100755 index d4a8cdf..0000000 --- a/test/unittest/kits_ndk/src/asset_operation_test.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2023 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 "asset_operation_test.h" - -#include -#include - -#include "asset_api.h" -#include "asset_test_common.h" - -using namespace testing::ext; -namespace UnitTest::AssetOperationTest { -class AssetOperationTest : public testing::Test { -public: - static void SetUpTestCase(void); - - static void TearDownTestCase(void); - - void SetUp(void); - - void TearDown(void); -}; - -void AssetOperationTest::SetUpTestCase(void) -{ -} - -void AssetOperationTest::TearDownTestCase(void) -{ -} - -void AssetOperationTest::SetUp(void) -{ -} - -void AssetOperationTest::TearDown(void) -{ -} - -/** - * @tc.name: AssetOperationTest.AssetOperationTest001 - * @tc.desc: Free blob with nullptr, expect non-crsh - * @tc.type: FUNC - * @tc.result:0 - */ -HWTEST_F(AssetOperationTest, AssetOperationTest001, TestSize.Level0) -{ - OH_Asset_FreeBlob(nullptr); -} - -/** - * @tc.name: AssetOperationTest.AssetOperationTest002 - * @tc.desc: Free blob with nullptr data, expect non-crsh - * @tc.type: FUNC - * @tc.result:0 - */ -HWTEST_F(AssetOperationTest, AssetOperationTest002, TestSize.Level0) -{ - Asset_Blob blob = { .size = 0, .data = nullptr }; - OH_Asset_FreeBlob(&blob); -} - -/** - * @tc.name: AssetOperationTest.AssetOperationTest003 - * @tc.desc: Free blob with nullptr data, expect non-crsh - * @tc.type: FUNC - * @tc.result:0 - */ -HWTEST_F(AssetOperationTest, AssetOperationTest003, TestSize.Level0) -{ - Asset_Blob blob = { .size = 0, .data = reinterpret_cast(const_cast(__func__)) }; - OH_Asset_FreeBlob(&blob); -} - -/** - * @tc.name: AssetOperationTest.AssetOperationTest004 - * @tc.desc: Free result set with nullptr, expect non-crsh - * @tc.type: FUNC - * @tc.result:0 - */ -HWTEST_F(AssetOperationTest, AssetOperationTest004, TestSize.Level0) -{ - OH_Asset_FreeResultSet(nullptr); -} - -/** - * @tc.name: AssetOperationTest.AssetOperationTest005 - * @tc.desc: Free result set with nullptr, expect non-crsh - * @tc.type: FUNC - * @tc.result:0 - */ -HWTEST_F(AssetOperationTest, AssetOperationTest005, TestSize.Level0) -{ - Asset_ResultSet resultSet = { .count = 0, .results = nullptr }; - OH_Asset_FreeResultSet(&resultSet); -} -} \ No newline at end of file