Skip to content

Commit fca8507

Browse files
committed
add GitHub action for running unit tests
1 parent 48b6d34 commit fca8507

File tree

5 files changed

+111
-38
lines changed

5 files changed

+111
-38
lines changed

.github/workflows/npm-publish.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,32 @@ jobs:
1818
with:
1919
node-version: 16
2020
- run: npm ci
21+
22+
test:
23+
runs-on: ubuntu-latest
24+
25+
strategy:
26+
matrix:
27+
node-version: [18.x]
28+
29+
steps:
30+
- name: Checkout code
31+
uses: actions/checkout@v3
32+
33+
- name: Set up Node.js
34+
uses: actions/setup-node@v3
35+
with:
36+
node-version: ${{ matrix.node-version }}
37+
cache: 'npm'
38+
39+
- name: Install dependencies
40+
run: npm install
41+
42+
- name: Run Jest tests
43+
run: npm test
2144

2245
update-version-and-publish:
23-
needs: build
46+
needs: test
2447
runs-on: ubuntu-latest
2548
if: startsWith(github.ref, 'refs/tags/')
2649
steps:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
3-
.idea
3+
.idea
4+
coverage

src/crypto_utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ export function buildInputText(
125125
contractAddress: string,
126126
functionSelector: string
127127
) {
128-
if (plaintext >= BigInt(2) ** BigInt(128)) {
129-
throw new RangeError("Plaintext size must be 128 bits or smaller.")
128+
if (plaintext >= BigInt(2) ** BigInt(64)) {
129+
throw new RangeError("Plaintext size must be 64 bits or smaller.")
130130
}
131131

132132
// Convert the plaintext to bytes

tests/crypto_utils.test.ts

Lines changed: 82 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ describe('crypto_utils', () => {
8888

8989
expect(encryptedNumber).toEqual(ENCRYPTED_NUMBER)
9090
})
91+
92+
test('throws RangeError when the key length is not 16 bytes', () => {
93+
const NUMBER = new Uint8Array([])
94+
const AES_KEY = new Uint8Array([...ENCODED_AES_KEY, ...new Uint8Array([0])])
95+
96+
expect(() => encryptNumber(NUMBER, AES_KEY)).toThrow(RangeError)
97+
})
9198
})
9299

93100
test('decrypt - decrypt an unsigned integer', () => {
@@ -265,54 +272,96 @@ describe('crypto_utils', () => {
265272
expect(signature).toEqual(SIGNATURE)
266273
})
267274

268-
test('encrypt - encrypt the message "123', () => {
275+
describe('encrypt', () => {
269276
const AES_KEY = new Uint8Array([75, 4, 24, 193, 84, 61, 190, 112, 242, 21, 23, 91, 205, 223, 172, 66])
270-
const PLAINTEXT = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 57])
271-
const CIPHERTEXT = new Uint8Array([127, 171, 102, 2, 146, 178, 103, 87, 65, 62, 192, 34, 86, 183, 64, 25])
272-
const R = new Uint8Array([65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80])
273-
274-
const {ciphertext, r} = encrypt(AES_KEY, PLAINTEXT)
277+
278+
test('encrypt the message "123"', () => {
279+
const PLAINTEXT = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 57])
280+
const CIPHERTEXT = new Uint8Array([127, 171, 102, 2, 146, 178, 103, 87, 65, 62, 192, 34, 86, 183, 64, 25])
281+
const R = new Uint8Array([65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80])
282+
283+
const {ciphertext, r} = encrypt(AES_KEY, PLAINTEXT)
284+
285+
expect(ciphertext).toEqual(CIPHERTEXT)
286+
expect(r).toEqual(R)
287+
})
275288

276-
expect(ciphertext).toEqual(CIPHERTEXT)
277-
expect(R).toEqual(R)
289+
test('throw RangeError when the plaintext length is more than 16 bytes', () => {
290+
const PLAINTEXT = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
291+
292+
expect(() => encrypt(AES_KEY, PLAINTEXT)).toThrow(RangeError)
293+
})
278294
})
279295

280-
test('decrypt - decrypt the encrypted version of the message "123"', () => {
296+
describe('decrypt', () => {
281297
const AES_KEY = new Uint8Array([75, 4, 24, 193, 84, 61, 190, 112, 242, 21, 23, 91, 205, 223, 172, 66])
282-
const PLAINTEXT = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 57])
283-
const CIPHERTEXT = new Uint8Array([127, 171, 102, 2, 146, 178, 103, 87, 65, 62, 192, 34, 86, 183, 64, 25])
284-
const R = new Uint8Array([65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80])
285298

286-
const plaintext = decrypt(AES_KEY, R, CIPHERTEXT)
299+
test('decrypt the encrypted version of the message "123"', () => {
300+
const PLAINTEXT = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 57])
301+
const CIPHERTEXT = new Uint8Array([127, 171, 102, 2, 146, 178, 103, 87, 65, 62, 192, 34, 86, 183, 64, 25])
302+
const R = new Uint8Array([65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80])
303+
304+
const plaintext = decrypt(AES_KEY, R, CIPHERTEXT)
305+
306+
expect(plaintext).toEqual(PLAINTEXT)
307+
})
287308

