1 /*
2  * Copyright (c) 2023 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 "cf_test_sdk_common.h"
17 
18 #include <string>
19 #include <iostream>
20 
21 #include "cf_api.h"
22 #include "cf_param.h"
23 #include "cf_result.h"
24 
TestConstructParamSetIn(const CfParam * params,uint32_t cnt,CfParamSet ** paramSet)25 int32_t CertframeworkSdkTest::TestConstructParamSetIn(const CfParam *params, uint32_t cnt, CfParamSet **paramSet)
26 {
27     CfParamSet *tmp = NULL;
28     int32_t ret = CfInitParamSet(&tmp);
29     if (ret != CF_SUCCESS) {
30         return ret;
31     }
32 
33     ret = CfAddParams(tmp, params, cnt);
34     if (ret != CF_SUCCESS) {
35         CfFreeParamSet(&tmp);
36         return ret;
37     }
38 
39     ret = CfBuildParamSet(&tmp);
40     if (ret != CF_SUCCESS) {
41         CfFreeParamSet(&tmp);
42         return ret;
43     }
44 
45     *paramSet = tmp;
46     return CF_SUCCESS;
47 }
48 
CommonTest(CfObjectType type,const CfEncodingBlob * inData,const CfParam * params,uint32_t cnt,CfParamSet ** outParamSet)49 int32_t CertframeworkSdkTest::CommonTest(CfObjectType type, const CfEncodingBlob *inData,
50     const CfParam *params, uint32_t cnt, CfParamSet **outParamSet)
51 {
52     CfObject *object = nullptr;
53     int32_t ret = CfCreate(type, inData, &object);
54     if (ret != CF_SUCCESS) {
55         return ret;
56     }
57 
58     CfParamSet *inParamSet = nullptr;
59     ret = TestConstructParamSetIn(params, cnt, &inParamSet);
60     if (ret != CF_SUCCESS) {
61         object->destroy(&object);
62         return ret;
63     }
64 
65     if (params[0].tag == CF_TAG_GET_TYPE) {
66         ret = object->get(object, inParamSet, outParamSet);
67     } else {
68         ret = object->check(object, inParamSet, outParamSet);
69     }
70 
71     object->destroy(&object);
72     CfFreeParamSet(&inParamSet);
73     return ret;
74 }
75 
AbnormalTest(CfObjectType objType,const CfEncodingBlob * in,const CfParam * params,uint32_t cnt,int32_t optype)76 int32_t CertframeworkSdkTest::AbnormalTest(CfObjectType objType, const CfEncodingBlob *in,
77     const CfParam *params, uint32_t cnt, int32_t optype)
78 {
79     CfObject *abnormalObject = nullptr;
80     int32_t ret = CfCreate(objType, in, &abnormalObject);
81     if (ret != CF_SUCCESS) {
82         return ret;
83     }
84 
85     CfParamSet *inParamSet = nullptr;
86     ret = TestConstructParamSetIn(params, cnt, &inParamSet);
87     if (ret != CF_SUCCESS) {
88         abnormalObject->destroy(&abnormalObject);
89         return ret;
90     }
91 
92     CfParamSet *outParamSet = nullptr;
93     if (optype == OP_TYPE_CHECK) {
94         ret = abnormalObject->check(abnormalObject, inParamSet, &outParamSet);
95     } else if (optype == OP_TYPE_GET) {
96         ret = abnormalObject->get(abnormalObject, inParamSet, &outParamSet);
97     }
98 
99     abnormalObject->destroy(&abnormalObject);
100     CfFreeParamSet(&inParamSet);
101     if (ret == CF_SUCCESS) {
102         return CF_NOT_SUPPORT; /* expect not success */
103     }
104 
105     return CF_SUCCESS;
106 }
107 
108 
109 #ifdef TEST_PRINT_DATA
print_blob(const uint8_t * data,uint32_t len)110 static void print_blob(const uint8_t *data, uint32_t len)
111 {
112     printf("len %u", len);
113     for (uint32_t i = 0; i < len; i++) {
114         if ((i % 16) == 0) { /* Line breaks every 16 characters */
115             printf("\n");
116         }
117         printf("0x%02x, ", data[i]);
118     }
119     printf("\r\n");
120 }
121 
GetOutValue(const CfParamSet * resultParamSet)122 int32_t CertframeworkSdkTest::GetOutValue(const CfParamSet *resultParamSet)
123 {
124     CfParam *param = NULL;
125     int32_t ret = CfGetParam(resultParamSet, CF_TAG_RESULT_TYPE, &param);
126     if (ret != CF_SUCCESS) {
127         std::cout << "get CF_TAG_RESULT_TYPE failed" << std::endl;
128         return ret;
129     }
130 
131     int32_t type = param->int32Param;
132     CfParam *paramOut = NULL;
133     switch (type) {
134         case CF_TAG_TYPE_INT:
135             ret = CfGetParam(resultParamSet, CF_TAG_RESULT_INT, &paramOut);
136             if (ret == CF_SUCCESS) {
137                 std::cout << "value: " << paramOut->int32Param << std::endl;
138             }
139             break;
140         case CF_TAG_TYPE_UINT:
141             ret = CfGetParam(resultParamSet, CF_TAG_RESULT_UINT, &paramOut);
142             if (ret == CF_SUCCESS) {
143                 std::cout << "value: " << paramOut->uint32Param << std::endl;
144             }
145             break;
146         case CF_TAG_TYPE_ULONG:
147             ret = CfGetParam(resultParamSet, CF_TAG_RESULT_ULONG, &paramOut);
148             if (ret == CF_SUCCESS) {
149                 std::cout << "value: " << paramOut->uint64Param << std::endl;
150             }
151             break;
152         case CF_TAG_TYPE_BOOL:
153             ret = CfGetParam(resultParamSet, CF_TAG_RESULT_BOOL, &paramOut);
154             if (ret == CF_SUCCESS) {
155                 std::cout << "value: " << paramOut->boolParam << std::endl;
156             }
157             break;
158         case CF_TAG_TYPE_BYTES:
159             for (uint32_t i = 0; i < resultParamSet->paramsCnt; i++) {
160                 if (CfGetTagType((CfTag)(resultParamSet->params[i].tag)) == CF_TAG_TYPE_BYTES) {
161                     std::cout << "i: " << i << " ";
162                     print_blob(resultParamSet->params[i].blob.data, resultParamSet->params[i].blob.size);
163                 }
164             }
165             break;
166         default:
167             ret = CF_INVALID_PARAMS;
168             break;
169     }
170     return ret;
171 }
172 #endif
173 
174