From c44e668b8d78a27b2a11b64fb0f73844c2c912b9 Mon Sep 17 00:00:00 2001 From: jakecastelli Date: Sat, 20 Jul 2024 11:18:43 +0700 Subject: [PATCH 1/2] test: add coverage for webstorage quota --- test/parallel/test-webstorage.js | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/parallel/test-webstorage.js b/test/parallel/test-webstorage.js index effeb53f4f154a..fee44a8ffe83f7 100644 --- a/test/parallel/test-webstorage.js +++ b/test/parallel/test-webstorage.js @@ -109,3 +109,39 @@ test('localStorage is persisted if it is used', async () => { assert.strictEqual(cp.code, 0); assert.match(cp.stdout, /barbaz/); }); + +test('localStorage can store and retrieve a max of 10 MB quota', async () => { + const localStorageFile = nextLocalStorage(); + const cpToFill = await spawnPromisified(process.execPath, [ + '--experimental-webstorage', + '--localstorage-file', localStorageFile, + // Each character is 2 bytes + '-pe', `for (let i = 0; i < 10; i++) { + localStorage[i.toString().repeat(1 * 1024 * 1024 / 2)] = ''; + } + `, + ]); + const cpToVerify = await spawnPromisified(process.execPath, [ + '--experimental-webstorage', + '--localstorage-file', localStorageFile, + '-pe', `localStorage.anything = 'should fail';`, + ]); + + assert.doesNotMatch(cpToFill.stderr, /QuotaExceededError/); + assert.match(cpToVerify.stderr, /QuotaExceededError: Setting the value exceeded the quota/); +}); + +test('sessionStorage can store a max of 10 MB quota', async () => { + const cpToFill = await spawnPromisified(process.execPath, [ + '--experimental-webstorage', + // Each character is 2 bytes + '-pe', `for (let i = 0; i < 10; i++) { + sessionStorage[i.toString().repeat(1 * 1024 * 1024 / 2)] = ''; + } + + sessionStorage.anything = 'should fail'; + `, + ]); + + assert.match(cpToFill.stderr, /QuotaExceededError/); +}); From 70490d4159a63bfe27af0e4acc99481ff6ea8fdd Mon Sep 17 00:00:00 2001 From: jakecastelli Date: Sun, 28 Jul 2024 23:53:52 +0930 Subject: [PATCH 2/2] fixup! improve test --- test/parallel/test-webstorage.js | 62 ++++++++++++++++---------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/test/parallel/test-webstorage.js b/test/parallel/test-webstorage.js index fee44a8ffe83f7..4da6b67bd2932b 100644 --- a/test/parallel/test-webstorage.js +++ b/test/parallel/test-webstorage.js @@ -4,7 +4,7 @@ const tmpdir = require('../common/tmpdir'); const assert = require('node:assert'); const { join } = require('node:path'); const { readdir } = require('node:fs/promises'); -const { test } = require('node:test'); +const { test, describe } = require('node:test'); const { spawnPromisified } = common; let cnt = 0; @@ -110,38 +110,38 @@ test('localStorage is persisted if it is used', async () => { assert.match(cp.stdout, /barbaz/); }); -test('localStorage can store and retrieve a max of 10 MB quota', async () => { - const localStorageFile = nextLocalStorage(); - const cpToFill = await spawnPromisified(process.execPath, [ - '--experimental-webstorage', - '--localstorage-file', localStorageFile, - // Each character is 2 bytes - '-pe', `for (let i = 0; i < 10; i++) { - localStorage[i.toString().repeat(1 * 1024 * 1024 / 2)] = ''; - } - `, - ]); - const cpToVerify = await spawnPromisified(process.execPath, [ - '--experimental-webstorage', - '--localstorage-file', localStorageFile, - '-pe', `localStorage.anything = 'should fail';`, - ]); - assert.doesNotMatch(cpToFill.stderr, /QuotaExceededError/); - assert.match(cpToVerify.stderr, /QuotaExceededError: Setting the value exceeded the quota/); -}); +describe('webstorage quota for localStorage and sessionStorage', () => { + const MAX_STORAGE_SIZE = 10 * 1024 * 1024; -test('sessionStorage can store a max of 10 MB quota', async () => { - const cpToFill = await spawnPromisified(process.execPath, [ - '--experimental-webstorage', - // Each character is 2 bytes - '-pe', `for (let i = 0; i < 10; i++) { - sessionStorage[i.toString().repeat(1 * 1024 * 1024 / 2)] = ''; - } + test('localStorage can store and retrieve a max of 10 MB quota', async () => { + const localStorageFile = nextLocalStorage(); + const cp = await spawnPromisified(process.execPath, [ + '--experimental-webstorage', + '--localstorage-file', localStorageFile, + // Each character is 2 bytes + '-pe', ` + localStorage['a'.repeat(${MAX_STORAGE_SIZE} / 2)] = ''; + console.error('filled'); + localStorage.anything = 'should fail'; + `, + ]); - sessionStorage.anything = 'should fail'; - `, - ]); + assert.match(cp.stderr, /filled/); + assert.match(cp.stderr, /QuotaExceededError: Setting the value exceeded the quota/); + }); + + test('sessionStorage can store a max of 10 MB quota', async () => { + const cp = await spawnPromisified(process.execPath, [ + '--experimental-webstorage', + // Each character is 2 bytes + '-pe', `sessionStorage['a'.repeat(${MAX_STORAGE_SIZE} / 2)] = ''; + console.error('filled'); + sessionStorage.anything = 'should fail'; + `, + ]); - assert.match(cpToFill.stderr, /QuotaExceededError/); + assert.match(cp.stderr, /filled/); + assert.match(cp.stderr, /QuotaExceededError/); + }); });