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"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
name string
|
|
|
|
filename string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"mflac0_rc4(512)", "mflac0_rc4", false},
|
|
|
|
{"mflac_map(256)", "mflac_map", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
raw, target, err := loadTestDataQmcDecoder(tt.filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := NewMflac0Decoder(bytes.NewReader(raw))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|