github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/inet_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 inet4 = Inet{ 28 Addr: inetAddr4, 29 Port: 9042, 30 } 31 32 var inet4Bytes = append(inetAddr4Bytes, 33 0, 0, 0x23, 0x52, //port 34 ) 35 36 var inet6 = Inet{ 37 Addr: inetAddr6, 38 Port: 9042, 39 } 40 41 var inet6Bytes = append(inetAddr6Bytes, 42 0, 0, 0x23, 0x52, //port 43 ) 44 45 func TestReadInet(t *testing.T) { 46 tests := []struct { 47 name string 48 source []byte 49 expected *Inet 50 remaining []byte 51 err error 52 }{ 53 {"IPv4 INET", inet4Bytes[:], &inet4, []byte{}, nil}, 54 {"IPv6 INET", inet6Bytes[:], &inet6, []byte{}, nil}, 55 {"INET with remaining", append(inet4Bytes[:], 1, 2, 3, 4), &inet4, []byte{1, 2, 3, 4}, nil}, 56 { 57 "cannot read INET length", 58 []byte{}, 59 nil, 60 []byte{}, 61 fmt.Errorf("cannot read [inet] address: %w", fmt.Errorf("cannot read [inetaddr] length: %w", fmt.Errorf("cannot read [byte]: %w", errors.New("EOF")))), 62 }, 63 { 64 "not enough bytes to read [inet] IPv4 content", 65 []byte{4, 192, 168, 1}, 66 nil, 67 []byte{}, 68 fmt.Errorf("cannot read [inet] address: %w", fmt.Errorf("cannot read [inetaddr] IPv4 content: %w", errors.New("unexpected EOF"))), 69 }, 70 { 71 "not enough bytes to read [inet] IPv6 content", 72 []byte{16, 0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73}, 73 nil, 74 []byte{}, 75 fmt.Errorf("cannot read [inet] address: %w", fmt.Errorf("cannot read [inetaddr] IPv6 content: %w", errors.New("unexpected EOF"))), 76 }, 77 { 78 "cannot read [inet] port number", 79 []byte{4, 192, 168, 1, 1, 0, 0, 0}, 80 nil, 81 []byte{}, 82 fmt.Errorf("cannot read [inet] port number: %w", fmt.Errorf("cannot read [int]: %w", errors.New("unexpected EOF"))), 83 }, 84 } 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 buf := bytes.NewBuffer(tt.source) 88 actual, err := ReadInet(buf) 89 assert.Equal(t, tt.expected, actual) 90 assert.Equal(t, tt.remaining, buf.Bytes()) 91 assert.Equal(t, tt.err, err) 92 }) 93 } 94 } 95 96 func TestWriteInet(t *testing.T) { 97 tests := []struct { 98 name string 99 input *Inet 100 expected []byte 101 err error 102 }{ 103 { 104 "IPv4 INET", 105 &inet4, 106 inet4Bytes, 107 nil, 108 }, 109 { 110 "IPv6 INET", 111 &inet6, 112 inet6Bytes, 113 nil, 114 }, 115 { 116 "INET with remaining", 117 &inet4, 118 inet4Bytes, 119 nil, 120 }, 121 { 122 "cannot write nil INET", 123 nil, 124 nil, 125 errors.New("cannot write nil [inet]"), 126 }, 127 } 128 for _, tt := range tests { 129 t.Run(tt.name, func(t *testing.T) { 130 buf := &bytes.Buffer{} 131 err := WriteInet(tt.input, buf) 132 assert.Equal(t, tt.expected, buf.Bytes()) 133 assert.Equal(t, tt.err, err) 134 }) 135 } 136 } 137 138 func TestLengthOfInet(t *testing.T) { 139 tests := []struct { 140 name string 141 input *Inet 142 expected int 143 err error 144 }{ 145 { 146 "IPv4 INET", 147 &inet4, 148 LengthOfByte + net.IPv4len + LengthOfInt, 149 nil, 150 }, 151 { 152 "IPv6 INET", 153 &inet6, 154 LengthOfByte + net.IPv6len + LengthOfInt, 155 nil, 156 }, 157 { 158 "nil INET", 159 nil, 160 -1, 161 errors.New("cannot compute nil [inet] length"), 162 }, 163 { 164 "nil INET addr", 165 &Inet{}, 166 -1, 167 errors.New("cannot compute nil [inetaddr] length"), 168 }, 169 } 170 for _, tt := range tests { 171 t.Run(tt.name, func(t *testing.T) { 172 actual, err := LengthOfInet(tt.input) 173 assert.Equal(t, tt.expected, actual) 174 assert.Equal(t, tt.err, err) 175 }) 176 } 177 }