-
Notifications
You must be signed in to change notification settings - Fork 7
/
option_test.go
67 lines (61 loc) · 1.43 KB
/
option_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
// Copyright 2016 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tcpinfo_test
import (
"runtime"
"testing"
"github.com/mikioh/tcpinfo"
"github.com/mikioh/tcpopt"
)
func TestMarshalAndParse(t *testing.T) {
opts := make([]tcpopt.Option, 0, 3)
switch runtime.GOOS {
case "darwin", "freebsd", "netbsd":
opts = append(opts, &tcpinfo.Info{})
case "linux":
opts = append(opts, &tcpinfo.Info{})
opts = append(opts, &tcpinfo.CCInfo{})
opts = append(opts, tcpinfo.CCAlgorithm("vegas"))
default:
t.Skipf("%s/%s", runtime.GOOS, runtime.GOARCH)
}
for _, o := range opts {
if o.Level() <= 0 {
t.Fatalf("got %#x; want greater than zero", o.Level())
}
if o.Name() <= 0 {
t.Fatalf("got %#x; want greater than zero", o.Name())
}
b, err := o.Marshal()
if err != nil {
t.Fatal(err)
}
if len(b) == 0 {
continue
}
oo, err := tcpopt.Parse(o.Level(), o.Name(), b)
if err != nil {
t.Fatal(err)
}
if oo, ok := oo.(*tcpinfo.Info); ok {
if _, err := oo.MarshalJSON(); err != nil {
t.Fatal(err)
}
}
}
}
func TestParseWithVariousBufferLengths(t *testing.T) {
for _, o := range []tcpopt.Option{
&tcpinfo.Info{},
&tcpinfo.CCInfo{},
tcpinfo.CCAlgorithm("vegas"),
} {
for i := 0; i < 256; i++ {
b := make([]byte, i)
if _, err := tcpopt.Parse(o.Level(), o.Name(), b); err == nil {
break
}
}
}
}