Hash :
9dbdc919
Author :
Date :
2018-10-07T09:19:48
Replace a few sprintf calls with smtp_stpcpy, a portable version of stpcpy. Some sprintf implementations might fail from OOM condition.
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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
/**
* @file
* @brief Test the smtp-client library.
* @author James Humphrey (mail@somnisoft.com)
* @version 1.00
*
* This smtp-client testing framework has 100% branch coverage on POSIX
* systems. It requires a Postfix SMTP server that supports all of the
* connection security and authentication methods. These functional tests
* also require the user to manually check and ensure that the destination
* addresses received all of the test emails.
*
* This software has been placed into the public domain using CC0.
*
* @section test_seams_countdown_global
*
* The test harnesses control most of the test seams through the use of
* global counter values.
*
* Setting a global counter to -1 will make the test seam function operate
* as it normally would. If set to a positive value, the value will continue
* to decrement every time the function gets called. When the counter reaches
* 0, the test seam will force the function to return an error value.
*
* For example, initially setting the counter to 0 will force the test seam
* to return an error condition the first time it gets called. Setting the
* value to 1 initially will force the test seam to return an error condition
* on the second time it gets called.
*/
#ifndef SMTP_TEST_H
#define SMTP_TEST_H
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "../src/smtp.h"
#ifdef SMTP_OPENSSL
# include <openssl/bio.h>
# include <openssl/err.h>
# include <openssl/ssl.h>
# include <openssl/x509.h>
# include <openssl/x509v3.h>
#endif /* SMTP_OPENSSL */
struct smtp_command;
struct str_getdelimfd;
/**
* Maximum size of an RFC 2822 date string.
*
* Also defined in @ref smtp.c. Redefined here because the unit tests
* need to use this max size when calling the @ref smtp_date_rfc_2822 function.
*/
#define SMTP_DATE_MAX_SZ 32
ssize_t
smtp_base64_decode(const char *const buf,
unsigned char **decode);
char *
smtp_base64_encode(const char *const buf,
ssize_t buflen);
char *
smtp_bin2hex(const unsigned char *const s,
size_t slen);
int
smtp_str_getdelimfd(struct str_getdelimfd *const gdfd);
void
smtp_str_getdelimfd_free(struct str_getdelimfd *const gdfd);
char *
smtp_stpcpy(char *s1,
const char *s2);
char *
smtp_strdup(const char *s);
char *
smtp_str_replace(const char *const search,
const char *const replace,
const char *const s);
int
smtp_utf8_charlen(unsigned char c);
int
smtp_str_has_nonascii_utf8(const char *const s);
ssize_t
smtp_strnlen_utf8(const char *s,
size_t maxlen);
char *
smtp_chunk_split(const char *const s,
int chunklen,
const char *const end);
char *
smtp_ffile_get_contents(FILE *stream,
size_t *bytes_read);
char *
smtp_file_get_contents(const char *const filename,
size_t *bytes_read);
int
smtp_parse_cmd_line(char *const line,
struct smtp_command *const cmd);
int
smtp_date_rfc_2822(char *const date);
int
smtp_address_validate_email(const char *const email);
int
smtp_address_validate_name(const char *const name);
int
smtp_attachment_validate_name(const char *const name);
int
smtp_header_key_validate(const char *const key);
int
smtp_header_value_validate(const char *const value);
/* test seams */
BIO *
smtp_test_seam_bio_new_socket(int sock,
int close_flag);
int
smtp_test_seam_bio_should_retry(BIO *bio);
void *
smtp_test_seam_calloc(size_t nelem,
size_t elsize);
int
smtp_test_seam_close(int fildes);
int
smtp_test_seam_connect(int socket,
const struct sockaddr *address,
socklen_t address_len);
unsigned long
smtp_test_seam_err_peek_error(void);
int
smtp_test_seam_fclose(FILE *stream);
int
smtp_test_seam_ferror(FILE *stream);
struct tm *
smtp_test_seam_gmtime_r(const time_t *timep,
struct tm *result);
unsigned char *
smtp_test_seam_hmac(const EVP_MD *evp_md,
const void *key,
int key_len,
const unsigned char *d,
int n,
unsigned char *md,
unsigned int *md_len);
struct tm *
smtp_test_seam_localtime_r(const time_t *timep,
struct tm *result);
void *
smtp_test_seam_malloc(size_t size);
time_t
smtp_test_seam_mktime(struct tm *timeptr);
void *
smtp_test_seam_realloc(void *ptr,
size_t size);
ssize_t
smtp_test_seam_recv(int socket,
void *buffer,
size_t length,
int flags);
int
smtp_test_seam_select(int nfds,
fd_set *readfds,
fd_set *writefds,
fd_set *errorfds,
struct timeval *timeout);
ssize_t
smtp_test_seam_send(int socket,
const void *buffer,
size_t length,
int flags);
int
smtp_test_seam_socket(int domain,
int type,
int protocol);
int
smtp_test_seam_ssl_connect(SSL *ssl);
SSL_CTX *
smtp_test_seam_ssl_ctx_new(const SSL_METHOD *method);
int
smtp_test_seam_ssl_do_handshake(SSL *ssl);
X509 *
smtp_test_seam_ssl_get_peer_certificate(const SSL *ssl);
int
smtp_test_seam_x509_check_host(X509 *cert,
const char *name,
size_t namelen,
unsigned int flags,
char **peername);
SSL *
smtp_test_seam_ssl_new(SSL_CTX *ctx);
int
smtp_test_seam_ssl_read(SSL *ssl,
void *buf,
int num);
int
smtp_test_seam_ssl_write(SSL *ssl,
const void *buf,
int num);
int
smtp_test_seam_sprintf(char *s,
const char *format, ...);
time_t
smtp_test_seam_time(time_t *tloc);
/**
* Counter for @ref smtp_test_seam_bio_new_socket.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_bio_new_socket_ctr;
/**
* Counter for @ref smtp_test_seam_bio_should_retry.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_bio_should_retry_ctr;
/**
* Value to force the BIO_should_retry() function to return.
*
* This value will only get returned if
* @ref g_smtp_test_err_bio_should_retry_ctr
* has a value of 0 and this does not have a value of -1.
*/
int g_smtp_test_err_bio_should_retry_rc;
/**
* Counter for @ref smtp_test_seam_calloc.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_calloc_ctr;
/**
* Counter for @ref smtp_test_seam_close.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_close_ctr;
/**
* Counter for @ref smtp_test_seam_connect.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_connect_ctr;
/**
* Counter for @ref smtp_test_seam_err_peek_error.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_err_peek_error_ctr;
/**
* Counter for @ref smtp_test_seam_fclose.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_fclose_ctr;
/**
* Counter for @ref smtp_test_seam_ferror.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_ferror_ctr;
/**
* Counter for @ref smtp_test_seam_gmtime_r.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_gmtime_r_ctr;
/**
* Counter for @ref smtp_test_seam_hmac.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_hmac_ctr;
/**
* Counter for @ref smtp_test_seam_localtime_r.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_localtime_r_ctr;
/**
* Counter for @ref smtp_test_seam_malloc.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_malloc_ctr;
/**
* Counter for @ref smtp_test_seam_mktime.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_mktime_ctr;
/**
* Counter for @ref smtp_test_seam_realloc.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_realloc_ctr;
/**
* Counter for @ref smtp_test_seam_recv.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_recv_ctr;
/**
* Value to force the recv() function to return.
*
* This value will only get returned if
* @ref g_smtp_test_err_recv_ctr
* has a value of 0 and this does not have a value of -1.
*/
int g_smtp_test_err_recv_rc;
/**
* Set the received bytes in recv() and SSL_read() to this value if it
* contains a null-terminated string at least one bytes long.
*
* This makes it easier to inject a bad server response for testing the
* smtp-client handling of those bad responses.
*
* See @ref test_seams_countdown_global for more details.
*/
char g_smtp_test_err_recv_bytes[90];
/**
* Counter for @ref smtp_test_seam_select.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_select_ctr;
/**
* Counter for @ref smtp_test_seam_send.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_send_ctr;
/**
* Counter for @ref smtp_test_seam_socket.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_socket_ctr;
/**
* Counter for @ref smtp_test_seam_ssl_connect.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_ssl_connect_ctr;
/**
* Counter for @ref smtp_test_seam_ssl_ctx_new.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_ssl_ctx_new_ctr;
/**
* Counter for @ref smtp_test_seam_ssl_do_handshake.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_ssl_do_handshake_ctr;
/**
* Counter for @ref smtp_test_seam_ssl_get_peer_certificate.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_ssl_get_peer_certificate_ctr;
/**
* Counter for @ref smtp_test_seam_x509_check_host.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_x509_check_host_ctr;
/**
* Counter for @ref smtp_test_seam_ssl_new.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_ssl_new_ctr;
/**
* Counter for @ref smtp_test_seam_ssl_read.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_ssl_read_ctr;
/**
* Counter for @ref smtp_test_seam_ssl_write.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_ssl_write_ctr;
/**
* Counter for @ref smtp_test_seam_sprintf.
*
* See @ref test_seams_countdown_global for more details.
*/
int g_smtp_test_err_sprintf_ctr;
/**
* Value to force the sprintf() function to return.
*
* This value will only get returned if @ref g_smtp_test_err_sprintf_ctr has
* a value of 0.
*/
int g_smtp_test_err_sprintf_rc;
/**
* Indicates if the time() function should return a custom value.
*
* This can get set to one of two values:
* - 0 - The time() function will operate normally.
* - !0 - The time() function will return the value specified in
* @ref g_smtp_test_time_ret_value.
*/
int g_smtp_test_time_custom_ret;
/**
* Value to force the time() function to return.
*
* This value will only get returned if @ref g_smtp_test_time_custom_ret has
* a positive value.
*/
time_t g_smtp_test_time_ret_value;
#endif /* SMTP_TEST_H */