-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathserver_test.go
116 lines (105 loc) · 2.93 KB
/
server_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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package udp
import (
"net"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/elastic/beats/v7/filebeat/inputsource"
)
const (
maxMessageSize = 20
maxSocketSize = 0
timeout = time.Second * 15
)
type info struct {
message []byte
mt inputsource.NetworkMetadata
}
func TestReceiveEventFromUDP(t *testing.T) {
// Excluding udp6 for now, since it fails in our CI
for _, network := range []string{networkUDP, networkUDP4} {
t.Run(network, func(t *testing.T) {
testReceiveEventFromUDPWithNetwork(t, network)
})
}
}
func testReceiveEventFromUDPWithNetwork(t *testing.T, network string) {
tests := []struct {
name string
message []byte
expected []byte
}{
{
name: "Sending a message under the MaxMessageSize limit",
message: []byte("Hello world"),
expected: []byte("Hello world"),
},
{
name: "Sending a message over the MaxMessageSize limit will truncate the message",
message: []byte("Hello world not so nice"),
expected: []byte("Hello world not so n"),
},
}
ch := make(chan info)
host := "localhost:0"
config := &Config{
Host: host,
MaxMessageSize: maxMessageSize,
Timeout: timeout,
ReadBuffer: maxSocketSize,
Network: network,
}
fn := func(message []byte, metadata inputsource.NetworkMetadata) {
ch <- info{message: message, mt: metadata}
}
s := New(config, fn)
err := s.Start()
if !assert.NoError(t, err) {
return
}
defer s.Stop()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
conn, err := net.Dial(s.network(), s.localaddress)
if !assert.NoError(t, err) {
return
}
defer conn.Close()
_, err = conn.Write(test.message)
if !assert.NoError(t, err) {
return
}
info := <-ch
assert.Equal(t, test.expected, info.message)
if runtime.GOOS == "windows" {
if len(test.expected) < len(test.message) {
assert.Nil(t, info.mt.RemoteAddr)
assert.True(t, info.mt.Truncated)
} else {
assert.NotNil(t, info.mt.RemoteAddr)
assert.False(t, info.mt.Truncated)
}
} else {
assert.NotNil(t, info.mt.RemoteAddr)
assert.False(t, info.mt.Truncated)
}
})
}
}