From e50a64797a0996b058c73cc60844e3d437ee6348 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Wed, 27 Mar 2024 17:49:16 +0530 Subject: [PATCH] chore(tests): include tests --- .prettierignore | 2 ++ package.json | 2 +- src/base64.ts | 2 +- src/index.ts | 2 ++ src/wg.ts | 2 -- tests/generateKeypair.luau | 11 ++++++++++ tests/generatePublicKey.luau | 42 ++++++++++++++++++++++++++++++++++++ 7 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 .prettierignore create mode 100644 tests/generateKeypair.luau create mode 100644 tests/generatePublicKey.luau diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..87e7f06 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +out/ +tests/ \ No newline at end of file diff --git a/package.json b/package.json index c62f5c8..6249c6c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "wg-lua", + "name": "@rbxts/wg-lua", "version": "1.0.0", "description": "A lua implementation of the wireguard keygen algorithm.", "main": "out/init.lua", diff --git a/src/base64.ts b/src/base64.ts index 42a9389..bff6e48 100644 --- a/src/base64.ts +++ b/src/base64.ts @@ -33,7 +33,7 @@ const _atob = (asc: string) => { let r1: number; let r2: number; - for (let i = 0; i < asc.size(); ) { + for (let i = 0; i < asc.size(); i++) { u24 = (b64Table[string.byte(asc, i++)[0]] << 18) | (b64Table[string.byte(asc, i++)[0]] << 12) | diff --git a/src/index.ts b/src/index.ts index f13e880..d84a789 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,8 @@ export const wireguard: Wireguard = { const privateKey = generatePrivateKey(); const publicKey = generatePublicKey(privateKey); + print(privateKey); + return { publicKey: atob(publicKey), privateKey: atob(privateKey), diff --git a/src/wg.ts b/src/wg.ts index 74c2140..e781a39 100644 --- a/src/wg.ts +++ b/src/wg.ts @@ -1,5 +1,3 @@ -const { atob } = require<{ atob: (buf: number[]) => string }>("base64"); - function gf(init?: number[]): number[] { const r = new Array(16, 0); if (init) { diff --git a/tests/generateKeypair.luau b/tests/generateKeypair.luau new file mode 100644 index 0000000..bf7baf1 --- /dev/null +++ b/tests/generateKeypair.luau @@ -0,0 +1,11 @@ +local wg = require("../out/").wireguard + +local BASE64_PAT = "^(?:%a%d+/?){4}*(?:%a%d+/?){2}==|(?:%a%d+/?){3}=?$" + +local keypair = wg:generateKeypair() + +assert(#keypair.publicKey == 44, "expected public key to be 44 bytes") +assert(#keypair.privateKey == 44, "expected private key to be 44 bytes") + +assert(keypair.privateKey:match(BASE64_PAT), "expected private key to be base64 encoded") +assert(keypair.publicKey:match(BASE64_PAT), "expected public key to be base64 encoded") diff --git a/tests/generatePublicKey.luau b/tests/generatePublicKey.luau new file mode 100644 index 0000000..1043017 --- /dev/null +++ b/tests/generatePublicKey.luau @@ -0,0 +1,42 @@ +local wg = require("../out/").wireguard + +local PRIVATE_KEY = { + [1] = 208, + [2] = 109, + [3] = 43, + [4] = 223, + [5] = 41, + [6] = 233, + [7] = 180, + [8] = 88, + [9] = 228, + [10] = 1, + [11] = 132, + [12] = 145, + [13] = 79, + [14] = 164, + [15] = 143, + [16] = 199, + [17] = 134, + [18] = 67, + [19] = 153, + [20] = 226, + [21] = 151, + [22] = 39, + [23] = 198, + [24] = 16, + [25] = 30, + [26] = 109, + [27] = 90, + [28] = 11, + [29] = 22, + [30] = 4, + [31] = 217, + [32] = 105, +} +local PUBLIC_KEY = "mYqWwJuiVXsXqfqXOKOKVTTZRovUXqzPkRtz1DwX1Wc=" + +local publicKey = wg:generatePublicKey(PRIVATE_KEY) + +assert(#publicKey == 44, "expected public key to be 44 bytes") +assert(publicKey == PUBLIC_KEY, "expected the correct public key")