diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 069ac9f9824d41352775a81321635ff848d4113b..eea4bd2b0447f203990d131423a129541b71d666 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -316,7 +316,10 @@ ohos_unittest("ani_file_hash_test") { resource_config_file = "../resource/ohos_test.xml" - sources = [ "mod_hash/hash_core_test.cpp" ] + sources = [ + "mod_hash/hash_core_test.cpp", + "mod_hash/hash_stream_test.cpp", + ] sources += ani_file_hash_core include_dirs = [ @@ -325,6 +328,7 @@ ohos_unittest("ani_file_hash_test") { "${file_api_path}/interfaces/kits/js/src/common/file_helper", "${file_api_path}/interfaces/kits/js/src/mod_fs", "${file_api_path}/interfaces/kits/js/src/mod_hash", + "${file_api_path}/interfaces/kits/js/src/mod_hash/class_hashstream", ] deps = [ diff --git a/interfaces/test/unittest/js/mod_hash/hash_stream_test.cpp b/interfaces/test/unittest/js/mod_hash/hash_stream_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bac6fb12df02c272221fba46c83cd91d8ff0919f --- /dev/null +++ b/interfaces/test/unittest/js/mod_hash/hash_stream_test.cpp @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2025 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 +#include +#include +#include "hs_hashstream.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +static const string g_filePath = "/data/test/HashStreamTest.txt"; +class HashStreamTest : public testing::Test { +public: + static void SetUpTestCase(void) + { + int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644); + close(fd); + }; + static void TearDownTestCase() + { + rmdir(g_filePath.c_str()); + }; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @tc.name: GetHsEntityTest_0001 + * @tc.desc: Test function of GetHsEntity() interface for null entity. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, GetHsEntityTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin GetHsEntityTest_0001"; + unique_ptr streamEntity = make_unique(); + HsHashStream stream(streamEntity); + stream.entity = nullptr; + auto [succ, entity] = stream.GetHsEntity(); + EXPECT_FALSE(succ); + + GTEST_LOG_(INFO) << "HashStreamTest-end GetHsEntityTest_0001"; +} + +/** + * @tc.name: GetHsEntityTest_0002 + * @tc.desc: Test function of GetHsEntity() interface for has entity. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, GetHsEntityTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin GetHsEntityTest_0002"; + unique_ptr streamEntity = make_unique(); + HsHashStream stream(streamEntity); + stream.entity = nullptr; + auto [succ, entity] = stream.GetHsEntity(); + EXPECT_TRUE(succ); + entity = nullptr; + + GTEST_LOG_(INFO) << "HashStreamTest-end GetHsEntityTest_0002"; +} + +/** + * @tc.name: UpdateTest_0001 + * @tc.desc: Test function of Update() interface for null entity. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, UpdateTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin UpdateTest_0001"; + unique_ptr streamEntity = make_unique(); + HsHashStream stream(streamEntity); + stream.entity = nullptr; + ArrayBuffer buffer ArrayBuffer { nullptr, 0 }; + + auto ret = stream.Update(buffer); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashStreamTest-end UpdateTest_0001"; +} + +/** + * @tc.name: UpdateTest_0002 + * @tc.desc: Test function of Update() interface for md5. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, UpdateTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin UpdateTest_0002"; + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + unique_ptr streamEntity(rawPtr); + streamEntity->algType = HASH_ALGORITHM_TYPE_MD5; + MD5_CTX ctx; + MD5_Init(&ctx); + streamEntity->md5Ctx = ctx; + HsHashStream stream(move(streamEntity)); + + auto buffer = std::make_shared(4096); + ArrayBuffer arrayBuffer ArrayBuffer { buffer.c_str(), 4096 }; + + auto ret = stream.Update(arrayBuffer); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashStreamTest-end UpdateTest_0002"; +} + +/** + * @tc.name: UpdateTest_0003 + * @tc.desc: Test function of Update() interface for sha1. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, UpdateTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin UpdateTest_0003"; + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + unique_ptr streamEntity(rawPtr); + streamEntity->algType = HASH_ALGORITHM_TYPE_SHA1; + SHA_CTX ctx; + SHA1_Init(&ctx); + streamEntity->shaCtx = ctx; + HsHashStream stream(move(streamEntity)); + + auto buffer = std::make_shared(4096); + ArrayBuffer arrayBuffer ArrayBuffer { buffer.c_str(), 4096 }; + + auto ret = stream.Update(arrayBuffer); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashStreamTest-end UpdateTest_0003"; +} + +/** + * @tc.name: UpdateTest_0004 + * @tc.desc: Test function of Update() interface for sha256. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, UpdateTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin UpdateTest_0004"; + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + unique_ptr streamEntity(rawPtr); + streamEntity->algType = HASH_ALGORITHM_TYPE_SHA1; + SHA256_CTX ctx; + SHA256_Init(&ctx); + streamEntity->sha256Ctx = ctx; + HsHashStream stream(move(streamEntity)); + + auto buffer = std::make_shared(4096); + ArrayBuffer arrayBuffer ArrayBuffer { buffer.c_str(), 4096 }; + + auto ret = stream.Update(arrayBuffer); + EXPECT_TRUE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashStreamTest-end UpdateTest_0004"; +} + +/** + * @tc.name: DigestTest_0001 + * @tc.desc: Test function of Digest() interface for null entity. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, DigestTest_0001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin DigestTest_0001"; + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + unique_ptr streamEntity(rawPtr); + HsHashStream stream(streamEntity); + stream.entity = nullptr; + + auto ret = stream.Digest(); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashStreamTest-end DigestTest_0001"; +} + +/** + * @tc.name: DigestTest_0002 + * @tc.desc: Test function of Digest() interface for md5. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, DigestTest_0002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin DigestTest_0002"; + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + unique_ptr streamEntity(rawPtr); + streamEntity->algType = HASH_ALGORITHM_TYPE_MD5; + MD5_CTX ctx; + MD5_Init(&ctx); + streamEntity->md5Ctx = ctx; + HsHashStream stream(move(streamEntity)); + + auto ret = stream.Digest(); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashStreamTest-end DigestTest_0002"; +} + +/** + * @tc.name: DigestTest_0003 + * @tc.desc: Test function of Digest() interface for sha1. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, DigestTest_0003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin DigestTest_0003"; + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + unique_ptr streamEntity(rawPtr); + streamEntity->algType = HASH_ALGORITHM_TYPE_SHA1; + SHA_CTX ctx; + SHA1_Init(&ctx); + streamEntity->shaCtx = ctx; + HsHashStream stream(move(streamEntity)); + + auto ret = stream.Digest(); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashStreamTest-end DigestTest_0003"; +} + +/** + * @tc.name: DigestTest_0004 + * @tc.desc: Test function of Digest() interface for sha256. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(HashStreamTest, DigestTest_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HashStreamTest-begin DigestTest_0004"; + HsHashStreamEntity *rawPtr = new (std::nothrow) HsHashStreamEntity(); + unique_ptr streamEntity(rawPtr); + streamEntity->algType = HASH_ALGORITHM_TYPE_SHA256; + SHA256_CTX ctx; + SHA256_Init(&ctx); + streamEntity->sha256Ctx = ctx; + HsHashStream stream(move(streamEntity)); + + auto ret = stream.Digest(); + EXPECT_FALSE(ret.IsSuccess()); + + GTEST_LOG_(INFO) << "HashStreamTest-end DigestTest_0004"; +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file