github.com/lightlus/netstack@v1.2.0/tcpip/network/fragmentation/reassembler_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 fragmentation
    16  
    17  import (
    18  	"math"
    19  	"reflect"
    20  	"testing"
    21  )
    22  
    23  type updateHolesInput struct {
    24  	first uint16
    25  	last  uint16
    26  	more  bool
    27  }
    28  
    29  var holesTestCases = []struct {
    30  	comment string
    31  	in      []updateHolesInput
    32  	want    []hole
    33  }{
    34  	{
    35  		comment: "No fragments. Expected holes: {[0 -> inf]}.",
    36  		in:      []updateHolesInput{},
    37  		want:    []hole{{first: 0, last: math.MaxUint16, deleted: false}},
    38  	},
    39  	{
    40  		comment: "One fragment at beginning. Expected holes: {[2, inf]}.",
    41  		in:      []updateHolesInput{{first: 0, last: 1, more: true}},
    42  		want: []hole{
    43  			{first: 0, last: math.MaxUint16, deleted: true},
    44  			{first: 2, last: math.MaxUint16, deleted: false},
    45  		},
    46  	},
    47  	{
    48  		comment: "One fragment in the middle. Expected holes: {[0, 0], [3, inf]}.",
    49  		in:      []updateHolesInput{{first: 1, last: 2, more: true}},
    50  		want: []hole{
    51  			{first: 0, last: math.MaxUint16, deleted: true},
    52  			{first: 0, last: 0, deleted: false},
    53  			{first: 3, last: math.MaxUint16, deleted: false},
    54  		},
    55  	},
    56  	{
    57  		comment: "One fragment at the end. Expected holes: {[0, 0]}.",
    58  		in:      []updateHolesInput{{first: 1, last: 2, more: false}},
    59  		want: []hole{
    60  			{first: 0, last: math.MaxUint16, deleted: true},
    61  			{first: 0, last: 0, deleted: false},
    62  		},
    63  	},
    64  	{
    65  		comment: "One fragment completing a packet. Expected holes: {}.",
    66  		in:      []updateHolesInput{{first: 0, last: 1, more: false}},
    67  		want: []hole{
    68  			{first: 0, last: math.MaxUint16, deleted: true},
    69  		},
    70  	},
    71  	{
    72  		comment: "Two non-overlapping fragments completing a packet. Expected holes: {}.",
    73  		in: []updateHolesInput{
    74  			{first: 0, last: 1, more: true},
    75  			{first: 2, last: 3, more: false},
    76  		},
    77  		want: []hole{
    78  			{first: 0, last: math.MaxUint16, deleted: true},
    79  			{first: 2, last: math.MaxUint16, deleted: true},
    80  		},
    81  	},
    82  	{
    83  		comment: "Two overlapping fragments completing a packet. Expected holes: {}.",
    84  		in: []updateHolesInput{
    85  			{first: 0, last: 2, more: true},
    86  			{first: 2, last: 3, more: false},
    87  		},
    88  		want: []hole{
    89  			{first: 0, last: math.MaxUint16, deleted: true},
    90  			{first: 3, last: math.MaxUint16, deleted: true},
    91  		},
    92  	},
    93  }
    94  
    95  func TestUpdateHoles(t *testing.T) {
    96  	for _, c := range holesTestCases {
    97  		r := newReassembler(0)
    98  		for _, i := range c.in {
    99  			r.updateHoles(i.first, i.last, i.more)
   100  		}
   101  		if !reflect.DeepEqual(r.holes, c.want) {
   102  			t.Errorf("Test \"%s\" produced unexepetced holes. Got %v. Want %v", c.comment, r.holes, c.want)
   103  		}
   104  	}
   105  }