Add test for immutable content

This commit is contained in:
nolash 2021-10-19 12:36:17 +02:00
parent b05fc43086
commit 341a7eebec
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 86 additions and 42 deletions

View File

@ -37,7 +37,6 @@ function handleNoMergeGet(db, digest, keystore) {
format: 'binary', format: 'binary',
}; };
pgp.decrypt(opts).then((plainText) => { pgp.decrypt(opts).then((plainText) => {
console.debug('immutable ', rs.rows[0]['owner_fingerprint'], immutable);
let r; let r;
if (immutable) { if (immutable) {
r = plainText.data; r = plainText.data;

View File

@ -174,7 +174,6 @@ async function processRequest(req, res) {
} }
}); });
req.on('end', async (d) => { req.on('end', async (d) => {
console.debug('hedaers ', req.headers);
let inputContentType = req.headers['content-type']; let inputContentType = req.headers['content-type'];
let debugString = 'executing mode ' + mod ; let debugString = 'executing mode ' + mod ;
if (data !== undefined) { if (data !== undefined) {
@ -211,6 +210,7 @@ async function processRequest(req, res) {
res.end(); res.end();
return; return;
} }
content = '';
break; break;
//case 'get:automerge:server': //case 'get:automerge:server':
// content = await handlers.handleServerMergeGet(db, digest, keystore); // content = await handlers.handleServerMergeGet(db, digest, keystore);
@ -262,6 +262,9 @@ async function processRequest(req, res) {
// (new TextEncoder().encode(content)).length; // (new TextEncoder().encode(content)).length;
//} //}
const responseContentLength = content.length; const responseContentLength = content.length;
//if (responseContentLength === undefined) {
// responseContentLength = 0;
//}
res.writeHead(statusCode, { res.writeHead(statusCode, {
"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Origin": "*",
"Content-Type": contentType, "Content-Type": contentType,

View File

@ -7,6 +7,8 @@ import * as handlers from '../scripts/server/handlers';
import { Envelope, Syncable, ArgPair, PGPKeyStore, PGPSigner, KeyStore, Signer } from '@cicnet/crdt-meta'; import { Envelope, Syncable, ArgPair, PGPKeyStore, PGPSigner, KeyStore, Signer } from '@cicnet/crdt-meta';
import { SqliteAdapter } from '../src/db'; import { SqliteAdapter } from '../src/db';
const hashOfFoo = '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae';
function createKeystore() { function createKeystore() {
const pksa = fs.readFileSync(__dirname + '/privatekeys.asc', 'utf-8'); const pksa = fs.readFileSync(__dirname + '/privatekeys.asc', 'utf-8');
const pubksa = fs.readFileSync(__dirname + '/publickeys.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 () => { await it('server_merge_empty', async () => {
// const keystore = await createKeystore(); const keystore = await createKeystore();
// const signer = new PGPSigner(keystore); const signer = new PGPSigner(keystore);
//
// const db = await createDatabase(__dirname + '/db.three.sqlite'); const db = await createDatabase(__dirname + '/db.three.sqlite');
//
// const digest = '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; const digest = '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef';
// let o:any = { let o:any = {
// foo: 'bar', foo: 'bar',
// xyzzy: 42, xyzzy: 42,
// } }
// let j:any = JSON.stringify(o); let j:any = JSON.stringify(o);
//
// let signMaterial = await handlers.handleServerMergePost(j, db, digest, keystore, signer); let signMaterial = await handlers.handleServerMergePost(j, db, digest, keystore, signer);
// assert(signMaterial) assert(signMaterial)
//
// const env = Envelope.fromJSON(signMaterial); const env = Envelope.fromJSON(signMaterial);
//
// console.log('envvvv', env); console.log('envvvv', env);
//
// const signedData = await signData(env.o['digest'], keystore); const signedData = await signData(env.o['digest'], keystore);
// console.log('signed', signedData); console.log('signed', signedData);
//
// o = { o = {
// 'm': env, 'm': env,
// 's': signedData, 's': signedData,
// } }
// j = JSON.stringify(o); j = JSON.stringify(o);
// console.log(j); console.log(j);
//
// let v = await handlers.handleServerMergePut(j, db, digest, keystore, signer); let v = await handlers.handleServerMergePut(j, db, digest, keystore, signer);
// assert(v); assert(v);
//
// j = await handlers.handleNoMergeGet(db, digest, keystore); j = await handlers.handleNoMergeGet(db, digest, keystore);
// assert(j); // true-ish assert(j); // true-ish
// o = JSON.parse(j); o = JSON.parse(j[0]);
// console.log(o); console.log(o);
//
// db.close(); 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]);
});
}); });