gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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 "gvisor.dev/gvisor/pkg/buffer" 22 "gvisor.dev/gvisor/pkg/tcpip/faketime" 23 "gvisor.dev/gvisor/pkg/tcpip/stack" 24 ) 25 26 const trueSegSize = stack.PacketBufferStructSize + segSize 27 28 type segmentSizeWants struct { 29 DataSize int 30 SegMemSize int 31 } 32 33 func checkSegmentSize(t *testing.T, name string, seg *segment, want segmentSizeWants) { 34 t.Helper() 35 got := segmentSizeWants{ 36 DataSize: seg.payloadSize(), 37 SegMemSize: seg.segMemSize(), 38 } 39 if diff := cmp.Diff(want, got); diff != "" { 40 t.Errorf("%s differs (-want +got):\n%s", name, diff) 41 } 42 } 43 44 func TestSegmentMerge(t *testing.T) { 45 var clock faketime.NullClock 46 id := stack.TransportEndpointID{} 47 seg1 := newOutgoingSegment(id, &clock, buffer.MakeWithView(buffer.NewViewSize(10))) 48 defer seg1.DecRef() 49 seg2 := newOutgoingSegment(id, &clock, buffer.MakeWithView(buffer.NewViewSize(20))) 50 defer seg2.DecRef() 51 52 checkSegmentSize(t, "seg1", seg1, segmentSizeWants{ 53 DataSize: 10, 54 SegMemSize: trueSegSize + 10, 55 }) 56 checkSegmentSize(t, "seg2", seg2, segmentSizeWants{ 57 DataSize: 20, 58 SegMemSize: trueSegSize + 20, 59 }) 60 61 seg1.merge(seg2) 62 63 checkSegmentSize(t, "seg1", seg1, segmentSizeWants{ 64 DataSize: 30, 65 SegMemSize: trueSegSize + 30, 66 }) 67 checkSegmentSize(t, "seg2", seg2, segmentSizeWants{ 68 DataSize: 0, 69 SegMemSize: trueSegSize, 70 }) 71 }