1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
/***************************************************************************
*
* Copyright (C) 2018-2025 - ZmartZone Holding BV
*
* 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.
*
* @Author: Hans Zandbelt - hans.zandbelt@openidc.com
*
**************************************************************************/
#include "check_liboauth2.h"
#include "oauth2/mem.h"
#include "oauth2/util.h"
#include <check.h>
#include <curl/curl.h>
#include <stdlib.h>
static oauth2_mem_alloc_fn_t _save_alloc = NULL;
static oauth2_mem_realloc_fn_t _save_realloc = NULL;
static oauth2_mem_dealloc_fn_t _save_dealloc = NULL;
static void *test_alloc(size_t amt)
{
return malloc(amt);
}
static void *test_realloc(void *ptr, size_t amt)
{
return realloc(ptr, amt);
}
static void test_dealloc(void *ptr)
{
free(ptr);
}
static void test_mem_functions_set()
{
_save_alloc = oauth2_mem_get_alloc();
_save_realloc = oauth2_mem_get_realloc();
_save_dealloc = oauth2_mem_get_dealloc();
oauth2_mem_set_alloc_funcs(test_alloc, test_realloc, test_dealloc);
}
static void test_mem_functions_reset()
{
oauth2_mem_set_alloc_funcs(_save_alloc, _save_realloc, _save_dealloc);
_save_alloc = NULL;
_save_realloc = NULL;
_save_dealloc = NULL;
}
static void *test_alloc3(size_t amt, const char *file, int line)
{
return malloc(amt);
}
static void *test_realloc3(void *ptr, size_t amt, const char *file, int line)
{
return realloc(ptr, amt);
}
static void test_dealloc3(void *ptr, const char *file, int line)
{
free(ptr);
}
static oauth2_mem_alloc3_fn_t _save_alloc3 = NULL;
static oauth2_mem_realloc3_fn_t _save_realloc3 = NULL;
static oauth2_mem_dealloc3_fn_t _save_dealloc3 = NULL;
static void test_mem_functions_set3()
{
_save_alloc3 = oauth2_mem_get_alloc3();
_save_realloc3 = oauth2_mem_get_realloc3();
_save_dealloc3 = oauth2_mem_get_dealloc3();
oauth2_mem_set_alloc_ex_funcs(test_alloc3, test_realloc3,
test_dealloc3);
}
/*
* TODO: why does this result in a timeout?
* probably we can call this only once anyhow, but would it affect the
other check_util tests?
* perhaps separate it out in a different suite then?
static void test_mem_functions_reset3() {
cjose_set_alloc_ex_funcs(_save_alloc3, _save_realloc3, _save_dealloc3);
oauth2_mem_set_alloc_ex_funcs(_save_alloc3, _save_realloc3,
_save_dealloc3);
_save_alloc3 = NULL;
_save_realloc3 = NULL;
_save_dealloc3 = NULL;
}
*/
static oauth2_log_t *_log = 0;
static void setup(void)
{
// provide coverage for oauth2_mem_calloc_callback
// NB: the setup for cURL can only be initialized once and stays this
// way
test_mem_functions_set();
CURL *curl1 = NULL, *curl2 = NULL;
curl1 = curl_easy_init();
curl2 = curl_easy_duphandle(curl1);
curl_easy_cleanup(curl2);
curl_easy_cleanup(curl1);
// for coverage
oauth2_log_free(NULL);
_log = oauth2_init(OAUTH2_LOG_TRACE1, 0);
}
static void teardown(void)
{
oauth2_shutdown(_log);
}
START_TEST(test_log)
{
// mostly to complete coverage
// TODO: could return bytes written from oauth2_log statements
oauth2_debug(NULL, NULL);
// TOOD: could return bool from oauth2_log_sink_add
oauth2_log_sink_add(_log, &oauth2_log_sink_stderr);
oauth2_info(_log, NULL);
oauth2_info(_log, "");
oauth2_log_sink_level_set(&oauth2_log_sink_stderr, OAUTH2_LOG_ERROR);
}
END_TEST
START_TEST(test_mem)
{
void *ptr = NULL;
ck_assert(NULL != oauth2_mem_get_alloc());
ck_assert(NULL != oauth2_mem_get_realloc());
ck_assert(NULL != oauth2_mem_get_dealloc());
ck_assert(NULL != oauth2_mem_get_alloc3());
ck_assert(NULL != oauth2_mem_get_realloc3());
ck_assert(NULL != oauth2_mem_get_dealloc3());
test_mem_functions_set();
ck_assert(NULL != oauth2_mem_get_alloc());
ck_assert(NULL != oauth2_mem_get_realloc());
ck_assert(NULL != oauth2_mem_get_dealloc());
ck_assert(NULL != oauth2_mem_get_alloc3());
ck_assert(NULL != oauth2_mem_get_realloc3());
ck_assert(NULL != oauth2_mem_get_dealloc3());
ptr = oauth2_mem_alloc(8);
ptr = oauth2_mem_get_realloc()(ptr, 8);
oauth2_mem_free(ptr);
test_mem_functions_reset();
ck_assert(NULL != oauth2_mem_get_alloc());
ck_assert(NULL != oauth2_mem_get_realloc());
ck_assert(NULL != oauth2_mem_get_dealloc());
ck_assert(NULL != oauth2_mem_get_alloc3());
ck_assert(NULL != oauth2_mem_get_realloc3());
ck_assert(NULL != oauth2_mem_get_dealloc3());
test_mem_functions_set3();
ck_assert(NULL != oauth2_mem_get_alloc());
ck_assert(NULL != oauth2_mem_get_realloc());
ck_assert(NULL != oauth2_mem_get_dealloc());
ck_assert(NULL != oauth2_mem_get_alloc3());
ck_assert(NULL != oauth2_mem_get_realloc3());
ck_assert(NULL != oauth2_mem_get_dealloc3());
ptr = oauth2_mem_alloc(8);
ptr = oauth2_mem_get_realloc()(ptr, 8);
oauth2_mem_free(ptr);
// test_mem_functions_reset3();
}
END_TEST
START_TEST(test_strdup)
{
char *src = NULL, *dst = NULL;
src = "bla";
dst = NULL;
dst = oauth2_strdup(src);
ck_assert_ptr_ne(dst, NULL);
ck_assert_str_eq(src, dst);
oauth2_mem_free(dst);
src = NULL;
dst = NULL;
dst = oauth2_strdup(src);
ck_assert_ptr_eq(dst, NULL);
oauth2_mem_free(dst);
}
END_TEST
START_TEST(test_base64url_encode)
{
size_t dst_len;
char *dst;
const char *plain = "Node.js is awesome.";
const char *encoded = "Tm9kZS5qcyBpcyBhd2Vzb21lLg";
dst_len = oauth2_base64url_encode(_log, (const uint8_t *)plain,
strlen(plain), &dst);
ck_assert_int_eq(dst_len, strlen(encoded));
ck_assert_str_eq(dst, encoded);
oauth2_mem_free(dst);
dst = NULL;
dst_len = oauth2_base64url_encode(_log, NULL, 0, &dst);
ck_assert_ptr_eq(dst, NULL);
ck_assert_int_eq(dst_len, 0);
dst_len = oauth2_base64url_encode(_log, NULL, 0, NULL);
ck_assert_int_eq(dst_len, 0);
dst_len = oauth2_base64url_encode(_log, (const uint8_t *)"", 0, NULL);
ck_assert_int_eq(dst_len, 0);
}
END_TEST
START_TEST(test_base64url_decode)
{
uint8_t *dst;
size_t dst_len;
bool rc;
const char *encoded = "Tm9kZS5qcyBpcyBhd2Vzb21lLg";
const char *plain = "Node.js is awesome.";
rc = oauth2_base64url_decode(_log, encoded, &dst, &dst_len);
ck_assert_int_eq(rc, true);
ck_assert_int_eq(dst_len, strlen(plain));
ck_assert(strncmp((const char *)dst, plain, dst_len) == 0);
oauth2_mem_free(dst);
dst = NULL;
dst_len = 0;
rc = oauth2_base64url_decode(_log, NULL, &dst, &dst_len);
ck_assert_int_eq(rc, false);
ck_assert_ptr_eq(dst, NULL);
ck_assert_int_eq(dst_len, 0);
rc = oauth2_base64url_decode(_log, NULL, NULL, 0);
ck_assert_int_eq(rc, false);
ck_assert_ptr_eq(dst, NULL);
ck_assert_int_eq(dst_len, 0);
rc = oauth2_base64url_decode(_log, "", NULL, 0);
ck_assert_int_eq(rc, false);
ck_assert_ptr_eq(dst, NULL);
ck_assert_int_eq(dst_len, 0);
}
END_TEST
START_TEST(test_url_encode)
{
char *src = NULL, *dst = NULL, *enc = NULL;
src = "bla bla";
enc = "bla%20bla";
dst = oauth2_url_encode(_log, src);
ck_assert_str_eq(dst, enc);
oauth2_mem_free(dst);
src = "Hello Günter";
enc = "Hello%20G%C3%BCnter";
dst = oauth2_url_encode(_log, src);
ck_assert_str_eq(dst, enc);
oauth2_mem_free(dst);
dst = NULL;
src = NULL;
dst = oauth2_url_encode(_log, src);
ck_assert_ptr_eq(dst, NULL);
}
END_TEST
START_TEST(test_url_decode)
{
char *dst = NULL, *src = NULL, *dec = NULL;
src = "bla%20bla";
dec = "bla bla";
dst = oauth2_url_decode(_log, src);
ck_assert_str_eq(dst, dec);
oauth2_mem_free(dst);
dst = NULL;
src = "http://www.example.com/path/foo+bar/path?query+name=query+value";
dec = "http://www.example.com/path/foo bar/path?query name=query value";
dst = oauth2_url_decode(_log, src);
ck_assert_str_eq(dst, dec);
oauth2_mem_free(dst);
src = "Hello%20G%C3%BCnter";
dec = "Hello Günter";
dst = oauth2_url_decode(_log, src);
ck_assert_str_eq(dst, dec);
oauth2_mem_free(dst);
dst = NULL;
src = NULL;
dst = oauth2_url_decode(_log, src);
ck_assert_ptr_eq(dst, NULL);
}
END_TEST
START_TEST(test_html_encode)
{
char *src = NULL, *dst = NULL, *enc = NULL;
src = "bla&bla";
enc = "bla&bla";
dst = oauth2_html_escape(_log, src);
ck_assert_str_eq(dst, enc);
oauth2_mem_free(dst);
dst = NULL;
// https://www.w3schools.com/php/func_string_htmlentities.asp
src = "<a href=\"https://www.w3schools.com\">Go to w3schools.com</a>";
enc = "<a href="https://www.w3schools.com">Go to "
"w3schools.com</a>";
dst = oauth2_html_escape(_log, src);
ck_assert_str_eq(dst, enc);
oauth2_mem_free(dst);
dst = NULL;
src = NULL;
dst = oauth2_html_escape(_log, src);
ck_assert_ptr_eq(dst, NULL);
}
END_TEST
START_TEST(test_random)
{
char *rv = NULL;
rv = oauth2_rand_str(_log, 8);
ck_assert_ptr_ne(rv, NULL);
ck_assert_str_ne(rv, "");
ck_assert_int_eq(strlen(rv), 8);
oauth2_mem_free(rv);
rv = oauth2_rand_str(_log, 16);
ck_assert_ptr_ne(rv, NULL);
ck_assert_str_ne(rv, "");
ck_assert_int_eq(strlen(rv), 16);
oauth2_mem_free(rv);
rv = oauth2_rand_str(_log, 7);
ck_assert_ptr_ne(rv, NULL);
ck_assert_str_ne(rv, "");
ck_assert_int_eq(strlen(rv), 7);
oauth2_mem_free(rv);
}
END_TEST
Suite *oauth2_check_util_suite()
{
Suite *s = suite_create("util");
TCase *c = tcase_create("core");
tcase_add_checked_fixture(c, setup, teardown);
tcase_add_test(c, test_log);
tcase_add_test(c, test_mem);
tcase_add_test(c, test_strdup);
tcase_add_test(c, test_base64url_encode);
tcase_add_test(c, test_base64url_decode);
tcase_add_test(c, test_url_encode);
tcase_add_test(c, test_url_decode);
tcase_add_test(c, test_html_encode);
tcase_add_test(c, test_random);
suite_add_tcase(s, c);
return s;
}
|