chore(tests): include tests

This commit is contained in:
Erica Marigold 2024-03-27 17:49:16 +05:30
parent ad8321955d
commit e50a64797a
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
7 changed files with 59 additions and 4 deletions

2
.prettierignore Normal file
View file

@ -0,0 +1,2 @@
out/
tests/

View file

@ -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",

View file

@ -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) |

View file

@ -19,6 +19,8 @@ export const wireguard: Wireguard = {
const privateKey = generatePrivateKey();
const publicKey = generatePublicKey(privateKey);
print(privateKey);
return {
publicKey: atob(publicKey),
privateKey: atob(privateKey),

View file

@ -1,5 +1,3 @@
const { atob } = require<{ atob: (buf: number[]) => string }>("base64");
function gf(init?: number[]): number[] {
const r = new Array<number>(16, 0);
if (init) {

View file

@ -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")

View file

@ -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")