288-
expect(plaintext).toEqual(PLAINTEXT)
309+
test('throw RangeError when the ciphertext length is not 16 bytes', () => {
310+
const CIPHERTEXT = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
311+
const R = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
312+
313+
expect(() => decrypt(AES_KEY, R, CIPHERTEXT)).toThrow(RangeError)
314+
})
315+
316+
test('throw RangeError when the random number length is not 16 bytes', () => {
317+
const CIPHERTEXT = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
318+
const R = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
319+
320+
expect(() => decrypt(AES_KEY, R, CIPHERTEXT)).toThrow(RangeError)
321+
})
289322
})
290323

291-
test('buildInputText - build input text from an arbitrary unsigned integer', () => {
324+
describe('buildInputText', () => {
292325
const PRIVATE_KEY = '0x526c9f9fe2fc41fb30fd0dbba1d4d76d774030166ef9f819b361046f5a5b4a34'
293326
const USER_KEY = '4b0418c1543dbe70f215175bcddfac42'
294327
const CONTRACT_ADDRESS = '0x0000000000000000000000000000000000000001'
295328
const FUNCTION_SELECTOR = '0x11223344'
296-
const PLAINTEXT = BigInt(123456789)
297-
const CIPHERTEXT = BigInt('57746566665648186614314868401232944930131032659899191889449469207176985595728')
298-
const SIGNATURE = new Uint8Array([
299-
107, 101, 60, 6, 104, 180, 5, 44, 192, 241, 70,
300-
65, 133, 22, 238, 224, 181, 178, 135, 106, 186, 212,
301-
163, 59, 209, 140, 139, 149, 168, 81, 118, 143, 28,
302-
124, 161, 162, 20, 29, 32, 74, 84, 57, 78, 157,
303-
28, 13, 25, 212, 226, 122, 48, 137, 229, 78, 189,
304-
155, 80, 192, 41, 79, 205, 22, 164, 133, 1
305-
])
306329

307-
const {ciphertext, signature} = buildInputText(
308-
PLAINTEXT,
309-
{ wallet: new Wallet(PRIVATE_KEY), userKey: USER_KEY },
310-
CONTRACT_ADDRESS,
311-
FUNCTION_SELECTOR
312-
)
330+
test('build input text from an arbitrary unsigned integer', () => {
331+
const PLAINTEXT = BigInt(123456789)
332+
const CIPHERTEXT = BigInt('57746566665648186614314868401232944930131032659899191889449469207176985595728')
333+
const SIGNATURE = new Uint8Array([
334+
107, 101, 60, 6, 104, 180, 5, 44, 192, 241, 70,
335+
65, 133, 22, 238, 224, 181, 178, 135, 106, 186, 212,
336+
163, 59, 209, 140, 139, 149, 168, 81, 118, 143, 28,
337+
124, 161, 162, 20, 29, 32, 74, 84, 57, 78, 157,
338+
28, 13, 25, 212, 226, 122, 48, 137, 229, 78, 189,
339+
155, 80, 192, 41, 79, 205, 22, 164, 133, 1
340+
])
341+
342+
const {ciphertext, signature} = buildInputText(
343+
PLAINTEXT,
344+
{ wallet: new Wallet(PRIVATE_KEY), userKey: USER_KEY },
345+
CONTRACT_ADDRESS,
346+
FUNCTION_SELECTOR
347+
)
348+
349+
expect(ciphertext).toEqual(CIPHERTEXT)
350+
expect(signature).toEqual(SIGNATURE)
351+
})
313352

314-
expect(ciphertext).toEqual(CIPHERTEXT)
315-
expect(signature).toEqual(SIGNATURE)
353+
test('throw RangeError when the value of plaintext is greater than (2 ^ 64) - 1', () => {
354+
const PLAINTEXT = BigInt(2) ** BigInt(64)
355+
const CIPHERTEXT = BigInt('')
356+
const SIGNATURE = new Uint8Array([])
357+
358+
expect(() => buildInputText(
359+
PLAINTEXT,
360+
{ wallet: new Wallet(PRIVATE_KEY), userKey: USER_KEY },
361+
CONTRACT_ADDRESS,
362+
FUNCTION_SELECTOR
363+
)).toThrow(RangeError)
364+
})
316365
})
317366

318367
test('decryptUint - decrypt the ciphertext of an arbitrary unsigned integer', () => {

tests/ethers_utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,4 @@ describe('Your Module Tests', () => {
170170

171171
expect(isConnected).toBe(true)
172172
})
173-
})
173+
})

0 commit comments

Comments
 (0)