smtp-client
SMTP Client C Library
seams.c
Go to the documentation of this file.
1 
13 #include <assert.h>
14 #include <errno.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #include <unistd.h>
21 
22 #include "test.h"
23 
29 
35 
40 
45 
50 
55 
61 
66 
71 
76 
81 
87 
92 
97 
102 
107 
112 
117 
122 
127 
132 
138 
144 
150 
155 
161 
167 
173 
179 
185 
190 
195 
200 
205 
210 
215 
220 
225 
230 
243 int
244 smtp_test_seam_dec_err_ctr(int *const test_err_ctr){
245  if(*test_err_ctr >= 0){
246  *test_err_ctr -= 1;
247  if(*test_err_ctr < 0){
248  return 1;
249  }
250  }
251  return 0;
252 }
253 
262 BIO *
264  int close_flag){
266  return NULL;
267  }
268  return BIO_new_socket(sock, close_flag);
269 }
270 
278 int
281  return 0;
282  }
285  }
286  return BIO_should_retry(bio);
287 }
288 
297 void *
299  size_t elsize){
301  errno = ENOMEM;
302  return NULL;
303  }
304  return calloc(nelem, elsize);
305 }
306 
314 int
317  errno = EBADF;
318  return -1;
319  }
320  return close(fildes);
321 }
322 
332 int
334  const struct sockaddr *address,
335  socklen_t address_len){
337  errno = ECONNREFUSED;
338  return -1;
339  }
340  return connect(socket, address, address_len);
341 }
342 
350 unsigned long
353  return 1;
354  }
355  return ERR_peek_error();
356 }
357 
365 int smtp_test_seam_fclose(FILE *stream){
367  errno = EBADF;
368  return EOF;
369  }
370  return fclose(stream);
371 }
372 
381 int
382 smtp_test_seam_ferror(FILE *stream){
384  return 1;
385  }
386  return ferror(stream);
387 }
388 
398 struct tm *
399 smtp_test_seam_gmtime_r(const time_t *timep,
400  struct tm *result){
402  return NULL;
403  }
404  return gmtime_r(timep, result);
405 }
406 
420 unsigned char *
421 smtp_test_seam_hmac(const EVP_MD *evp_md,
422  const void *key,
423  int key_len,
424  const unsigned char *d,
425  size_t n,
426  unsigned char *md,
427  unsigned int *md_len){
429  return NULL;
430  }
431  return HMAC(evp_md, key, key_len, d, n, md, md_len);
432 }
433 
443 struct tm *
444 smtp_test_seam_localtime_r(const time_t *timep,
445  struct tm *result){
447  return NULL;
448  }
449  return localtime_r(timep, result);
450 }
451 
459 void *
462  errno = ENOMEM;
463  return NULL;
464  }
465  return malloc(size);
466 }
467 
475 time_t
476 smtp_test_seam_mktime(struct tm *timeptr){
478  return -1;
479  }
480  return mktime(timeptr);
481 }
482 
492 void *
494  size_t size){
496  errno = ENOMEM;
497  return NULL;
498  }
499  return realloc(ptr, size);
500 }
501 
512 long
514  void *buffer,
515  size_t length,
516  int flags){
517  size_t bytes_inject_len;
518 
520  if(g_smtp_test_err_recv_rc != -1){
522  }
524  bytes_inject_len = strlen(g_smtp_test_err_recv_bytes);
525  assert(bytes_inject_len < length && bytes_inject_len < LONG_MAX);
526  memcpy(buffer, g_smtp_test_err_recv_bytes, bytes_inject_len);
527  return (long)bytes_inject_len;
528  }
529  errno = EBADF;
530  return -1;
531  }
532  return recv(socket, buffer, length, flags);
533 }
534 
551 int
553  fd_set *readfds,
554  fd_set *writefds,
555  fd_set *errorfds,
556  struct timeval *timeout){
558  errno = EINTR;
559  return -1;
560  }
561  return select(nfds, readfds, writefds, errorfds, timeout);
562 }
563 
574 ssize_t
576  const void *buffer,
577  size_t length,
578  int flags){
579  long sent_bytes;
580  size_t bytes_to_send;
581 
583  errno = EBADF;
584  sent_bytes = -1;
585  }
586  else{
587  bytes_to_send = length;
589  bytes_to_send = 1;
590  }
591  sent_bytes = send(socket, buffer, bytes_to_send, flags);
592  }
593  return sent_bytes;
594 }
595 
605 int
607  int type,
608  int protocol){
610  errno = EINVAL;
611  return -1;
612  }
613  return socket(domain, type, protocol);
614 }
615 
623 int
626  return 0;
627  }
628  return SSL_connect(ssl);
629 }
630 
638 SSL_CTX *
639 smtp_test_seam_ssl_ctx_new(const SSL_METHOD *method){
641  return NULL;
642  }
643  return SSL_CTX_new(method);
644 }
645 
653 int
656  return 0;
657  }
658  return SSL_do_handshake(ssl);
659 }
660 
668 X509 *
671  return NULL;
672  }
673  return SSL_get_peer_certificate(ssl);
674 }
675 
689 int
691  const char *name,
692  size_t namelen,
693  unsigned int flags,
694  char **peername){
696  return -1;
697  }
698  return X509_check_host(cert, name, namelen, flags, peername);
699 }
700 
708 SSL *
711  return NULL;
712  }
713  return SSL_new(ctx);
714 }
715 
725 int
727  void *buf,
728  int num){
729  size_t bytes_inject_len;
730 
733  bytes_inject_len = strlen(g_smtp_test_err_recv_bytes);
734  assert(bytes_inject_len < (size_t)num && bytes_inject_len < INT_MAX);
735  memcpy(buf, g_smtp_test_err_recv_bytes, bytes_inject_len);
736  return (int)bytes_inject_len;
737  }
738  return -1;
739  }
740  return SSL_read(ssl, buf, num);
741 }
742 
752 int
754  const void *buf,
755  int num){
757  return -1;
758  }
759  return SSL_write(ssl, buf, num);
760 }
761 
770 int
772  const char *format, ...){
773  va_list ap;
774  int rc;
775 
777  errno = ENOMEM;
779  }
780  va_start(ap, format);
781  rc = vsprintf(s, format, ap);
782  va_end(ap);
783  return rc;
784 }
785 
792 size_t
793 smtp_test_seam_strlen(const char *s){
794  size_t result;
795 
798  }
799  else{
800  result = strlen(s);
801  }
802  return result;
803 }
804 
812 time_t
813 smtp_test_seam_time(time_t *tloc){
816  }
817  return time(tloc);
818 }
819 
int g_smtp_test_err_fclose_ctr
Definition: seams.c:65
#define SSL_CTX_new
Definition: seams.h:209
int g_smtp_test_err_ssl_read_ctr
Definition: seams.c:194
unsigned long smtp_test_seam_err_peek_error(void)
Definition: seams.c:351
int smtp_test_seam_ssl_write(SSL *ssl, const void *buf, int num)
Definition: seams.c:753
SSL * smtp_test_seam_ssl_new(SSL_CTX *ctx)
Definition: seams.c:709
int g_smtp_test_err_err_peek_error_ctr
Definition: seams.c:60
int g_smtp_test_err_recv_rc
Definition: seams.c:111
SSL_CTX * smtp_test_seam_ssl_ctx_new(const SSL_METHOD *method)
Definition: seams.c:639
int smtp_test_seam_ssl_connect(SSL *ssl)
Definition: seams.c:624
#define SSL_read
Definition: seams.h:249
#define SSL_do_handshake
Definition: seams.h:217
BIO * smtp_test_seam_bio_new_socket(int sock, int close_flag)
Definition: seams.c:263
#define calloc
Definition: seams.h:73
#define time
Definition: seams.h:281
unsigned char * smtp_test_seam_hmac(const EVP_MD *evp_md, const void *key, int key_len, const unsigned char *d, size_t n, unsigned char *md, unsigned int *md_len)
Definition: seams.c:421
int smtp_test_seam_socket(int domain, int type, int protocol)
Definition: seams.c:606
#define ferror
Definition: seams.h:113
X509 * smtp_test_seam_ssl_get_peer_certificate(const SSL *ssl)
Definition: seams.c:669
#define SSL_write
Definition: seams.h:257
#define HMAC
Definition: seams.h:129
Test the smtp-client library.
int g_smtp_test_err_close_ctr
Definition: seams.c:49
#define realloc
Definition: seams.h:161
#define ERR_peek_error
Definition: seams.h:97
int g_smtp_test_err_ssl_ctx_new_ctr
Definition: seams.c:166
int g_smtp_test_err_hmac_ctr
Definition: seams.c:80
#define close
Definition: seams.h:81
int g_smtp_test_err_ssl_new_ctr
Definition: seams.c:189
int smtp_test_seam_connect(int socket, const struct sockaddr *address, socklen_t address_len)
Definition: seams.c:333
int g_smtp_test_err_mktime_ctr
Definition: seams.c:96
int g_smtp_test_err_localtime_r_ctr
Definition: seams.c:86
int g_smtp_test_err_ssl_connect_ctr
Definition: seams.c:160
int g_smtp_test_strlen_custom_ret
Definition: seams.c:214
time_t smtp_test_seam_mktime(struct tm *timeptr)
Definition: seams.c:476
int g_smtp_test_err_ssl_get_peer_certificate_ctr
Definition: seams.c:178
int g_smtp_test_err_recv_ctr
Definition: seams.c:106
#define SSL_new
Definition: seams.h:241
#define recv
Definition: seams.h:169
#define SSL_get_peer_certificate
Definition: seams.h:225
struct tm * smtp_test_seam_gmtime_r(const time_t *timep, struct tm *result)
Definition: seams.c:399
int g_smtp_test_err_realloc_ctr
Definition: seams.c:101
int g_smtp_test_err_x509_check_host_ctr
Definition: seams.c:184
int g_smtp_test_err_ssl_write_ctr
Definition: seams.c:199
int g_smtp_test_err_sprintf_ctr
Definition: seams.c:204
int g_smtp_test_err_si_sub_size_t_ctr
Definition: seams.c:143
#define gmtime_r
Definition: seams.h:121
#define mktime
Definition: seams.h:153
#define socket
Definition: seams.h:193
int smtp_test_seam_ssl_read(SSL *ssl, void *buf, int num)
Definition: seams.c:726
#define SSL_connect
Definition: seams.h:201
int g_smtp_test_err_bio_should_retry_rc
Definition: seams.c:39
#define connect
Definition: seams.h:89
int smtp_test_seam_x509_check_host(X509 *cert, const char *name, size_t namelen, unsigned int flags, char **peername)
Definition: seams.c:690
int g_smtp_test_err_ferror_ctr
Definition: seams.c:70
void * smtp_test_seam_realloc(void *ptr, size_t size)
Definition: seams.c:493
int g_smtp_test_err_gmtime_r_ctr
Definition: seams.c:75
size_t g_smtp_test_strlen_ret_value
Definition: seams.c:219
int smtp_test_seam_ferror(FILE *stream)
Definition: seams.c:382
int g_smtp_test_err_bio_new_socket_ctr
Definition: seams.c:28
#define malloc
Definition: seams.h:145
void * smtp_test_seam_calloc(size_t nelem, size_t elsize)
Definition: seams.c:298
int smtp_test_seam_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
Definition: seams.c:552
int smtp_test_seam_sprintf(char *s, const char *format,...)
Definition: seams.c:771
#define X509_check_host
Definition: seams.h:233
int g_smtp_test_send_one_byte
Definition: seams.c:131
char g_smtp_test_err_recv_bytes[90]
Definition: seams.c:116
struct tm * smtp_test_seam_localtime_r(const time_t *timep, struct tm *result)
Definition: seams.c:444
int g_smtp_test_err_si_add_size_t_ctr
Definition: seams.c:137
#define send
Definition: seams.h:185
int g_smtp_test_err_malloc_ctr
Definition: seams.c:91
#define strlen
Definition: seams.h:273
int smtp_test_seam_close(int fildes)
Definition: seams.c:315
int g_smtp_test_time_custom_ret
Definition: seams.c:224
int sock
Definition: smtp.c:159
int smtp_test_seam_fclose(FILE *stream)
Definition: seams.c:365
long smtp_test_seam_recv(int socket, void *buffer, size_t length, int flags)
Definition: seams.c:513
int g_smtp_test_err_select_ctr
Definition: seams.c:121
#define select
Definition: seams.h:177
size_t smtp_test_seam_strlen(const char *s)
Definition: seams.c:793
int g_smtp_test_err_connect_ctr
Definition: seams.c:54
#define localtime_r
Definition: seams.h:137
#define BIO_new_socket
Definition: seams.h:57
int g_smtp_test_err_si_mul_size_t_ctr
Definition: seams.c:149
int smtp_test_seam_dec_err_ctr(int *const test_err_ctr)
Definition: seams.c:244
time_t smtp_test_seam_time(time_t *tloc)
Definition: seams.c:813
int g_smtp_test_err_sprintf_rc
Definition: seams.c:209
#define fclose
Definition: seams.h:105
#define BIO_should_retry
Definition: seams.h:65
void * smtp_test_seam_malloc(size_t size)
Definition: seams.c:460
ssize_t smtp_test_seam_send(int socket, const void *buffer, size_t length, int flags)
Definition: seams.c:575
int g_smtp_test_err_send_ctr
Definition: seams.c:126
int g_smtp_test_err_bio_should_retry_ctr
Definition: seams.c:34
int g_smtp_test_err_ssl_do_handshake_ctr
Definition: seams.c:172
int g_smtp_test_err_socket_ctr
Definition: seams.c:154
int smtp_test_seam_bio_should_retry(BIO *bio)
Definition: seams.c:279
int smtp_test_seam_ssl_do_handshake(SSL *ssl)
Definition: seams.c:654
time_t g_smtp_test_time_ret_value
Definition: seams.c:229
int g_smtp_test_err_calloc_ctr
Definition: seams.c:44