Skip to content

Commit c1a7222

Browse files
author
Justin Boswell
authored
Fixed inverted logic in base64 encode/decode (aws#126)
* Fixed inverted logic in base64 encode/decode * Added base64 unit test, fixed Base64Encode
1 parent 2e9625f commit c1a7222

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

source/Types.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace Aws
6565
ByteBuf tempBuf = aws_byte_buf_from_array(output.data(), output.size());
6666
tempBuf.len = 0;
6767

68-
if (aws_base64_decode(&toDecode, &tempBuf))
68+
if (aws_base64_decode(&toDecode, &tempBuf) == AWS_OP_SUCCESS)
6969
{
7070
return output;
7171
}
@@ -86,8 +86,14 @@ namespace Aws
8686
ByteBuf tempBuf = aws_byte_buf_from_array(output.data(), output.size());
8787
tempBuf.len = 0;
8888

89-
if (aws_base64_encode(&toEncode, &tempBuf))
89+
if (aws_base64_encode(&toEncode, &tempBuf) == AWS_OP_SUCCESS)
9090
{
91+
// encoding appends a null terminator, and accounts for it in the encoded length,
92+
// which makes the string 1 character too long
93+
if (output.back() == 0)
94+
{
95+
output.pop_back();
96+
}
9197
return output;
9298
}
9399
}

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_test_case(EventLoopResourceSafety)
1212
add_net_test_case(MqttClientResourceSafety)
1313
add_test_case(ClientBootstrapResourceSafety)
1414
add_net_test_case(TLSContextResourceSafety)
15+
add_test_case(Base64RoundTrip)
1516
add_test_case(DateTimeBinding)
1617
add_test_case(BasicJsonParsing)
1718
add_test_case(JsonNullParsing)

tests/TypesTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
#include <aws/crt/Api.h>
16+
#include <aws/crt/Types.h>
17+
#include <aws/testing/aws_test_harness.h>
18+
19+
static int s_base64_round_trip(struct aws_allocator *allocator, void *ctx)
20+
{
21+
(void)ctx;
22+
Aws::Crt::ApiHandle apiHandle(allocator);
23+
24+
{
25+
Aws::Crt::String test_data = "foobar";
26+
Aws::Crt::String expected = "Zm9vYmFy";
27+
28+
const Aws::Crt::Vector<uint8_t> test_vector(test_data.begin(), test_data.end());
29+
const Aws::Crt::Vector<uint8_t> expected_vector(expected.begin(), expected.end());
30+
31+
Aws::Crt::String encoded = Aws::Crt::Base64Encode(test_vector);
32+
ASSERT_BIN_ARRAYS_EQUALS(expected.data(), expected.size(), encoded.data(), encoded.size());
33+
34+
Aws::Crt::Vector<uint8_t> decoded = Aws::Crt::Base64Decode(encoded);
35+
ASSERT_BIN_ARRAYS_EQUALS(test_vector.data(), test_vector.size(), decoded.data(), decoded.size());
36+
}
37+
38+
return 0;
39+
}
40+
41+
AWS_TEST_CASE(Base64RoundTrip, s_base64_round_trip)

0 commit comments

Comments
 (0)