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 <gtest/gtest.h>
17 #include "cm_test_log.h"
18 #include "cm_test_common.h"
19 #include "cert_manager_api.h"
20 
21 using namespace testing::ext;
22 using namespace CertmanagerTest;
23 namespace {
24 static const int TIMES_PERFORMANCE = 10;
25 
26 struct CertAbstractResult {
27     struct CertAbstract certAbstract;
28     bool bExpectResult;
29 };
30 
31 struct CertAbstractResult g_listexpectResult[] = {
32     {
33         {
34             "1d3472b9.0",
35             "GlobalSign",
36             true, "CN=GlobalSign,OU=GlobalSign ECC Root CA - R5,O=GlobalSign"
37         },
38         true
39     },
40     {
41         {
42             "4bfab552.0",
43             "Starfield Technologies, Inc.",
44             true,
45             "CN=Starfield Root Certificate Authority - G2,OU=,O=Starfield Technologies, Inc."
46         },
47         true
48     },
49     {
50         {
51             "4f316efb.0",
52             "SwissSign AG",
53             true,
54             "CN=SwissSign Gold CA - G2,OU=,O=SwissSign AG"
55         },
56         true
57     }
58 };
59 
60 class CmGetCertListTest : public testing::Test {
61 public:
62     static void SetUpTestCase(void);
63 
64     static void TearDownTestCase(void);
65 
66     void SetUp();
67 
68     void TearDown();
69 
70     struct CertList *lstCert;
71 };
72 
SetUpTestCase(void)73 void CmGetCertListTest::SetUpTestCase(void)
74 {
75     SetATPermission();
76 }
77 
TearDownTestCase(void)78 void CmGetCertListTest::TearDownTestCase(void)
79 {
80 }
81 
SetUp()82 void CmGetCertListTest::SetUp()
83 {
84     InitCertList(&lstCert);
85 }
86 
TearDown()87 void CmGetCertListTest::TearDown()
88 {
89     FreeCertList(lstCert);
90 }
91 
92 /**
93  * @tc.name: SimpleGetCertListTest001
94  * @tc.desc: Test CertManager get cert list interface base function
95  * @tc.type: FUNC
96  * @tc.require: AR000H0MJA /SR000H096P
97  */
98 HWTEST_F(CmGetCertListTest, SimpleGetCertListTest001, TestSize.Level0)
99 {
100     int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, lstCert);
101     EXPECT_EQ(ret, CM_SUCCESS) << "CmGetCertList failed,retcode:" << ret;
102 }
103 
104 /**
105  * @tc.name: PerformanceGetCertListTest002
106  * @tc.desc: Test CertManager get cert list interface performance
107  * @tc.type: FUNC
108  * @tc.require: AR000H0MJA /SR000H096P
109  */
110 HWTEST_F(CmGetCertListTest, PerformanceGetCertListTest002, TestSize.Level0)
111 {
112     for (int times = 0; times < TIMES_PERFORMANCE; ++times) {
113         struct CertList *listCert = NULL;
114         ASSERT_TRUE(InitCertList(&listCert) == CM_SUCCESS);
115         int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, listCert);
116         EXPECT_EQ(ret, CM_SUCCESS) << "CmGetCertList Performance failed,retcode:" << ret;
117         FreeCertList(listCert);
118     }
119 }
120 
121 /**
122  * @tc.name: GetCertListContent003
123  * @tc.desc: Test CertManager get cert list content interface function
124  * @tc.type: FUNC
125  * @tc.require: AR000H0MJA /SR000H096P
126  */
127 HWTEST_F(CmGetCertListTest, GetCertListContent003, TestSize.Level0)
128 {
129     int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, lstCert);
130     EXPECT_EQ(ret, CM_SUCCESS) << "firstUserCtx CmGetCertList failed,retcode:" << ret;
131 
132     uint32_t length = sizeof(g_listexpectResult) / sizeof(g_listexpectResult[0]);
133     bool bFind = false;
134     for (uint32_t j = 0; j < length; ++j) {
135         bFind = FindCertAbstract(&(g_listexpectResult[j].certAbstract), lstCert);
136 
137         EXPECT_EQ(bFind, g_listexpectResult[j].bExpectResult) << DumpCertList(lstCert);
138     }
139 }
140 
141 /**
142  * @tc.name: AppGetCertListCompare004
143  * @tc.desc: Test CertManager get cert list compare interface function
144  * @tc.type: FUNC
145  * @tc.require: AR000H0MJA /SR000H096P
146  */
147 HWTEST_F(CmGetCertListTest, AppGetCertListCompare004, TestSize.Level0)
148 {
149     int32_t ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, lstCert);
150     EXPECT_EQ(ret, CM_SUCCESS) << "first  CmGetCertList failed,retcode:" << ret;
151 
152     struct CertList *secondListCert = NULL;
153     ASSERT_TRUE(InitCertList(&secondListCert) == CM_SUCCESS);
154     ret = CmGetCertList(CM_SYSTEM_TRUSTED_STORE, secondListCert);
155     EXPECT_EQ(ret, CM_SUCCESS) << "secondUserCtx CmGetCertList failed,retcode:" << ret;
156 
157     EXPECT_EQ(lstCert->certsCount, secondListCert->certsCount) << "firstUserCtx count:" << lstCert->certsCount
158         << "secondUserCtx count:" << secondListCert->certsCount;
159 
160     FreeCertList(secondListCert);
161 }
162 
163 /**
164  * @tc.name: ExceptionGetCertList005
165  * @tc.desc: Test CertManager get cert list interface abnormal function
166  * @tc.type: FUNC
167  * @tc.require: AR000H0MJA /SR000H096P
168  */
169 HWTEST_F(CmGetCertListTest, ExceptionGetCertList005, TestSize.Level0)
170 {
171     EXPECT_EQ(CmGetCertList(CM_SYSTEM_TRUSTED_STORE, NULL), CMR_ERROR_NULL_POINTER);
172     EXPECT_EQ(CmGetCertList(10, lstCert), CMR_ERROR_INVALID_ARGUMENT);
173 }
174 }