github.com/vpnishe/netstack@v1.10.6/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/vpnishe/netstack/tcpip" 21 "github.com/vpnishe/netstack/tcpip/buffer" 22 "github.com/vpnishe/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, pkt tcpip.PacketBuffer) { 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, protocol tcpip.NetworkProtocolNumber, pkt tcpip.PacketBuffer) *tcpip.Error { 69 e.writeCount++ 70 return nil 71 } 72 73 // WritePackets implements stack.LinkEndpoint.WritePackets. 74 func (e *countedEndpoint) WritePackets(r *stack.Route, _ *stack.GSO, hdrs []stack.PacketDescriptor, payload buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) (int, *tcpip.Error) { 75 e.writeCount += len(hdrs) 76 return len(hdrs), nil 77 } 78 79 func (e *countedEndpoint) WriteRawPacket(buffer.VectorisedView) *tcpip.Error { 80 e.writeCount++ 81 return nil 82 } 83 84 // Wait implements stack.LinkEndpoint.Wait. 85 func (*countedEndpoint) Wait() {} 86 87 func TestWaitWrite(t *testing.T) { 88 ep := &countedEndpoint{} 89 wep := New(ep) 90 91 // Write and check that it goes through. 92 wep.WritePacket(nil, nil /* gso */, 0, tcpip.PacketBuffer{}) 93 if want := 1; ep.writeCount != want { 94 t.Fatalf("Unexpected writeCount: got=%v, want=%v", ep.writeCount, want) 95 } 96 97 // Wait on dispatches, then try to write. It must go through. 98 wep.WaitDispatch() 99 wep.WritePacket(nil, nil /* gso */, 0, tcpip.PacketBuffer{}) 100 if want := 2; ep.writeCount != want { 101 t.Fatalf("Unexpected writeCount: got=%v, want=%v", ep.writeCount, want) 102 } 103 104 // Wait on writes, then try to write. It must not go through. 105 wep.WaitWrite() 106 wep.WritePacket(nil, nil /* gso */, 0, tcpip.PacketBuffer{}) 107 if want := 2; ep.writeCount != want { 108 t.Fatalf("Unexpected writeCount: got=%v, want=%v", ep.writeCount, want) 109 } 110 } 111 112 func TestWaitDispatch(t *testing.T) { 113 ep := &countedEndpoint{} 114 wep := New(ep) 115 116 // Check that attach happens. 117 wep.Attach(ep) 118 if want := 1; ep.attachCount != want { 119 t.Fatalf("Unexpected attachCount: got=%v, want=%v", ep.attachCount, want) 120 } 121 122 // Dispatch and check that it goes through. 123 ep.dispatcher.DeliverNetworkPacket(ep, "", "", 0, tcpip.PacketBuffer{}) 124 if want := 1; ep.dispatchCount != want { 125 t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) 126 } 127 128 // Wait on writes, then try to dispatch. It must go through. 129 wep.WaitWrite() 130 ep.dispatcher.DeliverNetworkPacket(ep, "", "", 0, tcpip.PacketBuffer{}) 131 if want := 2; ep.dispatchCount != want { 132 t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) 133 } 134 135 // Wait on dispatches, then try to dispatch. It must not go through. 136 wep.WaitDispatch() 137 ep.dispatcher.DeliverNetworkPacket(ep, "", "", 0, tcpip.PacketBuffer{}) 138 if want := 2; ep.dispatchCount != want { 139 t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want) 140 } 141 } 142 143 func TestOtherMethods(t *testing.T) { 144 const ( 145 mtu = 0xdead 146 capabilities = 0xbeef 147 hdrLen = 0x1234 148 linkAddr = "test address" 149 ) 150 ep := &countedEndpoint{ 151 mtu: mtu, 152 capabilities: capabilities, 153 hdrLen: hdrLen, 154 linkAddr: linkAddr, 155 } 156 wep := New(ep) 157 158 if v := wep.MTU(); v != mtu { 159 t.Fatalf("Unexpected mtu: got=%v, want=%v", v, mtu) 160 } 161 162 if v := wep.Capabilities(); v != capabilities { 163 t.Fatalf("Unexpected capabilities: got=%v, want=%v", v, capabilities) 164 } 165 166 if v := wep.MaxHeaderLength(); v != hdrLen { 167 t.Fatalf("Unexpected MaxHeaderLength: got=%v, want=%v", v, hdrLen) 168 } 169 170 if v := wep.LinkAddress(); v != linkAddr { 171 t.Fatalf("Unexpected LinkAddress: got=%q, want=%q", v, linkAddr) 172 } 173 }