lux/tests/lux_rpc_data_test.go

84 lines
1.9 KiB
Go

package tests
import (
"encoding/xml"
"lux/rpc"
"testing"
)
func TestRequestDecoding(t *testing.T) {
var request rpc.LuxRpcRequest
err := xml.Unmarshal([]byte(`<request id="1" controller="keystore" command="new-host" />`), &request)
if err != nil {
t.Fatal(err)
}
t.Log(request)
err = xml.Unmarshal([]byte(`<request id="2" controller="router" command="show-routes" />`), &request)
if err != nil {
t.Fatal(err)
}
t.Log(request)
err = xml.Unmarshal([]byte(`
<request id="3" controller="node" command="query">
<host hostname="test-host" />
<host id="0002" />
</request>`), &request)
if err != nil {
t.Fatal(err)
}
t.Log(request)
err = xml.Unmarshal([]byte(`
<request id="4" controller="node" command="resolve-wan">
<host hostname="test-host" />
</request>`), &request)
if err != nil {
t.Fatal(err)
}
t.Log(request)
}
func TestResponseEncoding(t *testing.T) {
xmlBytes, err := xml.Marshal(rpc.LuxRpcResponse{
RequestID: 1,
Keystore: rpc.LuxRpcKeyStore{
Nodes: []rpc.LuxRpcKeyNode{
{ID: "1111", KeyBlob: "base64 key", IVBlob: "base64 iv"},
},
Hosts: []rpc.LuxRpcKeyHost{
{ID: "0002", KeyBlob: "base64 key", IVBlob: "base64 iv"},
},
},
})
if err != nil {
t.Fatal(err)
}
t.Log(string(xmlBytes))
xmlBytes, err = xml.Marshal(rpc.LuxRpcResponse{
RequestID: 2,
Routes: []rpc.LuxRpcRoute{
{Type: 1, Target: "1111", Source: "0001", Destination: "127.0.0.1:1234"},
{Type: 2, Target: "1112", Source: "0001", Destination: "127.0.0.1:4321"},
},
})
if err != nil {
t.Fatal(err)
}
t.Log(string(xmlBytes))
xmlBytes, err = xml.Marshal(rpc.LuxRpcResponse{
RequestID: 3,
Hosts: []rpc.LuxRpcHost{
{HostID: "0001", Hostname: "test-host1"},
{HostID: "0002", Hostname: "test-host2"},
},
})
if err != nil {
t.Fatal(err)
}
t.Log(string(xmlBytes))
}