-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgns_test.go
127 lines (107 loc) · 3.41 KB
/
gns_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Author: Niels A.D.
// Project: gamenetworkingsockets (https://github.com/nielsAD/gns)
// License: Mozilla Public License, v2.0
package gns_test
import (
"net"
"testing"
"github.com/nielsAD/gns"
)
func TestConfigValue(t *testing.T) {
if val := gns.NewConfigValue(gns.ConfigSendBufferSize, 333); val.Int32() != 333 {
t.Fatal("Int ConfigValue mismatch")
}
if val := gns.NewConfigValue(gns.ConfigSendBufferSize, int32(12345)); val.Int32() != 12345 {
t.Fatal("Int32 ConfigValue mismatch")
}
if val := gns.NewConfigValue(gns.ConfigSendBufferSize, int64(67890)); val.Int64() != 67890 {
t.Fatal("Int64 ConfigValue mismatch")
}
if val := gns.NewConfigValue(gns.ConfigSendBufferSize, 22.0); val.Float() != 22.0 {
t.Fatal("Float ConfigValue mismatch")
}
}
func TestIdentity(t *testing.T) {
if id := gns.ParseIdentity("ip:127.0.0.1"); id == nil || id.Type() != gns.IdentityTypeIPAddress {
t.Fatal("IP identity expected")
} else if !id.Valid() || id.String() != "ip:127.0.0.1" {
t.Fatal("IP identity mismatch '" + id.String() + "'")
}
if id := gns.ParseIdentity("str:GenericString"); id == nil || id.Type() != gns.IdentityTypeGenericString {
t.Fatal("GenericString identity expected")
} else if !id.Valid() || id.String() != "str:GenericString" {
t.Fatal("GenericString identity mismatch '" + id.String() + "'")
}
if id := gns.ParseIdentity("gen:47656e657269634279746573"); id == nil || id.Type() != gns.IdentityTypeGenericBytes {
t.Fatal("GenericBytes identity expected")
} else if !id.Valid() || id.String() != "gen:47656e657269634279746573" {
t.Fatal("GenericBytes identity mismatch '" + id.String() + "'")
}
if id := gns.ParseIdentity("RandomString"); id.Valid() {
t.Fatal("nil expected for random string")
}
}
func TestIPAddr(t *testing.T) {
ref := net.UDPAddr{
IP: net.IP{1, 2, 3, 4},
Port: 5678,
}
in := gns.NewIPAddr(&ref)
out := in.UDPAddr()
if ref.String() != out.String() {
t.Fatal("ip4: in != out", in, *out)
}
ref.IP = net.IPv6linklocalallrouters
in = gns.NewIPAddr(&ref)
out = in.UDPAddr()
if ref.String() != out.String() {
t.Fatal("ip6: in != out", in, *out)
}
}
func TestMessage(t *testing.T) {
if err := gns.InitLibrary(nil); err != nil {
t.Fatal(err)
}
defer gns.KillLibrary()
msg := gns.InvalidConnection.NewMessage(123, gns.SendReliableNoNagle)
if msg == nil {
t.Fatal("NewMessage")
}
defer msg.Release()
if msg.Conn() != gns.InvalidConnection {
t.Fatal("Conn")
}
if msg.Size() != 123 {
t.Fatal("Size")
}
if msg.Flags() != gns.SendReliableNoNagle {
t.Fatal("Flags")
}
}
func TestListenSocket(t *testing.T) {
if err := gns.InitLibrary(nil); err != nil {
t.Fatal(err)
}
defer gns.KillLibrary()
gns.SetDebugOutputFunction(gns.DebugOutputTypeEverything, func(typ gns.DebugOutputType, msg string) {
t.Log("[DEBUG]", typ, msg)
})
if gns.InvalidListenSocket.Close() {
t.Fatal("InvalidListenSocket.Close")
}
addr := net.UDPAddr{IP: net.IPv6loopback, Port: 44696}
if l := gns.CreateListenSocketIP(gns.NewIPAddr(&addr), nil); l == gns.InvalidListenSocket {
t.Fatal("CreateListenSocketIP")
} else if !l.Close() {
t.Fatal("CloseListenSocket")
}
addr.Port++
if l := gns.CreateListenSocketIP(gns.NewIPAddr(&addr), gns.ConfigMap{
gns.ConfigTimeoutInitial: 10 * 1000,
gns.ConfigTimeoutConnected: 20 * 1000,
}); l == gns.InvalidListenSocket {
t.Fatal("CreateListenSocketIP with config")
} else if !l.Close() {
t.Fatal("CloseListenSocket with config")
}
}