Skip to content

Commit

Permalink
checkValidLabel tweak + test
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Feb 13, 2024
1 parent c68a889 commit 67803a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export function byteArrayFromHex(s: string): ByteArray {
}

export function uint256ToByteArray(i: BigInt): ByteArray {
let hex = i
.toHex()
.slice(2)
.padStart(64, "0");
let hex = i.toHex().slice(2).padStart(64, "0");
return byteArrayFromHex(hex);
}

Expand Down Expand Up @@ -75,23 +72,23 @@ export function checkValidLabel(name: string | null): boolean {
// for compiler
name = name!;
for (let i = 0; i < name.length; i++) {
let c = name.charCodeAt(i);
if (c === 0) {
let charCode = name.charCodeAt(i);
if (charCode === 0) {
// 0 = null byte
log.warning("Invalid label '{}' contained null byte. Skipping.", [name]);
return false;
} else if (c === 46) {
} else if (charCode === 46) {
// 46 = .
log.warning(
"Invalid label '{}' contained separator char '.'. Skipping.",
[name]
);
return false;
} else if (c === 91) {
} else if (charCode === 91) {
// 91 = [
log.warning("Invalid label '{}' contained char '['. Skipping.", [name]);
return false;
} else if (c === 93) {
} else if (charCode === 93) {
// 93 = ]
log.warning("Invalid label '{}' contained char ']'. Skipping.", [name]);
return false;
Expand Down
25 changes: 25 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { assert, describe, test } from "matchstick-as/assembly/index";
import { checkValidLabel } from "../src/utils";

describe("checkValidLabel()", () => {
test("returns false for null byte in label", () => {
const label = "example\0";
assert.assertTrue(checkValidLabel(label) === false);
});
test("returns false for '.' in label", () => {
const label = "example.123";
assert.assertTrue(checkValidLabel(label) === false);
});
test("returns false for '[' in label", () => {
const label = "[example123";
assert.assertTrue(checkValidLabel(label) === false);
});
test("returns false for ']' in label", () => {
const label = "example123]";
assert.assertTrue(checkValidLabel(label) === false);
});
test("returns true for normal label", () => {
const label = "example123";
assert.assertTrue(checkValidLabel(label) === true);
});
});

0 comments on commit 67803a7

Please sign in to comment.