Instrument Neutral Distributed Interface INDI 2.2.5
test_base64.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2016 Andy Kirkham. All rights reserved.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public
5 License version 2 as published by the Free Software Foundation.
6 .
7 This library is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 Library General Public License for more details.
11 .
12 You should have received a copy of the GNU Library General Public License
13 along with this library; see the file COPYING.LIB. If not, write to
14 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
15 Boston, MA 02110-1301, USA.
16*******************************************************************************/
17
18#include <gtest/gtest.h>
19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <cstdlib>
25#include <cstring>
26
27#include "base64.h"
28
29TEST(CORE_BASE64, Test_to64frombits)
30{
31 const char inp_msg[] = "FOOBARBAZ";
32 const size_t inp_len = sizeof(inp_msg) - 1;
33
34 const char out_msg[] = "Rk9PQkFSQkFa";
35 const size_t out_len = sizeof(out_msg) - 1;
36
37 char res_msg[out_len + 1] = {0,};
38 size_t res_len = 0;
39
40 res_len = to64frombits_s(
41 reinterpret_cast<unsigned char *>(res_msg),
42 reinterpret_cast<const unsigned char *>(inp_msg),
43 inp_len,
44 out_len
45 );
46 ASSERT_EQ(out_len, res_len);
47 ASSERT_STREQ(out_msg, res_msg);
48}
49
50TEST(CORE_BASE64, Test_from64tobits)
51{
52 const char inp_msg[] = "Rk9PQkFSQkFa";
53 // const size_t inp_len = sizeof(inp_msg) - 1;
54
55 const char out_msg[] = "FOOBARBAZ";
56 const size_t out_len = sizeof(out_msg) - 1;
57
58 char res_msg[out_len + 1] = {0,};
59 size_t res_len = 0;
60
61 res_len = from64tobits(res_msg, inp_msg);
62 ASSERT_EQ(out_len, res_len);
63 ASSERT_STREQ(out_msg, res_msg);
64}
65
66TEST(CORE_BASE64, Test_from64tobits_fast)
67{
68 const char inp_msg[] = "Rk9PQkFSQkFa";
69 const size_t inp_len = sizeof(inp_msg) - 1;
70
71 const char out_msg[] = "FOOBARBAZ";
72 const size_t out_len = sizeof(out_msg) - 1;
73
74 char res_msg[out_len + 1] = {0,};
75 size_t res_len = 0;
76
77 res_len = from64tobits_fast(res_msg, inp_msg, inp_len);
78 ASSERT_EQ(out_len, res_len);
79 ASSERT_STREQ(out_msg, res_msg);
80}
81
82TEST(CORE_BASE64, Test_from64tobits_fast_time)
83{
84 const char inp_msg[] = "Rk9PQkFSQkFa";
85 const size_t inp_len = sizeof(inp_msg) - 1;
86
87 const char out_msg[] = "FOOBARBAZ";
88 const size_t out_len = sizeof(out_msg) - 1;
89
90 char res_msg[out_len + 1] = {0,};
91
92 int i, iterations = 1000 * 1000 * 10;
93
94 for (i = 0; i < iterations; i++)
95 {
96 from64tobits_fast(res_msg, inp_msg, inp_len);
97 }
98}
99
100TEST(CORE_BASE64, Test_from64tobits_fast_with_bug_time)
101{
102 const char inp_msg[] = "Rk9PQkFSQkFa";
103 const size_t inp_len = sizeof(inp_msg) - 1;
104
105 const char out_msg[] = "FOOBARBAZ";
106 const size_t out_len = sizeof(out_msg) - 1;
107
108 char res_msg[out_len + 1] = {0,};
109
110 int i, iterations = 1000 * 1000 * 10;
111
112 for (i = 0; i < iterations; i++)
113 {
114 from64tobits_fast_with_bug(res_msg, inp_msg, inp_len);
115 }
116}
117
int from64tobits(char *out, const char *in)
Convert base64 to bytes array.
Definition base64.c:114
int to64frombits_s(unsigned char *out, const unsigned char *in, int inlen, size_t outlen)
Convert bytes array to base64.
Definition base64.c:63
int from64tobits_fast_with_bug(char *out, const char *in, int inlen)
Definition base64.c:199
int from64tobits_fast(char *out, const char *in, int inlen)
Definition base64.c:122
TEST(CORE_BASE64, Test_to64frombits)