github.com/google/netstack@v0.0.0-20191123085552-55fcc16cd0eb/tcpip/header/checksum_test.go (about)

     1  // Copyright 2019 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 header provides the implementation of the encoding and decoding of
    16  // network protocol headers.
    17  package header_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/netstack/tcpip/buffer"
    23  	"github.com/google/netstack/tcpip/header"
    24  )
    25  
    26  func TestChecksumVVWithOffset(t *testing.T) {
    27  	testCases := []struct {
    28  		name      string
    29  		vv        buffer.VectorisedView
    30  		off, size int
    31  		initial   uint16
    32  		want      uint16
    33  	}{
    34  		{
    35  			name: "empty",
    36  			vv: buffer.NewVectorisedView(0, []buffer.View{
    37  				buffer.NewViewFromBytes([]byte{1, 9, 0, 5, 4}),
    38  			}),
    39  			off:  0,
    40  			size: 0,
    41  			want: 0,
    42  		},
    43  		{
    44  			name: "OneView",
    45  			vv: buffer.NewVectorisedView(0, []buffer.View{
    46  				buffer.NewViewFromBytes([]byte{1, 9, 0, 5, 4}),
    47  			}),
    48  			off:  0,
    49  			size: 5,
    50  			want: 1294,
    51  		},
    52  		{
    53  			name: "TwoViews",
    54  			vv: buffer.NewVectorisedView(0, []buffer.View{
    55  				buffer.NewViewFromBytes([]byte{1, 9, 0, 5, 4}),
    56  				buffer.NewViewFromBytes([]byte{4, 3, 7, 1, 2, 123}),
    57  			}),
    58  			off:  0,
    59  			size: 11,
    60  			want: 33819,
    61  		},
    62  		{
    63  			name: "TwoViewsWithOffset",
    64  			vv: buffer.NewVectorisedView(0, []buffer.View{
    65  				buffer.NewViewFromBytes([]byte{98, 1, 9, 0, 5, 4}),
    66  				buffer.NewViewFromBytes([]byte{4, 3, 7, 1, 2, 123}),
    67  			}),
    68  			off:  1,
    69  			size: 11,
    70  			want: 33819,
    71  		},
    72  		{
    73  			name: "ThreeViewsWithOffset",
    74  			vv: buffer.NewVectorisedView(0, []buffer.View{
    75  				buffer.NewViewFromBytes([]byte{98, 1, 9, 0, 5, 4}),
    76  				buffer.NewViewFromBytes([]byte{98, 1, 9, 0, 5, 4}),
    77  				buffer.NewViewFromBytes([]byte{4, 3, 7, 1, 2, 123}),
    78  			}),
    79  			off:  7,
    80  			size: 11,
    81  			want: 33819,
    82  		},
    83  		{
    84  			name: "ThreeViewsWithInitial",
    85  			vv: buffer.NewVectorisedView(0, []buffer.View{
    86  				buffer.NewViewFromBytes([]byte{77, 11, 33, 0, 55, 44}),
    87  				buffer.NewViewFromBytes([]byte{98, 1, 9, 0, 5, 4}),
    88  				buffer.NewViewFromBytes([]byte{4, 3, 7, 1, 2, 123, 99}),
    89  			}),
    90  			initial: 77,
    91  			off:     7,
    92  			size:    11,
    93  			want:    33896,
    94  		},
    95  	}
    96  	for _, tc := range testCases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			if got, want := header.ChecksumVVWithOffset(tc.vv, tc.initial, tc.off, tc.size), tc.want; got != want {
    99  				t.Errorf("header.ChecksumVVWithOffset(%v) = %v, want: %v", tc, got, tc.want)
   100  			}
   101  			v := tc.vv.ToView()
   102  			v.TrimFront(tc.off)
   103  			v.CapLength(tc.size)
   104  			if got, want := header.Checksum(v, tc.initial), tc.want; got != want {
   105  				t.Errorf("header.Checksum(%v) = %v, want: %v", tc, got, tc.want)
   106  			}
   107  		})
   108  	}
   109  }