plugctl/plug_test.go

113 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-01-07 21:22:19 +11:00
/*
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.
*/
2023-01-06 16:29:05 +11:00
package plugctl
import (
"bytes"
2023-01-06 17:52:03 +11:00
"crypto/rand"
2023-01-06 16:29:05 +11:00
"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)
}
2023-01-06 17:52:03 +11:00
t.Logf("in[:4] = %v", in[:4])
2023-01-06 16:29:05 +11:00
in = in[4:]
want := []byte(`{"system":{"set_relay_state":{"state":1}}}`)
2023-01-06 17:52:03 +11:00
got, err := io.ReadAll(newDecrypter(bytes.NewReader(in)))
2023-01-06 16:29:05 +11:00
if err != nil {
t.Fatalf("io.ReadAll(decryptReader) error: %v", err)
}
if !bytes.Equal(got, want) {
2023-01-06 17:52:03 +11:00
t.Errorf("io.ReadAll(decryptReader) = %q, want %q", got, want)
2023-01-06 16:29:05 +11:00
}
}
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
2023-01-06 17:52:03 +11:00
if _, err := newEncrypter(&buf).Write(in); err != nil {
t.Fatalf("newEncrypter(buf).Write(in) error = %v", err)
}
2023-01-06 16:29:05 +11:00
if got := buf.Bytes(); !bytes.Equal(got, want) {
2023-01-06 17:52:03 +11:00
t.Errorf("buf.Bytes() = %02x, want %02x", got, want)
2023-01-06 16:29:05 +11:00
}
}
2023-01-06 17:52:03 +11:00
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) {
2023-01-07 21:22:19 +11:00
t.Skip("Hits the real device")
2023-01-06 17:52:03 +11:00
p := Plug{Addr: "192.168.86.10:9999"}
2023-01-07 21:22:19 +11:00
si, err := p.SysInfo()
2023-01-06 17:52:03 +11:00
if err != nil {
2023-01-07 21:22:19 +11:00
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)
2023-01-06 17:52:03 +11:00
}
}