2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Lukasz Pawelczyk <l.pawelczyk@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
20 * @file test_debug.cpp
21 * @author Lukasz Pawelczyk <l.pawelczyk@samsung.com>
22 * @brief Debug internal API unit tests.
25 #include <boost/test/unit_test.hpp>
30 #include <openssl/err.h>
31 #include <openssl/rsa.h>
32 #include <openssl/dsa.h>
33 #include <openssl/pem.h>
34 #include <openssl/ec.h>
36 #include <yaca_error.h>
37 #include "../src/debug.h"
43 int error_cb_called = 0;
45 /* Has to be the same as BUF_SIZE in error_dump */
46 const size_t BUF_SIZE = 512;
47 /* Has to be the same as ELLIPSIS in error_dump */
48 const char ELLIPSIS[] = "...\n";
50 char last_buf[BUF_SIZE] = {};
52 void debug_error_cb(const char *buf)
56 BOOST_REQUIRE(strlen(buf) < BUF_SIZE);
57 memcpy(last_buf, buf, BUF_SIZE);
60 struct CallbackCleanup
68 yaca_debug_set_error_cb(NULL);
74 BOOST_AUTO_TEST_SUITE(TESTS_DEBUG)
76 BOOST_AUTO_TEST_CASE(T001__positive__translate_error)
83 const std::vector<struct error_args> eargs = {
84 {YACA_INVALID_ERROR, "Error not defined"},
85 {YACA_ERROR_NONE, "YACA_ERROR_NONE"},
86 {YACA_ERROR_INVALID_PARAMETER, "YACA_ERROR_INVALID_PARAMETER"},
87 {YACA_ERROR_OUT_OF_MEMORY, "YACA_ERROR_OUT_OF_MEMORY"},
88 {YACA_ERROR_INTERNAL, "YACA_ERROR_INTERNAL"},
89 {YACA_ERROR_DATA_MISMATCH, "YACA_ERROR_DATA_MISMATCH"},
90 {YACA_ERROR_INVALID_PASSWORD, "YACA_ERROR_INVALID_PASSWORD"},
93 for (const auto &ea: eargs) {
94 std::string ret = yaca_debug_translate_error(ea.err);
95 BOOST_REQUIRE(ret == ea.msg);
99 BOOST_FIXTURE_TEST_CASE(T002__positive__debug_set_error_cb, CallbackCleanup)
101 ERROR_DUMP(YACA_ERROR_INTERNAL);
102 BOOST_REQUIRE(error_cb_called == 0);
104 yaca_debug_set_error_cb(&debug_error_cb);
105 ERROR_DUMP(YACA_ERROR_INTERNAL);
106 BOOST_REQUIRE(error_cb_called == 1);
108 ERROR_DUMP(YACA_ERROR_INTERNAL);
109 BOOST_REQUIRE(error_cb_called == 2);
111 yaca_debug_set_error_cb(NULL);
112 ERROR_DUMP(YACA_ERROR_INTERNAL);
113 ERROR_DUMP(YACA_ERROR_INTERNAL);
114 BOOST_REQUIRE(error_cb_called == 2);
117 BOOST_FIXTURE_TEST_CASE(T003__positive__error_dump, CallbackCleanup)
119 yaca_debug_set_error_cb(&debug_error_cb);
121 /* I won't check the string that's been generated. It's too
122 * volatile. Some regexp can be implemented at most. */
123 ERR_raise(ERR_LIB_PEM, PEM_R_READ_KEY);
124 ERROR_DUMP(YACA_ERROR_INTERNAL);
125 BOOST_REQUIRE(error_cb_called == 1);
126 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
127 ERROR_DUMP(-1 * YACA_ERROR_INTERNAL);
128 BOOST_REQUIRE(error_cb_called == 2);
130 /* The check that makes sense though is ellipsis. Also it'll
131 * trigger the ellipsis code so it's at least a crash check.
132 * No, those errors don't have to make any sense. */
133 ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
134 ERR_raise(ERR_LIB_RSA, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
135 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
136 ERR_raise(ERR_LIB_RSA, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
137 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
138 ERR_raise(ERR_LIB_RSA, RSA_R_WRONG_SIGNATURE_LENGTH);
139 ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
140 ERR_raise(ERR_LIB_RSA, RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
141 ERROR_DUMP(YACA_ERROR_INTERNAL);
142 BOOST_REQUIRE(error_cb_called == 3);
144 std::string ret(last_buf);
145 std::string ellipsis = ret.substr(ret.size() - strlen(ELLIPSIS));
146 BOOST_REQUIRE(ellipsis == ELLIPSIS);
149 BOOST_FIXTURE_TEST_CASE(T004__positive__error_handle, CallbackCleanup)
154 yaca_error_e expected;
158 const std::vector<struct error_args> eargs = {
159 {-1, -1, YACA_ERROR_INTERNAL, 0},
160 {ERR_PACK(ERR_LIB_RSA, 0, RSA_R_DATA_GREATER_THAN_MOD_LEN),
161 -1, YACA_ERROR_INVALID_PARAMETER, 0},
162 {ERR_PACK(ERR_LIB_RSA, 0, RSA_R_KEY_SIZE_TOO_SMALL),
163 -1, YACA_ERROR_INVALID_PARAMETER, 0},
164 {ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_TOO_LONG),
165 ERR_PACK(ERR_LIB_RSA, 0, ERR_R_RSA_LIB),
166 YACA_ERROR_INVALID_PASSWORD, 0},
167 {ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_HEADER_TOO_LONG),
168 ERR_PACK(ERR_LIB_RSA, 0, RSA_R_DIGEST_NOT_ALLOWED),
169 YACA_ERROR_INVALID_PARAMETER, 0},
170 {ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_PASSWORD_READ),
171 -1, YACA_ERROR_INVALID_PASSWORD, 0},
172 {ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_DECRYPT),
173 -1, YACA_ERROR_INVALID_PASSWORD, 0},
174 {ERR_PACK(ERR_LIB_RSA, 0, RSA_R_BLOCK_TYPE_IS_NOT_01),
175 -1, YACA_ERROR_INVALID_PARAMETER, 0},
176 {ERR_PACK(ERR_LIB_EC, 0, ERR_R_MALLOC_FAILURE),
177 -1, YACA_ERROR_OUT_OF_MEMORY, 0},
178 {ERR_PACK(ERR_LIB_EC, 0, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED),
179 -1, YACA_ERROR_INVALID_PARAMETER, 0},
180 {ERR_PACK(ERR_LIB_EC, 0, ERR_R_PASSED_NULL_PARAMETER),
181 -1, YACA_ERROR_INVALID_PARAMETER, 0},
182 {ERR_PACK(ERR_LIB_SSL, 0, ERR_R_INTERNAL_ERROR),
183 -1, YACA_ERROR_INTERNAL, 0},
184 {ERR_PACK(ERR_LIB_EC, 0, ERR_R_DISABLED),
185 -1, YACA_ERROR_INTERNAL, 0},
186 {ERR_PACK(ERR_LIB_RSA, 0, RSA_R_BAD_SIGNATURE),
187 -1, YACA_ERROR_INTERNAL, 1},
188 {ERR_PACK(ERR_LIB_DSA, 0, DSA_R_BN_ERROR),
189 -1, YACA_ERROR_INTERNAL, 1},
190 {ERR_PACK(ERR_LIB_EC, 0, EC_R_SLOT_FULL),
191 -1, YACA_ERROR_INTERNAL, 1},
194 yaca_debug_set_error_cb(&debug_error_cb);
196 for (const auto &ea: eargs) {
200 ERR_raise(ERR_GET_LIB(ea.err1), ERR_GET_REASON(ea.err1));
203 ERR_raise(ERR_GET_LIB(ea.err2), ERR_GET_REASON(ea.err2));
206 int ret = ERROR_HANDLE();
208 BOOST_REQUIRE(ret == ea.expected);
209 BOOST_REQUIRE(error_cb_called == ea.cb_called);
214 BOOST_AUTO_TEST_SUITE_END()