forked from dropbox/llama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp_test.go
More file actions
61 lines (54 loc) · 1.42 KB
/
Copy pathudp_test.go
File metadata and controls
61 lines (54 loc) · 1.42 KB
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
package udprobe
import (
"bytes"
"net"
"testing"
pb "github.com/nsw3550/udprobe/proto"
"google.golang.org/protobuf/proto"
)
func TestProbeProtobuf(t *testing.T) {
// Test with good data first
signature := []byte("abcdefghij")
data := &pb.Probe{
Signature: signature,
Tos: 46,
Sent: 123456789,
}
marshaled, err := proto.Marshal(data)
if err != nil {
t.Fatal("Failed to marshal probe data:", err)
}
unmarshaled := &pb.Probe{}
err = proto.Unmarshal(marshaled, unmarshaled)
if err != nil {
t.Fatal("Failed to unmarshal probe data:", err)
}
// Compare the actual structs
if !bytes.Equal(unmarshaled.Signature, data.Signature) ||
unmarshaled.Tos != data.Tos ||
unmarshaled.Sent != data.Sent {
t.Error("Data unmarshaled, but lost in translation")
}
// Now verify that bad data doesn't work
badData := []byte{1, 2, 3, 4, 5}
err = proto.Unmarshal(badData, &pb.Probe{})
if err == nil {
t.Error("No error returned for bad data (though unmarshal might succeed with empty/corrupt proto)")
}
}
func TestSetTos(t *testing.T) {
// Resolve a local addr
myAddr, _ := net.ResolveUDPAddr("udp", ":0")
// Create a connection
conn, _ := net.ListenUDP("udp", myAddr)
// Set the ToS value
tosVal := 240
newTos := byte(tosVal)
SetTos(conn, newTos)
// Verify the ToS value
val := GetTos(conn)
if val != newTos {
t.Error("New ToS value not set correctly. Set", tosVal, "and got",
val, "instead.")
}
}