-
Notifications
You must be signed in to change notification settings - Fork 7
/
packageudp_test.go
54 lines (49 loc) · 1.68 KB
/
packageudp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package shoveler
import (
"encoding/json"
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPackageUdp(t *testing.T) {
// No mapping enabled
ip := net.UDPAddr{IP: net.ParseIP("192.168.0.7"), Port: 12345}
config := Config{}
packaged := PackageUdp([]byte("asdf"), &ip, &config)
assert.NotEmpty(t, packaged)
// Parse back the json
var pkg Message
err := json.Unmarshal(packaged, &pkg)
assert.NoError(t, err)
assert.Equal(t, ip.String(), pkg.Remote, "Remote IP should be the same")
assert.Equal(t, "YXNkZg==", pkg.Data, "Data should be base64 encoded")
}
func TestPackageUdp_Mapping(t *testing.T) {
// Mapping enabled
ip := net.UDPAddr{IP: net.ParseIP("192.168.0.8"), Port: 12345}
config := Config{IpMapAll: "172.0.0.9"}
packaged := PackageUdp([]byte("asdf"), &ip, &config)
assert.NotEmpty(t, packaged)
// Parse back the json
var pkg Message
err := json.Unmarshal(packaged, &pkg)
assert.NoError(t, err)
assert.Equal(t, "172.0.0.9:12345", pkg.Remote, "Remote IP should be the same")
assert.Equal(t, "YXNkZg==", pkg.Data, "Data should be base64 encoded")
}
func TestPackageUdp_MappingMultiple(t *testing.T) {
// Mapping enabled
ip := net.UDPAddr{IP: net.ParseIP("192.168.0.8"), Port: 12345}
config := Config{}
config.IpMap = make(map[string]string)
config.IpMap["192.168.0.8"] = "172.0.0.10"
config.IpMap["192.168.0.9"] = "172.0.0.11"
packaged := PackageUdp([]byte("asdf"), &ip, &config)
assert.NotEmpty(t, packaged)
// Parse back the json
var pkg Message
err := json.Unmarshal(packaged, &pkg)
assert.NoError(t, err)
assert.Equal(t, "172.0.0.10:12345", pkg.Remote, "Remote IP should be the same")
assert.Equal(t, "YXNkZg==", pkg.Data, "Data should be base64 encoded")
}