Browse Source

Tests and documentation for base64.

pull/225/head
EinMByte 9 years ago
parent
commit
28a4f4173d
  1. 36
      base64.cpp
  2. 19
      base64.h
  3. 2
      filelist.mk
  4. 49
      tests/Data.cpp
  5. 1
      tests/Identity.cpp

36
base64.cpp

@ -45,23 +45,8 @@ namespace data @@ -45,23 +45,8 @@ namespace data
static char P64 = '=';
/*
*
* ByteStreamToBase64
* ------------------
*
* Converts binary encoded data to BASE64 format.
*
*/
size_t /* Number of bytes in the encoded buffer */
ByteStreamToBase64 (
const uint8_t * InBuffer, /* Input buffer, binary data */
size_t InCount, /* Number of bytes in the input buffer */
char * OutBuffer, /* output buffer */
size_t len /* length of output buffer */
)
size_t ByteStreamToBase64(const uint8_t* InBuffer, size_t InCount, char* OutBuffer, size_t len)
{
unsigned char * ps;
unsigned char * pd;
@ -124,23 +109,8 @@ namespace data @@ -124,23 +109,8 @@ namespace data
return outCount;
}
/*
*
* Base64ToByteStream
* ------------------
*
* Converts BASE64 encoded data to binary format. If input buffer is
* not properly padded, buffer of negative length is returned
*
*/
size_t /* Number of output bytes */
Base64ToByteStream (
const char * InBuffer, /* BASE64 encoded buffer */
size_t InCount, /* Number of input bytes */
uint8_t * OutBuffer, /* output buffer length */
size_t len /* length of output buffer */
)
size_t Base64ToByteStream(const char * InBuffer, size_t InCount, uint8_t* OutBuffer, size_t len)
{
unsigned char * ps;
unsigned char * pd;
@ -154,7 +124,7 @@ namespace data @@ -154,7 +124,7 @@ namespace data
if (isFirstTime) iT64Build();
n = InCount/4;
m = InCount%4;
if (!m)
if(InCount && !m)
outCount = 3*n;
else {
outCount = 0;

19
base64.h

@ -9,7 +9,26 @@ namespace i2p @@ -9,7 +9,26 @@ namespace i2p
namespace data
{
/*
* Base64 encodes an array of bytes.
* @return the number of characters in the output buffer
* @param InBuffer array of input bytes to be encoded
* @param InCount length of the input array
* @param OutBuffer array of output characters
* @param len length of the output buffer
*/
size_t ByteStreamToBase64 (const uint8_t * InBuffer, size_t InCount, char * OutBuffer, size_t len);
/**
* Decodes base 64 encoded data to an array of bytes.
* @return the number of bytes in the output buffer
* @param InBuffer array of input characters to be decoded
* @param InCount length of the input array
* @param OutBuffer array of output bytes
* @param len length of the output buffer
* @todo Do not return a negative value on failure, size_t could be unsigned.
*/
size_t Base64ToByteStream (const char * InBuffer, size_t InCount, uint8_t * OutBuffer, size_t len );
const char * GetBase64SubstitutionTable ();

2
filelist.mk

@ -25,4 +25,4 @@ LIB_SRC := $(COMMON_SRC) \ @@ -25,4 +25,4 @@ LIB_SRC := $(COMMON_SRC) \
api.cpp
TESTS_SRC := $(COMMON_SRC) \
tests/Utility.cpp tests/Identity.cpp
tests/Utility.cpp tests/Identity.cpp tests/Data.cpp

49
tests/Data.cpp

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "../Identity.h"
BOOST_AUTO_TEST_SUITE(DataTests)
using namespace i2p::data;
BOOST_AUTO_TEST_CASE(Base64EncodeEmpty)
{
ByteStreamToBase64(nullptr, 0, nullptr, 0);
}
BOOST_AUTO_TEST_CASE(Base64DecodeEmpty)
{
Base64ToByteStream(nullptr, 0, nullptr, 0);
}
BOOST_AUTO_TEST_CASE(Base64Encode)
{
const uint8_t input[] = {
0x53, 0xd3, 0x60, 0xfa, 0xf9, 0x58, 0xd0, 0x5e, 0x41, 0xa9, 0x6c,
0xf1, 0x9f, 0xc4, 0xe, 0x23, 0x9b, 0xca, 0xb1, 0x61, 0xa7, 0x33, 0xcf,
0x1f, 0x30
};
const char* output = "U9Ng-vlY0F5BqWzxn8QOI5vKsWGnM88fMA==";
char result[36];
ByteStreamToBase64(input, 25, result, 36);
BOOST_CHECK_EQUAL_COLLECTIONS(result, result + 36, output, output + 36);
}
BOOST_AUTO_TEST_CASE(Base64Decode)
{
const char* input = "U9Ng-vlY0F5BqWzxn8QOI5vKsWGnM88fMA==";
const uint8_t output[] = {
0x53, 0xd3, 0x60, 0xfa, 0xf9, 0x58, 0xd0, 0x5e, 0x41, 0xa9, 0x6c,
0xf1, 0x9f, 0xc4, 0xe, 0x23, 0x9b, 0xca, 0xb1, 0x61, 0xa7, 0x33, 0xcf,
0x1f, 0x30
};
uint8_t result[25];
Base64ToByteStream(input, 36, result, 25);
BOOST_CHECK_EQUAL_COLLECTIONS(result, result + 25, output, output + 25);
}
BOOST_AUTO_TEST_SUITE_END()

1
tests/Identity.cpp

@ -8,5 +8,4 @@ BOOST_AUTO_TEST_SUITE(IdentityTests) @@ -8,5 +8,4 @@ BOOST_AUTO_TEST_SUITE(IdentityTests)
BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save