1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <stdlib.h> 16 #include <stdio.h> 17 #include <pthread.h> 18 #include <string.h> 19 #include <limits.h> 20 21 #include "crypto_framework_random_test.h" 22 #include "blob.h" 23 #include "log.h" 24 25 #include <gtest/gtest.h> 26 27 using namespace testing::ext; 28 namespace Unittest::CryptoFrameworkRandomNapiTest { 29 class OHCryptoFrameworkRandomNapiTest : public testing::Test { 30 public: SetUpTestCase(void)31 static void SetUpTestCase(void) {}; TearDownTestCase(void)32 static void TearDownTestCase(void) {}; SetUp()33 void SetUp() {}; TearDown()34 void TearDown() {}; 35 }; 36 37 class RANDOM_TEST : public testing::TestWithParam<int> { 38 public: SetUpTestCase()39 static void SetUpTestCase() {}; TearDownTestCase()40 static void TearDownTestCase() {}; 41 }; 42 43 class RANDOM_SEED_TEST : public testing::TestWithParam<int> { 44 public: SetUpTestCase()45 static void SetUpTestCase() {}; TearDownTestCase()46 static void TearDownTestCase() {}; 47 }; 48 49 int g_randomLen[] = { 50 1, 11, 16, 32, 64, 128, 256, 512, 1024, 2048, 3072, 4096, 8192, 1024 * 1024 51 }; 52 INSTANTIATE_TEST_CASE_P(OHCryptoFrameworkRandomNapiTest, RANDOM_TEST, ::testing::ValuesIn(g_randomLen)); 53 54 /** 55 * @tc.number SUB_Security_CryptoFramework_NAPI_Random_Test_0100 56 * @tc.name OHCryptoFrameworkRandomNapiTest/RANDOM_TEST.SUB_Security_CryptoFramework_NAPI_Random_Test_0100/x 57 * @tc.desc algorithm is Kdf 58 * @tc.size Medium 59 * @tc.type Func 60 * @tc.level Level0 61 */ 62 HWTEST_P(RANDOM_TEST, SUB_Security_CryptoFramework_NAPI_Random_Test_0100, TestSize.Level0) 63 { 64 int len = GetParam(); 65 OH_CryptoRand *ctx = nullptr; 66 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 67 68 EXPECT_EQ(OH_CryptoRand_Create(&ctx), CRYPTO_SUCCESS); 69 if (len == INT_MAX) { 70 EXPECT_EQ(OH_CryptoRand_GenerateRandom(ctx, len, &out), CRYPTO_MEMORY_ERROR); 71 } else { 72 EXPECT_EQ(OH_CryptoRand_GenerateRandom(ctx, len, &out), CRYPTO_SUCCESS); 73 EXPECT_EQ(out.len, len); 74 } 75 const char *algoName = OH_CryptoRand_GetAlgoName(ctx); 76 EXPECT_TRUE(memcmp("CTR_DRBG", algoName, strlen(algoName)) == 0); 77 78 OH_CryptoRand_Destroy(ctx); 79 OH_Crypto_FreeDataBlob(&out); 80 } 81 82 INSTANTIATE_TEST_CASE_P(OHCryptoFrameworkRandomNapiTest, RANDOM_SEED_TEST, ::testing::ValuesIn(g_randomLen)); 83 84 /** 85 * @tc.number SUB_Security_CryptoFramework_NAPI_Random_Test_0200 86 * @tc.name OHCryptoFrameworkRandomNapiTest/RANDOM_SEED_TEST.SUB_Security_CryptoFramework_NAPI_Random_Test_0200/x 87 * @tc.desc algorithm is Kdf 88 * @tc.size Medium 89 * @tc.type Func 90 * @tc.level Level0 91 */ 92 HWTEST_P(RANDOM_SEED_TEST, SUB_Security_CryptoFramework_NAPI_Random_Test_0200, TestSize.Level0) 93 { 94 int len = GetParam(); 95 OH_CryptoRand *ctx = nullptr; 96 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 97 uint8_t seedData[] = { 98 0x30, 0x79, 0x02, 0x21, 0x00, 0x9b, 0x17, 0xa5, 0xd3, 0xac, 0xcc, 0xf3, 0x3c, 0x2e, 0x36, 0x23 99 }; 100 Crypto_DataBlob seed = {.data = seedData, .len = sizeof(seedData) / sizeof(seedData[0])}; 101 102 EXPECT_EQ(OH_CryptoRand_Create(&ctx), CRYPTO_SUCCESS); 103 EXPECT_EQ(OH_CryptoRand_SetSeed(ctx, &seed), CRYPTO_SUCCESS); 104 if (len == INT_MAX) { 105 EXPECT_EQ(OH_CryptoRand_GenerateRandom(ctx, len, &out), CRYPTO_MEMORY_ERROR); 106 } else { 107 EXPECT_EQ(OH_CryptoRand_GenerateRandom(ctx, len, &out), CRYPTO_SUCCESS); 108 EXPECT_EQ(out.len, len); 109 } 110 const char *algoName = OH_CryptoRand_GetAlgoName(ctx); 111 EXPECT_TRUE(memcmp("CTR_DRBG", algoName, strlen(algoName)) == 0); 112 113 OH_CryptoRand_Destroy(ctx); 114 OH_Crypto_FreeDataBlob(&out); 115 } 116 117 /** 118 * @tc.number SUB_Security_CryptoFramework_NAPI_Random_Test_0300 119 * @tc.name OHCryptoFrameworkRandomNapiTest.SUB_Security_CryptoFramework_NAPI_Random_Test_0300 120 * @tc.desc algorithm is Kdf 121 * @tc.size Medium 122 * @tc.type Func 123 * @tc.level Level0 124 */ 125 HWTEST_F(OHCryptoFrameworkRandomNapiTest, SUB_Security_CryptoFramework_NAPI_Random_Test_0300, TestSize.Level0) 126 { 127 int len = -1; 128 OH_CryptoRand *ctx = nullptr; 129 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 130 131 EXPECT_EQ(OH_CryptoRand_Create(&ctx), CRYPTO_SUCCESS); 132 EXPECT_EQ(OH_CryptoRand_GenerateRandom(ctx, len, &out), CRYPTO_PARAMETER_CHECK_FAILED); 133 134 135 OH_CryptoRand_Destroy(ctx); 136 OH_Crypto_FreeDataBlob(&out); 137 } 138 139 /** 140 * @tc.number SUB_Security_CryptoFramework_NAPI_Random_Test_0400 141 * @tc.name OHCryptoFrameworkRandomNapiTest.SUB_Security_CryptoFramework_NAPI_Random_Test_0400 142 * @tc.desc algorithm is Kdf 143 * @tc.size Medium 144 * @tc.type Func 145 * @tc.level Level0 146 */ 147 HWTEST_F(OHCryptoFrameworkRandomNapiTest, SUB_Security_CryptoFramework_NAPI_Random_Test_0400, TestSize.Level0) 148 { 149 int len = 0; 150 OH_CryptoRand *ctx = nullptr; 151 Crypto_DataBlob out = {.data = nullptr, .len = 0}; 152 153 EXPECT_EQ(OH_CryptoRand_Create(&ctx), CRYPTO_SUCCESS); 154 EXPECT_EQ(OH_CryptoRand_GenerateRandom(ctx, len, &out), CRYPTO_PARAMETER_CHECK_FAILED); 155 156 157 OH_CryptoRand_Destroy(ctx); 158 OH_Crypto_FreeDataBlob(&out); 159 } 160 161 } // namespace Unittest::CryptoFrameworkRandomNapiTest