Add tests for ckmc-type-converter.cpp 91/258691/5
authorMichał Szaknis <m.szaknis@samsung.com>
Fri, 21 May 2021 08:18:35 +0000 (10:18 +0200)
committerMichał Szaknis <m.szaknis@samsung.com>
Tue, 25 May 2021 10:42:13 +0000 (12:42 +0200)
Change-Id: Idd90ad6f954ec491d718b955bea2f43624986160

unit-tests/CMakeLists.txt
unit-tests/test_ckmc_converter.cpp [new file with mode: 0644]

index 643ae55..6f8636f 100644 (file)
@@ -68,6 +68,7 @@ SET(UNIT_TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/test_base64.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test_binary-queue.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test_certificate.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/test_ckmc_converter.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test_client-common.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test_comm-manager.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test_crypto-logic.cpp
diff --git a/unit-tests/test_ckmc_converter.cpp b/unit-tests/test_ckmc_converter.cpp
new file mode 100644 (file)
index 0000000..9a9d41e
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ *  Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#include <utility>
+#include "boost_macros_wrapper.h"
+#include "ckmc-type-converter.h"
+
+namespace {
+
+const std::pair<int, int> CKMC_ERRORS[] = {
+    {CKM_API_SUCCESS,                       CKMC_ERROR_NONE},
+    {CKM_API_ERROR_SOCKET,                  CKMC_ERROR_SOCKET},
+    {CKM_API_ERROR_BAD_REQUEST,             CKMC_ERROR_BAD_REQUEST},
+    {CKM_API_ERROR_BAD_RESPONSE,            CKMC_ERROR_BAD_RESPONSE},
+    {CKM_API_ERROR_SEND_FAILED,             CKMC_ERROR_SEND_FAILED},
+    {CKM_API_ERROR_RECV_FAILED,             CKMC_ERROR_RECV_FAILED},
+    {CKM_API_ERROR_AUTHENTICATION_FAILED,   CKMC_ERROR_AUTHENTICATION_FAILED},
+    {CKM_API_ERROR_INPUT_PARAM,             CKMC_ERROR_INVALID_PARAMETER},
+    {CKM_API_ERROR_BUFFER_TOO_SMALL,        CKMC_ERROR_BUFFER_TOO_SMALL},
+    {CKM_API_ERROR_OUT_OF_MEMORY,           CKMC_ERROR_OUT_OF_MEMORY},
+    {CKM_API_ERROR_ACCESS_DENIED,           CKMC_ERROR_PERMISSION_DENIED},
+    {CKM_API_ERROR_SERVER_ERROR,            CKMC_ERROR_SERVER_ERROR},
+    {CKM_API_ERROR_DB_LOCKED,               CKMC_ERROR_DB_LOCKED},
+    {CKM_API_ERROR_DB_ERROR,                CKMC_ERROR_DB_ERROR},
+    {CKM_API_ERROR_DB_ALIAS_EXISTS,         CKMC_ERROR_DB_ALIAS_EXISTS},
+    {CKM_API_ERROR_DB_ALIAS_UNKNOWN,        CKMC_ERROR_DB_ALIAS_UNKNOWN},
+    {CKM_API_ERROR_VERIFICATION_FAILED,     CKMC_ERROR_VERIFICATION_FAILED},
+    {CKM_API_ERROR_INVALID_FORMAT,          CKMC_ERROR_INVALID_FORMAT},
+    {CKM_API_ERROR_FILE_ACCESS_DENIED,      CKMC_ERROR_FILE_ACCESS_DENIED},
+    {CKM_API_ERROR_NOT_EXPORTABLE,          CKMC_ERROR_NOT_EXPORTABLE},
+    {CKM_API_ERROR_FILE_SYSTEM,             CKMC_ERROR_FILE_SYSTEM},
+    {CKM_API_ERROR_NOT_SUPPORTED,           CKMC_ERROR_NOT_SUPPORTED},
+    {CKM_API_ERROR_UNKNOWN,                 CKMC_ERROR_UNKNOWN}
+};
+
+const std::pair<int, int> CKMC_OSCP_STATUSES[] = {
+    {CKM_API_OCSP_STATUS_GOOD,              CKMC_OCSP_STATUS_GOOD},
+    {CKM_API_OCSP_STATUS_UNSUPPORTED,       CKMC_OCSP_ERROR_UNSUPPORTED},
+    {CKM_API_OCSP_STATUS_REVOKED,           CKMC_OCSP_STATUS_REVOKED},
+    {CKM_API_OCSP_STATUS_NET_ERROR,         CKMC_OCSP_ERROR_NET},
+    {CKM_API_OCSP_STATUS_INVALID_URL,       CKMC_OCSP_ERROR_INVALID_URL},
+    {CKM_API_OCSP_STATUS_INVALID_RESPONSE,  CKMC_OCSP_ERROR_INVALID_RESPONSE},
+    {CKM_API_OCSP_STATUS_REMOTE_ERROR,      CKMC_OCSP_ERROR_REMOTE},
+    {CKM_API_OCSP_STATUS_INTERNAL_ERROR,    CKMC_OCSP_ERROR_INTERNAL}
+};
+
+}
+
+BOOST_AUTO_TEST_SUITE(CKMCTypeConverter)
+
+POSITIVE_TEST_CASE(CKMCErrorsAndStatusses) {
+    for (auto & i : CKMC_ERRORS) {
+        BOOST_REQUIRE(to_ckmc_error(i.first) == i.second);
+    }
+
+    for (auto & i : CKMC_OSCP_STATUSES) {
+        BOOST_REQUIRE(to_ckmc_ocsp_status(i.first) == i.second);
+    }
+
+    int permissionMask = 0;
+
+    int status = access_to_permission_mask(CKMC_AR_READ, permissionMask);
+    BOOST_REQUIRE(status == CKMC_ERROR_NONE);
+    BOOST_REQUIRE(permissionMask == CKMC_PERMISSION_READ);
+
+    status = access_to_permission_mask(CKMC_AR_READ_REMOVE, permissionMask);
+    BOOST_REQUIRE(status == CKMC_ERROR_NONE);
+    BOOST_REQUIRE(permissionMask == (CKMC_PERMISSION_READ | CKMC_PERMISSION_REMOVE));
+}
+
+NEGATIVE_TEST_CASE(CKMCErrorsAndStatusses) {
+    const int some_random_value = 0x1eadbeef;
+    BOOST_REQUIRE(to_ckmc_error(some_random_value) == CKMC_ERROR_UNKNOWN);
+    BOOST_REQUIRE(to_ckmc_ocsp_status(some_random_value) == CKMC_OCSP_STATUS_UNKNOWN);
+
+    int permissionMask = 0;
+    int status = access_to_permission_mask(static_cast<ckmc_access_right_e>(some_random_value), permissionMask);
+    BOOST_REQUIRE(status == CKMC_ERROR_INVALID_PARAMETER);
+    BOOST_REQUIRE(permissionMask == 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()