github.com/google/netstack@v0.0.0-20191123085552-55fcc16cd0eb/tcpip/network/fragmentation/frag_heap_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  	"container/heap"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/google/netstack/tcpip/buffer"
    23  )
    24  
    25  var reassambleTestCases = []struct {
    26  	comment string
    27  	in      []fragment
    28  	want    buffer.VectorisedView
    29  }{
    30  	{
    31  		comment: "Non-overlapping in-order",
    32  		in: []fragment{
    33  			{offset: 0, vv: vv(1, "0")},
    34  			{offset: 1, vv: vv(1, "1")},
    35  		},
    36  		want: vv(2, "0", "1"),
    37  	},
    38  	{
    39  		comment: "Non-overlapping out-of-order",
    40  		in: []fragment{
    41  			{offset: 1, vv: vv(1, "1")},
    42  			{offset: 0, vv: vv(1, "0")},
    43  		},
    44  		want: vv(2, "0", "1"),
    45  	},
    46  	{
    47  		comment: "Duplicated packets",
    48  		in: []fragment{
    49  			{offset: 0, vv: vv(1, "0")},
    50  			{offset: 0, vv: vv(1, "0")},
    51  		},
    52  		want: vv(1, "0"),
    53  	},
    54  	{
    55  		comment: "Overlapping in-order",
    56  		in: []fragment{
    57  			{offset: 0, vv: vv(2, "01")},
    58  			{offset: 1, vv: vv(2, "12")},
    59  		},
    60  		want: vv(3, "01", "2"),
    61  	},
    62  	{
    63  		comment: "Overlapping out-of-order",
    64  		in: []fragment{
    65  			{offset: 1, vv: vv(2, "12")},
    66  			{offset: 0, vv: vv(2, "01")},
    67  		},
    68  		want: vv(3, "01", "2"),
    69  	},
    70  	{
    71  		comment: "Overlapping subset in-order",
    72  		in: []fragment{
    73  			{offset: 0, vv: vv(3, "012")},
    74  			{offset: 1, vv: vv(1, "1")},
    75  		},
    76  		want: vv(3, "012"),
    77  	},
    78  	{
    79  		comment: "Overlapping subset out-of-order",
    80  		in: []fragment{
    81  			{offset: 1, vv: vv(1, "1")},
    82  			{offset: 0, vv: vv(3, "012")},
    83  		},
    84  		want: vv(3, "012"),
    85  	},
    86  }
    87  
    88  func TestReassamble(t *testing.T) {
    89  	for _, c := range reassambleTestCases {
    90  		t.Run(c.comment, func(t *testing.T) {
    91  			h := make(fragHeap, 0, 8)
    92  			heap.Init(&h)
    93  			for _, f := range c.in {
    94  				heap.Push(&h, f)
    95  			}
    96  			got, err := h.reassemble()
    97  			if err != nil {
    98  				t.Fatal(err)
    99  			}
   100  			if !reflect.DeepEqual(got, c.want) {
   101  				t.Errorf("got reassemble(%+v) = %v, want = %v", c.in, got, c.want)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func TestReassambleFailsForNonZeroOffset(t *testing.T) {
   108  	h := make(fragHeap, 0, 8)
   109  	heap.Init(&h)
   110  	heap.Push(&h, fragment{offset: 1, vv: vv(1, "0")})
   111  	_, err := h.reassemble()
   112  	if err == nil {
   113  		t.Errorf("reassemble() did not fail when the first packet had offset != 0")
   114  	}
   115  }
   116  
   117  func TestReassambleFailsForHoles(t *testing.T) {
   118  	h := make(fragHeap, 0, 8)
   119  	heap.Init(&h)
   120  	heap.Push(&h, fragment{offset: 0, vv: vv(1, "0")})
   121  	heap.Push(&h, fragment{offset: 2, vv: vv(1, "1")})
   122  	_, err := h.reassemble()
   123  	if err == nil {
   124  		t.Errorf("reassemble() did not fail when there was a hole in the packet")
   125  	}
   126  }