1
0
forked from um/cli
cli/algo/qmc/key_derive_test.go

58 lines
1.4 KiB
Go
Raw Normal View History

2021-12-13 12:42:07 +00:00
package qmc
import (
"fmt"
2021-12-13 12:42:07 +00:00
"os"
"reflect"
"testing"
)
func TestSimpleMakeKey(t *testing.T) {
expect := []byte{0x69, 0x56, 0x46, 0x38, 0x2b, 0x20, 0x15, 0x0b}
t.Run("106,8", func(t *testing.T) {
if got := simpleMakeKey(106, 8); !reflect.DeepEqual(got, expect) {
t.Errorf("simpleMakeKey() = %v, want %v", got, expect)
}
})
}
func loadDecryptKeyData(name string) ([]byte, []byte, error) {
keyRaw, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key_raw.bin", name))
2021-12-13 12:42:07 +00:00
if err != nil {
return nil, nil, err
2021-12-13 12:42:07 +00:00
}
keyDec, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key.bin", name))
2021-12-13 12:42:07 +00:00
if err != nil {
return nil, nil, err
2021-12-13 12:42:07 +00:00
}
return keyRaw, keyDec, nil
}
func TestDecryptKey(t *testing.T) {
2021-12-13 12:42:07 +00:00
tests := []struct {
name string
filename string
wantErr bool
2021-12-13 12:42:07 +00:00
}{
{"mflac0_rc4(512)", "mflac0_rc4", false},
{"mflac_map(256)", "mflac_map", false},
2022-01-08 22:21:35 +00:00
{"mflac_rc4(256)", "mflac_rc4", false},
{"mgg_map(256)", "mgg_map", false},
2021-12-13 12:42:07 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2022-01-08 22:21:35 +00:00
raw, want, err := loadDecryptKeyData(tt.filename)
if err != nil {
t.Fatalf("load test data failed: %s", err)
}
got, err := deriveKey(raw)
2021-12-13 12:42:07 +00:00
if (err != nil) != tt.wantErr {
t.Errorf("deriveKey() error = %v, wantErr %v", err, tt.wantErr)
2021-12-13 12:42:07 +00:00
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("deriveKey() got = %v..., want %v...",
string(got[:32]), string(want[:32]))
2021-12-13 12:42:07 +00:00
}
})
}
}