1 /*
2  * Copyright (c) 2022 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 
16 #include "UTTest_dm_crypto.h"
17 
18 #include <cinttypes>
19 #include <iostream>
20 
21 #include "dm_constants.h"
22 #include "dm_crypto.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace {
27     constexpr int32_t SALT_STRING_LENGTH = 16;
28     const std::string SALT_DEFAULT = "salt_defsalt_def";
29     const std::string UDID_SAMPLE = "3fde738fb2b8c910023d949166125bc9ed49e9e2fc8f4826d652b2839def2238";
30 }
SetUp()31 void DmCryptoTest::SetUp()
32 {
33 }
TearDown()34 void DmCryptoTest::TearDown()
35 {
36 }
SetUpTestCase()37 void DmCryptoTest::SetUpTestCase()
38 {
39 }
TearDownTestCase()40 void DmCryptoTest::TearDownTestCase()
41 {
42 }
43 
44 /**
45  * @tc.name: GetSecRandom_01
46  * @tc.type: FUNC
47  */
48 HWTEST_F(DmCryptoTest, GetSecRandom_01, testing::ext::TestSize.Level0)
49 {
50     const int32_t len = 8;
51     uint8_t buffer[len] = {0};
52     int32_t ret = Crypto::GetSecRandom(buffer, len);
53     EXPECT_EQ(ret, 0);
54 }
55 
56 /**
57  * @tc.name: GetSecSalt_01
58  * @tc.type: FUNC
59  */
60 HWTEST_F(DmCryptoTest, GetSecSalt_01, testing::ext::TestSize.Level0)
61 {
62     std::string salt = Crypto::GetSecSalt();
63     EXPECT_EQ(salt.length(), SALT_STRING_LENGTH);
64     EXPECT_NE(salt, SALT_DEFAULT);
65 
66     std::cout << "Random Salt: " << salt << std::endl;
67 }
68 
69 /**
70  * @tc.name: GetUdidHash_01
71  * @tc.type: FUNC
72  */
73 HWTEST_F(DmCryptoTest, GetUdidHash_01, testing::ext::TestSize.Level0)
74 {
75     char udidHash[DEVICE_UUID_LENGTH] = {0};
76     EXPECT_EQ(Crypto::GetUdidHash(UDID_SAMPLE, reinterpret_cast<uint8_t *>(udidHash)), DM_OK);
77 
78     std::string res(udidHash);
79     std::cout << "udidHash sample: " << res << std::endl;
80 }
81 
82 /**
83  * @tc.name: GetHashWithSalt_01
84  * @tc.type: FUNC
85  */
86 HWTEST_F(DmCryptoTest, GetHashWithSalt_01, testing::ext::TestSize.Level0)
87 {
88     std::string text1 = "c9ed49e9e2fc8f4826d652b2839d";
89     std::string text2 = "aaadfasdfasdfc9ed49e9e2sadfasdffc8f4826d6asdfasdf52basdf2839d";
90     std::string salt1 = Crypto::GetSecSalt();
91     std::string salt2 = Crypto::GetSecSalt();
92 
93     std::string hash1 = Crypto::GetHashWithSalt(text1, salt1);
94     std::string hash2 = Crypto::GetHashWithSalt(text1, salt2);
95 
96     EXPECT_STRNE(hash1.c_str(), hash2.c_str());
97     std::cout << "hash1: " << hash1 << std::endl;
98     std::cout << "hash2: " << hash2 << std::endl;
99 
100     std::string hash3 = Crypto::GetHashWithSalt(text1, salt1);
101     std::string hash4 = Crypto::GetHashWithSalt(text2, salt1);
102     EXPECT_STRNE(hash3.c_str(), hash4.c_str());
103     std::cout << "hash1: " << hash3 << std::endl;
104     std::cout << "hash2: " << hash4 << std::endl;
105 
106     std::string hash5 = Crypto::GetHashWithSalt(text2, salt1);
107     EXPECT_STREQ(hash5.c_str(), hash4.c_str());
108     std::cout << "hash5: " << hash5 << std::endl;
109     std::cout << "hash4: " << hash4 << std::endl;
110 
111     std::string hash6 = Crypto::GetHashWithSalt(text1, salt2);
112     EXPECT_STREQ(hash2.c_str(), hash6.c_str());
113 
114     std::cout << "hash2: " << hash2 << std::endl;
115     std::cout << "hash6: " << hash6 << std::endl;
116 }
117 } // DistributedHardware
118 } // OHOS