github.com/flowerwrong/netstack@v0.0.0-20191009141956-e5848263af28/tcpip/link/waitable/waitable_test.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package waitable 16 17 import ( 18 "testing" 19 20 "github.com/FlowerWrong/netstack/tcpip" 21 "github.com/FlowerWrong/netstack/tcpip/buffer" 22 "github.com/FlowerWrong/netstack/tcpip/stack" 23 ) 24 25 type countedEndpoint struct { 26 dispatchCount int 27 writeCount int 28 attachCount int 29 30 mtu uint32 31 capabilities stack.LinkEndpointCapabilities 32 hdrLen uint16 33 linkAddr tcpip.LinkAddress 34 35 dispatcher stack.NetworkDispatcher 36 } 37 38 func (e *countedEndpoint) DeliverNetworkPacket(linkEP stack.LinkEndpoint, remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) { 39 e.dispatchCount++ 40 } 41 42 func (e *countedEndpoint) Attach(dispatcher stack.NetworkDispatcher) { 43 e.attachCount++ 44 e.dispatcher = dispatcher 45 } 46 47 // IsAttached implements stack.LinkEndpoint.IsAttached. 48 func (e *countedEndpoint) IsAttached() bool { 49 return e.dispatcher != nil 50 } 51 52 func (e *countedEndpoint) MTU() uint32 { 53 return e.mtu 54 } 55 56 func (e *countedEndpoint) Capabilities() stack.LinkEndpointCapabilities { 57 return e.capabilities 58 } 59 60 func (e *countedEndpoint) MaxHeaderLength() uint16 { 61 return e.hdrLen 62 } 63 64 func (e *countedEndpoint) LinkAddress() tcpip.LinkAddress { 65 return e.linkAddr 66 } 67 68 func (e *countedEndpoint) WritePacket(r *stack.Route, _ *stack.GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) *tcpip.Error { 69 e.writeCount++ 70 return nil 71 } 72 73 // Wait implements stack.LinkEndpoint.Wait. 74 func (*countedEndpoint) Wait() {} 75 76 func TestWaitWrite(t *testing.T) { 77 ep := &countedEndpoint{} 78 wep := New(ep) 79 80 // Write and check that it goes through. 81 wep.WritePacket(nil, nil /* gso */, buffer.Prependable{}, buffer.VectorisedView{}, 0) 82 if want := 1; ep.writeCount != want { 83 t.Fatalf("Unexpected writeCount: got=%v, want=%v", ep.writeCount, want) 84 } 85 86 // Wait on dispatches, then try to write. It must go through. 87 wep.WaitDispatch() 88 wep.WritePacket(nil, nil /* gso */, buffer.Prependable{}, buffer.VectorisedView{}, 0) 89 if want := 2; ep.writeCount != want { 90 t.Fatalf("Unexpected writeCount: got=%v, want=%v", ep.writeCount, want) 91 } 92 93 // Wait on writes, then try to write. It must not go through. 94 wep.WaitWrite() 95 wep.WritePacket(nil, nil /* gso */, buffer.Prependable{}, buffer.VectorisedView{}, 0) 96 if want := 2; ep.writeCount != want { 97 t.Fatalf("Unexpected writeCount: got=%v, want=%v", ep.writeCount, want) 98 } 99 } 100 101 func TestWaitDispatch(t *testing.T) { 102 ep := &countedEndpoint{} 103 wep := New(ep) 104 105 // Check that attach happens. 106 wep.Attach(ep) 107 if want := 1; ep.attachCount != want { 108 t.Fatalf("Unexpected attachCount: got=%v, want=%v", ep.attachCount, want) 109 } 110 111 // Dispatch and check that it goes through. 112 ep.dispatcher.DeliverNetworkPacket(ep, "", "", 0, buffer.VectorisedView{}) 113 if want := 1; ep.dispatchCount != want { 114 t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) 115 } 116 117 // Wait on writes, then try to dispatch. It must go through. 118 wep.WaitWrite() 119 ep.dispatcher.DeliverNetworkPacket(ep, "", "", 0, buffer.VectorisedView{}) 120 if want := 2; ep.dispatchCount != want { 121 t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) 122 } 123 124 // Wait on dispatches, then try to dispatch. It must not go through. 125 wep.WaitDispatch() 126 ep.dispatcher.DeliverNetworkPacket(ep, "", "", 0, buffer.VectorisedView{}) 127 if want := 2; ep.dispatchCount != want { 128 t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) 129 } 130 } 131 132 func TestOtherMethods(t *testing.T) { 133 const ( 134 mtu = 0xdead 135 capabilities = 0xbeef 136 hdrLen = 0x1234 137 linkAddr = "test address" 138 ) 139 ep := &countedEndpoint{ 140 mtu: mtu, 141 capabilities: capabilities, 142 hdrLen: hdrLen, 143 linkAddr: linkAddr, 144 } 145 wep := New(ep) 146 147 if v := wep.MTU(); v != mtu { 148 t.Fatalf("Unexpected mtu: got=%v, want=%v", v, mtu) 149 } 150 151 if v := wep.Capabilities(); v != capabilities { 152 t.Fatalf("Unexpected capabilities: got=%v, want=%v", v, capabilities) 153 } 154 155 if v := wep.MaxHeaderLength(); v != hdrLen { 156 t.Fatalf("Unexpected MaxHeaderLength: got=%v, want=%v", v, hdrLen) 157 } 158 159 if v := wep.LinkAddress(); v != linkAddr { 160 t.Fatalf("Unexpected LinkAddress: got=%q, want=%q", v, linkAddr) 161 } 162 }