github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/inet_addr_test.go (about)

     1  // Copyright 2020 DataStax
     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 primitive
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  	"net"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var inetAddr4 = net.IPv4(192, 168, 1, 1)
    28  
    29  var inetAddr4Bytes = []byte{
    30  	4,              // length of IP
    31  	192, 168, 1, 1, // IP
    32  }
    33  
    34  // 2001:0db8:85a3:0000:0000:8a2e:0370:7334
    35  var inetAddr6 = net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34}
    36  
    37  var inetAddr6Bytes = []byte{
    38  	16,                                                                                             // length of IP
    39  	0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34, // IP
    40  }
    41  
    42  func TestReadInetAddr(t *testing.T) {
    43  	tests := []struct {
    44  		name      string
    45  		source    []byte
    46  		expected  net.IP
    47  		remaining []byte
    48  		err       error
    49  	}{
    50  		{"IPv4 InetAddr", inetAddr4Bytes[:], inetAddr4, []byte{}, nil},
    51  		{"IPv6 InetAddr", inetAddr6Bytes[:], inetAddr6, []byte{}, nil},
    52  		{"InetAddr with remaining", append(inetAddr4Bytes[:], 1, 2, 3, 4), inetAddr4, []byte{1, 2, 3, 4}, nil},
    53  		{
    54  			"cannot read InetAddr length",
    55  			[]byte{},
    56  			nil,
    57  			[]byte{},
    58  			fmt.Errorf("cannot read [inetaddr] length: %w", fmt.Errorf("cannot read [byte]: %w", errors.New("EOF"))),
    59  		},
    60  		{
    61  			"not enough bytes to read [inetaddr] IPv4 content",
    62  			[]byte{4, 192, 168, 1},
    63  			nil,
    64  			[]byte{},
    65  			fmt.Errorf("cannot read [inetaddr] IPv4 content: %w", errors.New("unexpected EOF")),
    66  		},
    67  		{
    68  			"not enough bytes to read [inetaddr] IPv6 content",
    69  			[]byte{16, 0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73},
    70  			nil,
    71  			[]byte{},
    72  			fmt.Errorf("cannot read [inetaddr] IPv6 content: %w", errors.New("unexpected EOF")),
    73  		},
    74  	}
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			buf := bytes.NewBuffer(tt.source)
    78  			actual, err := ReadInetAddr(buf)
    79  			assert.Equal(t, tt.expected, actual)
    80  			assert.Equal(t, tt.remaining, buf.Bytes())
    81  			assert.Equal(t, tt.err, err)
    82  		})
    83  	}
    84  }
    85  
    86  func TestWriteInetAddr(t *testing.T) {
    87  	tests := []struct {
    88  		name     string
    89  		input    net.IP
    90  		expected []byte
    91  		err      error
    92  	}{
    93  		{
    94  			"IPv4 InetAddr",
    95  			inetAddr4,
    96  			inetAddr4Bytes,
    97  			nil,
    98  		},
    99  		{
   100  			"IPv6 InetAddr",
   101  			inetAddr6,
   102  			inetAddr6Bytes,
   103  			nil,
   104  		},
   105  		{
   106  			"cannot write nil InetAddr",
   107  			nil,
   108  			nil,
   109  			errors.New("cannot write nil [inetaddr]"),
   110  		},
   111  	}
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			buf := &bytes.Buffer{}
   115  			err := WriteInetAddr(tt.input, buf)
   116  			assert.Equal(t, tt.expected, buf.Bytes())
   117  			assert.Equal(t, tt.err, err)
   118  		})
   119  	}
   120  }
   121  
   122  func TestLengthOfInetAddr(t *testing.T) {
   123  	tests := []struct {
   124  		name     string
   125  		input    net.IP
   126  		expected int
   127  		err      error
   128  	}{
   129  		{
   130  			"IPv4 InetAddr",
   131  			inetAddr4,
   132  			LengthOfByte + net.IPv4len,
   133  			nil,
   134  		},
   135  		{
   136  			"IPv6 InetAddr",
   137  			inetAddr6,
   138  			LengthOfByte + net.IPv6len,
   139  			nil,
   140  		},
   141  		{
   142  			"nil InetAddr",
   143  			nil,
   144  			-1,
   145  			errors.New("cannot compute nil [inetaddr] length"),
   146  		},
   147  	}
   148  	for _, tt := range tests {
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			actual, err := LengthOfInetAddr(tt.input)
   151  			assert.Equal(t, tt.expected, actual)
   152  			assert.Equal(t, tt.err, err)
   153  		})
   154  	}
   155  }