kee

Offline IOU signer with QR as transport
git clone https://holbrook.no/src/kee
Info | Log | Files | Refs | README | LICENSE

commit 7f1835d18f7cc08ecf5469cbb9becc335611eb3f
parent 9326a579ede983bd4c0109ca19c45dc2e7f4810d
Author: lash <dev@holbrook.no>
Date:   Tue, 14 May 2024 15:13:19 +0100

WIP rehabilitate tests, investigating db put data corrupt

Diffstat:
Msrc/db.h | 8++++----
Msrc/ledger.c | 51+++++++++++++++++++++++++++++++++++++++++----------
Msrc/tests/ledger.c | 8++++----
Mtestdata_asn1.py | 4++++
4 files changed, 53 insertions(+), 18 deletions(-)

diff --git a/src/db.h b/src/db.h @@ -49,10 +49,10 @@ struct db_ctx { MDB_cursor *crsr; MDB_val k; MDB_val v; - MDB_val *add_k; - MDB_val *add_v; - size_t add_cap; - size_t add_count; +// MDB_val *add_k; +// MDB_val *add_v; +// size_t add_cap; +// size_t add_count; enum DbKey current_key; int started; int browsing; diff --git a/src/ledger.c b/src/ledger.c @@ -985,17 +985,18 @@ static struct kee_ledger_item_t* get_item_by_idx(struct kee_ledger_t *ledger, in return item; } -int kee_ledger_item_put(struct kee_ledger_t *ledger, struct db_ctx *db, int idx) { +static int kee_ledger_item_put_buf(struct kee_ledger_t *ledger, struct db_ctx *db, int idx, char *k, char *v) { int r; size_t c; size_t l; - char mem[4096]; - char *k; - char *v; + //char mem[4096]; + //char *k; + //char *v; struct kee_ledger_item_t *item; - k = (char*)mem; - v = k + 2048; + //k = (char*)mem; + //k = mem; + //v = k + 2048; item = get_item_by_idx(ledger, idx); if (item == NULL) { @@ -1030,6 +1031,13 @@ int kee_ledger_item_put(struct kee_ledger_t *ledger, struct db_ctx *db, int idx) return ERR_OK; } +int kee_ledger_item_put(struct kee_ledger_t *ledger, struct db_ctx *db, int idx) { + char k[2048]; + char v[2048]; + + return kee_ledger_item_put_buf(ledger, db, idx, (char*)k, (char*)v); +} + /// \todo atomic put for ledger and items!! /// \todo guard local k/v buffer overflow int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { @@ -1037,12 +1045,18 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { int i; size_t c; size_t l; - char mem[4096]; + //char *mem; + //char mem[4096]; char *k; char *v; - k = (char*)mem; - v = k + 2048; + //mem = malloc(4096); + //k = (char*)mem; + //v = k + 2048; + //k = mem; + //v = k + 2048; + k = malloc(2048); + v = malloc(2048); k[0] = DbKeyReverse; memcpy(k+1, ledger->digest, DIGEST_LENGTH); @@ -1058,6 +1072,8 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { db_rewind(db); r = db_next(db, DbKeyLedgerHead, &k, &l, &v, &c); if (!r) { + free(k); + free(v); return ERR_DB_EXISTS; } } @@ -1066,6 +1082,8 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { l = db_key(DbKeyLedgerHead, NULL, k, 0); if (l == 0) { + free(k); + free(v); return ERR_FAIL; } @@ -1073,17 +1091,23 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { c = 2048; r = kee_ledger_serialize(ledger, v, &c); if (r) { + free(k); + free(v); return ERR_DB_FAIL; } r = db_start(db); if (r) { + free(k); + free(v); return ERR_DB_FAIL; } //r = db_put(db, k, l, v, c); r = db_add(db, k, l, v, c); if (r) { + free(k); + free(v); return ERR_DB_FAIL; } @@ -1095,21 +1119,28 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { //r = db_put(db, k, l, v+1, c-1); r = db_add(db, k, l, v+1, c-1); if (r) { + free(k); + free(v); return ERR_DB_FAIL; } i = 0; r = 0; while (r == 0) { - r = kee_ledger_item_put(ledger, db, i); + r = kee_ledger_item_put_buf(ledger, db, i, k, v); i++; } r = db_finish(db); if (r) { + free(k); + free(v); return ERR_DB_FAIL; } + free(k); + free(v); + return ERR_OK; } diff --git a/src/tests/ledger.c b/src/tests/ledger.c @@ -129,10 +129,10 @@ int test_put() { return 1; } - r = kee_ledger_item_put(&t.ledger, &t.db, 0); - if (r) { - return 1; - } + //r = kee_ledger_item_put(&t.ledger, &t.db, 0); + //if (r) { + // return 1; + //} return 0; diff --git a/testdata_asn1.py b/testdata_asn1.py @@ -555,6 +555,10 @@ if __name__ == '__main__': k = h.digest() data_add(data_dir, k, v) + o = LedgerContent(subject='foo', body='bar') + (k, v) = o.kv() + data_add(data_dir, k, v) + d = os.path.dirname(__file__) crypto_dir = os.path.join(d, 'testdata', 'crypt') try: