55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
|
package plugctl
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/base64"
|
||
|
"io"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestDecrypt(t *testing.T) {
|
||
|
// on message from hs100.sh
|
||
|
in64 := "AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog=="
|
||
|
in, err := base64.StdEncoding.DecodeString(in64)
|
||
|
if err != nil {
|
||
|
t.Fatalf("base64.DecodeString(%q) error: %v", in64, err)
|
||
|
}
|
||
|
in = in[4:]
|
||
|
want := []byte(`{"system":{"set_relay_state":{"state":1}}}`)
|
||
|
dec := newDecrypter(bytes.NewReader(in))
|
||
|
got, err := io.ReadAll(dec)
|
||
|
if err != nil {
|
||
|
t.Fatalf("io.ReadAll(decryptReader) error: %v", err)
|
||
|
}
|
||
|
if !bytes.Equal(got, want) {
|
||
|
t.Errorf("decrypt(%02x) = %q, want %q", in, got, want)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestEncrypt(t *testing.T) {
|
||
|
// on message from hs100.sh
|
||
|
want64 := "AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog=="
|
||
|
want, err := base64.StdEncoding.DecodeString(want64)
|
||
|
if err != nil {
|
||
|
t.Fatalf("base64.DecodeString(%q) error: %v", want64, err)
|
||
|
}
|
||
|
want = want[4:]
|
||
|
in := []byte(`{"system":{"set_relay_state":{"state":1}}}`)
|
||
|
var buf bytes.Buffer
|
||
|
newEncrypter(&buf).Write(in)
|
||
|
|
||
|
if got := buf.Bytes(); !bytes.Equal(got, want) {
|
||
|
t.Errorf("encrypt(%q) = %02x, want %02x", in, got, want)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// func TestEncryptDecrypt(t *testing.T) {
|
||
|
// want := make([]byte, 64)
|
||
|
// if _, err := rand.Read(want); err != nil {
|
||
|
// t.Fatalf("rand.Read(want) error: %v", err)
|
||
|
// }
|
||
|
// if got := decrypt(encrypt(want)); !bytes.Equal(got, want) {
|
||
|
// t.Errorf("decrypt(encrypt(%02x)) = %02x, want %02x", want, got, want)
|
||
|
// }
|
||
|
// }
|