github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/network/ipv4/stats_test.go (about) 1 // Copyright 2020 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 ipv4 16 17 import ( 18 "reflect" 19 "testing" 20 21 "github.com/SagerNet/gvisor/pkg/tcpip" 22 "github.com/SagerNet/gvisor/pkg/tcpip/stack" 23 "github.com/SagerNet/gvisor/pkg/tcpip/testutil" 24 ) 25 26 var _ stack.NetworkInterface = (*testInterface)(nil) 27 28 type testInterface struct { 29 stack.NetworkInterface 30 nicID tcpip.NICID 31 } 32 33 func (t *testInterface) ID() tcpip.NICID { 34 return t.nicID 35 } 36 37 func knownNICIDs(proto *protocol) []tcpip.NICID { 38 var nicIDs []tcpip.NICID 39 40 for k := range proto.mu.eps { 41 nicIDs = append(nicIDs, k) 42 } 43 44 return nicIDs 45 } 46 47 func TestClearEndpointFromProtocolOnClose(t *testing.T) { 48 s := stack.New(stack.Options{ 49 NetworkProtocols: []stack.NetworkProtocolFactory{NewProtocol}, 50 }) 51 proto := s.NetworkProtocolInstance(ProtocolNumber).(*protocol) 52 nic := testInterface{nicID: 1} 53 ep := proto.NewEndpoint(&nic, nil).(*endpoint) 54 var nicIDs []tcpip.NICID 55 56 proto.mu.Lock() 57 foundEP, hasEndpointBeforeClose := proto.mu.eps[nic.ID()] 58 nicIDs = knownNICIDs(proto) 59 proto.mu.Unlock() 60 61 if !hasEndpointBeforeClose { 62 t.Fatalf("expected to find the nic id %d in the protocol's endpoint map (%v)", nic.ID(), nicIDs) 63 } 64 if foundEP != ep { 65 t.Fatalf("found an incorrect endpoint mapped to nic id %d", nic.ID()) 66 } 67 68 ep.Close() 69 70 proto.mu.Lock() 71 _, hasEP := proto.mu.eps[nic.ID()] 72 nicIDs = knownNICIDs(proto) 73 proto.mu.Unlock() 74 if hasEP { 75 t.Fatalf("unexpectedly found an endpoint mapped to the nic id %d in the protocol's known nic ids (%v)", nic.ID(), nicIDs) 76 } 77 } 78 79 func TestMultiCounterStatsInitialization(t *testing.T) { 80 s := stack.New(stack.Options{ 81 NetworkProtocols: []stack.NetworkProtocolFactory{NewProtocol}, 82 }) 83 proto := s.NetworkProtocolInstance(ProtocolNumber).(*protocol) 84 var nic testInterface 85 ep := proto.NewEndpoint(&nic, nil).(*endpoint) 86 // At this point, the Stack's stats and the NetworkEndpoint's stats are 87 // expected to be bound by a MultiCounterStat. 88 refStack := s.Stats() 89 refEP := ep.stats.localStats 90 if err := testutil.ValidateMultiCounterStats(reflect.ValueOf(&ep.stats.ip).Elem(), []reflect.Value{reflect.ValueOf(&refEP.IP).Elem(), reflect.ValueOf(&refStack.IP).Elem()}); err != nil { 91 t.Error(err) 92 } 93 if err := testutil.ValidateMultiCounterStats(reflect.ValueOf(&ep.stats.icmp).Elem(), []reflect.Value{reflect.ValueOf(&refEP.ICMP).Elem(), reflect.ValueOf(&refStack.ICMP.V4).Elem()}); err != nil { 94 t.Error(err) 95 } 96 if err := testutil.ValidateMultiCounterStats(reflect.ValueOf(&ep.stats.igmp).Elem(), []reflect.Value{reflect.ValueOf(&refEP.IGMP).Elem(), reflect.ValueOf(&refStack.IGMP).Elem()}); err != nil { 97 t.Error(err) 98 } 99 }