2021-12-13 12:42:07 +00:00
|
|
|
package qmc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-12-13 14:54:08 +00:00
|
|
|
"fmt"
|
2021-12-13 12:42:07 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2022-12-04 15:14:06 +00:00
|
|
|
|
|
|
|
"unlock-music.dev/cli/algo/common"
|
2021-12-13 12:42:07 +00:00
|
|
|
)
|
|
|
|
|
2021-12-13 14:54:08 +00:00
|
|
|
func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
|
|
|
|
encBody, err := os.ReadFile(fmt.Sprintf("./testdata/%s_raw.bin", filename))
|
2021-12-13 12:42:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-12-13 14:54:08 +00:00
|
|
|
encSuffix, err := os.ReadFile(fmt.Sprintf("./testdata/%s_suffix.bin", filename))
|
2021-12-13 12:42:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:54:08 +00:00
|
|
|
target, err := os.ReadFile(fmt.Sprintf("./testdata/%s_target.bin", filename))
|
2021-12-13 12:42:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return bytes.Join([][]byte{encBody, encSuffix}, nil), target, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestMflac0Decoder_Read(t *testing.T) {
|
2021-12-13 14:54:08 +00:00
|
|
|
tests := []struct {
|
2021-12-13 18:19:32 +00:00
|
|
|
name string
|
2022-12-04 15:14:06 +00:00
|
|
|
fileExt string
|
2021-12-13 18:19:32 +00:00
|
|
|
wantErr bool
|
2021-12-13 14:54:08 +00:00
|
|
|
}{
|
2022-12-04 15:14:06 +00:00
|
|
|
{"mflac0_rc4", ".mflac0", false},
|
|
|
|
{"mflac_rc4", ".mflac", false},
|
|
|
|
{"mflac_map", ".mflac", false},
|
|
|
|
{"mgg_map", ".mgg", false},
|
|
|
|
{"qmc0_static", ".qmc0", false},
|
2021-12-13 14:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-12-13 18:19:32 +00:00
|
|
|
raw, target, err := loadTestDataQmcDecoder(tt.name)
|
2021-12-13 14:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:14:06 +00:00
|
|
|
d := NewDecoder(&common.DecoderParams{
|
|
|
|
Reader: bytes.NewReader(raw),
|
|
|
|
Extension: tt.fileExt,
|
|
|
|
})
|
2022-11-19 18:53:04 +00:00
|
|
|
if err := d.Validate(); err != nil {
|
|
|
|
t.Errorf("validate file error = %v", err)
|
2021-12-13 14:54:08 +00:00
|
|
|
}
|
2022-11-19 18:53:04 +00:00
|
|
|
|
2021-12-13 14:54:08 +00:00
|
|
|
buf := make([]byte, len(target))
|
|
|
|
if _, err := io.ReadFull(d, buf); err != nil {
|
|
|
|
t.Errorf("read bytes from decoder error = %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(buf, target) {
|
|
|
|
t.Errorf("Decrypt() got = %v, want %v", buf[:32], target[:32])
|
|
|
|
}
|
|
|
|
})
|
2021-12-13 12:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-12-13 18:19:32 +00:00
|
|
|
|
|
|
|
func TestMflac0Decoder_Validate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fileExt string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"mflac0_rc4", ".flac", false},
|
|
|
|
{"mflac_map", ".flac", false},
|
2021-12-13 18:56:33 +00:00
|
|
|
{"mgg_map", ".ogg", false},
|
2021-12-13 18:19:32 +00:00
|
|
|
{"qmc0_static", ".mp3", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
raw, _, err := loadTestDataQmcDecoder(tt.name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-12-04 15:14:06 +00:00
|
|
|
d := NewDecoder(&common.DecoderParams{
|
|
|
|
Reader: bytes.NewReader(raw),
|
|
|
|
Extension: tt.fileExt,
|
|
|
|
})
|
2021-12-13 18:19:32 +00:00
|
|
|
|
|
|
|
if err := d.Validate(); err != nil {
|
|
|
|
t.Errorf("read bytes from decoder error = %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|