github.com/matrixorigin/matrixone@v1.2.0/pkg/proxy/util_test.go (about)

     1  // Copyright 2021 - 2023 Matrix Origin
     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 proxy
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/frontend"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  const (
    27  	comQuery = 3
    28  )
    29  
    30  func makeSimplePacket(payload string) []byte {
    31  	l := 1 + len(payload)
    32  	data := make([]byte, l+4)
    33  	data[4] = comQuery
    34  	copy(data[5:], payload)
    35  	data[0] = byte(l)
    36  	data[1] = byte(l >> 8)
    37  	data[2] = byte(l >> 16)
    38  	data[3] = 0
    39  	return data
    40  }
    41  
    42  func packetLen(data []byte) (int32, error) {
    43  	if len(data) < 3 {
    44  		return 0, moerr.NewInternalErrorNoCtx("invalid data")
    45  	}
    46  	return int32(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16), nil
    47  }
    48  
    49  func TestConvert(t *testing.T) {
    50  	p1 := &frontend.Packet{
    51  		Length:     10,
    52  		SequenceID: 0,
    53  		Payload:    []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
    54  	}
    55  	b1 := packetToBytes(p1)
    56  	p2 := bytesToPacket(b1)
    57  	require.Equal(t, p1, p2)
    58  
    59  	b2 := []byte{3, 0, 0, 1, 1, 2, 3}
    60  	p3 := bytesToPacket(b2)
    61  	b3 := packetToBytes(p3)
    62  	require.Equal(t, b2, b3)
    63  }
    64  
    65  func TestPickTunnels(t *testing.T) {
    66  	ts := make(tunnelSet)
    67  	res := pickTunnels(ts, 3)
    68  	require.Equal(t, len(res), 0)
    69  
    70  	t1 := &tunnel{}
    71  	ts.add(t1)
    72  	res = pickTunnels(ts, 3)
    73  	require.Equal(t, len(res), 1)
    74  
    75  	t2 := &tunnel{}
    76  	ts.add(t2)
    77  	t3 := &tunnel{}
    78  	ts.add(t3)
    79  	res = pickTunnels(ts, 2)
    80  	require.Equal(t, len(res), 2)
    81  }
    82  
    83  func TestSortSlice(t *testing.T) {
    84  	var sorted = []any{"a", "b", "c", "d"}
    85  	var s1 = []any{"c", "b", "a", "d"}
    86  	var s2 = []any{"b", "a", "d", "c"}
    87  	newS1 := sortSlice(s1)
    88  	newS2 := sortSlice(s2)
    89  	for i := 0; i < len(s1); i++ {
    90  		require.Equal(t, sorted[i], newS1[i])
    91  		require.Equal(t, sorted[i], newS2[i])
    92  	}
    93  }
    94  
    95  func TestRawHash(t *testing.T) {
    96  	label := labelInfo{
    97  		Tenant: "t1",
    98  		Labels: map[string]string{
    99  			"k1": "v1",
   100  		},
   101  	}
   102  	require.Equal(t, 32, len(rawHash(label)))
   103  }
   104  
   105  func TestIsCmdQuery(t *testing.T) {
   106  	var data []byte
   107  	ret := isCmdQuery(data)
   108  	require.False(t, ret)
   109  
   110  	data = []byte{0, 0, 0, 0, 2, 0}
   111  	ret = isCmdQuery(data)
   112  	require.False(t, ret)
   113  
   114  	data = []byte{0, 0, 0, 0, 3, 0}
   115  	ret = isCmdQuery(data)
   116  	require.True(t, ret)
   117  }
   118  
   119  func TestIsCmdInitDB(t *testing.T) {
   120  	var data []byte
   121  	ret := isCmdInitDB(data)
   122  	require.False(t, ret)
   123  
   124  	data = []byte{0, 0, 0, 0, 20, 0}
   125  	ret = isCmdInitDB(data)
   126  	require.False(t, ret)
   127  
   128  	data = []byte{0, 0, 0, 0, 2, 0}
   129  	ret = isCmdInitDB(data)
   130  	require.True(t, ret)
   131  }
   132  
   133  func TestIsCmdStmtPrepare(t *testing.T) {
   134  	var data []byte
   135  	ret := isCmdStmtPrepare(data)
   136  	require.False(t, ret)
   137  
   138  	data = []byte{0, 0, 0, 0, 20, 0}
   139  	ret = isCmdStmtPrepare(data)
   140  	require.False(t, ret)
   141  
   142  	data = []byte{0, 0, 0, 0, byte(cmdStmtPrepare), 0}
   143  	ret = isCmdStmtPrepare(data)
   144  	require.True(t, ret)
   145  }
   146  
   147  func TestIsCmdStmtClose(t *testing.T) {
   148  	var data []byte
   149  	ret := isCmdStmtClose(data)
   150  	require.False(t, ret)
   151  
   152  	data = []byte{0, 0, 0, 0, 20, 0}
   153  	ret = isCmdStmtClose(data)
   154  	require.False(t, ret)
   155  
   156  	data = []byte{0, 0, 0, 0, byte(cmdStmtClose), 0}
   157  	ret = isCmdStmtClose(data)
   158  	require.True(t, ret)
   159  }
   160  
   161  func TestIsOKPacket(t *testing.T) {
   162  	var data []byte
   163  	ret := isOKPacket(data)
   164  	require.False(t, ret)
   165  
   166  	data = []byte{0, 0, 0, 0, 2, 0}
   167  	ret = isOKPacket(data)
   168  	require.False(t, ret)
   169  
   170  	data = []byte{0, 0, 0, 0, 0, 0}
   171  	ret = isOKPacket(data)
   172  	require.True(t, ret)
   173  }
   174  
   175  func TestIsEOFPacket(t *testing.T) {
   176  	var data []byte
   177  	ret := isEOFPacket(data)
   178  	require.False(t, ret)
   179  
   180  	data = []byte{0, 0, 0, 0, 2, 0}
   181  	ret = isEOFPacket(data)
   182  	require.False(t, ret)
   183  
   184  	data = []byte{0, 0, 0, 0, 0xFE, 0}
   185  	ret = isEOFPacket(data)
   186  	require.True(t, ret)
   187  }
   188  
   189  func TestIsErrPacket(t *testing.T) {
   190  	var data []byte
   191  	ret := isErrPacket(data)
   192  	require.False(t, ret)
   193  
   194  	data = []byte{0, 0, 0, 0, 2, 0}
   195  	ret = isErrPacket(data)
   196  	require.False(t, ret)
   197  
   198  	data = []byte{0, 0, 0, 0, 0xFF, 0}
   199  	ret = isErrPacket(data)
   200  	require.True(t, ret)
   201  }
   202  
   203  func TestContainIP(t *testing.T) {
   204  	cidrs := []string{"192.168.20.0/24", "192.168.10.0/24"}
   205  	ipNetList := make([]*net.IPNet, 0, 2)
   206  	for _, cidr := range cidrs {
   207  		_, ipNet, err := net.ParseCIDR(cidr)
   208  		require.NoError(t, err)
   209  		ipNetList = append(ipNetList, ipNet)
   210  	}
   211  	ip := net.ParseIP("192.168.10.10")
   212  	require.True(t, containIP(ipNetList, ip))
   213  	ip = net.ParseIP("192.168.30.10")
   214  	require.False(t, containIP(ipNetList, ip))
   215  }