github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_miscellaneous_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  
    13  package memex
    14  
    15  import (
    16  	"math"
    17  	"strings"
    18  	"time"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    22  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    23  	"github.com/whtcorpsinc/milevadb/types"
    24  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    25  	"github.com/whtcorpsinc/milevadb/soliton/solitonutil"
    26  )
    27  
    28  func (s *testEvaluatorSuite) TestInetAton(c *C) {
    29  	tbl := []struct {
    30  		Input    interface{}
    31  		Expected interface{}
    32  	}{
    33  		{"", nil},
    34  		{nil, nil},
    35  		{"255.255.255.255", 4294967295},
    36  		{"0.0.0.0", 0},
    37  		{"127.0.0.1", 2130706433},
    38  		{"0.0.0.256", nil},
    39  		{"113.14.22.3", 1896748547},
    40  		{"127", 127},
    41  		{"127.255", 2130706687},
    42  		{"127,256", nil},
    43  		{"127.2.1", 2130837505},
    44  		{"123.2.1.", nil},
    45  		{"127.0.0.1.1", nil},
    46  	}
    47  
    48  	dtbl := tblToDtbl(tbl)
    49  	fc := funcs[ast.InetAton]
    50  	for _, t := range dtbl {
    51  		f, err := fc.getFunction(s.ctx, s.datumsToConstants(t["Input"]))
    52  		c.Assert(err, IsNil)
    53  		d, err := evalBuiltinFunc(f, chunk.Event{})
    54  		c.Assert(err, IsNil)
    55  		c.Assert(d, solitonutil.CausetEquals, t["Expected"][0])
    56  	}
    57  }
    58  
    59  func (s *testEvaluatorSuite) TestIsIPv4(c *C) {
    60  	tests := []struct {
    61  		ip     string
    62  		expect interface{}
    63  	}{
    64  		{"192.168.1.1", 1},
    65  		{"255.255.255.255", 1},
    66  		{"10.t.255.255", 0},
    67  		{"10.1.2.3.4", 0},
    68  		{"2001:250:207:0:0:eef2::1", 0},
    69  		{"::ffff:1.2.3.4", 0},
    70  		{"1...1", 0},
    71  		{"192.168.1.", 0},
    72  		{".168.1.2", 0},
    73  		{"168.1.2", 0},
    74  		{"1.2.3.4.5", 0},
    75  	}
    76  	fc := funcs[ast.IsIPv4]
    77  	for _, test := range tests {
    78  		ip := types.NewStringCauset(test.ip)
    79  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{ip}))
    80  		c.Assert(err, IsNil)
    81  		result, err := evalBuiltinFunc(f, chunk.Event{})
    82  		c.Assert(err, IsNil)
    83  		c.Assert(result, solitonutil.CausetEquals, types.NewCauset(test.expect))
    84  	}
    85  	// test NULL input for is_ipv4
    86  	var argNull types.Causet
    87  	f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull}))
    88  	r, err := evalBuiltinFunc(f, chunk.Event{})
    89  	c.Assert(err, IsNil)
    90  	c.Assert(r, solitonutil.CausetEquals, types.NewCauset(0))
    91  }
    92  
    93  func (s *testEvaluatorSuite) TestUUID(c *C) {
    94  	f, err := newFunctionForTest(s.ctx, ast.UUID)
    95  	c.Assert(err, IsNil)
    96  	d, err := f.Eval(chunk.Event{})
    97  	c.Assert(err, IsNil)
    98  	parts := strings.Split(d.GetString(), "-")
    99  	c.Assert(len(parts), Equals, 5)
   100  	for i, p := range parts {
   101  		switch i {
   102  		case 0:
   103  			c.Assert(len(p), Equals, 8)
   104  		case 1:
   105  			c.Assert(len(p), Equals, 4)
   106  		case 2:
   107  			c.Assert(len(p), Equals, 4)
   108  		case 3:
   109  			c.Assert(len(p), Equals, 4)
   110  		case 4:
   111  			c.Assert(len(p), Equals, 12)
   112  		}
   113  	}
   114  	_, err = funcs[ast.UUID].getFunction(s.ctx, s.datumsToConstants(nil))
   115  	c.Assert(err, IsNil)
   116  }
   117  
   118  func (s *testEvaluatorSuite) TestAnyValue(c *C) {
   119  	tbl := []struct {
   120  		arg interface{}
   121  		ret interface{}
   122  	}{
   123  		{nil, nil},
   124  		{1234, 1234},
   125  		{-0x99, -0x99},
   126  		{3.1415926, 3.1415926},
   127  		{"Hello, World", "Hello, World"},
   128  	}
   129  	for _, t := range tbl {
   130  		fc := funcs[ast.AnyValue]
   131  		f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeCausets(t.arg)))
   132  		c.Assert(err, IsNil)
   133  		r, err := evalBuiltinFunc(f, chunk.Event{})
   134  		c.Assert(err, IsNil)
   135  		c.Assert(r, solitonutil.CausetEquals, types.NewCauset(t.ret))
   136  	}
   137  }
   138  
   139  func (s *testEvaluatorSuite) TestIsIPv6(c *C) {
   140  	tests := []struct {
   141  		ip     string
   142  		expect interface{}
   143  	}{
   144  		{"2001:250:207:0:0:eef2::1", 1},
   145  		{"2001:0250:0207:0001:0000:0000:0000:ff02", 1},
   146  		{"2001:250:207::eff2::1,", 0},
   147  		{"192.168.1.1", 0},
   148  		{"::ffff:1.2.3.4", 1},
   149  	}
   150  	fc := funcs[ast.IsIPv6]
   151  	for _, test := range tests {
   152  		ip := types.NewStringCauset(test.ip)
   153  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{ip}))
   154  		c.Assert(err, IsNil)
   155  		result, err := evalBuiltinFunc(f, chunk.Event{})
   156  		c.Assert(err, IsNil)
   157  		c.Assert(result, solitonutil.CausetEquals, types.NewCauset(test.expect))
   158  	}
   159  	// test NULL input for is_ipv6
   160  	var argNull types.Causet
   161  	f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull}))
   162  	r, err := evalBuiltinFunc(f, chunk.Event{})
   163  	c.Assert(err, IsNil)
   164  	c.Assert(r, solitonutil.CausetEquals, types.NewCauset(0))
   165  }
   166  
   167  func (s *testEvaluatorSuite) TestInetNtoa(c *C) {
   168  	tests := []struct {
   169  		ip     int
   170  		expect interface{}
   171  	}{
   172  		{167773449, "10.0.5.9"},
   173  		{2063728641, "123.2.0.1"},
   174  		{0, "0.0.0.0"},
   175  		{545460846593, nil},
   176  		{-1, nil},
   177  		{math.MaxUint32, "255.255.255.255"},
   178  	}
   179  	fc := funcs[ast.InetNtoa]
   180  	for _, test := range tests {
   181  		ip := types.NewCauset(test.ip)
   182  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{ip}))
   183  		c.Assert(err, IsNil)
   184  		result, err := evalBuiltinFunc(f, chunk.Event{})
   185  		c.Assert(err, IsNil)
   186  		c.Assert(result, solitonutil.CausetEquals, types.NewCauset(test.expect))
   187  	}
   188  
   189  	var argNull types.Causet
   190  	f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull}))
   191  	r, err := evalBuiltinFunc(f, chunk.Event{})
   192  	c.Assert(err, IsNil)
   193  	c.Assert(r.IsNull(), IsTrue)
   194  }
   195  
   196  func (s *testEvaluatorSuite) TestInet6NtoA(c *C) {
   197  	tests := []struct {
   198  		ip     []byte
   199  		expect interface{}
   200  	}{
   201  		// Success cases
   202  		{[]byte{0x00, 0x00, 0x00, 0x00}, "0.0.0.0"},
   203  		{[]byte{0x0A, 0x00, 0x05, 0x09}, "10.0.5.9"},
   204  		{[]byte{0xFD, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, 0x55, 0xCA, 0xFF, 0xFE,
   205  			0xFA, 0x90, 0x89}, "fdfe::5a55:caff:fefa:9089"},
   206  		{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x01,
   207  			0x02, 0x03, 0x04}, "::ffff:1.2.3.4"},
   208  		{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
   209  			0xFF, 0xFF, 0xFF}, "::ffff:255.255.255.255"},
   210  		// Fail cases
   211  		{[]byte{}, nil},                 // missing bytes
   212  		{[]byte{0x0A, 0x00, 0x05}, nil}, // missing a byte ipv4
   213  		{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
   214  			0xFF, 0xFF}, nil}, // missing a byte ipv6
   215  	}
   216  	fc := funcs[ast.Inet6Ntoa]
   217  	for _, test := range tests {
   218  		ip := types.NewCauset(test.ip)
   219  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{ip}))
   220  		c.Assert(err, IsNil)
   221  		result, err := evalBuiltinFunc(f, chunk.Event{})
   222  		c.Assert(err, IsNil)
   223  		c.Assert(result, solitonutil.CausetEquals, types.NewCauset(test.expect))
   224  	}
   225  
   226  	var argNull types.Causet
   227  	f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull}))
   228  	r, err := evalBuiltinFunc(f, chunk.Event{})
   229  	c.Assert(err, IsNil)
   230  	c.Assert(r.IsNull(), IsTrue)
   231  }
   232  
   233  func (s *testEvaluatorSuite) TestInet6AtoN(c *C) {
   234  	tests := []struct {
   235  		ip     string
   236  		expect interface{}
   237  	}{
   238  		{"0.0.0.0", []byte{0x00, 0x00, 0x00, 0x00}},
   239  		{"10.0.5.9", []byte{0x0A, 0x00, 0x05, 0x09}},
   240  		{"fdfe::5a55:caff:fefa:9089", []byte{0xFD, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, 0x55, 0xCA, 0xFF, 0xFE, 0xFA, 0x90, 0x89}},
   241  		{"::ffff:1.2.3.4", []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x01, 0x02, 0x03, 0x04}},
   242  		{"", nil},
   243  		{"Not IP address", nil},
   244  		{"::ffff:255.255.255.255", []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
   245  	}
   246  	fc := funcs[ast.Inet6Aton]
   247  	for _, test := range tests {
   248  		ip := types.NewCauset(test.ip)
   249  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{ip}))
   250  		c.Assert(err, IsNil)
   251  		result, err := evalBuiltinFunc(f, chunk.Event{})
   252  		c.Assert(err, IsNil)
   253  		c.Assert(result, solitonutil.CausetEquals, types.NewCauset(test.expect))
   254  	}
   255  
   256  	var argNull types.Causet
   257  	f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull}))
   258  	r, err := evalBuiltinFunc(f, chunk.Event{})
   259  	c.Assert(err, IsNil)
   260  	c.Assert(r.IsNull(), IsTrue)
   261  }
   262  
   263  func (s *testEvaluatorSuite) TestIsIPv4Mapped(c *C) {
   264  	tests := []struct {
   265  		ip     []byte
   266  		expect interface{}
   267  	}{
   268  		{[]byte{}, 0},
   269  		{[]byte{0x10, 0x10, 0x10, 0x10}, 0},
   270  		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x1, 0x2, 0x3, 0x4}, 1},
   271  		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xff, 0xff, 0x1, 0x2, 0x3, 0x4}, 0},
   272  		{[]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6}, 0},
   273  	}
   274  	fc := funcs[ast.IsIPv4Mapped]
   275  	for _, test := range tests {
   276  		ip := types.NewCauset(test.ip)
   277  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{ip}))
   278  		c.Assert(err, IsNil)
   279  		result, err := evalBuiltinFunc(f, chunk.Event{})
   280  		c.Assert(err, IsNil)
   281  		c.Assert(result, solitonutil.CausetEquals, types.NewCauset(test.expect))
   282  	}
   283  
   284  	var argNull types.Causet
   285  	f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull}))
   286  	r, err := evalBuiltinFunc(f, chunk.Event{})
   287  	c.Assert(err, IsNil)
   288  	c.Assert(r, solitonutil.CausetEquals, types.NewCauset(int64(0)))
   289  }
   290  
   291  func (s *testEvaluatorSuite) TestIsIPv4Compat(c *C) {
   292  	tests := []struct {
   293  		ip     []byte
   294  		expect interface{}
   295  	}{
   296  		{[]byte{}, 0},
   297  		{[]byte{0x10, 0x10, 0x10, 0x10}, 0},
   298  		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x2, 0x3, 0x4}, 1},
   299  		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x1, 0x2, 0x3, 0x4}, 0},
   300  		{[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xff, 0xff, 0x1, 0x2, 0x3, 0x4}, 0},
   301  		{[]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6}, 0},
   302  	}
   303  	fc := funcs[ast.IsIPv4Compat]
   304  	for _, test := range tests {
   305  		ip := types.NewCauset(test.ip)
   306  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{ip}))
   307  		c.Assert(err, IsNil)
   308  		result, err := evalBuiltinFunc(f, chunk.Event{})
   309  		c.Assert(err, IsNil)
   310  		c.Assert(result, solitonutil.CausetEquals, types.NewCauset(test.expect))
   311  	}
   312  
   313  	var argNull types.Causet
   314  	f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull}))
   315  	r, err := evalBuiltinFunc(f, chunk.Event{})
   316  	c.Assert(err, IsNil)
   317  	c.Assert(r, solitonutil.CausetEquals, types.NewCauset(0))
   318  }
   319  
   320  func (s *testEvaluatorSuite) TestNameConst(c *C) {
   321  	dec := types.NewDecFromFloatForTest(123.123)
   322  	tm := types.NewTime(types.FromGoTime(time.Now()), allegrosql.TypeDatetime, 6)
   323  	du := types.Duration{Duration: 12*time.Hour + 1*time.Minute + 1*time.Second, Fsp: types.DefaultFsp}
   324  	cases := []struct {
   325  		defCausName string
   326  		arg     interface{}
   327  		isNil   bool
   328  		asserts func(d types.Causet)
   329  	}{
   330  		{"test_int", 3, false, func(d types.Causet) {
   331  			c.Assert(d.GetInt64(), Equals, int64(3))
   332  		}},
   333  		{"test_float", 3.14159, false, func(d types.Causet) {
   334  			c.Assert(d.GetFloat64(), Equals, 3.14159)
   335  		}},
   336  		{"test_string", "MilevaDB", false, func(d types.Causet) {
   337  			c.Assert(d.GetString(), Equals, "MilevaDB")
   338  		}},
   339  		{"test_null", nil, true, func(d types.Causet) {
   340  			c.Assert(d.HoTT(), Equals, types.HoTTNull)
   341  		}},
   342  		{"test_decimal", dec, false, func(d types.Causet) {
   343  			c.Assert(d.GetMysqlDecimal().String(), Equals, dec.String())
   344  		}},
   345  		{"test_time", tm, false, func(d types.Causet) {
   346  			c.Assert(d.GetMysqlTime().String(), Equals, tm.String())
   347  		}},
   348  		{"test_duration", du, false, func(d types.Causet) {
   349  			c.Assert(d.GetMysqlDuration().String(), Equals, du.String())
   350  		}},
   351  	}
   352  
   353  	for _, t := range cases {
   354  		f, err := newFunctionForTest(s.ctx, ast.NameConst, s.primitiveValsToConstants([]interface{}{t.defCausName, t.arg})...)
   355  		c.Assert(err, IsNil)
   356  		d, err := f.Eval(chunk.Event{})
   357  		c.Assert(err, IsNil)
   358  		t.asserts(d)
   359  	}
   360  }