key_derive_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package qmc
  2. import (
  3. "fmt"
  4. "os"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestSimpleMakeKey(t *testing.T) {
  9. expect := []byte{0x69, 0x56, 0x46, 0x38, 0x2b, 0x20, 0x15, 0x0b}
  10. t.Run("106,8", func(t *testing.T) {
  11. if got := simpleMakeKey(106, 8); !reflect.DeepEqual(got, expect) {
  12. t.Errorf("simpleMakeKey() = %v, want %v", got, expect)
  13. }
  14. })
  15. }
  16. func loadDecryptKeyData(name string) ([]byte, []byte, error) {
  17. keyRaw, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key_raw.bin", name))
  18. if err != nil {
  19. return nil, nil, err
  20. }
  21. keyDec, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key.bin", name))
  22. if err != nil {
  23. return nil, nil, err
  24. }
  25. return keyRaw, keyDec, nil
  26. }
  27. func TestDecryptKey(t *testing.T) {
  28. tests := []struct {
  29. name string
  30. filename string
  31. wantErr bool
  32. }{
  33. {"mflac0_rc4(512)", "mflac0_rc4", false},
  34. {"mflac_map(256)", "mflac_map", false},
  35. {"mflac_rc4(256)", "mflac_rc4", false},
  36. {"mgg_map(256)", "mgg_map", false},
  37. }
  38. for _, tt := range tests {
  39. t.Run(tt.name, func(t *testing.T) {
  40. raw, want, err := loadDecryptKeyData(tt.filename)
  41. if err != nil {
  42. t.Fatalf("load test data failed: %s", err)
  43. }
  44. got, err := deriveKey(raw)
  45. if (err != nil) != tt.wantErr {
  46. t.Errorf("deriveKey() error = %v, wantErr %v", err, tt.wantErr)
  47. return
  48. }
  49. if !reflect.DeepEqual(got, want) {
  50. t.Errorf("deriveKey() got = %v..., want %v...",
  51. string(got[:32]), string(want[:32]))
  52. }
  53. })
  54. }
  55. }