1*6dce56b2SKevin Athey // RUN: %clangxx %s -o %t && %run %t %p
2e03c350eSVitaly Buka 
3e9c8d0ffSKevin Athey #include <assert.h>
4e9c8d0ffSKevin Athey #include <resolv.h>
5e9c8d0ffSKevin Athey #include <stdlib.h>
6e9c8d0ffSKevin Athey #include <string.h>
7e9c8d0ffSKevin Athey #include <sys/types.h>
8e9c8d0ffSKevin Athey 
main(int iArgc,char * szArgv[])9e9c8d0ffSKevin Athey int main(int iArgc, char *szArgv[]) {
10e9c8d0ffSKevin Athey   // Check NTOP writing
11e9c8d0ffSKevin Athey   const char *src = "base64 test data";
12e9c8d0ffSKevin Athey   size_t src_len = strlen(src);
13e9c8d0ffSKevin Athey   size_t dst_len = ((src_len + 2) / 3) * 4 + 1;
14e9c8d0ffSKevin Athey   char dst[dst_len];
15e9c8d0ffSKevin Athey   int res = b64_ntop(reinterpret_cast<const unsigned char *>(src), src_len, dst,
16e9c8d0ffSKevin Athey                      dst_len);
17e9c8d0ffSKevin Athey   assert(res >= 0);
18e9c8d0ffSKevin Athey   assert(strcmp(dst, "YmFzZTY0IHRlc3QgZGF0YQ==") == 0);
19e9c8d0ffSKevin Athey 
20e9c8d0ffSKevin Athey   // Check PTON writing
21e9c8d0ffSKevin Athey   unsigned char target[dst_len];
22e9c8d0ffSKevin Athey   res = b64_pton(dst, target, dst_len);
23e9c8d0ffSKevin Athey   assert(res >= 0);
24e9c8d0ffSKevin Athey   assert(strncmp(reinterpret_cast<const char *>(target), src, res) == 0);
25e9c8d0ffSKevin Athey 
26e9c8d0ffSKevin Athey   // Check NTOP writing for zero length src
27e9c8d0ffSKevin Athey   src = "";
28e9c8d0ffSKevin Athey   src_len = strlen(src);
29e9c8d0ffSKevin Athey   assert(((src_len + 2) / 3) * 4 + 1 < dst_len);
30e9c8d0ffSKevin Athey   res = b64_ntop(reinterpret_cast<const unsigned char *>(src), src_len, dst,
31e9c8d0ffSKevin Athey                  dst_len);
32e9c8d0ffSKevin Athey   assert(res >= 0);
33e9c8d0ffSKevin Athey   assert(strcmp(dst, "") == 0);
34e9c8d0ffSKevin Athey 
35e9c8d0ffSKevin Athey   // Check PTON writing for zero length src
36e9c8d0ffSKevin Athey   dst[0] = '\0';
37e9c8d0ffSKevin Athey   res = b64_pton(dst, target, dst_len);
38e9c8d0ffSKevin Athey   assert(res >= 0);
39e9c8d0ffSKevin Athey   assert(strncmp(reinterpret_cast<const char *>(target), src, res) == 0);
40e9c8d0ffSKevin Athey 
41e9c8d0ffSKevin Athey   return 0;
42e9c8d0ffSKevin Athey }
43