qmc_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package qmc
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "os"
  7. "reflect"
  8. "testing"
  9. "unlock-music.dev/cli/algo/common"
  10. )
  11. func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
  12. encBody, err := os.ReadFile(fmt.Sprintf("./testdata/%s_raw.bin", filename))
  13. if err != nil {
  14. return nil, nil, err
  15. }
  16. encSuffix, err := os.ReadFile(fmt.Sprintf("./testdata/%s_suffix.bin", filename))
  17. if err != nil {
  18. return nil, nil, err
  19. }
  20. target, err := os.ReadFile(fmt.Sprintf("./testdata/%s_target.bin", filename))
  21. if err != nil {
  22. return nil, nil, err
  23. }
  24. return bytes.Join([][]byte{encBody, encSuffix}, nil), target, nil
  25. }
  26. func TestMflac0Decoder_Read(t *testing.T) {
  27. tests := []struct {
  28. name string
  29. fileExt string
  30. wantErr bool
  31. }{
  32. {"mflac0_rc4", ".mflac0", false},
  33. {"mflac_rc4", ".mflac", false},
  34. {"mflac_map", ".mflac", false},
  35. {"mgg_map", ".mgg", false},
  36. {"qmc0_static", ".qmc0", false},
  37. }
  38. for _, tt := range tests {
  39. t.Run(tt.name, func(t *testing.T) {
  40. raw, target, err := loadTestDataQmcDecoder(tt.name)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. d := NewDecoder(&common.DecoderParams{
  45. Reader: bytes.NewReader(raw),
  46. Extension: tt.fileExt,
  47. })
  48. if err := d.Validate(); err != nil {
  49. t.Errorf("validate file error = %v", err)
  50. }
  51. buf := make([]byte, len(target))
  52. if _, err := io.ReadFull(d, buf); err != nil {
  53. t.Errorf("read bytes from decoder error = %v", err)
  54. return
  55. }
  56. if !reflect.DeepEqual(buf, target) {
  57. t.Errorf("Decrypt() got = %v, want %v", buf[:32], target[:32])
  58. }
  59. })
  60. }
  61. }
  62. func TestMflac0Decoder_Validate(t *testing.T) {
  63. tests := []struct {
  64. name string
  65. fileExt string
  66. wantErr bool
  67. }{
  68. {"mflac0_rc4", ".flac", false},
  69. {"mflac_map", ".flac", false},
  70. {"mgg_map", ".ogg", false},
  71. {"qmc0_static", ".mp3", false},
  72. }
  73. for _, tt := range tests {
  74. t.Run(tt.name, func(t *testing.T) {
  75. raw, _, err := loadTestDataQmcDecoder(tt.name)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. d := NewDecoder(&common.DecoderParams{
  80. Reader: bytes.NewReader(raw),
  81. Extension: tt.fileExt,
  82. })
  83. if err := d.Validate(); err != nil {
  84. t.Errorf("read bytes from decoder error = %v", err)
  85. return
  86. }
  87. })
  88. }
  89. }