polemos/pcsdk/commands_test.go

61 lines
1.6 KiB
Go
Raw Normal View History

package pcsdk
import (
"encoding/json"
"net/netip"
"testing"
"github.com/google/uuid"
"github.com/thefeli73/polemos/state"
)
func TestCommandCreateJsonParse(t *testing.T) {
ip, _ := netip.ParseAddr("127.0.0.99")
2023-05-02 15:27:43 +02:00
id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
uuid := state.CustomUUID(id)
m := create(5555, 6666, ip, uuid)
t.Logf("%+v\n", m)
msg, err := json.Marshal(m)
if err != nil {
t.Fatalf(`%q`, err)
}
expected := "{\"create\":{\"incoming_port\":5555,\"destination_port\":6666,\"destination_ip\":\"127.0.0.99\",\"id\":\"87e79cbc-6df6-4462-8412-85d6c473e3b1\"}}"
if string(msg) != expected {
t.Fatalf(
"\nExpected:\t %q\nGot:\t\t %q\n", expected, msg)
}
}
2023-04-19 14:00:41 +02:00
func TestCommandModifyJsonParse(t *testing.T) {
ip, _ := netip.ParseAddr("127.0.0.99")
2023-05-02 15:27:43 +02:00
id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
uuid := state.CustomUUID(id)
m := modify(8888, ip, uuid)
2023-04-19 14:00:41 +02:00
msg, err := json.Marshal(m)
if err != nil {
t.Fatalf(`%q`, err)
}
expected := "{\"modify\":{\"destination_port\":8888,\"destination_ip\":\"127.0.0.99\",\"id\":\"87e79cbc-6df6-4462-8412-85d6c473e3b1\"}}"
if string(msg) != expected {
t.Fatalf(
"\nExpected:\t %q\nGot:\t\t %q\n", expected, msg)
}
}
2023-04-19 14:00:41 +02:00
func TestCommandDeleteJsonParse(t *testing.T) {
2023-05-02 15:27:43 +02:00
id, _ := uuid.Parse("87e79cbc-6df6-4462-8412-85d6c473e3b1")
uuid := state.CustomUUID(id)
m := delete(uuid)
2023-04-19 14:00:41 +02:00
msg, err := json.Marshal(m)
if err != nil {
t.Fatalf(`%q`, err)
}
expected := "{\"delete\":{\"id\":\"87e79cbc-6df6-4462-8412-85d6c473e3b1\"}}"
if string(msg) != expected {
t.Fatalf(
"\nExpected:\t %q\nGot:\t\t %q\n", expected, msg)
}
}