63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/zlib"
|
|
"encoding/hex"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestQRCModifiedDESVectors(t *testing.T) {
|
|
input := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}
|
|
expected := []byte{
|
|
0xfd, 0x0e, 0x64, 0x06, 0x65, 0xbe, 0x74, 0x13,
|
|
0x77, 0x63, 0x3b, 0x02, 0x45, 0x4e, 0x70, 0x7a,
|
|
}
|
|
key := [8]byte{'T', 'E', 'S', 'T', '!', 'K', 'E', 'Y'}
|
|
transformQRCBlocks(input, newQRCDes(key, true))
|
|
if !bytes.Equal(input, expected) {
|
|
t.Fatalf("unexpected decrypt vector: %x", input)
|
|
}
|
|
transformQRCBlocks(input, newQRCDes(key, false))
|
|
if !bytes.Equal(input, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}) {
|
|
t.Fatalf("unexpected encrypt vector: %x", input)
|
|
}
|
|
}
|
|
|
|
func TestDecryptQRCAndConvertXML(t *testing.T) {
|
|
document := []byte(`<QrcInfos><LyricInfo LyricContent="[ti:测试]
[1498,2200]a (1498,200)i (1698,200)
[3849,1200]完整(3849,400)歌词(4249,500)"/></QrcInfos>`)
|
|
var compressed bytes.Buffer
|
|
writer := zlib.NewWriter(&compressed)
|
|
if _, err := writer.Write(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := writer.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
encrypted := append([]byte(nil), compressed.Bytes()...)
|
|
if remainder := len(encrypted) % 8; remainder != 0 {
|
|
encrypted = append(encrypted, make([]byte, 8-remainder)...)
|
|
}
|
|
transformQRCBlocks(encrypted, newQRCDes(qrcKey1, false))
|
|
transformQRCBlocks(encrypted, newQRCDes(qrcKey2, true))
|
|
transformQRCBlocks(encrypted, newQRCDes(qrcKey3, false))
|
|
|
|
result, err := decryptQRCLyric(hex.EncodeToString(encrypted))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(result, "[00:01.498]a i") || !strings.Contains(result, "[00:03.849]完整歌词") {
|
|
t.Fatalf("unexpected converted lyrics: %q", result)
|
|
}
|
|
}
|
|
|
|
func TestDecryptQRCRejectsInvalidInput(t *testing.T) {
|
|
if _, err := decryptQRCLyric("not-hex"); err == nil {
|
|
t.Fatal("expected invalid hex to be rejected")
|
|
}
|
|
if _, err := decryptQRCLyric("00"); err == nil {
|
|
t.Fatal("expected invalid block length to be rejected")
|
|
}
|
|
}
|