Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for bigIntToBase64Url #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion auth/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,19 @@ <h2>Message log</h2>
) {
throw new Error("cannot create uint8array from invalid hex string");
}

var buffer = new Uint8Array(
hexString.match(/../g).map((h) => parseInt(h, 16))
);
if (!length) {
return buffer;
}
if (hexString.length / 2 > length) {
throw new Error(
"hex value cannot fit in a buffer of " + length + " byte(s)"
);
}

var paddedBuffer = new Uint8Array(length);
paddedBuffer.set(buffer, length - buffer.length);
return paddedBuffer;
Expand Down Expand Up @@ -588,7 +595,7 @@ <h2>Message log</h2>
/**
* Converts a `BigInt` into a base64url encoded string
* @param {BigInt} num
* @param {number} length: optional number of bytes contained in the resulting string
* @param {number} length: optional number of bytes contained in the intermediary buffer before encoding as base64url
* @return {string}
*/
var bigIntToBase64Url = function (num, length) {
Expand Down Expand Up @@ -990,6 +997,7 @@ <h2>Message log</h2>
base64urlEncode,
base64urlDecode,
base58checkDecode,
bigIntToBase64Url,
bigIntToHex,
stringToBase64urlString,
uint8arrayToHexString,
Expand Down
18 changes: 18 additions & 0 deletions auth/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ describe("TKHQ", () => {
expect(TKHQ.uint8arrayFromHexString("01", 2).toString()).toEqual("0,1");
// Happy path: if length parameter is omitted, do not pad the resulting buffer
expect(TKHQ.uint8arrayFromHexString("01").toString()).toEqual("1");
// Error case: hex value cannot fit in desired length
expect(() => {
TKHQ.uint8arrayFromHexString("0100", 1).toString(); // the number 256 cannot fit into 1 byte
}).toThrow("hex value cannot fit in a buffer of 1 byte(s)");
});

it("contains bigIntToHex", () => {
Expand All @@ -275,6 +279,20 @@ describe("TKHQ", () => {
}).toThrow("number cannot fit in a hex string of 2 characters");
});

it("contains bigIntToBase64Url", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rest of the above is formatting; this is the net-new test suite collection that needs review :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a separate PR to deal with just prettifying: #50

expect(TKHQ.bigIntToBase64Url(BigInt(1))).toEqual("AQ");
expect(TKHQ.bigIntToBase64Url(BigInt(1), 2)).toEqual("AAE");

// extrapolate to larger numbers
expect(TKHQ.bigIntToBase64Url(BigInt(255))).toEqual("_w"); // max 1 byte
expect(TKHQ.bigIntToBase64Url(BigInt(255), 2)).toEqual("AP8"); // max 1 byte expressed in 2 bytes

// error case
expect(() => {
TKHQ.bigIntToBase64Url(BigInt(256), 1);
}).toThrow("hex value cannot fit in a buffer of 1 byte(s)");
});

it("logs messages and sends messages up", async () => {
// TODO: test logMessage / sendMessageUp
expect(true).toBe(true);
Expand Down
Loading