e30dca97af9897935663ba15625a08319f3aeb41
[ric-plt/e2.git] / RIC-E2-TERMINATION / TEST / base64 / testBase64.cpp
1 /*
2  * Copyright 2019 AT&T Intellectual Property
3  * Copyright 2019 Nokia
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 //
19 // Created by adi ENZEL on 11/27/19.
20 //
21
22 #include "base64.h"
23 #include <mdclog/mdclog.h>
24 #include <cgreen/cgreen.h>
25 #include <cstdio>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <cstring>
29
30 using namespace std;
31
32 Describe(base64);
33 BeforeEach(base64) {}
34 AfterEach(base64) {}
35
36 using namespace cgreen;
37
38 void init_log() {
39     mdclog_attr_t *attr;
40     mdclog_attr_init(&attr);
41     mdclog_attr_set_ident(attr, "TestConfiguration");
42     mdclog_init(attr);
43     mdclog_attr_destroy(attr);
44 }
45
46 const char *data = "ABC123Test Lets Try this' input and see What \"happens\"";
47
48 Ensure(base64, encDec) {
49     string str = "ABC123Test Lets Try this' input and see What \"happens\"";
50     auto *buf = (unsigned char *)malloc(str.length() * 2);
51     auto length = (long)(str.length() * 2);
52     base64::encode((unsigned char *)str.c_str(), str.length(), buf, length);
53     auto *backBackBuff = (unsigned char *)malloc(length);
54     auto length2 = length;
55     assert_that(base64::decode(buf, length, backBackBuff, length2) == 0);
56     std::string str1( backBackBuff, backBackBuff + sizeof backBackBuff / sizeof backBackBuff[0]);
57
58     assert_that(str.length() == (ulong)length2)
59     //auto val = str.compare((const char *)backBackBuff);
60     assert_that(str.compare((const char *)backBackBuff) == 0)
61     free(backBackBuff);
62     free(buf);
63 }
64
65 Ensure(base64, errorsHandling) {
66     string str = "ABC123Test Lets Try this' input and see What \"happens\"";
67     auto *buf = (unsigned char *)malloc(str.length() * 2);
68     auto length = (long)(str.length());
69     assert_that(base64::encode((unsigned char *)str.c_str(), str.length(), buf, length) == -1);
70     length = (long)(str.length() * 2);
71     assert_that(base64::encode((unsigned char *)str.c_str(), str.length(), buf, length) == 0);
72     auto *backBackBuff = (unsigned char *)malloc(length);
73     auto length2 = length >> 2;
74     assert_that(base64::decode(buf, length, backBackBuff, length2) == -1);
75     //std::string str1( backBackBuff, backBackBuff + sizeof backBackBuff / sizeof backBackBuff[0]);
76     auto length1 = 0l;
77     assert_that(base64::encode((unsigned char *)str.c_str(), str.length(), nullptr , length) == -1);
78 //    assert_that(base64::encode((unsigned char *)str.c_str(), str.length(), nullptr , length) == -1);
79     assert_that(base64::encode(nullptr, str.length(), backBackBuff , length) == -1);
80     assert_that(base64::encode((unsigned char *)str.c_str(), length1, backBackBuff , length) == -1);
81     assert_that(base64::encode(nullptr, str.length(), backBackBuff , length1) == -1);
82     length1 = -1;
83     assert_that(base64::encode((unsigned char *)str.c_str(), length1, backBackBuff , length) == -1);
84     assert_that(base64::encode(nullptr, str.length(), backBackBuff , length1) == -1);
85
86 }
87
88
89 int main(const int argc, char **argv) {
90     mdclog_severity_t loglevel = MDCLOG_INFO;
91     init_log();
92     mdclog_level_set(loglevel);
93
94     //TestSuite *suite = create_test_suite();
95     TestSuite *suite = create_named_test_suite_(__FUNCTION__, __FILE__, __LINE__);
96
97     add_test_with_context(suite, base64, encDec);
98     add_test_with_context(suite, base64, errorsHandling);
99
100     return cgreen::run_test_suite(suite, create_text_reporter());
101
102 }