From 341a7eebec55b20a20a611b7258f3deab0ad220e Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 19 Oct 2021 12:36:17 +0200 Subject: [PATCH] Add test for immutable content --- apps/cic-meta/scripts/server/handlers.ts | 1 - apps/cic-meta/scripts/server/server.ts | 5 +- apps/cic-meta/tests/server.ts | 122 +++++++++++++++-------- 3 files changed, 86 insertions(+), 42 deletions(-) diff --git a/apps/cic-meta/scripts/server/handlers.ts b/apps/cic-meta/scripts/server/handlers.ts index 946fdcfa..6cb71b15 100644 --- a/apps/cic-meta/scripts/server/handlers.ts +++ b/apps/cic-meta/scripts/server/handlers.ts @@ -37,7 +37,6 @@ function handleNoMergeGet(db, digest, keystore) { format: 'binary', }; pgp.decrypt(opts).then((plainText) => { - console.debug('immutable ', rs.rows[0]['owner_fingerprint'], immutable); let r; if (immutable) { r = plainText.data; diff --git a/apps/cic-meta/scripts/server/server.ts b/apps/cic-meta/scripts/server/server.ts index c3a33502..66fde62a 100755 --- a/apps/cic-meta/scripts/server/server.ts +++ b/apps/cic-meta/scripts/server/server.ts @@ -174,7 +174,6 @@ async function processRequest(req, res) { } }); req.on('end', async (d) => { - console.debug('hedaers ', req.headers); let inputContentType = req.headers['content-type']; let debugString = 'executing mode ' + mod ; if (data !== undefined) { @@ -211,6 +210,7 @@ async function processRequest(req, res) { res.end(); return; } + content = ''; break; //case 'get:automerge:server': // content = await handlers.handleServerMergeGet(db, digest, keystore); @@ -262,6 +262,9 @@ async function processRequest(req, res) { // (new TextEncoder().encode(content)).length; //} const responseContentLength = content.length; + //if (responseContentLength === undefined) { + // responseContentLength = 0; + //} res.writeHead(statusCode, { "Access-Control-Allow-Origin": "*", "Content-Type": contentType, diff --git a/apps/cic-meta/tests/server.ts b/apps/cic-meta/tests/server.ts index 2440abeb..f226f9c0 100644 --- a/apps/cic-meta/tests/server.ts +++ b/apps/cic-meta/tests/server.ts @@ -7,6 +7,8 @@ import * as handlers from '../scripts/server/handlers'; import { Envelope, Syncable, ArgPair, PGPKeyStore, PGPSigner, KeyStore, Signer } from '@cicnet/crdt-meta'; import { SqliteAdapter } from '../src/db'; +const hashOfFoo = '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'; + function createKeystore() { const pksa = fs.readFileSync(__dirname + '/privatekeys.asc', 'utf-8'); const pubksa = fs.readFileSync(__dirname + '/publickeys.asc', 'utf-8'); @@ -276,45 +278,85 @@ describe('server', async () => { // -// await it('server_merge_empty', async () => { -// const keystore = await createKeystore(); -// const signer = new PGPSigner(keystore); -// -// const db = await createDatabase(__dirname + '/db.three.sqlite'); -// -// const digest = '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; -// let o:any = { -// foo: 'bar', -// xyzzy: 42, -// } -// let j:any = JSON.stringify(o); -// -// let signMaterial = await handlers.handleServerMergePost(j, db, digest, keystore, signer); -// assert(signMaterial) -// -// const env = Envelope.fromJSON(signMaterial); -// -// console.log('envvvv', env); -// -// const signedData = await signData(env.o['digest'], keystore); -// console.log('signed', signedData); -// -// o = { -// 'm': env, -// 's': signedData, -// } -// j = JSON.stringify(o); -// console.log(j); -// -// let v = await handlers.handleServerMergePut(j, db, digest, keystore, signer); -// assert(v); -// -// j = await handlers.handleNoMergeGet(db, digest, keystore); -// assert(j); // true-ish -// o = JSON.parse(j); -// console.log(o); -// -// db.close(); -// }); + await it('server_merge_empty', async () => { + const keystore = await createKeystore(); + const signer = new PGPSigner(keystore); + + const db = await createDatabase(__dirname + '/db.three.sqlite'); + + const digest = '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; + let o:any = { + foo: 'bar', + xyzzy: 42, + } + let j:any = JSON.stringify(o); + + let signMaterial = await handlers.handleServerMergePost(j, db, digest, keystore, signer); + assert(signMaterial) + + const env = Envelope.fromJSON(signMaterial); + + console.log('envvvv', env); + + const signedData = await signData(env.o['digest'], keystore); + console.log('signed', signedData); + + o = { + 'm': env, + 's': signedData, + } + j = JSON.stringify(o); + console.log(j); + + let v = await handlers.handleServerMergePut(j, db, digest, keystore, signer); + assert(v); + + j = await handlers.handleNoMergeGet(db, digest, keystore); + assert(j); // true-ish + o = JSON.parse(j[0]); + console.log(o); + + db.close(); + }); + + await it('immutable_nodigest', async() => { + const keystore = await createKeystore(); + const db = await createDatabase(__dirname + '/db.three.sqlite'); + + const s:string = 'foo'; + let r; + r = await handlers.handleImmutablePost(s, db, undefined, keystore, 'text/plain'); + assert(r[0]); + assert(hashOfFoo == r[1]); + + r = await handlers.handleImmutablePost(s, db, undefined, keystore, 'text/plain'); + assert(!r[0]); + assert(hashOfFoo == r[1]); + + const b:Uint8Array = new TextEncoder().encode(s); + r = await handlers.handleImmutablePost(b, db, undefined, keystore, 'text/plain'); + assert(!r[0]); + assert(hashOfFoo == r[1]); + }); + + await it('immutable_digest', async() => { + const keystore = await createKeystore(); + const db = await createDatabase(__dirname + '/db.three.sqlite'); + + const s:string = 'foo'; + const b:Uint8Array = new TextEncoder().encode(s); + let r; + r = await handlers.handleImmutablePost(b, db, hashOfFoo, keystore, 'application/octet-stream'); + assert(r[0]); + assert(hashOfFoo == r[1]); + + r = await handlers.handleImmutablePost(b, db, hashOfFoo, keystore, 'application/octet-stream'); + assert(!r[0]); + assert(hashOfFoo == r[1]); + + r = await handlers.handleImmutablePost(s, db, hashOfFoo, keystore, 'text/plain'); + assert(!r[0]); + assert(hashOfFoo == r[1]); + }); });