github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/transport/tcp/segment_test.go (about) 1 // Copyright 2021 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 tcp 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "github.com/SagerNet/gvisor/pkg/tcpip/buffer" 22 "github.com/SagerNet/gvisor/pkg/tcpip/faketime" 23 "github.com/SagerNet/gvisor/pkg/tcpip/stack" 24 ) 25 26 type segmentSizeWants struct { 27 DataSize int 28 SegMemSize int 29 } 30 31 func checkSegmentSize(t *testing.T, name string, seg *segment, want segmentSizeWants) { 32 t.Helper() 33 got := segmentSizeWants{ 34 DataSize: seg.data.Size(), 35 SegMemSize: seg.segMemSize(), 36 } 37 if diff := cmp.Diff(got, want); diff != "" { 38 t.Errorf("%s differs (-want +got):\n%s", name, diff) 39 } 40 } 41 42 func TestSegmentMerge(t *testing.T) { 43 var clock faketime.NullClock 44 id := stack.TransportEndpointID{} 45 seg1 := newOutgoingSegment(id, &clock, buffer.NewView(10)) 46 defer seg1.decRef() 47 seg2 := newOutgoingSegment(id, &clock, buffer.NewView(20)) 48 defer seg2.decRef() 49 50 checkSegmentSize(t, "seg1", seg1, segmentSizeWants{ 51 DataSize: 10, 52 SegMemSize: SegSize + 10, 53 }) 54 checkSegmentSize(t, "seg2", seg2, segmentSizeWants{ 55 DataSize: 20, 56 SegMemSize: SegSize + 20, 57 }) 58 59 seg1.merge(seg2) 60 61 checkSegmentSize(t, "seg1", seg1, segmentSizeWants{ 62 DataSize: 30, 63 SegMemSize: SegSize + 30, 64 }) 65 checkSegmentSize(t, "seg2", seg2, segmentSizeWants{ 66 DataSize: 0, 67 SegMemSize: SegSize, 68 }) 69 }