test_base64.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Test of \c enc_mime_encode_base64() implementation
4  *
5  * Copyright (c) 2012-2022 by the developers. See the LICENSE file for details.
6  */
7 
8 
9 /* ========================================================================== */
10 /* Include headers */
11 
12 #include "posix.h" /* Include this first because of feature test macros */
13 
14 #include <stdio.h>
15 
16 #include "encoding.h"
17 #include "test.h"
18 #include "test_base64.h"
19 
20 
21 /* ========================================================================== */
22 /*! \addtogroup TEST */
23 /*! @{ */
24 
25 
26 /* ========================================================================== */
27 /*! \brief Test \c enc_mime_encode_base64() implementation
28  *
29  * The following cases are tested:
30  * - Data size is a multiple of 3 bytes
31  * - One padding character required
32  * - Two padding characters required
33  *
34  * \return
35  * - \c EXIT_SUCCESS on success
36  * - \c EXIT_FAILURE on error
37  */
38 
39 int test_base64(void)
40 {
41 #define TS_NUM (size_t) 4 /* Number of test strings */
42  static const char* ts[TS_NUM] =
43  {
44  "This is a test!",
45  "This is a test!!",
46  "This is a test!!!",
47  "0123456789.0123456789.00000000"
48  };
49  static const char* rs[TS_NUM] = {
50  "VGhpcyBpcyBhIHRlc3Qh",
51  "VGhpcyBpcyBhIHRlc3QhIQ==",
52  "VGhpcyBpcyBhIHRlc3QhISE=",
53  "MDEyMzQ1Njc4OS4wMTIzNDU2Nzg5LjAwMDAwMDAw"
54  };
55  int res = POSIX_EXIT_SUCCESS;
56  int rv;
57  size_t i;
58  const char* buf = NULL;
59 
60  for(i = 0; i < TS_NUM; ++i)
61  {
62  rv = enc_mime_encode_base64(&buf, ts[i], strlen(ts[i]));
63  if(rv)
64  {
65  print_error("Base64 encoder failed");
66  res = POSIX_EXIT_FAILURE;
67  break;
68  }
69  if(strcmp(rs[i], buf))
70  {
71  print_error("Result is not correct");
72  /* For debugging */
73  fprintf(stderr, TEST_TAB "Input data: \"%s\"\n", ts[i]);
74  fprintf(stderr, TEST_TAB "Result is : \"%s\"\n", buf);
75  fprintf(stderr, TEST_TAB "Should be : \"%s\"\n", rs[i]);
76  res = POSIX_EXIT_FAILURE;
77  break;
78  }
79  enc_free((void*) buf);
80  }
81 
82  return(res);
83 }
84 
85 
86 /*! @} */
87 
88 /* EOF */
TEST_TAB
#define TEST_TAB
Tabulator to indent messages from test programs.
Definition: test.h:13
test_base64
int test_base64(void)
Test enc_mime_encode_base64() implementation.
Definition: test_base64.c:39
enc_free
void enc_free(void *p)
Free an object allocated by encoding module.
Definition: encoding.c:8868
enc_mime_encode_base64
int enc_mime_encode_base64(const char **enc, const char *data, size_t len)
Encode binary data to base64.
Definition: encoding.c:4744
print_error
void print_error(const char *)
Print error message.
Definition: main.cxx:276

Generated at 2024-04-27 using  doxygen