-
Notifications
You must be signed in to change notification settings - Fork 6
/
config_test.go
148 lines (140 loc) · 3.67 KB
/
config_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// -----------------------------------------------------------------------------
// github.com/balacode/udpt /[config_test.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package udpt
import (
"fmt"
"os"
"regexp"
"testing"
"time"
)
// to run all tests in this file:
// go test -v -run Test_config_*
// -----------------------------------------------------------------------------
// NewDebugConfig(logWriter ...io.Writer) *Configuration
//
// go test -run Test_config_NewDebugConfig_
// debug configuration should match the one returned
// by NewDefaultConfig() but with logging activated
func Test_config_NewDebugConfig_1(t *testing.T) {
//
// returns *Configuration as a string and strips memory addresses
formatStruct := func(cf *Configuration) string {
s := fmt.Sprintf("%#v", cf)
rx := regexp.MustCompile(`\)\(0x.*?\), `)
ret := string(rx.ReplaceAll([]byte(s), []byte("), ")))
return ret
}
// --------------------
got := NewDebugConfig()
// --------------------
gotS := formatStruct(got)
//
want := NewDefaultConfig()
want.VerboseSender = true
want.VerboseReceiver = true
want.LogWriter = os.Stdout
wantS := formatStruct(want)
//
if gotS != wantS {
t.Error("0xEB0A18", "\n",
"want:", wantS, "\n",
" got:", gotS,
)
}
}
// (cf *Configuration) Validate() error
//
// go test -run Test_config_Configuration_Validate_
//
func Test_config_Configuration_Validate_(t *testing.T) {
makeValidConfig := func() *Configuration {
return &Configuration{
//
// Components:
Cipher: &aesCipher{},
Compressor: &zlibCompressor{},
//
// Limits:
PacketSizeLimit: 1450,
PacketPayloadSize: 1024,
SendBufferSize: 16 * 1024 * 2014, // 16 MiB
SendRetries: 10,
//
// Timeouts and Intervals:
ReplyTimeout: 15 * time.Second,
SendPacketInterval: 2 * time.Millisecond,
SendRetryInterval: 250 * time.Millisecond,
SendWaitInterval: 50 * time.Millisecond,
WriteTimeout: 15 * time.Second,
}
}
{
var cf = makeValidConfig()
cf.Cipher = nil
err := cf.Validate()
if !matchError(err, "nil Configuration.Cipher") {
t.Error("0xE65F82", "wrong error:", err)
}
}
{
var cf = makeValidConfig()
cf.Compressor = nil
err := cf.Validate()
if !matchError(err, "nil Configuration.Compressor") {
t.Error("0xE2CF8C", "wrong error:", err)
}
}
{
var cf = makeValidConfig()
cf.PacketSizeLimit = 8 - 1
err := cf.Validate()
if !matchError(err, "invalid Configuration.PacketSizeLimit") {
t.Error("0xE6E9BA", "wrong error:", err)
}
}
{
var cf = makeValidConfig()
cf.PacketSizeLimit = (65535 - 8) + 1
err := cf.Validate()
if !matchError(err, "invalid Configuration.PacketSizeLimit") {
t.Error("0xED50EB", "wrong error:", err)
}
}
{
var cf = makeValidConfig()
cf.PacketPayloadSize = 0
err := cf.Validate()
if !matchError(err, "invalid Configuration.PacketPayloadSize") {
t.Error("0xEA00A5", "wrong error:", err)
}
}
{
var cf = makeValidConfig()
cf.PacketSizeLimit = 1000
cf.PacketPayloadSize = 1000 - 200 + 1
err := cf.Validate()
if !matchError(err, "invalid Configuration.PacketPayloadSize") {
t.Error("0xEC92E8", "wrong error:", err)
}
}
{
var cf = makeValidConfig()
cf.SendBufferSize = -1
err := cf.Validate()
if !matchError(err, "invalid Configuration.SendBufferSize") {
t.Error("0xE2FF75", "wrong error:", err)
}
}
{
var cf = makeValidConfig()
cf.SendRetries = -1
err := cf.Validate()
if !matchError(err, "invalid Configuration.SendRetries") {
t.Error("0xE0DE62", "wrong error:", err)
}
}
}
// end