提交 4c826bc7 编写于 作者: M Matias Romeo

add tests for crypto api

上级 5ab11964
......@@ -94,3 +94,10 @@ struct test_db {
static unsigned int key_i128i128_general();
};
struct test_crypto {
static unsigned int test_sha256();
static unsigned int sha256_no_data();
static unsigned int asert_sha256_false();
static unsigned int asert_sha256_true();
static unsigned int asert_no_data();
};
#include <eoslib/eos.hpp>
#include <eoslib/crypto.h>
#include "test_api.hpp"
static const char test1[] = "abc";
static const unsigned char test1_ok[] = {
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
};
const char test2[] = "";
const unsigned char test2_ok[] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
};
static const char test3[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
static const unsigned char test3_ok[] = {
0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
};
static const char test4[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
static const unsigned char test4_ok[] = {
0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80,
0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37,
0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51,
0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1
};
static const char test5[] = "message digest";
static const unsigned char test5_ok[] = {
0xf7, 0x84, 0x6f, 0x55, 0xcf, 0x23, 0xe1, 0x4e,
0xeb, 0xea, 0xb5, 0xb4, 0xe1, 0x55, 0x0c, 0xad,
0x5b, 0x50, 0x9e, 0x33, 0x48, 0xfb, 0xc4, 0xef,
0xa3, 0xa1, 0x41, 0x3d, 0x39, 0x3c, 0xb6, 0x50
};
extern "C" {
uint32_t my_strlen(const char *str) {
uint32_t len = 0;
while(str[len]) ++len;
return len;
}
bool my_memcmp(void *s1, void *s2, uint32_t n)
{
unsigned char *c1 = (unsigned char *)s1;
unsigned char *c2 = (unsigned char *)s2;
for (uint32_t i = 0; i < n; ++i) {
if (c1[i] != c2[i]) {
eos::print("Rompio en index ", i);
return false;
}
}
return true;
}
void my_to_hex(void *vdata, uint32_t datalen, char *dst, uint32_t dst_len) {
unsigned char *data = (unsigned char *)vdata;
const char symbols[] = "0123456789abcdef";
uint32_t i = 0;
for(i = 0; i < dst_len-1 && i/2 < datalen; ++i) {
unsigned char b = !(i % 2) ? data[i/2] >> 4 : data[i/2] & 0x0F;
dst[i] = symbols[ b ];
}
dst[i]=0;
}
}
unsigned int test_crypto::test_sha256() {
uint256 tmp;
sha256( (char *)test1, my_strlen(test1), &tmp );
WASM_ASSERT( my_memcmp((void *)test1_ok, &tmp, sizeof(uint256)), "sha256 test1" );
sha256( (char *)test3, my_strlen(test3), &tmp );
WASM_ASSERT( my_memcmp((void *)test3_ok, &tmp, sizeof(uint256)), "sha256 test3" );
sha256( (char *)test4, my_strlen(test4), &tmp );
WASM_ASSERT( my_memcmp((void *)test4_ok, &tmp, sizeof(uint256)), "sha256 test4" );
sha256( (char *)test5, my_strlen(test5), &tmp );
WASM_ASSERT( my_memcmp((void *)test5_ok, &tmp, sizeof(uint256)), "sha256 test5" );
return WASM_TEST_PASS;
}
unsigned int test_crypto::sha256_no_data() {
uint256 tmp;
sha256( (char *)test2, my_strlen(test2), &tmp );
WASM_ASSERT( my_memcmp((void *)test2_ok, &tmp, sizeof(uint256)), "sha256 test2" );
return WASM_TEST_PASS;
}
unsigned int test_crypto::asert_sha256_false() {
uint256 tmp;
sha256( (char *)test1, my_strlen(test1), &tmp );
tmp.words[0] ^= (uint64_t)(-1);
assert_sha256( (char *)test1, my_strlen(test1), &tmp);
return WASM_TEST_FAIL;
}
unsigned int test_crypto::asert_sha256_true() {
uint256 tmp;
sha256( (char *)test1, my_strlen(test1), &tmp );
assert_sha256( (char *)test1, my_strlen(test1), &tmp);
sha256( (char *)test3, my_strlen(test3), &tmp );
assert_sha256( (char *)test3, my_strlen(test3), &tmp);
sha256( (char *)test4, my_strlen(test4), &tmp );
assert_sha256( (char *)test4, my_strlen(test4), &tmp);
sha256( (char *)test5, my_strlen(test5), &tmp );
assert_sha256( (char *)test5, my_strlen(test5), &tmp);
return WASM_TEST_PASS;
}
unsigned int test_crypto::asert_no_data() {
uint256 *tmp = (uint256*)test2_ok;
assert_sha256( (char *)test2, my_strlen(test2), tmp);
return WASM_TEST_FAIL;
}
\ No newline at end of file
......@@ -369,6 +369,17 @@ BOOST_FIXTURE_TEST_CASE(test_all, testing_fixture)
//Test db (i128i128)
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_db", "key_i128i128_general"), {}, {} ) == WASM_TEST_PASS, "test_db::key_i128i128_general()" );
//Test crypto
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_crypto", "test_sha256"), {}, {} ) == WASM_TEST_PASS, "test_crypto::test_sha256()" );
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( TEST_METHOD("test_crypto", "sha256_no_data"), {}, {} ),
fc::assert_exception, is_assert_exception );
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( TEST_METHOD("test_crypto", "asert_sha256_false"), {}, {} ),
fc::assert_exception, is_assert_exception );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_crypto", "asert_sha256_true"), {}, {} ) == WASM_TEST_PASS, "test_crypto::asert_sha256_true()" );
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( TEST_METHOD("test_crypto", "asert_no_data"), {}, {} ),
fc::assert_exception, is_assert_exception );
} FC_LOG_AND_RETHROW() }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册