From 581c53739693f54150fcef25fc0fdd4119c2a7b0 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 12 Sep 2022 09:36:19 +0200 Subject: [PATCH] test(crypto-js): Properly test `Qr.toQrCode. --- bindings/matrix-sdk-crypto-js/package.json | 9 +-- .../matrix-sdk-crypto-js/tests/device.test.js | 59 +++++++++++++++++-- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/package.json b/bindings/matrix-sdk-crypto-js/package.json index ed439f237..02f1108c7 100644 --- a/bindings/matrix-sdk-crypto-js/package.json +++ b/bindings/matrix-sdk-crypto-js/package.json @@ -26,12 +26,13 @@ "pkg/matrix_sdk_crypto.d.ts" ], "devDependencies": { - "wasm-pack": "^0.10.2", + "canvas": "^2.10.1", + "cross-env": "^7.0.3", + "fake-indexeddb": "^4.0", "jest": "^28.1.0", "typedoc": "^0.22.17", - "cross-env": "^7.0.3", - "yargs-parser": "~21.0.1", - "fake-indexeddb": "^4.0" + "wasm-pack": "^0.10.2", + "yargs-parser": "~21.0.1" }, "engines": { "node": ">= 10" diff --git a/bindings/matrix-sdk-crypto-js/tests/device.test.js b/bindings/matrix-sdk-crypto-js/tests/device.test.js index 441134936..ace7e8715 100644 --- a/bindings/matrix-sdk-crypto-js/tests/device.test.js +++ b/bindings/matrix-sdk-crypto-js/tests/device.test.js @@ -719,13 +719,60 @@ describe('Key Verification', () => { expect(qrCode).toBeInstanceOf(QrCode); - const buffer = qrCode.renderIntoBuffer(); + let canvasBuffer; - expect(buffer).toBeInstanceOf(Uint8ClampedArray); - // 45px ⨉ 45px - expect(buffer).toHaveLength(45 * 45); - // 0 for a white pixel, 1 for a black pixel. - expect(buffer.every(p => p == 0 || p == 1)).toStrictEqual(true); + { + const buffer = qrCode.renderIntoBuffer(); + + expect(buffer).toBeInstanceOf(Uint8ClampedArray); + // 45px ⨉ 45px + expect(buffer).toHaveLength(45 * 45); + // 0 for a white pixel, 1 for a black pixel. + expect(buffer.every(p => p == 0 || p == 1)).toStrictEqual(true); + + const { Canvas } = require('canvas'); + const canvas = new Canvas(45, 45); + + const context = canvas.getContext('2d'); + // New image data, filled with black, transparent pixels. + const imageData = context.createImageData(45, 45); + const data = imageData.data; + + for ( + let dataNth = 0, + bufferNth = 0; + dataNth < data.length && bufferNth < buffer.length; + dataNth += 4, + bufferNth += 1 + ) { + // White pixel + if (buffer[bufferNth] == 0) { + data[dataNth] = 255; + data[dataNth + 1] = 255; + data[dataNth + 2] = 255; + data[dataNth + 3] = 255; + } + } + + context.putImageData(imageData, 0, 0); + canvasBuffer = canvas.toBuffer('image/png'); + } + + // Want to see the QR code? Uncomment the following block. + /* + { + const fs = require('fs/promises'); + const path = require('path'); + const os = require('os'); + + const tempDirectory = await fs.mkdtemp(path.join(os.tmpdir(), 'matrix-sdk-crypto--')); + const qrCodeFile = path.join(tempDirectory, 'qrcode.png'); + + console.log(`View the QR code at \`${qrCodeFile}\`.`); + + expect(await fs.writeFile(qrCodeFile, canvasBuffer)).toBeUndefined(); + } + */ }); }); });