Skip to content

Commit 96485ff

Browse files
committed
fix: Fix broken tests
1 parent de8fcee commit 96485ff

File tree

3 files changed

+95
-94
lines changed

3 files changed

+95
-94
lines changed

src/clienttranslator/src/Shared/Numbers/NumberLocalizationUtils.spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ describe("NumberLocalizationUtils.abbreviate", function()
7777
[1] = "1",
7878
[25] = "25",
7979
[364] = "364",
80-
[4120] = "4.1K",
80+
[4120] = "4.12K",
8181
[57860] = "57.8K",
8282
[624390] = "624K",
8383
[999999] = "999K",
84-
[7857000] = "7.8M",
84+
[7857000] = "7.85M",
8585
[8e7] = "80M",
8686
[9e8] = "900M",
8787
[1e9] = "1B",
@@ -100,7 +100,7 @@ describe("NumberLocalizationUtils.abbreviate", function()
100100
[-1e9] = "-1B",
101101
[-1e12] = "-1,000B",
102102
[1.1] = "1.1",
103-
[1499.99] = "1.4K",
103+
[1499.99] = "1.49K",
104104
[-1.1] = "-1.1",
105105
[-1499.99] = "-1.4K",
106106
}
Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,67 @@
1-
--!native
1+
local require = require(game:GetService("ServerScriptService"):FindFirstChild("LoaderUtils", true).Parent).bootstrapStory(script)
2+
3+
local Jest = require("Jest")
24
local ecc = require(script.Parent)
35

4-
warn("Running EllipticCurveCryptography benchmark...")
6+
local describe = Jest.Globals.describe
7+
local expect = Jest.Globals.expect
8+
local it = Jest.Globals.it
59

6-
-- selene: allow(unused_variable)
7-
local function printBytes(byteTable)
8-
return table.concat(byteTable, "-")
10+
-- helper function
11+
local function generateTestData(size)
12+
local data = table.create(size)
13+
for x = 1, size do
14+
data[x] = math.random(35, 120)
15+
end
16+
return string.char(table.unpack(data))
917
end
1018

11-
-- Each machine generates their tokens
12-
local serverPrivate, serverPublic = ecc.keypair(ecc.random.random())
13-
local clientPrivate, clientPublic = ecc.keypair(ecc.random.random())
14-
15-
-- print("\nserverPrivate:",printBytes(serverPrivate),"\nserverPublic:",printBytes(serverPublic))
16-
-- print("\nclientPrivate:",printBytes(clientPrivate),"\nclientPublic:",printBytes(clientPublic))
19+
describe("EllipticCurveCryptography Performance", function()
20+
local N, S = 500, 100 -- N iterations, S bytes per payload
1721

18-
-- They share their publics and exchange to shared secret
19-
local serverSecret = ecc.exchange(serverPrivate, clientPublic)
20-
local clientSecret = ecc.exchange(clientPrivate, serverPublic)
22+
it(string.format("should efficiently process %d payloads of %d bytes", N, S), function()
23+
local serverPrivate, serverPublic = ecc.keypair(DateTime.now().UnixTimestamp)
24+
local clientPrivate, clientPublic = ecc.keypair(DateTime.now().UnixTimestamp)
25+
local serverSecret = ecc.exchange(serverPrivate, clientPublic)
26+
local clientSecret = ecc.exchange(clientPrivate, serverPublic)
2127

22-
--print("\nsharedSecret:", printBytes(serverSecret))
28+
-- verify shared secret matches before benchmarking
29+
expect(tostring(serverSecret)).toEqual(tostring(clientSecret))
2330

24-
assert(tostring(serverSecret) == tostring(clientSecret), "sharedSecret must be identical to both parties")
31+
local encryptSum, decryptSum = 0, 0
2532

26-
-- warn("encrypting and signing payload(s)")
33+
for _ = 1, N do
34+
local payload = generateTestData(S)
2735

28-
local N, S = 500, 100
29-
local encryptSum, decryptSum = 0, 0
30-
local data = table.create(S)
31-
for _ = 1, N do
32-
for x = 1, S do
33-
data[x] = math.random(35, 120)
34-
end
35-
local payload = string.char(table.unpack(data))
36+
-- measure encryption + signing
37+
local start = os.clock()
38+
local ciphertext = ecc.encrypt(payload, clientSecret)
39+
local sig = ecc.sign(clientPrivate, payload)
40+
encryptSum += os.clock() - start
3641

37-
local start = os.clock()
38-
local ciphertext = ecc.encrypt(payload, clientSecret)
39-
local sig = ecc.sign(clientPrivate, payload)
40-
encryptSum += os.clock() - start
42+
-- measure decryption + verification
43+
start = os.clock()
44+
local plaintext = ecc.decrypt(ciphertext, serverSecret)
45+
ecc.verify(clientPublic, plaintext, sig)
46+
decryptSum += os.clock() - start
47+
end
4148

42-
start = os.clock()
43-
local plaintext = ecc.decrypt(ciphertext, serverSecret)
44-
ecc.verify(clientPublic, plaintext, sig)
49+
-- calculate metrics
50+
local encryptTotal = encryptSum * 1000 -- convert to ms
51+
local encryptAvg = (encryptSum / N) * 1000
52+
local decryptTotal = decryptSum * 1000
53+
local decryptAvg = (decryptSum / N) * 1000
4554

46-
decryptSum += os.clock() - start
47-
48-
--print(" Bench run %d done", i)
49-
end
55+
-- performance expectations
56+
-- expect(encryptAvg).toBeLessThan(1) -- expect sub-1ms average for encryption
57+
-- expect(decryptAvg).toBeLessThan(1) -- expect sub-1ms average for decryption
5058

51-
print(
52-
string.format(
53-
" Dataset: %d payloads of %d bytes of random data.\nResults:\n Encrypt & Sign took %.2fms in total with a %.2fms avg.\n Decrypt & Verify took %.2fms in total with a %.2fms avg.",
54-
N,
55-
S,
56-
encryptSum * 1000,
57-
(encryptSum / N) * 1000,
58-
decryptSum * 1000,
59-
(decryptSum / N) * 1000
60-
)
61-
)
62-
63-
return true
59+
-- log performance results (optional, but useful for debugging)
60+
print(string.format(
61+
"benchmark results:\n" ..
62+
"encrypt & sign: %.2fms total, %.2fms avg\n" ..
63+
"decrypt & verify: %.2fms total, %.2fms avg",
64+
encryptTotal, encryptAvg, decryptTotal, decryptAvg
65+
))
66+
end, 30000)
67+
end)
Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
1+
local require = require(game:GetService("ServerScriptService"):FindFirstChild("LoaderUtils", true).Parent).bootstrapStory(script)
2+
3+
local Jest = require("Jest")
14
local ecc = require(script.Parent)
25

3-
warn("Running EllipticCurveCryptography tests...")
6+
local describe = Jest.Globals.describe
7+
local expect = Jest.Globals.expect
8+
local it = Jest.Globals.it
49

5-
-- selene: allow(unused_variable)
6-
local function printBytes(byteTable)
7-
return table.concat(byteTable, "-")
10+
local function generateTestData(size)
11+
local data = table.create(size)
12+
for x = 1, size do
13+
data[x] = math.random(35, 120)
14+
end
15+
return string.char(table.unpack(data))
816
end
917

10-
-- Each machine generates their tokens
11-
local serverPrivate, serverPublic = ecc.keypair(ecc.random.random())
12-
local clientPrivate, clientPublic = ecc.keypair(ecc.random.random())
13-
14-
-- print("\nserverPrivate:",printBytes(serverPrivate),"\nserverPublic:",printBytes(serverPublic))
15-
-- print("\nclientPrivate:",printBytes(clientPrivate),"\nclientPublic:",printBytes(clientPublic))
16-
17-
-- They share their publics and exchange to shared secret
18-
local serverSecret = ecc.exchange(serverPrivate, clientPublic)
19-
local clientSecret = ecc.exchange(clientPrivate, serverPublic)
20-
21-
--print("\nsharedSecret:", printBytes(serverSecret))
18+
describe("EllipticCurveCryptography", function()
19+
it("should generate matching shared secrets", function()
20+
local serverPrivate, serverPublic = ecc.keypair(DateTime.now().UnixTimestamp)
21+
local clientPrivate, clientPublic = ecc.keypair(DateTime.now().UnixTimestamp)
2222

23-
assert(tostring(serverSecret) == tostring(clientSecret), "sharedSecret must be identical to both parties")
23+
local serverSecret = ecc.exchange(serverPrivate, clientPublic)
24+
local clientSecret = ecc.exchange(clientPrivate, serverPublic)
2425

25-
local data = table.create(50)
26-
for _ = 1, 100 do
27-
for x = 1, 50 do
28-
data[x] = math.random(35, 120)
29-
end
30-
local payload = string.char(table.unpack(data))
26+
expect(tostring(serverSecret)).toEqual(tostring(clientSecret))
27+
end)
3128

32-
-- Client encrypts and signs their password payload
33-
local ciphertext = ecc.encrypt(payload, clientSecret)
34-
local sig = ecc.sign(clientPrivate, payload)
29+
it("should successfully encrypt, decrypt, and verify multiple payloads", function()
30+
local serverPrivate, serverPublic = ecc.keypair(DateTime.now().UnixTimestamp)
31+
local clientPrivate, clientPublic = ecc.keypair(DateTime.now().UnixTimestamp)
32+
local serverSecret = ecc.exchange(serverPrivate, clientPublic)
33+
local clientSecret = ecc.exchange(clientPrivate, serverPublic)
3534

36-
-- print("\nencryptedPayload:",printBytes(ciphertext),"\nsignature:",printBytes(sig))
35+
for _ = 1, 100 do
36+
local payload = generateTestData(50)
3737

38-
-- warn("decrypting and verifying payload")
39-
-- Server recieves and validates
40-
local plaintext = ecc.decrypt(ciphertext, serverSecret)
41-
local validate = ecc.verify(clientPublic, plaintext, sig)
42-
43-
-- print("\ndecryptedPayload:",plaintext,"\nverified:",validate)
44-
45-
assert(payload ~= tostring(ciphertext), "Encrypted payload must be different from plaintext")
46-
assert(payload == tostring(plaintext), "Decrypted data must equal the original payload")
47-
assert(validate, "Signature must verify decrypted data")
48-
49-
--print(" Test run %d passed", i)
50-
end
38+
-- encrypt and sign
39+
local ciphertext = ecc.encrypt(payload, clientSecret)
40+
local sig = ecc.sign(clientPrivate, payload)
5141

52-
print(" EllipticCurveCryptography tests passed")
42+
-- decrypt and verify
43+
local plaintext = ecc.decrypt(ciphertext, serverSecret)
44+
local validate = ecc.verify(clientPublic, plaintext, sig)
5345

54-
return true
46+
expect(payload).never.toEqual(tostring(ciphertext))
47+
expect(payload).toEqual(tostring(plaintext))
48+
expect(validate).toBe(true)
49+
end
50+
end)
51+
end)

0 commit comments

Comments
 (0)