Migrate to openssl3
[platform/core/security/yaca.git] / tests / test_debug.cpp
1 /*
2  *  Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Lukasz Pawelczyk <l.pawelczyk@samsung.com>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18
19 /**
20  * @file    test_debug.cpp
21  * @author  Lukasz Pawelczyk <l.pawelczyk@samsung.com>
22  * @brief   Debug internal API unit tests.
23  */
24
25 #include <boost/test/unit_test.hpp>
26 #include <string>
27 #include <vector>
28 #include <iostream>
29
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>
35
36 #include <yaca_error.h>
37 #include "../src/debug.h"
38 #include "common.h"
39
40
41 namespace {
42
43 int error_cb_called = 0;
44
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";
49
50 char last_buf[BUF_SIZE] = {};
51
52 void debug_error_cb(const char *buf)
53 {
54         ++error_cb_called;
55
56         BOOST_REQUIRE(strlen(buf) < BUF_SIZE);
57         memcpy(last_buf, buf, BUF_SIZE);
58 }
59
60 struct CallbackCleanup
61 {
62         CallbackCleanup()
63         {
64                 error_cb_called = 0;
65         }
66         ~CallbackCleanup()
67         {
68                 yaca_debug_set_error_cb(NULL);
69         }
70 };
71
72 }
73
74 BOOST_AUTO_TEST_SUITE(TESTS_DEBUG)
75
76 BOOST_AUTO_TEST_CASE(T001__positive__translate_error)
77 {
78         struct error_args {
79                 yaca_error_e err;
80                 std::string msg;
81         };
82
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"},
91         };
92
93         for (const auto &ea: eargs) {
94                 std::string ret = yaca_debug_translate_error(ea.err);
95                 BOOST_REQUIRE(ret == ea.msg);
96         }
97 }
98
99 BOOST_FIXTURE_TEST_CASE(T002__positive__debug_set_error_cb, CallbackCleanup)
100 {
101         ERROR_DUMP(YACA_ERROR_INTERNAL);
102         BOOST_REQUIRE(error_cb_called == 0);
103
104         yaca_debug_set_error_cb(&debug_error_cb);
105         ERROR_DUMP(YACA_ERROR_INTERNAL);
106         BOOST_REQUIRE(error_cb_called == 1);
107
108         ERROR_DUMP(YACA_ERROR_INTERNAL);
109         BOOST_REQUIRE(error_cb_called == 2);
110
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);
115 }
116
117 BOOST_FIXTURE_TEST_CASE(T003__positive__error_dump, CallbackCleanup)
118 {
119         yaca_debug_set_error_cb(&debug_error_cb);
120
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);
129
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);
143
144         std::string ret(last_buf);
145         std::string ellipsis = ret.substr(ret.size() - strlen(ELLIPSIS));
146         BOOST_REQUIRE(ellipsis == ELLIPSIS);
147 }
148
149 BOOST_FIXTURE_TEST_CASE(T004__positive__error_handle, CallbackCleanup)
150 {
151         struct error_args {
152                 long err1;
153                 long err2;
154                 yaca_error_e expected;
155                 int cb_called;
156         };
157
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},
192         };
193
194         yaca_debug_set_error_cb(&debug_error_cb);
195
196         for (const auto &ea: eargs) {
197                 error_cb_called = 0;
198
199                 if (ea.err1 != -1) {
200                         ERR_raise(ERR_GET_LIB(ea.err1), ERR_GET_REASON(ea.err1));
201                 }
202                 if (ea.err2 != -1) {
203                         ERR_raise(ERR_GET_LIB(ea.err2), ERR_GET_REASON(ea.err2));
204                 }
205
206                 int ret = ERROR_HANDLE();
207
208                 BOOST_REQUIRE(ret == ea.expected);
209                 BOOST_REQUIRE(error_cb_called == ea.cb_called);
210         }
211
212 }
213
214 BOOST_AUTO_TEST_SUITE_END()