/usr/share/gocode/src/github.com/stvp/go-udp-testing/udp.go is in golang-github-stvp-go-udp-testing-dev 0.0~git20150316.0.abcd331-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | // Package udp implements UDP test helpers. It lets you assert that certain
// strings must or must not be sent to a given local UDP listener.
package udp
import (
"net"
"runtime"
"strings"
"testing"
"time"
)
var (
addr *string
listener *net.UDPConn
Timeout time.Duration = time.Millisecond
)
type fn func()
// SetAddr sets the UDP port that will be listened on.
func SetAddr(a string) {
addr = &a
}
func start(t *testing.T) {
resAddr, err := net.ResolveUDPAddr("udp", *addr)
if err != nil {
t.Fatal(err)
}
listener, err = net.ListenUDP("udp", resAddr)
if err != nil {
t.Fatal(err)
}
}
func stop(t *testing.T) {
if err := listener.Close(); err != nil {
t.Fatal(err)
}
}
func getMessage(t *testing.T, body fn) string {
start(t)
defer stop(t)
result := make(chan string)
go func() {
message := make([]byte, 1024*32)
listener.SetReadDeadline(time.Now().Add(Timeout))
n, _, _ := listener.ReadFrom(message)
result <- string(message[0:n])
}()
body()
return <-result
}
func get(t *testing.T, match string, body fn) (got string, equals bool, contains bool) {
got = getMessage(t, body)
equals = got == match
contains = strings.Contains(got, match)
return got, equals, contains
}
func printLocation(t *testing.T) {
_, file, line, _ := runtime.Caller(2)
t.Errorf("At: %s:%d", file, line)
}
// ShouldReceiveOnly will fire a test error if the given function doesn't send
// exactly the given string over UDP.
func ShouldReceiveOnly(t *testing.T, expected string, body fn) {
got, equals, _ := get(t, expected, body)
if !equals {
printLocation(t)
t.Errorf("Expected: %#v", expected)
t.Errorf("But got: %#v", got)
}
}
// ShouldNotReceiveOnly will fire a test error if the given function sends
// exactly the given string over UDP.
func ShouldNotReceiveOnly(t *testing.T, notExpected string, body fn) {
_, equals, _ := get(t, notExpected, body)
if equals {
printLocation(t)
t.Errorf("Expected not to get: %#v", notExpected)
}
}
// ShouldReceive will fire a test error if the given function doesn't send the
// given string over UDP.
func ShouldReceive(t *testing.T, expected string, body fn) {
got, _, contains := get(t, expected, body)
if !contains {
printLocation(t)
t.Errorf("Expected to find: %#v", expected)
t.Errorf("But got: %#v", got)
}
}
// ShouldNotReceive will fire a test error if the given function sends the
// given string over UDP.
func ShouldNotReceive(t *testing.T, expected string, body fn) {
got, _, contains := get(t, expected, body)
if contains {
printLocation(t)
t.Errorf("Expected not to find: %#v", expected)
t.Errorf("But got: %#v", got)
}
}
// ShouldReceiveAll will fire a test error unless all of the given strings are
// sent over UDP.
func ShouldReceiveAll(t *testing.T, expected []string, body fn) {
got := getMessage(t, body)
failed := false
for _, str := range expected {
if !strings.Contains(got, str) {
if !failed {
printLocation(t)
failed = true
}
t.Errorf("Expected to find: %#v", str)
}
}
if failed {
t.Errorf("But got: %#v", got)
}
}
// ShouldNotReceiveAny will fire a test error if any of the given strings are
// sent over UDP.
func ShouldNotReceiveAny(t *testing.T, unexpected []string, body fn) {
got := getMessage(t, body)
failed := false
for _, str := range unexpected {
if strings.Contains(got, str) {
if !failed {
printLocation(t)
failed = true
}
t.Errorf("Expected not to find: %#v", str)
}
}
if failed {
t.Errorf("But got: %#v", got)
}
}
func ShouldReceiveAllAndNotReceiveAny(t *testing.T, expected []string, unexpected []string, body fn) {
got := getMessage(t, body)
failed := false
for _, str := range expected {
if !strings.Contains(got, str) {
if !failed {
printLocation(t)
failed = true
}
t.Errorf("Expected to find: %#v", str)
}
}
for _, str := range unexpected {
if strings.Contains(got, str) {
if !failed {
printLocation(t)
failed = true
}
t.Errorf("Expected not to find: %#v", str)
}
}
if failed {
t.Errorf("but got: %#v", got)
}
}
|