2021-12-13 12:42:07 +00:00
|
|
|
package qmc
|
|
|
|
|
|
|
|
import (
|
2021-12-13 14:54:08 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-12-13 14:54:08 +00:00
|
|
|
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 {
|
2021-12-13 14:54:08 +00:00
|
|
|
return nil, nil, err
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
2021-12-13 14:54:08 +00:00
|
|
|
keyDec, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key.bin", name))
|
2021-12-13 12:42:07 +00:00
|
|
|
if err != nil {
|
2021-12-13 14:54:08 +00:00
|
|
|
return nil, nil, err
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
2021-12-13 14:54:08 +00:00
|
|
|
return keyRaw, keyDec, nil
|
|
|
|
}
|
|
|
|
func TestDecryptKey(t *testing.T) {
|
2021-12-13 12:42:07 +00:00
|
|
|
tests := []struct {
|
2021-12-13 14:54:08 +00:00
|
|
|
name string
|
|
|
|
filename string
|
|
|
|
wantErr bool
|
2021-12-13 12:42:07 +00:00
|
|
|
}{
|
2021-12-13 14:54:08 +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)
|
|
|
|
}
|
2022-11-18 23:25:42 +00:00
|
|
|
got, err := deriveKey(raw)
|
2021-12-13 12:42:07 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2022-11-18 23:25:42 +00:00
|
|
|
t.Errorf("deriveKey() error = %v, wantErr %v", err, tt.wantErr)
|
2021-12-13 12:42:07 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-13 14:54:08 +00:00
|
|
|
if !reflect.DeepEqual(got, want) {
|
2022-11-18 23:25:42 +00:00
|
|
|
t.Errorf("deriveKey() got = %v..., want %v...",
|
2021-12-13 14:54:08 +00:00
|
|
|
string(got[:32]), string(want[:32]))
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|