blob: e9e86dd13845576b7ba1a3926a8fddad29cf77c1 [file] [log] [blame]
Roger Meier4cb8e402012-05-27 18:05:16 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <boost/test/auto_unit_test.hpp>
21#include <thrift/protocol/TBase64Utils.h>
22
23using apache::thrift::protocol::base64_encode;
24using apache::thrift::protocol::base64_decode;
25
Konrad Grochowski16a23a62014-11-13 15:33:38 +010026BOOST_AUTO_TEST_SUITE(Base64Test)
Roger Meier4cb8e402012-05-27 18:05:16 +000027
28void setupTestData(int i, uint8_t* data, int& len) {
29 len = 0;
30 do {
31 data[len] = (uint8_t)(i & 0xFF);
32 i >>= 8;
33 len++;
34 } while ((len < 3) && (i != 0));
35
36 BOOST_ASSERT(i == 0);
37}
38
39void checkEncoding(uint8_t* data, int len) {
40 for (int i = 0; i < len; i++) {
41 BOOST_ASSERT(isalnum(data[i]) || data[i] == '/' || data[i] == '+');
42 }
43}
44
Konrad Grochowski16a23a62014-11-13 15:33:38 +010045BOOST_AUTO_TEST_CASE(test_Base64_Encode_Decode) {
Roger Meier4cb8e402012-05-27 18:05:16 +000046 int len;
47 uint8_t testInput[3];
48 uint8_t testOutput[4];
49
50 // Test all possible encoding / decoding cases given the
51 // three byte limit for base64_encode.
52
53 for (int i = 0xFFFFFF; i >= 0; i--) {
54
55 // fill testInput based on i
56 setupTestData(i, testInput, len);
57
58 // encode the test data, then decode it again
59 base64_encode(testInput, len, testOutput);
60
61 // verify each byte has a valid Base64 value (alphanumeric or either + or /)
62 checkEncoding(testOutput, len);
63
64 // decode output and check that it matches input
65 base64_decode(testOutput, len + 1);
66 BOOST_ASSERT(0 == memcmp(testInput, testOutput, len));
Roger Meier4cb8e402012-05-27 18:05:16 +000067 }
68}
69
70BOOST_AUTO_TEST_SUITE_END()