forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_tests.c
91 lines (79 loc) · 2.78 KB
/
serialize_tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**********************************************************************
* Copyright (c) 2015 Jonas Schnelli *
* Copyright (c) 2022 bluezr *
* Copyright (c) 2022 The Dogecoin Foundation *
* Distributed under the MIT software license, see the accompanying *
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
**********************************************************************/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dogecoin/cstr.h>
#include <dogecoin/serialize.h>
#include <dogecoin/utils.h>
void test_serialize()
{
cstring* s3 = cstr_new("foo");
cstring* s2 = cstr_new_sz(200);
struct const_buffer buf2;
uint16_t num0;
uint32_t num1;
int32_t num1a;
uint64_t num2;
int64_t num2a;
uint32_t num3;
char strbuf[255];
cstring* deser_test;
struct const_buffer buf3;
uint16_t u16;
uint32_t u32;
uint64_t u64;
int32_t i32;
uint8_t hash[32] = {0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03};
uint8_t hashc[32];
ser_u16(s2, 0xAAFF);
ser_u32(s2, 0xFFFFFFFF);
ser_s32(s2, 0xFFFFFFFF);
ser_u64(s2, 0x99FF99FFDDBBAAFF);
ser_s64(s2, 0x99FF99FFDDBBAAFF);
ser_varlen(s2, 10);
ser_varlen(s2, 1000);
ser_varlen(s2, 100000000);
ser_str(s2, "test", 4);
ser_varstr(s2, s3);
ser_u256(s2, hash);
cstr_free(s3, true);
buf2.p = s2->str;
buf2.len = s2->len;
deser_u16(&num0, &buf2);
assert(num0 == 43775); /* 0xAAFF */
deser_u32(&num1, &buf2);
assert(num1 == 4294967295); /* 0xFFFFFFFF */
deser_s32(&num1a, &buf2);
assert(num1a == -1); /* 0xFFFFFFFF signed */
deser_u64(&num2, &buf2);
assert(num2 == 0x99FF99FFDDBBAAFF); /* 0x99FF99FFDDBBAAFF */
deser_s64(&num2a, &buf2);
assert(num2a == (int64_t)0x99FF99FFDDBBAAFF); /* 0x99FF99FFDDBBAAFF */
deser_varlen(&num3, &buf2);
assert(num3 == 10);
deser_varlen(&num3, &buf2);
assert(num3 == 1000);
deser_varlen(&num3, &buf2);
assert(num3 == 100000000);
deser_str(strbuf, &buf2, 255);
assert(strncmp(strbuf, "test", 4) == 0);
deser_test = cstr_new_sz(0);
deser_varstr(&deser_test, &buf2);
assert(strncmp(deser_test->str, "foo", 3) == 0);
deser_u256(hashc, &buf2);
assert(memcmp(hash, hashc, 32) == 0);
cstr_free(deser_test, true);
cstr_free(s2, true);
buf3.p = NULL, buf3.len = 0;
assert(deser_u16(&u16, &buf3) == false);
assert(deser_u32(&u32, &buf3) == false);
assert(deser_u64(&u64, &buf3) == false);
assert(deser_s32(&i32, &buf3) == false);
}