112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
/*
|
|
Copyright 2023 Josh Deprez
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package plugctl
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"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)
|
|
}
|
|
t.Logf("in[:4] = %v", in[:4])
|
|
in = in[4:]
|
|
want := []byte(`{"system":{"set_relay_state":{"state":1}}}`)
|
|
got, err := io.ReadAll(newDecrypter(bytes.NewReader(in)))
|
|
if err != nil {
|
|
t.Fatalf("io.ReadAll(decryptReader) error: %v", err)
|
|
}
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("io.ReadAll(decryptReader) = %q, want %q", 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
|
|
if _, err := newEncrypter(&buf).Write(in); err != nil {
|
|
t.Fatalf("newEncrypter(buf).Write(in) error = %v", err)
|
|
}
|
|
|
|
if got := buf.Bytes(); !bytes.Equal(got, want) {
|
|
t.Errorf("buf.Bytes() = %02x, want %02x", 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)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
newEncrypter(&buf).Write(want)
|
|
got, err := io.ReadAll(newDecrypter(&buf))
|
|
if err != nil {
|
|
t.Fatalf("io.ReadAll(newDecrypter(&buf))")
|
|
}
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("decrypt(encrypt(%02x)) = %02x, want %02x", want, got, want)
|
|
}
|
|
}
|
|
|
|
func TestReadStatusFromRealPlug(t *testing.T) {
|
|
t.Skip("Hits the real device")
|
|
|
|
p := Plug{Addr: "192.168.86.10:9999"}
|
|
|
|
si, err := p.SysInfo()
|
|
if err != nil {
|
|
t.Errorf("Plug.SysInfo() error = %v", err)
|
|
}
|
|
t.Logf("SysInfo = %#v", *si)
|
|
}
|
|
|
|
func TestTurnOn(t *testing.T) {
|
|
t.Skip("Hits the real device")
|
|
|
|
p := Plug{Addr: "192.168.86.10:9999"}
|
|
|
|
if err := p.TurnOn(); err != nil {
|
|
t.Errorf("Plug.TurnOn() error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTurnOff(t *testing.T) {
|
|
t.Skip("Hits the real device")
|
|
|
|
p := Plug{Addr: "192.168.86.10:9999"}
|
|
|
|
if err := p.TurnOff(); err != nil {
|
|
t.Errorf("Plug.TurnOff() error = %v", err)
|
|
}
|
|
}
|