From 95a7bf5595bf57dfc26e2424a4a77fb5f83f1340 Mon Sep 17 00:00:00 2001 From: sunjiakun Date: Wed, 11 Dec 2024 15:28:11 +0800 Subject: [PATCH] add TDD Signed-off-by: sunjiakun --- .../test/unittest/src/hap_verify_test.cpp | 30 ++++- .../unittest/src/random_access_file_test.cpp | 120 +++++++++++++++++- 2 files changed, 148 insertions(+), 2 deletions(-) diff --git a/interfaces/innerkits/appverify/test/unittest/src/hap_verify_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/hap_verify_test.cpp index d9d06d1..5f9fe79 100644 --- a/interfaces/innerkits/appverify/test/unittest/src/hap_verify_test.cpp +++ b/interfaces/innerkits/appverify/test/unittest/src/hap_verify_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-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 @@ -31,6 +31,8 @@ using namespace testing::ext; using namespace OHOS::Security::Verify; namespace { +const std::string BUNDLE_NAME = "bundleName"; +const std::string APP_IDENTIFIER = "appIdentifier"; class HapVerifyTest : public testing::Test { public: static void SetUpTestCase(void); @@ -260,4 +262,30 @@ HWTEST_F(HapVerifyTest, HapVerify003, TestSize.Level0) ASSERT_EQ(profile.versionName, TEST_VERSION_NAME); ASSERT_EQ(profile.distributionType, AppDistType::OS_INTEGRATION); } + +/** + * @tc.name: HapVerifyTest.ParseBundleNameAndAppIdentifier001 + * @tc.desc: test Parse BundleName And AppIdentifier + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyTest, ParseBundleNameAndAppIdentifier001, TestSize.Level0) +{ + int32_t fileFd = -1; + std::string bundleName = BUNDLE_NAME; + std::string appIdentifier = APP_IDENTIFIER; + ASSERT_EQ(ParseBundleNameAndAppIdentifier(fileFd, bundleName, appIdentifier), OPEN_FILE_ERROR); +} + +/** + * @tc.name: HapVerifyTest.ParseBundleNameAndAppIdentifier002 + * @tc.desc: test Parse BundleName And AppIdentifier + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyTest, ParseBundleNameAndAppIdentifier002, TestSize.Level0) +{ + int32_t fileFd = 1; + std::string bundleName = BUNDLE_NAME; + std::string appIdentifier = APP_IDENTIFIER; + ASSERT_EQ(ParseBundleNameAndAppIdentifier(fileFd, bundleName, appIdentifier), OPEN_FILE_ERROR); +} } diff --git a/interfaces/innerkits/appverify/test/unittest/src/random_access_file_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/random_access_file_test.cpp index 4d79538..1b055d8 100644 --- a/interfaces/innerkits/appverify/test/unittest/src/random_access_file_test.cpp +++ b/interfaces/innerkits/appverify/test/unittest/src/random_access_file_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-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 @@ -18,6 +18,7 @@ #include #include +#include #include "common/hap_byte_buffer.h" #include "common/random_access_file.h" @@ -119,4 +120,121 @@ HWTEST_F(RandomAccessFileTest, ReadFileFullyFromOffsetTest001, TestSize.Level1) ASSERT_TRUE(ret == READ_OFFSET_OUT_OF_RANGE); buffer.reset(nullptr); } + +/** + * @tc.name: Test ReadFileFullyFromOffsetTest function + * @tc.desc: The static function will return each reading result; + * @tc.type: FUNC + */ +HWTEST_F(RandomAccessFileTest, ReadFileFullyFromOffsetTest002, TestSize.Level1) +{ + RandomAccessFile randomAccessFile; + std::string filePath = "./test_hapverify.zip"; + int32_t fileFd = open(filePath.c_str(), O_RDONLY); + ASSERT_TRUE(randomAccessFile.InitWithFd(fileFd)); + ASSERT_EQ(randomAccessFile.ReadFileFullyFromOffset(nullptr, 0, 0), DEST_BUFFER_IS_NULL); +} + +/** + * @tc.name: Test ReadFileFullyFromOffsetTest function + * @tc.desc: The static function will return each reading result; + * @tc.type: FUNC + */ +HWTEST_F(RandomAccessFileTest, ReadFileFullyFromOffsetTest003, TestSize.Level1) +{ + RandomAccessFile randomAccessFile; + std::string filePath = "./test_hapverify.zip"; + int32_t fileFd = open(filePath.c_str(), O_RDONLY); + ASSERT_TRUE(randomAccessFile.InitWithFd(fileFd)); + std::unique_ptr buffer = std::make_unique(TEST_RANDOMREAD_LENGTH); + ASSERT_TRUE(buffer != nullptr); + ASSERT_TRUE(randomAccessFile.ReadFileFullyFromOffset(buffer.get(), 0, TEST_RANDOMREAD_LENGTH) > 0); + buffer.reset(nullptr); +} + +/** + * @tc.name: Test ReadFileFullyFromOffsetTest function + * @tc.desc: The static function will return each reading result; + * @tc.type: FUNC + */ +HWTEST_F(RandomAccessFileTest, ReadFileFullyFromOffsetTest004, TestSize.Level1) +{ + RandomAccessFile randomAccessFile; + std::string filePath = "./test_hapverify.zip"; + int32_t fileFd = open(filePath.c_str(), O_RDONLY); + ASSERT_TRUE(randomAccessFile.InitWithFd(fileFd)); + std::unique_ptr buffer = std::make_unique(TEST_RANDOMREAD_LENGTH); + ASSERT_TRUE(buffer != nullptr); + ASSERT_EQ(randomAccessFile.ReadFileFullyFromOffset(buffer.get(), -1, TEST_RANDOMREAD_LENGTH), DEST_BUFFER_IS_NULL); + buffer.reset(nullptr); +} + +/** + * @tc.name: Test ReadFileFullyFromOffsetTest function + * @tc.desc: The static function will return each reading result; + * @tc.type: FUNC + */ +HWTEST_F(RandomAccessFileTest, ReadFileFullyFromOffsetTest005, TestSize.Level1) +{ + RandomAccessFile randomAccessFile; + std::string filePath = "./test_hapverify.zip"; + int32_t fileFd = open(filePath.c_str(), O_RDONLY); + ASSERT_TRUE(randomAccessFile.InitWithFd(fileFd)); + HapByteBuffer hapBuffer; + ASSERT_EQ(randomAccessFile.ReadFileFullyFromOffset(hapBuffer, -1), DEST_BUFFER_IS_NULL); +} + +/** + * @tc.name: Test ReadFileFullyFromOffsetTest function + * @tc.desc: The static function will return each reading result; + * @tc.type: FUNC + */ +HWTEST_F(RandomAccessFileTest, ReadFileFullyFromOffsetTest006, TestSize.Level1) +{ + RandomAccessFile randomAccessFile; + std::string filePath = "./test_hapverify.zip"; + int32_t fileFd = open(filePath.c_str(), O_RDONLY); + ASSERT_TRUE(randomAccessFile.InitWithFd(fileFd)); + HapByteBuffer hapBuffer(TEST_RANDOMREAD_LENGTH); + ASSERT_EQ(randomAccessFile.ReadFileFullyFromOffset(hapBuffer, -1), DEST_BUFFER_IS_NULL); +} + +/** + * @tc.name: Test ReadFileFullyFromOffsetTest function + * @tc.desc: The static function will return each reading result; + * @tc.type: FUNC + */ +HWTEST_F(RandomAccessFileTest, ReadFileFullyFromOffsetTest007, TestSize.Level1) +{ + RandomAccessFile randomAccessFile; + std::string filePath = "./test_hapverify.zip"; + int32_t fileFd = open(filePath.c_str(), O_RDONLY); + ASSERT_TRUE(randomAccessFile.InitWithFd(fileFd)); + HapByteBuffer hapBuffer(TEST_RANDOMREAD_LENGTH); + ASSERT_TRUE(randomAccessFile.ReadFileFullyFromOffset(hapBuffer, 0) > 0); +} + +/** + * @tc.name: Test InitWithFd function + * @tc.desc: The static function will return each reading result; + * @tc.type: FUNC + */ +HWTEST_F(RandomAccessFileTest, InitWithFd001, TestSize.Level1) +{ + RandomAccessFile randomAccessFile; + int32_t fileFd = -1; + ASSERT_FALSE(randomAccessFile.InitWithFd(fileFd)); +} + +/** + * @tc.name: Test InitWithFd function + * @tc.desc: The static function will return each reading result; + * @tc.type: FUNC + */ +HWTEST_F(RandomAccessFileTest, InitWithFd002, TestSize.Level1) +{ + RandomAccessFile randomAccessFile; + int32_t fileFd = 1; + ASSERT_FALSE(randomAccessFile.InitWithFd(fileFd)); +} } -- Gitee