github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/reasonmap_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  func TestReadReasonMap(t *testing.T) {
    28  	tests := []struct {
    29  		name     string
    30  		source   []byte
    31  		expected []*FailureReason
    32  		err      error
    33  	}{
    34  		{
    35  			"reason nmap empty",
    36  			[]byte{
    37  				0, 0, 0, 0, // length
    38  			},
    39  			[]*FailureReason{},
    40  			nil,
    41  		},
    42  		{
    43  			"reason map 1 key",
    44  			[]byte{
    45  				0, 0, 0, 1, // length
    46  				4, 192, 168, 1, 1, // key
    47  				0, 1, // value
    48  			},
    49  			[]*FailureReason{{net.IPv4(192, 168, 1, 1), FailureCodeTooManyTombstonesRead}},
    50  			nil,
    51  		},
    52  		{
    53  			"cannot read reason map length",
    54  			[]byte{
    55  				0, 0, 0,
    56  			},
    57  			nil,
    58  			fmt.Errorf("cannot read reason map length: %w", fmt.Errorf("cannot read [int]: %w", errors.New("unexpected EOF"))),
    59  		},
    60  		{
    61  			"cannot read reason map key",
    62  			[]byte{
    63  				0, 0, 0, 1, // length
    64  				4, 192, 168, 1,
    65  			},
    66  			nil,
    67  			fmt.Errorf("cannot read reason map key for element 0: %w", fmt.Errorf("cannot read [inetaddr] IPv4 content: %w", errors.New("unexpected EOF"))),
    68  		},
    69  		{
    70  			"cannot read reason map value",
    71  			[]byte{
    72  				0, 0, 0, 1, // length
    73  				4, 192, 168, 1, 1, // key
    74  				0, // incomplete value
    75  			},
    76  			nil,
    77  			fmt.Errorf("cannot read reason map value for element 0: %w", fmt.Errorf("cannot read [short]: %w", errors.New("unexpected EOF"))),
    78  		},
    79  	}
    80  	for _, tt := range tests {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			buf := bytes.NewBuffer(tt.source)
    83  			actual, err := ReadReasonMap(buf)
    84  			assert.Equal(t, tt.expected, actual)
    85  			assert.Equal(t, tt.err, err)
    86  		})
    87  	}
    88  }
    89  
    90  func TestWriteReasonMap(t *testing.T) {
    91  	tests := []struct {
    92  		name     string
    93  		input    []*FailureReason
    94  		expected []byte
    95  		err      error
    96  	}{
    97  		{
    98  			"empty string map",
    99  			[]*FailureReason{},
   100  			[]byte{0, 0, 0, 0},
   101  			nil,
   102  		},
   103  		// not officially allowed by the specs, but better safe than sorry
   104  		{
   105  			"nil string map",
   106  			nil,
   107  			[]byte{0, 0, 0, 0},
   108  			nil,
   109  		},
   110  		{
   111  			"map 1 key",
   112  			[]*FailureReason{{net.IPv4(192, 168, 1, 1), FailureCodeTooManyTombstonesRead}},
   113  			[]byte{
   114  				0, 0, 0, 1, // length
   115  				4, 192, 168, 1, 1, // key
   116  				0, 1, // value
   117  			},
   118  			nil,
   119  		},
   120  	}
   121  	for _, tt := range tests {
   122  		t.Run(tt.name, func(t *testing.T) {
   123  			buf := &bytes.Buffer{}
   124  			err := WriteReasonMap(tt.input, buf)
   125  			assert.Equal(t, tt.expected, buf.Bytes())
   126  			assert.Equal(t, tt.err, err)
   127  		})
   128  	}
   129  }
   130  
   131  func TestLengthOfReasonMap(t *testing.T) {
   132  	var inetAddr4Length, _ = LengthOfInetAddr(inetAddr4)
   133  	tests := []struct {
   134  		name     string
   135  		input    []*FailureReason
   136  		expected int
   137  		err      error
   138  	}{
   139  		{
   140  			"empty string map",
   141  			[]*FailureReason{},
   142  			LengthOfInt,
   143  			nil,
   144  		},
   145  		// not officially allowed by the specs, but better safe than sorry
   146  		{
   147  			"nil string map",
   148  			nil,
   149  			LengthOfInt,
   150  			nil,
   151  		},
   152  		{
   153  			"map 1 key",
   154  			[]*FailureReason{{net.IPv4(192, 168, 1, 1), 42}},
   155  			LengthOfInt + inetAddr4Length + LengthOfShort,
   156  			nil,
   157  		},
   158  	}
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			actual, err := LengthOfReasonMap(tt.input)
   162  			assert.Equal(t, tt.expected, actual)
   163  			assert.Equal(t, tt.err, err)
   164  		})
   165  	}
   166  }