Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #102 from libbitc/picocoin
Browse files Browse the repository at this point in the history
Update base58 and sighash tests
  • Loading branch information
jgarzik committed Jun 24, 2017
2 parents a42f969 + e1cf58d commit faa9b83
Show file tree
Hide file tree
Showing 38 changed files with 881 additions and 91 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ AC_CONFIG_FILES([
lib/Makefile
src/Makefile
test/Makefile
test/data/Makefile
external/Makefile
])
AC_OUTPUT
8 changes: 4 additions & 4 deletions lib/file_seq.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
#include "picocoin-config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ccoin/util.h>
#include <ccoin/util.h> // for file_seq_open

#include <fcntl.h> // for open, posix_fadvise
#include <unistd.h> // for close

int file_seq_open(const char *filename)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/script_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void bp_tx_sigserializer(cstring *s, const cstring *scriptCode,
if (nInput != nIn)
// Blank out other inputs' signatures
ser_varlen(s, (int)0);
else if (scriptCode == NULL)
cstr_append_c(s, 0);
else {
/** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */
struct const_buffer it = { scriptCode->str, scriptCode->len };
Expand Down
4 changes: 2 additions & 2 deletions src/rawtx.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ static void mutate_inputs(void)
static bool is_script_addr(unsigned char addrtype)
{
switch (addrtype) {
case 5: // mainnet
case 196: // testnet
case SCRIPT_ADDRESS: // mainnet
case SCRIPT_ADDRESS_TEST: // testnet
return true;
default:
return false;
Expand Down
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ parr
prng
script
script-parse
sighash
tx
tx-valid
util
Expand Down
20 changes: 6 additions & 14 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,23 @@ if ENABLE_VALGRIND
--error-exitcode=1
endif

SUBDIRS = data

AM_CPPFLAGS = -DTEST_SRCDIR=\"$(top_srcdir)/test\" \
-I$(top_srcdir)/include \
-I$(top_srcdir)/external/secp256k1/include

EXTRA_DIST = \
base58_encode_decode.json \
base58_keys_valid.json \
random.data \
blk0.json blk120383.json tx3e0dc3da.json \
blk0.ser blk120383.ser tx3e0dc3da.ser \
blks10.ser \
hdr193000.ser tn_hdr35141.ser \
tn_blk35133.ser wallet-basics.json \
script_tests.json tx_valid.json tx_invalid.json

noinst_LIBRARIES= libtest.a

libtest_a_SOURCES= libtest.h libtest.c chisq.c randtest.c

noinst_PROGRAMS = clist cstr coredefs hex hdkeys hashtab base58 fileio util \
crypto keystore keyset bloom mbr misc net \
crypto keystore keyset bloom mbr misc net sighash \
message parr prng script-parse tx block blockfile blkdb script \
tx-valid wallet wallet-basics chain-verf hash ctaes aes-util

TESTS = clist cstr coredefs hex hdkeys hashtab base58 fileio util \
crypto keystore keyset bloom mbr misc net \
crypto keystore keyset bloom mbr misc net sighash \
message parr prng script-parse tx block blockfile blkdb script \
tx-valid wallet wallet-basics chain-verf hash ctaes aes-util

Expand Down Expand Up @@ -64,7 +55,8 @@ parr_LDADD = $(COMMON_LDADD)
prng_LDADD = $(COMMON_LDADD)
script_LDADD = $(COMMON_LDADD)
script_parse_LDADD = $(COMMON_LDADD)
tx_LDADD = $(COMMON_LDADD)
sighash_LDADD = $(COMMON_LDADD)
tx_LDADD = $(COMMON_LDADD)
tx_valid_LDADD = $(COMMON_LDADD)
util_LDADD = $(COMMON_LDADD) $(top_builddir)/lib/libccoinnet.la
wallet_LDADD = $(COMMON_LDADD)
Expand Down
137 changes: 90 additions & 47 deletions test/base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
* Distributed under the MIT/X11 software license, see the accompanying
* file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/
#include "picocoin-config.h"

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ccoin/base58.h> // for base58_decode_check, etc
#include <ccoin/coredefs.h> // for bitc_address_type, etc
#include <ccoin/cstr.h> // for cstring, cstr_free, etc
#include <ccoin/hexcode.h> // for hex2str, decode_hex
#include <ccoin/key.h> // for bitc_key_static_shutdown
#include <ccoin/crypto/ripemd160.h> // for RIPEMD160_DIGEST_LENGTH
#include "libtest.h" // for dumphex, read_json, etc

#include <jansson.h>
#include "libtest.h"
#include <ccoin/base58.h>
#include <ccoin/hexcode.h>
#include <ccoin/key.h>
#include <ccoin/coredefs.h>

#include <assert.h> // for assert
#include <stdbool.h> // for bool, true
#include <stddef.h> // for size_t
#include <stdio.h> // for fprintf, NULL, stderr
#include <stdlib.h> // for free, calloc
#include <string.h> // for strcmp, memcmp, strlen

static void test_encode(const char *hexstr, const char *enc)
{
Expand Down Expand Up @@ -70,25 +76,22 @@ static void test_decode(const char *hexstr, const char *base58_str)
cstr_free(s, true);
}

static void runtest_encdec(const char *base_fn)
static void runtest_encdec(const char *json_base_fn)
{
char *fn = NULL;

fn = test_filename(base_fn);
json_t *data = read_json(fn);
assert(json_is_array(data));
char *json_fn = test_filename(json_base_fn);
json_t *tests = read_json(json_fn);
assert(json_is_array(tests));

size_t n_tests = json_array_size(data);
unsigned int i;
size_t n_tests = json_array_size(tests);
unsigned int idx;

for (i = 0; i < n_tests; i++) {
json_t *inner;
for (idx = 0; idx < n_tests; idx++) {
json_t *test = json_array_get(tests, idx);
assert(json_is_array(test));
assert(json_array_size(test) == 2);

inner = json_array_get(data, i);
assert(json_is_array(inner));

json_t *j_raw = json_array_get(inner, 0);
json_t *j_enc = json_array_get(inner, 1);
json_t *j_raw = json_array_get(test, 0);
json_t *j_enc = json_array_get(test, 1);
assert(json_is_string(j_raw));
assert(json_is_string(j_enc));

Expand All @@ -98,8 +101,8 @@ static void runtest_encdec(const char *base_fn)
json_string_value(j_enc));
}

free(fn);
json_decref(data);
free(json_fn);
json_decref(tests);
}

static void test_privkey_valid_enc(const char *base58_str,
Expand Down Expand Up @@ -228,37 +231,34 @@ static void test_pubkey_valid_dec(const char *base58_str,
assert(dec != NULL);

assert(addrtype == addrtype_dec);
assert(dec->len == 20);
assert(dec->len == RIPEMD160_DIGEST_LENGTH);
assert(payload->len == dec->len);
assert(memcmp(payload->str, dec->str, dec->len) == 0);

cstr_free(dec, true);
cstr_free(payload, true);
}

static void runtest_keys_valid(const char *base_fn)
static void runtest_keys_valid(const char *json_base_fn)
{
char *fn = NULL;

fn = test_filename(base_fn);
json_t *data = read_json(fn);
assert(json_is_array(data));

size_t n_tests = json_array_size(data);
unsigned int i;
char *json_fn = test_filename(json_base_fn);
json_t *tests = read_json(json_fn);
assert(json_is_array(tests));

for (i = 0; i < n_tests; i++) {
json_t *inner;
size_t n_tests = json_array_size(tests);
unsigned int idx;

inner = json_array_get(data, i);
assert(json_is_array(inner));
for (idx = 0; idx < n_tests; idx++) {
json_t *test = json_array_get(tests, idx);
assert(json_is_array(test));
assert(json_array_size(test) == 3);

json_t *j_base58 = json_array_get(inner, 0);
json_t *j_payload = json_array_get(inner, 1);
json_t *j_base58 = json_array_get(test, 0);
json_t *j_payload = json_array_get(test, 1);
assert(json_is_string(j_base58));
assert(json_is_string(j_payload));

json_t *j_meta = json_array_get(inner, 2);
json_t *j_meta = json_array_get(test, 2);
assert(json_is_object(j_meta));

json_t *j_addrtype = json_object_get(j_meta, "addrType");
Expand Down Expand Up @@ -296,14 +296,57 @@ static void runtest_keys_valid(const char *base_fn)
}
}

free(fn);
json_decref(data);
free(json_fn);
json_decref(tests);
}

static void runtest_keys_invalid(const char *json_base_fn)
{
char *json_fn = test_filename(json_base_fn);
json_t *tests = read_json(json_fn);
assert(json_is_array(tests));

size_t n_tests = json_array_size(tests);
unsigned int idx;

for (idx = 0; idx < n_tests; idx++) {

json_t *test = json_array_get(tests, idx);
assert(json_is_array(test));
assert(json_array_size(test) == 1);

json_t *j_base58 = json_array_get(test, 0);
assert(json_is_string(j_base58));

unsigned char addrtype;
cstring *payload = base58_decode_check(&addrtype, json_string_value(j_base58));
bool is_valid = (payload != NULL);

if (is_valid)
if ((addrtype == PUBKEY_ADDRESS_TEST) ||
(addrtype == PUBKEY_ADDRESS) ||
(addrtype == SCRIPT_ADDRESS_TEST) ||
(addrtype == SCRIPT_ADDRESS))
is_valid = (payload->len == RIPEMD160_DIGEST_LENGTH);
else if
((addrtype == PRIVKEY_ADDRESS_TEST) ||
(addrtype == PRIVKEY_ADDRESS))
is_valid = (payload->len == 32 || payload->len == 33 && payload->str[32] == 1);
else is_valid = false;

cstr_free(payload, true);
assert(!is_valid);
}

free(json_fn);
json_decref(tests);
}

int main (int argc, char *argv[])
{
runtest_encdec("base58_encode_decode.json");
runtest_keys_valid("base58_keys_valid.json");
runtest_encdec("data/base58_encode_decode.json");
runtest_keys_valid("data/base58_keys_valid.json");
runtest_keys_invalid("data/base58_keys_invalid.json");

bp_key_static_shutdown();
return 0;
Expand Down
4 changes: 2 additions & 2 deletions test/blkdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static void runtest(const char *ser_base_fn, const struct chain_info *chain,

int main (int argc, char *argv[])
{
runtest("hdr193000.ser", &chain_metadata[CHAIN_BITCOIN], 193000,
runtest("data/hdr193000.ser", &chain_metadata[CHAIN_BITCOIN], 193000,
"000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317");
runtest("tn_hdr35141.ser", &chain_metadata[CHAIN_TESTNET3], 35141,
runtest("data/tn_hdr35141.ser", &chain_metadata[CHAIN_TESTNET3], 35141,
"0000000000dde6ce4b9ad1e2a5be59f1b7ace6ef8d077d846263b0bfbc984f7f");

bp_key_static_shutdown();
Expand Down
4 changes: 2 additions & 2 deletions test/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ static void runtest(const char *json_fn_base, const char *ser_fn_base)

int main (int argc, char *argv[])
{
runtest("blk0.json", "blk0.ser");
runtest("blk120383.json", "blk120383.ser");
runtest("data/blk0.json", "data/blk0.ser");
runtest("data/blk120383.json", "data/blk120383.ser");

bp_key_static_shutdown();
return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/blockfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void runtest(const char *ser_fn_base)

int main (int argc, char *argv[])
{
runtest("blks10.ser");
runtest("data/blks10.ser");

bp_key_static_shutdown();
return 0;
Expand Down
11 changes: 11 additions & 0 deletions test/data/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

EXTRA_DIST = \
base58_encode_decode.json \
base58_keys_invalid.json base58_keys_valid.json \
blk0.json blk0.ser blks10.ser \
blk120383.json blk120383.ser \
hdr193000.ser tn_hdr35141.ser \
random.data script_tests.json sighash.json \
tn_blk35133.ser tx3e0dc3da.json tx3e0dc3da.ser \
tx_invalid.json tx_valid.json \
wallet-basics.json
File renamed without changes.

0 comments on commit faa9b83

Please sign in to comment.