1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-01 06:27:03 +03:00

Add a test app to assist in validating the SAHPool digest calculation fix.

FossilOrigin-Name: a1e304b8020025cc73a658bd8c7697d59b4f3ad96cac0a3e36553a3207d13dc6
This commit is contained in:
stephan
2025-02-03 17:21:54 +00:00
parent 40ce00b546
commit c97abeac0b
5 changed files with 247 additions and 12 deletions

View File

@ -659,7 +659,7 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
const fileDigest = new Uint32Array(HEADER_DIGEST_SIZE / 4);
sah.read(fileDigest, {at: HEADER_OFFSET_DIGEST});
const compDigest = this.computeDigest(this.#apBody, flags);
//console.warn("getAssociatedPath() compDigest",compDigest);
console.warn("getAssociatedPath() flags",flags.toString(16), "compDigest", compDigest);
if(fileDigest.every((v,i) => v===compDigest[i])){
// Valid digest
const pathBytes = this.#apBody.findIndex((v)=>0===v);
@ -700,7 +700,7 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
this.#apBody.fill(0, enc.written, HEADER_MAX_PATH_SIZE);
this.#dvBody.setUint32(HEADER_OFFSET_FLAGS, flags);
const digest = this.computeDigest(this.#apBody, flags);
//console.warn("setAssociatedPath(",path,") digest",digest);
console.warn("setAssociatedPath(",path,") digest",digest);
sah.write(this.#apBody, {at: 0});
sah.write(digest, {at: HEADER_OFFSET_DIGEST});
sah.flush();

View File

@ -0,0 +1,95 @@
/*
2025-01-31
The author disclaims copyright to this source code. In place of a
legal notice, here is a blessing:
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
***********************************************************************
This file is part of sahpool-pausing.js's demonstration of the
pause/unpause feature of the opfs-sahpool VFS.
*/
const clog = console.log.bind(console);
const wPost = (type,...args)=>postMessage({type, payload:args});
const log = (...args)=>{
clog("Worker:",...args);
wPost('log',...args);
}
const hasOpfs = ()=>{
return globalThis.FileSystemHandle
&& globalThis.FileSystemDirectoryHandle
&& globalThis.FileSystemFileHandle
&& globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle
&& navigator?.storage?.getDirectory;
};
if( !hasOpfs() ){
wPost('error',"OPFS not detected");
throw new Error("OPFS not detected");
}
clog("Importing sqlite3...");
const searchParams = new URL(self.location.href).searchParams;
importScripts(searchParams.get('sqlite3.dir') + '/sqlite3.js');
const runTests = function(sqlite3, poolUtil){
const fname = '/my.db';
let db = new poolUtil.OpfsSAHPoolDb(fname);
let n = (new Date()).valueOf();
try {
db.exec([
"create table if not exists t(a);"
]);
db.exec({
sql: "insert into t(a) values(?)",
bind: n++
});
log("Record count: ",db.selectValue("select count(*) from t"));
}finally{
db.close();
}
db = new poolUtil.OpfsSAHPoolDb(fname);
try {
db.exec({
sql: "insert into t(a) values(?)",
bind: n++
});
log("Record count: ",db.selectValue("select count(*) from t"));
}finally{
db.close();
}
const fname2 = '/my2.db';
db = new poolUtil.OpfsSAHPoolDb(fname2);
try {
db.exec([
"create table if not exists t(a);"
]);
db.exec({
sql: "insert into t(a) values(?)",
bind: n++
});
log("Record count: ",db.selectValue("select count(*) from t"));
}finally{
db.close();
}
};
globalThis.sqlite3InitModule().then(async function(sqlite3){
log("sqlite3 version:",sqlite3.capi.sqlite3_libversion(),
sqlite3.capi.sqlite3_sourceid());
const sahPoolConfig = {
name: 'opfs-sahpool-digest',
clearOnInit: false,
initialCapacity: 6
};
return sqlite3.installOpfsSAHPoolVfs(sahPoolConfig).then(poolUtil=>{
log('vfs acquired');
runTests(sqlite3, poolUtil);
});
});

View File

@ -0,0 +1,141 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<link rel="stylesheet" href="../../../common/emscripten.css"/>
<link rel="stylesheet" href="../../../common/testing.css"/>
<title>sqlite3 tester: OpfsSAHPool Digest</title>
<style></style>
</head>
<body><h1 id='color-target'></h1>
<p>
This is a test app for the digest calculation of the OPFS
SAHPool VFS. It requires running it with a new database created using
v3.49.0 or older, then running it again with a newer version, then
again with 3.49.0 or older.
</p>
<div class='input-wrapper'>
<input type='checkbox' id='cb-log-reverse'>
<label for='cb-log-reverse'>Reverse log order?</label>
</div>
<div id='test-output'></div>
<script>
/*
2025-02-03
The author disclaims copyright to this source code. In place of a
legal notice, here is a blessing:
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
***********************************************************************
This is a bugfix test for the OPFS SAHPool VFS. It requires setting up
a database created using v3.49.0 or older, then runnig it again with
a newer version.
*/
(function(){
'use strict';
document.querySelector('h1').innerHTML =
document.querySelector('title').innerHTML;
const mapToString = (v)=>{
switch(typeof v){
case 'number': case 'string': case 'boolean':
case 'undefined': case 'bigint':
return ''+v;
default: break;
}
if(null===v) return 'null';
if(v instanceof Error){
v = {
message: v.message,
stack: v.stack,
errorClass: v.name
};
}
return JSON.stringify(v,undefined,2);
};
const normalizeArgs = (args)=>args.map(mapToString);
const logTarget = document.querySelector('#test-output');
const logClass = function(cssClass,...args){
const ln = document.createElement('div');
if(cssClass){
for(const c of (Array.isArray(cssClass) ? cssClass : [cssClass])){
ln.classList.add(c);
}
}
ln.append(document.createTextNode(normalizeArgs(args).join(' ')));
logTarget.append(ln);
};
const cbReverse = document.querySelector('#cb-log-reverse');
//cbReverse.setAttribute('checked','checked');
const cbReverseKey = 'tester1:cb-log-reverse';
const cbReverseIt = ()=>{
logTarget.classList[cbReverse.checked ? 'add' : 'remove']('reverse');
//localStorage.setItem(cbReverseKey, cbReverse.checked ? 1 : 0);
};
cbReverse.addEventListener('change', cbReverseIt, true);
/*if(localStorage.getItem(cbReverseKey)){
cbReverse.checked = !!(+localStorage.getItem(cbReverseKey));
}*/
cbReverseIt();
const log = (...args)=>{
//console.log(...args);
logClass('',...args);
}
const warn = (...args)=>{
console.warn(...args);
logClass('warning',...args);
}
const error = (...args)=>{
console.error(...args);
logClass('error',...args);
};
const toss = (...args)=>{
error(...args);
throw new Error(args.join(' '));
};
const endOfWork = (passed=true)=>{
const eH = document.querySelector('#color-target');
const eT = document.querySelector('title');
if(passed){
log("End of work chain. If you made it this far, you win.");
eH.innerText = 'PASS: '+eH.innerText;
eH.classList.add('tests-pass');
eT.innerText = 'PASS: '+eT.innerText;
}else{
eH.innerText = 'FAIL: '+eH.innerText;
eH.classList.add('tests-fail');
eT.innerText = 'FAIL: '+eT.innerText;
}
};
log("Running opfs-sahpool digest tests...");
const W1 = new Worker('digest-worker.js?sqlite3.dir=../../../jswasm');
W1.onmessage = function({data}){
//log("onmessage:",data);
switch(data.type){
case 'log':
log('worker says:', ...data.payload);
break;
case 'error':
error('worker says:', ...data.payload);
endOfWork(false);
break;
case 'initialized':
log(data.workerId, ': Worker initialized',...data.payload);
break;
}
};
})();
</script>
</body>
</html>