|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + "net" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestPackageUdp(t *testing.T) { |
| 11 | + |
| 12 | + // No mapping enabled |
| 13 | + ip := net.UDPAddr{IP: net.ParseIP("192.168.0.7"), Port: 12345} |
| 14 | + packaged := packageUdp([]byte("asdf"), &ip) |
| 15 | + assert.NotEmpty(t, packaged) |
| 16 | + // Parse back the json |
| 17 | + var pkg Message |
| 18 | + err := json.Unmarshal(packaged, &pkg) |
| 19 | + assert.NoError(t, err) |
| 20 | + assert.Equal(t, ip.String(), pkg.Remote, "Remote IP should be the same") |
| 21 | + assert.Equal(t, "YXNkZg==", pkg.Data, "Data should be base64 encoded") |
| 22 | +} |
| 23 | + |
| 24 | +func TestPackageUdp_Mapping(t *testing.T) { |
| 25 | + // Mapping enabled |
| 26 | + ip := net.UDPAddr{IP: net.ParseIP("192.168.0.8"), Port: 12345} |
| 27 | + mapAll = "172.0.0.9" |
| 28 | + packaged := packageUdp([]byte("asdf"), &ip) |
| 29 | + assert.NotEmpty(t, packaged) |
| 30 | + // Parse back the json |
| 31 | + var pkg Message |
| 32 | + err := json.Unmarshal(packaged, &pkg) |
| 33 | + assert.NoError(t, err) |
| 34 | + assert.Equal(t, "172.0.0.9:12345", pkg.Remote, "Remote IP should be the same") |
| 35 | + assert.Equal(t, "YXNkZg==", pkg.Data, "Data should be base64 encoded") |
| 36 | + mapAll = "" |
| 37 | +} |
| 38 | + |
| 39 | +func TestPackageUdp_MappingMultiple(t *testing.T) { |
| 40 | + // Mapping enabled |
| 41 | + ip := net.UDPAddr{IP: net.ParseIP("192.168.0.8"), Port: 12345} |
| 42 | + ipMap = make(map[string]string) |
| 43 | + defer func() { |
| 44 | + ipMap = nil |
| 45 | + }() |
| 46 | + ipMap["192.168.0.8"] = "172.0.0.10" |
| 47 | + ipMap["192.168.0.9"] = "172.0.0.11" |
| 48 | + packaged := packageUdp([]byte("asdf"), &ip) |
| 49 | + assert.NotEmpty(t, packaged) |
| 50 | + // Parse back the json |
| 51 | + var pkg Message |
| 52 | + err := json.Unmarshal(packaged, &pkg) |
| 53 | + assert.NoError(t, err) |
| 54 | + assert.Equal(t, "172.0.0.10:12345", pkg.Remote, "Remote IP should be the same") |
| 55 | + assert.Equal(t, "YXNkZg==", pkg.Data, "Data should be base64 encoded") |
| 56 | +} |
0 commit comments