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

source code of compiler-rt/test/sanitizer_common/TestCases/Linux/b64.cpp