79 lines
2.9 KiB
Go
79 lines
2.9 KiB
Go
package codec_test
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/base64"
|
||
"encoding/binary"
|
||
"io"
|
||
"testing"
|
||
|
||
"github.com/stretchr/testify/assert"
|
||
"tuxpa.in/a/tplink/codec"
|
||
)
|
||
|
||
type testCase struct {
|
||
plain string
|
||
encryptedBuf []byte
|
||
encryptedBufWithHeader []byte
|
||
}
|
||
|
||
func mustDecode64(xs string) []byte {
|
||
val, err := base64.StdEncoding.DecodeString(xs)
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
return val
|
||
}
|
||
|
||
var testCases = map[string]testCase{
|
||
"setPowerStateOn": {
|
||
plain: `{"system":{"set_relay_state":{"state":1}}}`,
|
||
encryptedBuf: mustDecode64(`0PKB+Iv/mvfV75S2xaDUi/mc8JHot8Sw0aXA4tijgfKG55P21O7fot+i`),
|
||
encryptedBufWithHeader: mustDecode64(`AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog==`),
|
||
},
|
||
"setPowerStateOff": {
|
||
plain: `{"system":{"set_relay_state":{"state":0}}}`,
|
||
encryptedBuf: mustDecode64(`0PKB+Iv/mvfV75S2xaDUi/mc8JHot8Sw0aXA4tijgfKG55P21O7eo96j`),
|
||
encryptedBufWithHeader: mustDecode64(`AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu3qPeow==`),
|
||
},
|
||
"getSysInfo": {
|
||
plain: `{ "system":{ "get_sysinfo":null } }`,
|
||
encryptedBuf: mustDecode64(`0PDSodir37rX9c+0lLbRtMCf7JXmj+GH6MrwnuuH68u2lus=`),
|
||
encryptedBufWithHeader: mustDecode64(`AAAAI9Dw0qHYq9+61/XPtJS20bTAn+yV5o/hh+jK8J7rh+vLtpbr`),
|
||
},
|
||
"getConsumption": {
|
||
plain: `{ "emeter":{ "get_realtime":null } }`,
|
||
encryptedBuf: mustDecode64(`0PDSt9q/y67c/sS/n73av8uU5oPijvqT/pu5g+2Y9Ji4xeWY`),
|
||
encryptedBufWithHeader: mustDecode64(`AAAAJNDw0rfav8uu3P7Ev5+92r/LlOaD4o76k/6buYPtmPSYuMXlmA==`),
|
||
},
|
||
"specialChars": {
|
||
plain: `right single quotation mark:’ left double quotation mark:“ right double quotation mark:” kissing cat face with closed eyes:😽`,
|
||
encryptedBuf: mustDecode64(`2bDXv8vrmPGf+JTx0aDVus6v27Lds5P+n+2GvF7eR2cLbgh8XDhXIkAsSWkYbQJ2F2MKZQsrRidVPgTmZvraqMGmzrqa/pHkhuqPr96rxLDRpcyjze2A4ZP4wiCgPR12H2wfdhh/XzxdKQlvDm0IKF82QioKaQVqGXwYOF0kQTII+Gf/Qg==`),
|
||
encryptedBufWithHeader: mustDecode64(`AAAAhdmw17/L65jxn/iU8dGg1brOr9uy3bOT/p/thrxe3kdnC24IfFw4VyJALElpGG0CdhdjCmULK0YnVT4E5mb62qjBps66mv6R5Ibqj6/eq8Sw0aXMo83tgOGT+MIgoD0ddh9sH3YYf188XSkJbw5tCChfNkIqCmkFahl8GDhdJEEyCPhn/0I=`),
|
||
},
|
||
}
|
||
|
||
func TestEncoder(t *testing.T) {
|
||
for k, v := range testCases {
|
||
t.Run(k, func(t *testing.T) {
|
||
ans := new(bytes.Buffer)
|
||
enc := codec.NewXorEncoder(ans, codec.DefaultFirstByte)
|
||
_, err := enc.Write([]byte(v.plain))
|
||
assert.NoError(t, err)
|
||
assert.EqualValues(t, v.encryptedBuf, ans.Bytes())
|
||
lenb := binary.BigEndian.AppendUint32(nil, uint32(ans.Len()))
|
||
assert.EqualValues(t, v.encryptedBufWithHeader, append(lenb, ans.Bytes()...))
|
||
})
|
||
}
|
||
}
|
||
func TestDecoder(t *testing.T) {
|
||
for k, v := range testCases {
|
||
t.Run(k, func(t *testing.T) {
|
||
enc := codec.NewXorDecoder(bytes.NewBuffer(v.encryptedBuf), codec.DefaultFirstByte)
|
||
pkt, err := io.ReadAll(enc)
|
||
assert.NoError(t, err)
|
||
assert.EqualValues(t, v.plain, string(pkt))
|
||
})
|
||
}
|
||
}
|