• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CM_TEST_COMMON_H
17 #define CM_TEST_COMMON_H
18 
19 #include "cm_type.h"
20 #include "securec.h"
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24 
25 #include  <string>
26 
27 #define PERFORMACE_COUNT 10
28 
29 #define DELIMITER "$$$"
30 #define ENDOF  "\n"
31 
32 constexpr uint32_t CERT_KEY_ALG_RSA = 1;
33 constexpr uint32_t CERT_KEY_ALG_ECC = 2;
34 constexpr uint32_t CERT_KEY_ALG_RSA_512 = 3;
35 constexpr uint32_t CERT_KEY_ALG_RSA_1024 = 4;
36 constexpr uint32_t CERT_KEY_ALG_RSA_3072 = 5;
37 constexpr uint32_t CERT_KEY_ALG_RSA_4096 = 6;
38 constexpr uint32_t CERT_KEY_ALG_ECC_P224 = 7;
39 constexpr uint32_t CERT_KEY_ALG_ECC_P384 = 8;
40 constexpr uint32_t CERT_KEY_ALG_ECC_P521 = 9;
41 constexpr uint32_t CERT_KEY_ALG_ED25519 = 10;
42 constexpr uint32_t CERT_KEY_ALG_SM2_1 = 11;
43 constexpr uint32_t CERT_KEY_ALG_SM2_2 = 12;
44 constexpr uint32_t TEST_USERID = 100;
45 constexpr uint32_t SA_USERID = 0;
46 
47 namespace CertmanagerTest {
48 
49 using namespace OHOS::Security::AccessToken;
50 
51 class MockNativeToken {
52 public:
53     explicit MockNativeToken(const std::string& process);
54     ~MockNativeToken();
55 private:
56     uint64_t selfToken_;
57 };
58 
59 class MockHapToken {
60 public:
61     explicit MockHapToken();
62     ~MockHapToken();
63 private:
64     uint64_t selfToken_;
65     uint32_t mockToken_;
66 };
67 
68 class CmTestCommon {
69 public:
70     static constexpr int32_t DEFAULT_API_VERSION = 12;
71     static void SetTestEnvironment(uint64_t shellTokenId);
72     static void ResetTestEnvironment();
73     static uint64_t GetShellTokenId();
74     static void SetHapInfoAndPolicy(const std::string& bundle, const std::vector<std::string>& reqPerm,
75         bool isSystemApp, HapInfoParams& hapInfo, HapPolicyParams& hapPolicy);
76     static AccessTokenIDEx AllocTestHapToken(
77         const HapInfoParams& hapInfo, HapPolicyParams& hapPolicy);
78     static int32_t DeleteTestHapToken(AccessTokenID tokenID);
79     static AccessTokenID GetNativeTokenIdFromProcess(const std::string& process);
80     static AccessTokenIDEx SetupHapAndAllocateToken();
81 };
82 
83 void FreeCMBlobData(struct CmBlob *blob);
84 
85 void FreeCertList(struct CertList *certList);
86 
87 bool CompareCertInfo(const struct CertInfo *firstCert, const struct CertInfo *secondCert);
88 
89 bool CompareCertData(const struct CmBlob *firstData, const struct CmBlob *secondData);
90 
91 bool CompareCredential(const struct Credential *firstCredential, const struct Credential *secondCredential);
92 
93 bool CompareCredentialList(const struct CredentialAbstract *firstCert, const struct CredentialAbstract *secondCert);
94 
95 int32_t TestGenerateAppCert(const struct CmBlob *alias, uint32_t alg, uint32_t store);
96 
97 std::string DumpCertAbstractInfo(const struct CertAbstract *certAbstract);
98 std::string DumpCertInfo(const struct CertInfo* certInfo);
99 std::string DumpCertList(struct CertList *certList);
100 
101 int32_t InitCertList(struct CertList **cList);
102 
103 int32_t InitUserCertInfo(struct CertInfo **cInfo);
104 
105 int32_t InitCertInfo(struct CertInfo *certInfo);
106 
107 bool FindCertAbstract(const struct CertAbstract *abstract, const struct CertList *cList);
108 
109 void FreeCertInfo(struct CertInfo *cInfo);
110 }
111 #endif /* CM_TEST_COMMON_H */
112