2020-12-22 02:06:46 +00:00
|
|
|
package teamnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalRoute(t *testing.T) {
|
|
|
|
// Response from the teamnet route backend
|
|
|
|
data := `{
|
|
|
|
"network":"10.1.2.40/29",
|
|
|
|
"tunnel_id":"fba6ffea-807f-4e7a-a740-4184ee1b82c8",
|
|
|
|
"comment":"test",
|
|
|
|
"created_at":"2020-12-22T02:00:15.587008Z",
|
|
|
|
"deleted_at":null
|
|
|
|
}`
|
|
|
|
var r Route
|
2021-01-05 23:55:18 +00:00
|
|
|
err := json.Unmarshal([]byte(data), &r)
|
2020-12-22 02:06:46 +00:00
|
|
|
|
|
|
|
// Check everything worked
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uuid.MustParse("fba6ffea-807f-4e7a-a740-4184ee1b82c8"), r.TunnelID)
|
|
|
|
require.Equal(t, "test", r.Comment)
|
|
|
|
_, cidr, err := net.ParseCIDR("10.1.2.40/29")
|
|
|
|
require.NoError(t, err)
|
2021-01-05 23:55:18 +00:00
|
|
|
require.Equal(t, CIDR(*cidr), r.Network)
|
2020-12-22 02:06:46 +00:00
|
|
|
require.Equal(t, "test", r.Comment)
|
|
|
|
}
|
|
|
|
|
2021-01-05 23:55:18 +00:00
|
|
|
func TestUnmarshalDetailedRoute(t *testing.T) {
|
|
|
|
// Response from the teamnet route backend
|
|
|
|
data := `{
|
|
|
|
"network":"10.1.2.40/29",
|
|
|
|
"tunnel_id":"fba6ffea-807f-4e7a-a740-4184ee1b82c8",
|
|
|
|
"tunnel_name":"Mr. Tun",
|
|
|
|
"comment":"test",
|
|
|
|
"created_at":"2020-12-22T02:00:15.587008Z",
|
|
|
|
"deleted_at":null
|
|
|
|
}`
|
|
|
|
var r DetailedRoute
|
|
|
|
err := json.Unmarshal([]byte(data), &r)
|
|
|
|
|
|
|
|
// Check everything worked
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uuid.MustParse("fba6ffea-807f-4e7a-a740-4184ee1b82c8"), r.TunnelID)
|
|
|
|
require.Equal(t, "test", r.Comment)
|
|
|
|
_, cidr, err := net.ParseCIDR("10.1.2.40/29")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, CIDR(*cidr), r.Network)
|
|
|
|
require.Equal(t, "test", r.Comment)
|
|
|
|
require.Equal(t, "Mr. Tun", r.TunnelName)
|
|
|
|
}
|
|
|
|
|
2020-12-22 02:06:46 +00:00
|
|
|
func TestMarshalNewRoute(t *testing.T) {
|
|
|
|
_, network, err := net.ParseCIDR("1.2.3.4/32")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, network)
|
|
|
|
newRoute := NewRoute{
|
|
|
|
Network: *network,
|
|
|
|
TunnelID: uuid.New(),
|
|
|
|
Comment: "hi",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test where receiver is struct
|
|
|
|
serialized, err := json.Marshal(newRoute)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, strings.Contains(string(serialized), "tunnel_id"))
|
|
|
|
|
|
|
|
// Test where receiver is pointer to struct
|
|
|
|
serialized, err = json.Marshal(&newRoute)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, strings.Contains(string(serialized), "tunnel_id"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteTableString(t *testing.T) {
|
|
|
|
_, network, err := net.ParseCIDR("1.2.3.4/32")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, network)
|
2021-01-05 23:55:18 +00:00
|
|
|
r := DetailedRoute{
|
|
|
|
Network: CIDR(*network),
|
2020-12-22 02:06:46 +00:00
|
|
|
}
|
|
|
|
row := r.TableString()
|
|
|
|
fmt.Println(row)
|
|
|
|
require.True(t, strings.HasPrefix(row, "1.2.3.4/32"))
|
|
|
|
}
|