summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/link/fdbased/endpoint_test.go
diff options
context:
space:
mode:
authorBhasker Hariharan <bhaskerh@google.com>2019-05-21 15:23:12 -0700
committerShentubot <shentubot@google.com>2019-05-21 15:24:25 -0700
commit2ac0aeeb42ef517743a18224f9f6945c1c77d732 (patch)
tree3cc0942ae3948485013bc30dbb1404305b501209 /pkg/tcpip/link/fdbased/endpoint_test.go
parent9cdae51feca5cee9faa198161b92a0aeece52d6c (diff)
Refactor fdbased endpoint dispatcher code.
This is in preparation to support an fdbased endpoint that can read/dispatch packets from multiple underlying fds. Updates #231 PiperOrigin-RevId: 249337074 Change-Id: Id7d375186cffcf55ae5e38986e7d605a96916d35
Diffstat (limited to 'pkg/tcpip/link/fdbased/endpoint_test.go')
-rw-r--r--pkg/tcpip/link/fdbased/endpoint_test.go78
1 files changed, 49 insertions, 29 deletions
diff --git a/pkg/tcpip/link/fdbased/endpoint_test.go b/pkg/tcpip/link/fdbased/endpoint_test.go
index 31138e4ac..fd1722074 100644
--- a/pkg/tcpip/link/fdbased/endpoint_test.go
+++ b/pkg/tcpip/link/fdbased/endpoint_test.go
@@ -357,28 +357,6 @@ func TestBufConfigFirst(t *testing.T) {
}
}
-func build(bufConfig []int) *endpoint {
- e := &endpoint{
- views: make([][]buffer.View, MaxMsgsPerRecv),
- iovecs: make([][]syscall.Iovec, MaxMsgsPerRecv),
- msgHdrs: make([]rawfile.MMsgHdr, MaxMsgsPerRecv),
- }
-
- for i, _ := range e.views {
- e.views[i] = make([]buffer.View, len(bufConfig))
- }
- for i := range e.iovecs {
- e.iovecs[i] = make([]syscall.Iovec, len(bufConfig))
- }
- for k, msgHdr := range e.msgHdrs {
- msgHdr.Msg.Iov = &e.iovecs[k][0]
- msgHdr.Msg.Iovlen = uint64(len(bufConfig))
- }
-
- e.allocateViews(bufConfig)
- return e
-}
-
var capLengthTestCases = []struct {
comment string
config []int
@@ -416,19 +394,61 @@ var capLengthTestCases = []struct {
},
}
-func TestCapLength(t *testing.T) {
+func TestReadVDispatcherCapLength(t *testing.T) {
+ for _, c := range capLengthTestCases {
+ // fd does not matter for this test.
+ d := readVDispatcher{fd: -1, e: &endpoint{}}
+ d.views = make([]buffer.View, len(c.config))
+ d.iovecs = make([]syscall.Iovec, len(c.config))
+ d.allocateViews(c.config)
+
+ used := d.capViews(c.n, c.config)
+ if used != c.wantUsed {
+ t.Errorf("Test %q failed when calling capViews(%d, %v). Got %d. Want %d", c.comment, c.n, c.config, used, c.wantUsed)
+ }
+ lengths := make([]int, len(d.views))
+ for i, v := range d.views {
+ lengths[i] = len(v)
+ }
+ if !reflect.DeepEqual(lengths, c.wantLengths) {
+ t.Errorf("Test %q failed when calling capViews(%d, %v). Got %v. Want %v", c.comment, c.n, c.config, lengths, c.wantLengths)
+ }
+ }
+}
+
+func TestRecvMMsgDispatcherCapLength(t *testing.T) {
for _, c := range capLengthTestCases {
- e := build(c.config)
- used := e.capViews(0, c.n, c.config)
+ d := recvMMsgDispatcher{
+ fd: -1, // fd does not matter for this test.
+ e: &endpoint{},
+ views: make([][]buffer.View, 1),
+ iovecs: make([][]syscall.Iovec, 1),
+ msgHdrs: make([]rawfile.MMsgHdr, 1),
+ }
+
+ for i, _ := range d.views {
+ d.views[i] = make([]buffer.View, len(c.config))
+ }
+ for i := range d.iovecs {
+ d.iovecs[i] = make([]syscall.Iovec, len(c.config))
+ }
+ for k, msgHdr := range d.msgHdrs {
+ msgHdr.Msg.Iov = &d.iovecs[k][0]
+ msgHdr.Msg.Iovlen = uint64(len(c.config))
+ }
+
+ d.allocateViews(c.config)
+
+ used := d.capViews(0, c.n, c.config)
if used != c.wantUsed {
- t.Errorf("Test \"%s\" failed when calling capViews(%d, %v). Got %d. Want %d", c.comment, c.n, c.config, used, c.wantUsed)
+ t.Errorf("Test %q failed when calling capViews(%d, %v). Got %d. Want %d", c.comment, c.n, c.config, used, c.wantUsed)
}
- lengths := make([]int, len(e.views[0]))
- for i, v := range e.views[0] {
+ lengths := make([]int, len(d.views[0]))
+ for i, v := range d.views[0] {
lengths[i] = len(v)
}
if !reflect.DeepEqual(lengths, c.wantLengths) {
- t.Errorf("Test \"%s\" failed when calling capViews(%d, %v). Got %v. Want %v", c.comment, c.n, c.config, lengths, c.wantLengths)
+ t.Errorf("Test %q failed when calling capViews(%d, %v). Got %v. Want %v", c.comment, c.n, c.config, lengths, c.wantLengths)
}
}