forked from Stellar-split/split-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperty-address.test.ts
More file actions
105 lines (95 loc) · 2.99 KB
/
Copy pathproperty-address.test.ts
File metadata and controls
105 lines (95 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { describe, it, expect } from "vitest";
import * as fc from "fast-check";
import { Keypair, StrKey } from "@stellar/stellar-sdk";
import { isValidStellarAddress } from "../src/utils.js";
const VALID_ADDRESSES = Array.from({ length: 50 }, () => Keypair.random().publicKey());
describe("isValidStellarAddress (property-based)", () => {
it("returns true for all randomly generated Stellar keypairs", () => {
fc.assert(
fc.property(fc.constantFrom(...VALID_ADDRESSES), (address) => {
expect(isValidStellarAddress(address)).toBe(true);
}),
{ numRuns: 500 },
);
});
it("returns false for arbitrary non-G-prefixed strings", () => {
fc.assert(
fc.property(
fc.string({ minLength: 1, maxLength: 100 }),
(s) => {
fc.pre(!s.startsWith("G"));
expect(isValidStellarAddress(s)).toBe(false);
},
),
{ numRuns: 500 },
);
});
it("returns false for strings too short to be valid", () => {
fc.assert(
fc.property(
fc.string({ minLength: 1, maxLength: 55 }),
(s) => {
fc.pre(s.length < 56);
expect(isValidStellarAddress(s)).toBe(false);
},
),
{ numRuns: 500 },
);
});
it("returns false for G-prefixed strings with invalid base32 chars", () => {
fc.assert(
fc.property(
fc.integer({ min: 56, max: 100 }),
fc.constant("0123456789ABCDEFGHIJKLMNOPQRSTU.VWXYZ"),
(len, alphabet) => {
fc.pre(len > 1);
const invalid =
"G" +
Array.from(
{ length: len - 1 },
() => alphabet[Math.floor(Math.random() * alphabet.length)],
).join("");
const result = isValidStellarAddress(invalid);
expect(result).toBe(false);
},
),
{ numRuns: 500 },
);
});
it("returns false for empty string", () => {
expect(isValidStellarAddress("")).toBe(false);
});
it("returns false for 'null' and 'undefined' as strings", () => {
expect(isValidStellarAddress("null")).toBe(false);
expect(isValidStellarAddress("undefined")).toBe(false);
});
it("returns false for strings containing non-ASCII", () => {
fc.assert(
fc.property(
fc.string({ minLength: 1, maxLength: 50 }),
fc.integer({ min: 0x80, max: 0x10ffff }),
(prefix, codePoint) => {
const addr = prefix + String.fromCodePoint(codePoint);
expect(isValidStellarAddress(addr)).toBe(false);
},
),
{ numRuns: 500 },
);
});
it("valid addresses always start with 'G'", () => {
fc.assert(
fc.property(fc.constantFrom(...VALID_ADDRESSES), (address) => {
expect(address.startsWith("G")).toBe(true);
}),
{ numRuns: 500 },
);
});
it("valid addresses are exactly 56 characters", () => {
fc.assert(
fc.property(fc.constantFrom(...VALID_ADDRESSES), (address) => {
expect(address.length).toBe(56);
}),
{ numRuns: 500 },
);
});
});