77 lines
1.7 KiB
C
77 lines
1.7 KiB
C
|
//
|
||
|
// test_base58.h
|
||
|
// libp2p_xcode
|
||
|
//
|
||
|
// Created by John Jones on 11/7/16.
|
||
|
// Copyright © 2016 JMJAtlanta. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#ifndef test_base58_h
|
||
|
#define test_base58_h
|
||
|
|
||
|
#include "libp2p/crypto/encoding/base58.h"
|
||
|
|
||
|
/***
|
||
|
* tests the base58 encoding and decoding
|
||
|
*/
|
||
|
int test_base58_encode_decode() {
|
||
|
|
||
|
unsigned char original[3] = { 0x41, 0x42 ,0x00 };
|
||
|
|
||
|
size_t buffer_size = 10;
|
||
|
unsigned char buffer[buffer_size];
|
||
|
unsigned char* ptr = buffer;
|
||
|
|
||
|
int retVal = libp2p_crypto_encoding_base58_encode(original, 3, &ptr, &buffer_size);
|
||
|
if (retVal == 0)
|
||
|
return 0;
|
||
|
|
||
|
size_t result_size = 3;
|
||
|
unsigned char result[result_size];
|
||
|
unsigned char* ptr2 = result;
|
||
|
memset(result, 0, result_size);
|
||
|
|
||
|
retVal = libp2p_crypto_encoding_base58_decode(ptr, buffer_size, &ptr2, &result_size);
|
||
|
if (retVal == 0)
|
||
|
return 0;
|
||
|
|
||
|
for (int i = 0; i < 3; i++)
|
||
|
if (original[i] != result[i])
|
||
|
return 0;
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
int test_base58_size() {
|
||
|
unsigned char* unencoded = (unsigned char*)"Hello, World!";
|
||
|
size_t string_length = strlen((char*)unencoded) + 1;
|
||
|
|
||
|
size_t encoded_length = 100;
|
||
|
unsigned char encoded[encoded_length];
|
||
|
unsigned char* ptr = encoded;
|
||
|
|
||
|
// now encode it
|
||
|
libp2p_crypto_encoding_base58_encode(unencoded, string_length, &ptr, &encoded_length);
|
||
|
|
||
|
size_t size_enc = libp2p_crypto_encoding_base58_decode_size(ptr);
|
||
|
size_t max_size_enc = libp2p_crypto_encoding_base58_decode_max_size(ptr);
|
||
|
|
||
|
if (size_enc <= string_length && max_size_enc >= string_length)
|
||
|
return 1;
|
||
|
|
||
|
return 0;
|
||
|
|
||
|
}
|
||
|
|
||
|
int test_base58_max_size() {
|
||
|
unsigned char hash[5] = {'S', 'D', 'Y', 'h', 'd' };
|
||
|
|
||
|
size_t results = libp2p_crypto_encoding_base58_decode_max_size(hash);
|
||
|
if (results != 4)
|
||
|
return 0;
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
#endif /* test_base58_h */
|