Skip to content

Commit c5cef88

Browse files
committed
Add a test that uses a mode that can decrypt what it encrypts (CTR).
1 parent 89d90b3 commit c5cef88

File tree

6 files changed

+69
-1
lines changed

6 files changed

+69
-1
lines changed

crypto/api_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/vadv/gopher-lua-libs/filepath"
78
"github.com/vadv/gopher-lua-libs/hex"
9+
"github.com/vadv/gopher-lua-libs/ioutil"
810
"github.com/vadv/gopher-lua-libs/tests"
911
)
1012

1113
func TestApi(t *testing.T) {
12-
preload := tests.SeveralPreloadFuncs(Preload, hex.Preload)
14+
preload := tests.SeveralPreloadFuncs(
15+
Preload,
16+
filepath.Preload,
17+
hex.Preload,
18+
ioutil.Preload,
19+
)
1320
assert.NotZero(t, tests.RunLuaTestFile(t, preload, "./test/test_api.lua"))
1421
}

crypto/test/data/1.data.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world

crypto/test/data/1.expected.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�4� �����

crypto/test/data/1.init.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�¿: ������

crypto/test/data/1.key.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
��\��Q�.QԶ:!D

crypto/test/test_api.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local crypto = require("crypto")
22
local assert = require 'assert'
33
local hex = require 'hex'
44
local require = require 'require'
5+
local filepath = require 'filepath'
6+
local ioutil = require 'ioutil'
57

68
function TestMD5(t)
79
local tests = {
@@ -212,4 +214,59 @@ function TestAESEncrypt(t)
212214
got = hex.encode_to_string(got)
213215
assert:Equal(t, tt.err, err)
214216
end
217+
end
218+
219+
function TestAESDecrypt(t)
220+
tests = {
221+
{
222+
data = "138434a80bd7dcd9ee8adc",
223+
mode = "CTR",
224+
key = "86e15cbc1cbf510d8f2e51d4b63a2144",
225+
init = "e3057fc2bf103a09a1b2c3d4e5f60718",
226+
expected = "48656c6c6f20776f726c64", -- "Hello world" in hex
227+
wantErr = false,
228+
},
229+
}
230+
for _, tt in ipairs(tests) do
231+
t:Run("aes_decrypt in " .. tostring(tt.mode) .. " mode", function(t)
232+
local key, err = hex.decode_string(tt.key)
233+
require:NoError(t, err)
234+
local init, err = hex.decode_string(tt.init)
235+
require:NoError(t, err)
236+
local data, err = hex.decode_string(tt.data)
237+
require:NoError(t, err)
238+
local got, err = crypto.aes_decrypt(tt.mode, key, init, data)
239+
if tt.wantErr then
240+
require:Error(t, err)
241+
return
242+
end
243+
require:NoError(t, err)
244+
got, err = hex.encode_to_string(got)
245+
require:NoError(t, err)
246+
assert:Equal(t, tt.expected, got)
247+
end)
248+
end
249+
end
250+
251+
function TestAESCodecFile(t)
252+
for i = 1, 1 do
253+
local data, err = ioutil.read_file(filepath.join("test/data", tostring(i) .. ".data.bin"))
254+
require:NoError(t, err)
255+
local expected, err = ioutil.read_file(filepath.join("test/data", tostring(i) .. ".expected.bin"))
256+
require:NoError(t, err)
257+
local init, err = ioutil.read_file(filepath.join("test/data", tostring(i) .. ".init.bin"))
258+
require:NoError(t, err)
259+
local key, err = ioutil.read_file(filepath.join("test/data", tostring(i) .. ".key.bin"))
260+
require:NoError(t, err)
261+
t:Run("TestAESEncryptFile " .. tostring(i), function(t)
262+
local got, err = crypto.aes_encrypt("CTR", key, init, data)
263+
require:NoError(t, err)
264+
assert:Equal(t, expected, got)
265+
266+
local decrypted, err = crypto.aes_decrypt("CTR", key, init, got)
267+
t:Logf('data: "%s", decrypted: "%s"', data, decrypted)
268+
require:NoError(t, err)
269+
assert:Equal(t, data, decrypted)
270+
end)
271+
end
215272
end

0 commit comments

Comments
 (0)