Release 0.1.66
[platform/core/security/key-manager.git] / unit-tests / test_ckmc_converter.cpp
1 /*
2  *  Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 #include <utility>
18 #include "boost_macros_wrapper.h"
19 #include "ckmc-type-converter.h"
20
21 namespace {
22
23 const std::pair<int, int> CKMC_ERRORS[] = {
24     {CKM_API_SUCCESS,                       CKMC_ERROR_NONE},
25     {CKM_API_ERROR_SOCKET,                  CKMC_ERROR_SOCKET},
26     {CKM_API_ERROR_BAD_REQUEST,             CKMC_ERROR_BAD_REQUEST},
27     {CKM_API_ERROR_BAD_RESPONSE,            CKMC_ERROR_BAD_RESPONSE},
28     {CKM_API_ERROR_SEND_FAILED,             CKMC_ERROR_SEND_FAILED},
29     {CKM_API_ERROR_RECV_FAILED,             CKMC_ERROR_RECV_FAILED},
30     {CKM_API_ERROR_AUTHENTICATION_FAILED,   CKMC_ERROR_AUTHENTICATION_FAILED},
31     {CKM_API_ERROR_INPUT_PARAM,             CKMC_ERROR_INVALID_PARAMETER},
32     {CKM_API_ERROR_BUFFER_TOO_SMALL,        CKMC_ERROR_BUFFER_TOO_SMALL},
33     {CKM_API_ERROR_OUT_OF_MEMORY,           CKMC_ERROR_OUT_OF_MEMORY},
34     {CKM_API_ERROR_ACCESS_DENIED,           CKMC_ERROR_PERMISSION_DENIED},
35     {CKM_API_ERROR_SERVER_ERROR,            CKMC_ERROR_SERVER_ERROR},
36     {CKM_API_ERROR_DB_LOCKED,               CKMC_ERROR_DB_LOCKED},
37     {CKM_API_ERROR_DB_ERROR,                CKMC_ERROR_DB_ERROR},
38     {CKM_API_ERROR_DB_ALIAS_EXISTS,         CKMC_ERROR_DB_ALIAS_EXISTS},
39     {CKM_API_ERROR_DB_ALIAS_UNKNOWN,        CKMC_ERROR_DB_ALIAS_UNKNOWN},
40     {CKM_API_ERROR_VERIFICATION_FAILED,     CKMC_ERROR_VERIFICATION_FAILED},
41     {CKM_API_ERROR_INVALID_FORMAT,          CKMC_ERROR_INVALID_FORMAT},
42     {CKM_API_ERROR_FILE_ACCESS_DENIED,      CKMC_ERROR_FILE_ACCESS_DENIED},
43     {CKM_API_ERROR_NOT_EXPORTABLE,          CKMC_ERROR_NOT_EXPORTABLE},
44     {CKM_API_ERROR_FILE_SYSTEM,             CKMC_ERROR_FILE_SYSTEM},
45     {CKM_API_ERROR_NOT_SUPPORTED,           CKMC_ERROR_NOT_SUPPORTED},
46     {CKM_API_ERROR_UNKNOWN,                 CKMC_ERROR_UNKNOWN}
47 };
48
49 const std::pair<int, int> CKMC_OSCP_STATUSES[] = {
50     {CKM_API_OCSP_STATUS_GOOD,              CKMC_OCSP_STATUS_GOOD},
51     {CKM_API_OCSP_STATUS_UNSUPPORTED,       CKMC_OCSP_ERROR_UNSUPPORTED},
52     {CKM_API_OCSP_STATUS_REVOKED,           CKMC_OCSP_STATUS_REVOKED},
53     {CKM_API_OCSP_STATUS_NET_ERROR,         CKMC_OCSP_ERROR_NET},
54     {CKM_API_OCSP_STATUS_INVALID_URL,       CKMC_OCSP_ERROR_INVALID_URL},
55     {CKM_API_OCSP_STATUS_INVALID_RESPONSE,  CKMC_OCSP_ERROR_INVALID_RESPONSE},
56     {CKM_API_OCSP_STATUS_REMOTE_ERROR,      CKMC_OCSP_ERROR_REMOTE},
57     {CKM_API_OCSP_STATUS_INTERNAL_ERROR,    CKMC_OCSP_ERROR_INTERNAL}
58 };
59
60 }
61
62 BOOST_AUTO_TEST_SUITE(CKMCTypeConverter)
63
64 POSITIVE_TEST_CASE(CKMCErrorsAndStatusses) {
65     for (auto & i : CKMC_ERRORS) {
66         BOOST_REQUIRE(to_ckmc_error(i.first) == i.second);
67     }
68
69     for (auto & i : CKMC_OSCP_STATUSES) {
70         BOOST_REQUIRE(to_ckmc_ocsp_status(i.first) == i.second);
71     }
72
73     int permissionMask = 0;
74
75     int status = access_to_permission_mask(CKMC_AR_READ, permissionMask);
76     BOOST_REQUIRE(status == CKMC_ERROR_NONE);
77     BOOST_REQUIRE(permissionMask == CKMC_PERMISSION_READ);
78
79     status = access_to_permission_mask(CKMC_AR_READ_REMOVE, permissionMask);
80     BOOST_REQUIRE(status == CKMC_ERROR_NONE);
81     BOOST_REQUIRE(permissionMask == (CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE));
82 }
83
84 NEGATIVE_TEST_CASE(CKMCErrorsAndStatusses) {
85     const int some_random_value = 0x1eadbeef;
86     BOOST_REQUIRE(to_ckmc_error(some_random_value) == CKMC_ERROR_UNKNOWN);
87     BOOST_REQUIRE(to_ckmc_ocsp_status(some_random_value) == CKMC_OCSP_STATUS_UNKNOWN);
88
89     int permissionMask = 0;
90     int status = access_to_permission_mask(static_cast<ckmc_access_right_e>(some_random_value), permissionMask);
91     BOOST_REQUIRE(status == CKMC_ERROR_INVALID_PARAMETER);
92     BOOST_REQUIRE(permissionMask == 0);
93 }
94
95 BOOST_AUTO_TEST_SUITE_END()