From 5d29345147c6df64f4c3c97713be813ddfd8aa98 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 13:59:11 +0400 Subject: [PATCH 01/14] Pass parameter to encodeRainDocument test --- assembly/test_encoder.ts | 7 +++---- tests/encoding.test.js | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/assembly/test_encoder.ts b/assembly/test_encoder.ts index eefca67..88aabf9 100644 --- a/assembly/test_encoder.ts +++ b/assembly/test_encoder.ts @@ -1,6 +1,6 @@ import { CBOREncoder } from "./encoder"; -export function encodeRainDocument(): ArrayBuffer { +export function encodeRainDocument(dataStr:string): ArrayBuffer { const encoder = new CBOREncoder(); encoder.addObject(3); @@ -9,10 +9,9 @@ export function encodeRainDocument(): ArrayBuffer { // h'11223344' = [28, 74, 54,020] const arr: u8[] = []; - const bytes = "11223344"; let lengthBytes = 0; - for (let i = 0; i < bytes.length; i += 2) { - const byte = bytes.substr(i, 2); + for (let i = 0; i < dataStr.length; i += 2) { + const byte = dataStr.substr(i, 2); const value = parseInt(byte, 16); arr.push(u8(value)); lengthBytes += 1; diff --git a/tests/encoding.test.js b/tests/encoding.test.js index 9bf2d43..2be411f 100644 --- a/tests/encoding.test.js +++ b/tests/encoding.test.js @@ -41,8 +41,7 @@ let result; describe("Encoding", () => { it("Test encodeRainDocument function", () => { - result = encodeRainDocument(); - + result = encodeRainDocument("11223344"); assert.equal( "a3004411223344011bffc21bbf86cc199b02706170706c69636174696f6e2f63626f72", buf2hex(result) From 663b90216f328477cf4227f75e5b6c2c96a3e698 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 14:58:46 +0400 Subject: [PATCH 02/14] Remove unnecessary decode calls in rainCbor --- tests/rainCbor.test.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index afe1435..d5daa2b 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -11,10 +11,6 @@ describe("Rain Cbor", () => { const result = decodeRainCBOR_test(); assert.equal(result, true, "Failed when decoded a rain cbor sequence"); - decode("A3004412345678011BFFE5FFB4A3FF2CDE026474657874"); - decode("4f726465725f41"); - decode("6F726465725F61"); - const resultError = decode_assertError( "ffe5ffb4a3ff2cdea50058d08d91c10e82300c86df65672e9ae8c177c083315e8c31454a32031dd93ae34278778b828006f1b6a6fffeff6b7bac94a6d2b3539be793d112e4fb50a2daa824303a152982a2293d15c820350fdbf5e92dd81ade81a658548745af436b8d557554292043a1305ed232c81d46e3f014ef980e5a231848538b6e80e3503ed83ea6133441335e5eeae56a3df0f2c9152fdc9b758a3fcc3eb634b3a3afe5dc90b88d99bac314ee798a778eeffc03100bcdf16b06e3b94592b663608c3d43a273cd419472cc12022439f67699a70b6b43e2f800011bffe5ffb4a3ff2cde02706170706c69636174696f6e2f6a736f6e03676465666c6174650462656e" ); From 63e51121715d810fab9d453cb1867f5b71f22cff Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 14:59:03 +0400 Subject: [PATCH 03/14] Add test for decode() --- tests/decoding.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/decoding.test.js b/tests/decoding.test.js index 44c086d..1bd06fd 100644 --- a/tests/decoding.test.js +++ b/tests/decoding.test.js @@ -16,6 +16,7 @@ import { decodeAllInObj, decodeNestedObjs, decodeArrayInArray, + decode } from "../build/debug.js"; let result; @@ -73,6 +74,10 @@ describe("Decoding", () => { result = decodeObject() assert.equal(result, true); }) + it("Test decode function", () => { + result = decode("0x010101") + assert.deepEqual(result, {}); + }) }) // From cf3dd1bacf5c436b4bdfe0194bda51cf569a63e6 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 15:18:53 +0400 Subject: [PATCH 04/14] Add test for decodeRainCBOR_test and decode_assertError when error --- tests/decoding.test.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/decoding.test.js b/tests/decoding.test.js index 1bd06fd..4688dab 100644 --- a/tests/decoding.test.js +++ b/tests/decoding.test.js @@ -11,12 +11,11 @@ import { decodeObject, decodeArrayU8, decodeArray, - decodeAllInArray, decodeBytes, - decodeAllInObj, - decodeNestedObjs, + decodeRainCBOR_test, decodeArrayInArray, - decode + decode, + decode_assertError, } from "../build/debug.js"; let result; @@ -78,6 +77,14 @@ describe("Decoding", () => { result = decode("0x010101") assert.deepEqual(result, {}); }) + it("Test decodeRainCBOR_test function", () => { + result = decodeRainCBOR_test() + assert.deepEqual(result, true); + }) + it("Test decode_assertError function when error", () => { + result = decode_assertError("0101") + assert.equal(result, false); + }) }) // From e24211341a4a5f491270bb70f2e51611fde6fdf8 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 15:56:50 +0400 Subject: [PATCH 05/14] Pass arg to decodeRainCBOR_test --- assembly/test_decoder.ts | 4 +--- tests/decoding.test.js | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/assembly/test_decoder.ts b/assembly/test_decoder.ts index 5317922..a059b8c 100644 --- a/assembly/test_decoder.ts +++ b/assembly/test_decoder.ts @@ -40,10 +40,8 @@ export function decode(data: string): Value { return res; } -export function decodeRainCBOR_test(): bool { +export function decodeRainCBOR_test(rainSequenceEncoded: string): bool { // Use: https://cbor.me/ - const rainSequenceEncoded = - "A3004411223344011BFFC21BBF86CC199B02706170706C69636174696F6E2F63626F72A3004412345678011BFFE5FFB4A3FF2CDE02706170706C69636174696F6E2F6A736F6E"; const buff = stringToArrayBuffer(rainSequenceEncoded); const decoder = new CBORDecoder(buff); const res = decoder.parse(); diff --git a/tests/decoding.test.js b/tests/decoding.test.js index 4688dab..26837b6 100644 --- a/tests/decoding.test.js +++ b/tests/decoding.test.js @@ -78,7 +78,7 @@ describe("Decoding", () => { assert.deepEqual(result, {}); }) it("Test decodeRainCBOR_test function", () => { - result = decodeRainCBOR_test() + result = decodeRainCBOR_test("A3004411223344011BFFC21BBF86CC199B02706170706C69636174696F6E2F63626F72A3004412345678011BFFE5FFB4A3FF2CDE02706170706C69636174696F6E2F6A736F6E") assert.deepEqual(result, true); }) it("Test decode_assertError function when error", () => { From d4c3c511632a3bf03463f8573b86ddd87366b29c Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 16:22:01 +0400 Subject: [PATCH 06/14] Pass arg to decodeRainCBOR_test from rain cbor --- tests/rainCbor.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index d5daa2b..e0f1f6d 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -8,7 +8,7 @@ import { describe("Rain Cbor", () => { it("Test rain cbor", () => { - const result = decodeRainCBOR_test(); + const result = decodeRainCBOR_test("A3004411223344011BFFC21BBF86CC199B02706170706C69636174696F6E2F63626F72A3004412345678011BFFE5FFB4A3FF2CDE02706170706C69636174696F6E2F6A736F6E"); assert.equal(result, true, "Failed when decoded a rain cbor sequence"); const resultError = decode_assertError( From 197a85af6098bc86da7cd3d077cc56a06fed2cf5 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 16:39:04 +0400 Subject: [PATCH 07/14] Should return false for an invalid encoded rain sequence --- tests/rainCbor.test.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index e0f1f6d..95d24b3 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -1,20 +1,23 @@ -import { strict as assert } from "assert"; +import {strict as assert} from "assert"; import { - decodeRainCBOR_test, - decode, - decode_assertError + decodeRainCBOR_test, + decode_assertError } from "../build/debug.js"; describe("Rain Cbor", () => { - it("Test rain cbor", () => { + it("Test rain cbor", () => { - const result = decodeRainCBOR_test("A3004411223344011BFFC21BBF86CC199B02706170706C69636174696F6E2F63626F72A3004412345678011BFFE5FFB4A3FF2CDE02706170706C69636174696F6E2F6A736F6E"); - assert.equal(result, true, "Failed when decoded a rain cbor sequence"); + const result = decodeRainCBOR_test("A3004411223344011BFFC21BBF86CC199B02706170706C69636174696F6E2F63626F72A3004412345678011BFFE5FFB4A3FF2CDE02706170706C69636174696F6E2F6A736F6E"); + assert.equal(result, true, "Failed when decoded a rain cbor sequence"); - const resultError = decode_assertError( - "ffe5ffb4a3ff2cdea50058d08d91c10e82300c86df65672e9ae8c177c083315e8c31454a32031dd93ae34278778b828006f1b6a6fffeff6b7bac94a6d2b3539be793d112e4fb50a2daa824303a152982a2293d15c820350fdbf5e92dd81ade81a658548745af436b8d557554292043a1305ed232c81d46e3f014ef980e5a231848538b6e80e3503ed83ea6133441335e5eeae56a3df0f2c9152fdc9b758a3fcc3eb634b3a3afe5dc90b88d99bac314ee798a778eeffc03100bcdf16b06e3b94592b663608c3d43a273cd419472cc12022439f67699a70b6b43e2f800011bffe5ffb4a3ff2cde02706170706c69636174696f6e2f6a736f6e03676465666c6174650462656e" - ); - assert.equal(resultError, true); + }); - }); + it("Should return false for an invalid encoded rain sequence", () => { + + const resultError = decode_assertError( + "ffe5ffb4a3ff2cdea50058d08d91c10e82300c86df65672e9ae8c177c083315e8c31454a32031dd93ae34278778b828006f1b6a6fffeff6b7bac94a6d2b3539be793d112e4fb50a2daa824303a152982a2293d15c820350fdbf5e92dd81ade81a658548745af436b8d557554292043a1305ed232c81d46e3f014ef980e5a231848538b6e80e3503ed83ea6133441335e5eeae56a3df0f2c9152fdc9b758a3fcc3eb634b3a3afe5dc90b88d99bac314ee798a778eeffc03100bcdf16b06e3b94592b663608c3d43a273cd419472cc12022439f67699a70b6b43e2f800011bffe5ffb4a3ff2cde02706170706c69636174696f6e2f6a736f6e03676465666c6174650462656e" + ); + assert.equal(resultError, true); + + }); }); \ No newline at end of file From 83e8a10cb943b1bd200c6c7654affd2ac9352965 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 16:44:24 +0400 Subject: [PATCH 08/14] Add checks for decodeRainCBOR_test when input is "" or not cbor encoded --- tests/rainCbor.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index 95d24b3..6827f9e 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -20,4 +20,14 @@ describe("Rain Cbor", () => { assert.equal(resultError, true); }); + + it('should return false for an empty input string', () => { + const resultError = decodeRainCBOR_test(""); + assert.equal(resultError, false); + }); + + it('should return false for a non-CBOR encoded string', () => { + const resultError = decodeRainCBOR_test('this is not CBOR encoded'); + assert.equal(resultError, false); + }); }); \ No newline at end of file From 4dc08ceb97fa1993b4ad2d1f1e279c27bad77905 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 17:08:06 +0400 Subject: [PATCH 09/14] Update variable --- tests/rainCbor.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index 6827f9e..2f4d796 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -22,12 +22,12 @@ describe("Rain Cbor", () => { }); it('should return false for an empty input string', () => { - const resultError = decodeRainCBOR_test(""); - assert.equal(resultError, false); + const result = decodeRainCBOR_test(""); + assert.equal(result, false); }); it('should return false for a non-CBOR encoded string', () => { - const resultError = decodeRainCBOR_test('this is not CBOR encoded'); - assert.equal(resultError, false); + const result = decodeRainCBOR_test('this is not CBOR encoded'); + assert.equal(result, false); }); }); \ No newline at end of file From 5c83b60e8d2b17fe5e0f5a28f3889c75eef6643b Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 17:20:26 +0400 Subject: [PATCH 10/14] Add decodeRandomString function and test for Empty data --- assembly/test_decoder.ts | 8 ++++++++ tests/rainCbor.test.js | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/assembly/test_decoder.ts b/assembly/test_decoder.ts index a059b8c..78d46f5 100644 --- a/assembly/test_decoder.ts +++ b/assembly/test_decoder.ts @@ -463,3 +463,11 @@ export function decodeAllInArray(): boolean { bytesResult ); } + +export function decodeRandomString(dataStr:string): string { + const buff = stringToArrayBuffer(dataStr); + + const decoder = new CBORDecoder(buff); + const res = decoder.parse(); + return res.toString(); +} diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index 2f4d796..f611965 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -1,7 +1,8 @@ import {strict as assert} from "assert"; import { decodeRainCBOR_test, - decode_assertError + decode_assertError, + decodeRandomString } from "../build/debug.js"; describe("Rain Cbor", () => { @@ -30,4 +31,9 @@ describe("Rain Cbor", () => { const result = decodeRainCBOR_test('this is not CBOR encoded'); assert.equal(result, false); }); + + it('should return "Data_Empty" if passed empty string', () => { + const result = decodeRandomString(''); + assert.equal(result, 'DATA_EMPTY'); + }); }); \ No newline at end of file From 3fc2f0363ca6bfd758b969553e1140304ff0e020 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Thu, 11 Apr 2024 18:50:55 +0400 Subject: [PATCH 11/14] Failing test of not encoded data --- tests/rainCbor.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index f611965..423c80e 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -36,4 +36,9 @@ describe("Rain Cbor", () => { const result = decodeRandomString(''); assert.equal(result, 'DATA_EMPTY'); }); + + it('should check random string not cbor decoded', () => { + const result = decodeRandomString('Random not encoded string'); + assert.equal(result, 'DATA_EMPTY'); + }); }); \ No newline at end of file From a589ed28c0737808e61ca38ca43c60c1fa5752c4 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Fri, 12 Apr 2024 00:01:36 +0400 Subject: [PATCH 12/14] Fix random string index issue --- tests/rainCbor.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index 423c80e..af5e8c2 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -38,7 +38,7 @@ describe("Rain Cbor", () => { }); it('should check random string not cbor decoded', () => { - const result = decodeRandomString('Random not encoded string'); + const result = decodeRandomString('Random not cbor-encoded string'); assert.equal(result, 'DATA_EMPTY'); }); }); \ No newline at end of file From 467f78fd4082c2afb9a4e50f30fef884d6137629 Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Fri, 12 Apr 2024 00:03:55 +0400 Subject: [PATCH 13/14] Correct expect --- tests/rainCbor.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index af5e8c2..fe30240 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -39,6 +39,6 @@ describe("Rain Cbor", () => { it('should check random string not cbor decoded', () => { const result = decodeRandomString('Random not cbor-encoded string'); - assert.equal(result, 'DATA_EMPTY'); + assert.equal(result, '[0,0,0,0,0,12,11,0,14,12,NO_VALUE_0,13,0,0,0]'); }); }); \ No newline at end of file From bc4d1b5e1c0daa3401574c71e61fde1cfd54267a Mon Sep 17 00:00:00 2001 From: ninokeldishvili Date: Fri, 12 Apr 2024 13:48:50 +0400 Subject: [PATCH 14/14] Should returns error message for an invalid encoded rain sequence --- tests/rainCbor.test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/rainCbor.test.js b/tests/rainCbor.test.js index fe30240..008ae8c 100644 --- a/tests/rainCbor.test.js +++ b/tests/rainCbor.test.js @@ -2,7 +2,8 @@ import {strict as assert} from "assert"; import { decodeRainCBOR_test, decode_assertError, - decodeRandomString + decodeRandomString, + decode } from "../build/debug.js"; describe("Rain Cbor", () => { @@ -20,6 +21,14 @@ describe("Rain Cbor", () => { ); assert.equal(resultError, true); + }); + it("Should returns error message for an invalid encoded rain sequence", () => { + + const result = decodeRandomString( + "ffe5ffb4a3ff2cdea50058d08d91c10e82300c86df65672e9ae8c177c083315e8c31454a32031dd93ae34278778b828006f1b6a6fffeff6b7bac94a6d2b3539be793d112e4fb50a2daa824303a152982a2293d15c820350fdbf5e92dd81ade81a658548745af436b8d557554292043a1305ed232c81d46e3f014ef980e5a231848538b6e80e3503ed83ea6133441335e5eeae56a3df0f2c9152fdc9b758a3fcc3eb634b3a3afe5dc90b88d99bac314ee798a778eeffc03100bcdf16b06e3b94592b663608c3d43a273cd419472cc12022439f67699a70b6b43e2f800011bffe5ffb4a3ff2cde02706170706c69636174696f6e2f6a736f6e03676465666c6174650462656e" + ); + assert.equal(result, 'DATA_BAD_FORMED'); + }); it('should return false for an empty input string', () => {