github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_encryption_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  //
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package memex
    15  
    16  import (
    17  	"encoding/hex"
    18  	"strings"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    22  	"github.com/whtcorpsinc/BerolinaSQL/charset"
    23  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    24  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    25  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    26  	"github.com/whtcorpsinc/milevadb/types"
    27  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    28  	"github.com/whtcorpsinc/milevadb/soliton/defCauslate"
    29  	"github.com/whtcorpsinc/milevadb/soliton/replog"
    30  )
    31  
    32  var cryptTests = []struct {
    33  	origin   interface{}
    34  	password interface{}
    35  	crypt    interface{}
    36  }{
    37  	{"", "", ""},
    38  	{"whtcorpsinc", "1234567890123456", "2C35B5A4ADF391"},
    39  	{"whtcorpsinc", "asdfjasfwefjfjkj", "351CC412605905"},
    40  	{"WHTCORPS INC123", "123456789012345678901234", "7698723DC6DFE7724221"},
    41  	{"whtcorpsinc#%$%^", "*^%YTu1234567", "8634B9C55FF55E5B6328F449"},
    42  	{"whtcorpsinc", "", "4A77B524BD2C5C"},
    43  	{"分布式データベース", "pass1234@#$%%^^&", "80CADC8D328B3026D04FB285F36FED04BBCA0CC685BF78B1E687CE"},
    44  	{"分布式データベース", "分布式7782734adgwy1242", "0E24CFEF272EE32B6E0BFBDB89F29FB43B4B30DAA95C3F914444BC"},
    45  	{"whtcorpsinc", "密匙", "CE5C02A5010010"},
    46  	{"WHTCORPS INC数据库", "数据库passwd12345667", "36D5F90D3834E30E396BE3226E3B4ED3"},
    47  	{"数据库5667", 123.435, "B22196D0569386237AE12F8AAB"},
    48  	{nil, "数据库passwd12345667", nil},
    49  }
    50  
    51  func (s *testEvaluatorSuite) TestALLEGROSQLDecode(c *C) {
    52  	fc := funcs[ast.Decode]
    53  	for _, tt := range cryptTests {
    54  		str := types.NewCauset(tt.origin)
    55  		password := types.NewCauset(tt.password)
    56  
    57  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{str, password}))
    58  		c.Assert(err, IsNil)
    59  		crypt, err := evalBuiltinFunc(f, chunk.Event{})
    60  		c.Assert(err, IsNil)
    61  		c.Assert(toHex(crypt), DeepEquals, types.NewCauset(tt.crypt))
    62  	}
    63  	s.testNullInput(c, ast.Decode)
    64  }
    65  
    66  func (s *testEvaluatorSuite) TestALLEGROSQLEncode(c *C) {
    67  	fc := funcs[ast.Encode]
    68  	for _, test := range cryptTests {
    69  		password := types.NewCauset(test.password)
    70  		cryptStr := fromHex(test.crypt)
    71  
    72  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{cryptStr, password}))
    73  		c.Assert(err, IsNil)
    74  		str, err := evalBuiltinFunc(f, chunk.Event{})
    75  
    76  		c.Assert(err, IsNil)
    77  		c.Assert(str, DeepEquals, types.NewCauset(test.origin))
    78  	}
    79  	s.testNullInput(c, ast.Encode)
    80  }
    81  
    82  var aesTests = []struct {
    83  	mode   string
    84  	origin interface{}
    85  	params []interface{}
    86  	crypt  interface{}
    87  }{
    88  	// test for ecb
    89  	{"aes-128-ecb", "whtcorpsinc", []interface{}{"1234567890123456"}, "697BFE9B3F8C2F289DD82C88C7BC95C4"},
    90  	{"aes-128-ecb", "WHTCORPS INC123", []interface{}{"1234567890123456"}, "CEC348F4EF5F84D3AA6C4FA184C65766"},
    91  	{"aes-128-ecb", "whtcorpsinc", []interface{}{"123456789012345678901234"}, "6F1589686860C8E8C7A40A78B25FF2C0"},
    92  	{"aes-128-ecb", "whtcorpsinc", []interface{}{"123"}, "996E0CA8688D7AD20819B90B273E01C6"},
    93  	{"aes-128-ecb", "whtcorpsinc", []interface{}{123}, "996E0CA8688D7AD20819B90B273E01C6"},
    94  	{"aes-128-ecb", nil, []interface{}{123}, nil},
    95  	{"aes-192-ecb", "whtcorpsinc", []interface{}{"1234567890123456"}, "9B139FD002E6496EA2D5C73A2265E661"},
    96  	{"aes-256-ecb", "whtcorpsinc", []interface{}{"1234567890123456"}, "F80DCDEDDBE5663BDB68F74AEDDB8EE3"},
    97  	// test for cbc
    98  	{"aes-128-cbc", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "2ECA0077C5EA5768A0485AA522774792"},
    99  	{"aes-128-cbc", "whtcorpsinc", []interface{}{"123456789012345678901234", "1234567890123456"}, "483788634DA8817423BA0934FD2C096E"},
   100  	{"aes-192-cbc", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "516391DB38E908ECA93AAB22870EC787"},
   101  	{"aes-256-cbc", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "5D0E22C1E77523AEF5C3E10B65653C8F"},
   102  	{"aes-256-cbc", "whtcorpsinc", []interface{}{"12345678901234561234567890123456", "1234567890123456"}, "A26BA27CA4BE9D361D545AA84A17002D"},
   103  	{"aes-256-cbc", "whtcorpsinc", []interface{}{"1234567890123456", "12345678901234561234567890123456"}, "5D0E22C1E77523AEF5C3E10B65653C8F"},
   104  	// test for ofb
   105  	{"aes-128-ofb", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "0515A36BBF3DE0"},
   106  	{"aes-128-ofb", "whtcorpsinc", []interface{}{"123456789012345678901234", "1234567890123456"}, "C2A93A93818546"},
   107  	{"aes-192-ofb", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "FE09DCCF14D458"},
   108  	{"aes-256-ofb", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "2E70FCAC0C0834"},
   109  	{"aes-256-ofb", "whtcorpsinc", []interface{}{"12345678901234561234567890123456", "1234567890123456"}, "83E2B30A71F011"},
   110  	{"aes-256-ofb", "whtcorpsinc", []interface{}{"1234567890123456", "12345678901234561234567890123456"}, "2E70FCAC0C0834"},
   111  	// test for cfb
   112  	{"aes-128-cfb", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "0515A36BBF3DE0"},
   113  	{"aes-128-cfb", "whtcorpsinc", []interface{}{"123456789012345678901234", "1234567890123456"}, "C2A93A93818546"},
   114  	{"aes-192-cfb", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "FE09DCCF14D458"},
   115  	{"aes-256-cfb", "whtcorpsinc", []interface{}{"1234567890123456", "1234567890123456"}, "2E70FCAC0C0834"},
   116  	{"aes-256-cfb", "whtcorpsinc", []interface{}{"12345678901234561234567890123456", "1234567890123456"}, "83E2B30A71F011"},
   117  	{"aes-256-cfb", "whtcorpsinc", []interface{}{"1234567890123456", "12345678901234561234567890123456"}, "2E70FCAC0C0834"},
   118  }
   119  
   120  func (s *testEvaluatorSuite) TestAESEncrypt(c *C) {
   121  	fc := funcs[ast.AesEncrypt]
   122  	for _, tt := range aesTests {
   123  		variable.SetStochastikSystemVar(s.ctx.GetStochastikVars(), variable.BlockEncryptionMode, types.NewCauset(tt.mode))
   124  		args := []types.Causet{types.NewCauset(tt.origin)}
   125  		for _, param := range tt.params {
   126  			args = append(args, types.NewCauset(param))
   127  		}
   128  		f, err := fc.getFunction(s.ctx, s.datumsToConstants(args))
   129  		c.Assert(err, IsNil)
   130  		crypt, err := evalBuiltinFunc(f, chunk.Event{})
   131  		c.Assert(err, IsNil)
   132  		c.Assert(toHex(crypt), DeepEquals, types.NewCauset(tt.crypt))
   133  	}
   134  	variable.SetStochastikSystemVar(s.ctx.GetStochastikVars(), variable.BlockEncryptionMode, types.NewCauset("aes-128-ecb"))
   135  	s.testNullInput(c, ast.AesEncrypt)
   136  	s.testAmbiguousInput(c, ast.AesEncrypt)
   137  }
   138  
   139  func (s *testEvaluatorSuite) TestAESDecrypt(c *C) {
   140  	fc := funcs[ast.AesDecrypt]
   141  	for _, tt := range aesTests {
   142  		variable.SetStochastikSystemVar(s.ctx.GetStochastikVars(), variable.BlockEncryptionMode, types.NewCauset(tt.mode))
   143  		args := []types.Causet{fromHex(tt.crypt)}
   144  		for _, param := range tt.params {
   145  			args = append(args, types.NewCauset(param))
   146  		}
   147  		f, err := fc.getFunction(s.ctx, s.datumsToConstants(args))
   148  		c.Assert(err, IsNil)
   149  		str, err := evalBuiltinFunc(f, chunk.Event{})
   150  		c.Assert(err, IsNil)
   151  		if tt.origin == nil {
   152  			c.Assert(str.IsNull(), IsTrue)
   153  			continue
   154  		}
   155  		c.Assert(str, DeepEquals, types.NewDefCauslationStringCauset(tt.origin.(string), charset.DefCauslationBin, defCauslate.DefaultLen))
   156  	}
   157  	variable.SetStochastikSystemVar(s.ctx.GetStochastikVars(), variable.BlockEncryptionMode, types.NewCauset("aes-128-ecb"))
   158  	s.testNullInput(c, ast.AesDecrypt)
   159  	s.testAmbiguousInput(c, ast.AesDecrypt)
   160  }
   161  
   162  func (s *testEvaluatorSuite) testNullInput(c *C, fnName string) {
   163  	variable.SetStochastikSystemVar(s.ctx.GetStochastikVars(), variable.BlockEncryptionMode, types.NewCauset("aes-128-ecb"))
   164  	fc := funcs[fnName]
   165  	arg := types.NewStringCauset("str")
   166  	var argNull types.Causet
   167  	f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{arg, argNull}))
   168  	c.Assert(err, IsNil)
   169  	crypt, err := evalBuiltinFunc(f, chunk.Event{})
   170  	c.Assert(err, IsNil)
   171  	c.Assert(crypt.IsNull(), IsTrue)
   172  
   173  	f, err = fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull, arg}))
   174  	c.Assert(err, IsNil)
   175  	crypt, err = evalBuiltinFunc(f, chunk.Event{})
   176  	c.Assert(err, IsNil)
   177  	c.Assert(crypt.IsNull(), IsTrue)
   178  }
   179  
   180  func (s *testEvaluatorSuite) testAmbiguousInput(c *C, fnName string) {
   181  	fc := funcs[fnName]
   182  	arg := types.NewStringCauset("str")
   183  	// test for modes that require init_vector
   184  	variable.SetStochastikSystemVar(s.ctx.GetStochastikVars(), variable.BlockEncryptionMode, types.NewCauset("aes-128-cbc"))
   185  	_, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{arg, arg}))
   186  	c.Assert(err, NotNil)
   187  	f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{arg, arg, types.NewStringCauset("iv < 16 bytes")}))
   188  	c.Assert(err, IsNil)
   189  	_, err = evalBuiltinFunc(f, chunk.Event{})
   190  	c.Assert(err, NotNil)
   191  
   192  	// test for modes that do not require init_vector
   193  	variable.SetStochastikSystemVar(s.ctx.GetStochastikVars(), variable.BlockEncryptionMode, types.NewCauset("aes-128-ecb"))
   194  	f, err = fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{arg, arg, arg}))
   195  	c.Assert(err, IsNil)
   196  	_, err = evalBuiltinFunc(f, chunk.Event{})
   197  	c.Assert(err, IsNil)
   198  	warnings := s.ctx.GetStochastikVars().StmtCtx.GetWarnings()
   199  	c.Assert(len(warnings), GreaterEqual, 1)
   200  }
   201  
   202  func toHex(d types.Causet) (h types.Causet) {
   203  	if d.IsNull() {
   204  		return
   205  	}
   206  	x, _ := d.ToString()
   207  	h.SetString(strings.ToUpper(hex.EncodeToString(replog.Slice(x))), allegrosql.DefaultDefCauslationName)
   208  	return
   209  }
   210  
   211  func fromHex(str interface{}) (d types.Causet) {
   212  	if str == nil {
   213  		return
   214  	}
   215  	if s, ok := str.(string); ok {
   216  		h, _ := hex.DecodeString(s)
   217  		d.SetBytes(h)
   218  	}
   219  	return d
   220  }
   221  
   222  var sha1Tests = []struct {
   223  	origin interface{}
   224  	crypt  string
   225  }{
   226  	{"test", "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"},
   227  	{"c4pt0r", "034923dcabf099fc4c8917c0ab91ffcd4c2578a6"},
   228  	{"whtcorpsinc", "73bf9ef43a44f42e2ea2894d62f0917af149a006"},
   229  	{"foobar", "8843d7f92416211de9ebb963ff4ce28125932878"},
   230  	{1024, "128351137a9c47206c4507dcf2e6fbeeca3a9079"},
   231  	{123.45, "22f8b438ad7e89300b51d88684f3f0b9fa1d7a32"},
   232  }
   233  
   234  func (s *testEvaluatorSuite) TestSha1Hash(c *C) {
   235  	fc := funcs[ast.SHA]
   236  	for _, tt := range sha1Tests {
   237  		in := types.NewCauset(tt.origin)
   238  		f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{in}))
   239  		crypt, err := evalBuiltinFunc(f, chunk.Event{})
   240  		c.Assert(err, IsNil)
   241  		res, err := crypt.ToString()
   242  		c.Assert(err, IsNil)
   243  		c.Assert(res, Equals, tt.crypt)
   244  	}
   245  	// test NULL input for sha
   246  	var argNull types.Causet
   247  	f, _ := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{argNull}))
   248  	crypt, err := evalBuiltinFunc(f, chunk.Event{})
   249  	c.Assert(err, IsNil)
   250  	c.Assert(crypt.IsNull(), IsTrue)
   251  }
   252  
   253  var sha2Tests = []struct {
   254  	origin     interface{}
   255  	hashLength interface{}
   256  	crypt      interface{}
   257  	validCase  bool
   258  }{
   259  	{"whtcorpsinc", 0, "2871823be240f8ecd1d72f24c99eaa2e58af18b4b8ba99a4fc2823ba5c43930a", true},
   260  	{"whtcorpsinc", 224, "cd036dc9bec69e758401379c522454ea24a6327b48724b449b40c6b7", true},
   261  	{"whtcorpsinc", 256, "2871823be240f8ecd1d72f24c99eaa2e58af18b4b8ba99a4fc2823ba5c43930a", true},
   262  	{"whtcorpsinc", 384, "c50955b6b0c7b9919740d956849eedcb0f0f90bf8a34e8c1f4e071e3773f53bd6f8f16c04425ff728bed04de1b63db51", true},
   263  	{"whtcorpsinc", 512, "ea903c574370774c4844a83b7122105a106e04211673810e1baae7c2ae7aba2cf07465e02f6c413126111ef74a417232683ce7ba210052e63c15fc82204aad80", true},
   264  	{13572468, 0, "1c91ab1c162fd0cae60a5bb9880f3e7d5a133a65b6057a644b26973d9c55dcfe", true},
   265  	{13572468, 224, "8ad67735bbf49576219f364f4640d595357a440358d15bf6815a16e4", true},
   266  	{13572468, 256, "1c91ab1c162fd0cae60a5bb9880f3e7d5a133a65b6057a644b26973d9c55dcfe", true},
   267  	{13572468.123, 384, "3b4ee302435dc1e15251efd9f3982b1ca6fe4ac778d3260b7bbf3bea613849677eda830239420e448e4c6dc7c2649d89", true},
   268  	{13572468.123, 512, "4820aa3f2760836557dc1f2d44a0ba7596333fdb60c8a1909481862f4ab0921c00abb23d57b7e67a970363cc3fcb78b25b6a0d45cdcac0e87aa0c96bc51f7f96", true},
   269  	{nil, 224, nil, false},
   270  	{"whtcorpsinc", nil, nil, false},
   271  	{"whtcorpsinc", 123, nil, false},
   272  }
   273  
   274  func (s *testEvaluatorSuite) TestSha2Hash(c *C) {
   275  	fc := funcs[ast.SHA2]
   276  	for _, tt := range sha2Tests {
   277  		str := types.NewCauset(tt.origin)
   278  		hashLength := types.NewCauset(tt.hashLength)
   279  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{str, hashLength}))
   280  		c.Assert(err, IsNil)
   281  		crypt, err := evalBuiltinFunc(f, chunk.Event{})
   282  		c.Assert(err, IsNil)
   283  		if tt.validCase {
   284  			res, err := crypt.ToString()
   285  			c.Assert(err, IsNil)
   286  			c.Assert(res, Equals, tt.crypt)
   287  		} else {
   288  			c.Assert(crypt.IsNull(), IsTrue)
   289  		}
   290  	}
   291  }
   292  
   293  func (s *testEvaluatorSuite) TestMD5Hash(c *C) {
   294  	cases := []struct {
   295  		args     interface{}
   296  		expected string
   297  		isNil    bool
   298  		getErr   bool
   299  	}{
   300  		{"", "d41d8cd98f00b204e9800998ecf8427e", false, false},
   301  		{"a", "0cc175b9c0f1b6a831c399e269772661", false, false},
   302  		{"ab", "187ef4436122d1cc2f40dc2b92f0eba0", false, false},
   303  		{"abc", "900150983cd24fb0d6963f7d28e17f72", false, false},
   304  		{123, "202cb962ac59075b964b07152d234b70", false, false},
   305  		{"123", "202cb962ac59075b964b07152d234b70", false, false},
   306  		{123.123, "46ddc40585caa8abc07c460b3485781e", false, false},
   307  		{nil, "", true, false},
   308  	}
   309  	for _, t := range cases {
   310  		f, err := newFunctionForTest(s.ctx, ast.MD5, s.primitiveValsToConstants([]interface{}{t.args})...)
   311  		c.Assert(err, IsNil)
   312  		d, err := f.Eval(chunk.Event{})
   313  		if t.getErr {
   314  			c.Assert(err, NotNil)
   315  		} else {
   316  			c.Assert(err, IsNil)
   317  			if t.isNil {
   318  				c.Assert(d.HoTT(), Equals, types.HoTTNull)
   319  			} else {
   320  				c.Assert(d.GetString(), Equals, t.expected)
   321  			}
   322  		}
   323  	}
   324  	_, err := funcs[ast.MD5].getFunction(s.ctx, []Expression{NewZero()})
   325  	c.Assert(err, IsNil)
   326  
   327  }
   328  
   329  func (s *testEvaluatorSuite) TestRandomBytes(c *C) {
   330  	fc := funcs[ast.RandomBytes]
   331  	f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{types.NewCauset(32)}))
   332  	c.Assert(err, IsNil)
   333  	out, err := evalBuiltinFunc(f, chunk.Event{})
   334  	c.Assert(err, IsNil)
   335  	c.Assert(len(out.GetBytes()), Equals, 32)
   336  
   337  	f, err = fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{types.NewCauset(1025)}))
   338  	c.Assert(err, IsNil)
   339  	_, err = evalBuiltinFunc(f, chunk.Event{})
   340  	c.Assert(err, NotNil)
   341  	f, err = fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{types.NewCauset(-32)}))
   342  	c.Assert(err, IsNil)
   343  	_, err = evalBuiltinFunc(f, chunk.Event{})
   344  	c.Assert(err, NotNil)
   345  	f, err = fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{types.NewCauset(0)}))
   346  	c.Assert(err, IsNil)
   347  	_, err = evalBuiltinFunc(f, chunk.Event{})
   348  	c.Assert(err, NotNil)
   349  
   350  	f, err = fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{types.NewCauset(nil)}))
   351  	c.Assert(err, IsNil)
   352  	out, err = evalBuiltinFunc(f, chunk.Event{})
   353  	c.Assert(err, IsNil)
   354  	c.Assert(len(out.GetBytes()), Equals, 0)
   355  }
   356  
   357  func decodeHex(str string) []byte {
   358  	ret, err := hex.DecodeString(str)
   359  	if err != nil {
   360  		panic(err)
   361  	}
   362  	return ret
   363  }
   364  
   365  func (s *testEvaluatorSuite) TestCompress(c *C) {
   366  	tests := []struct {
   367  		in     interface{}
   368  		expect interface{}
   369  	}{
   370  		{"hello world", string(decodeHex("0B000000789CCA48CDC9C95728CF2FCA4901040000FFFF1A0B045D"))},
   371  		{"", ""},
   372  		{nil, nil},
   373  	}
   374  
   375  	fc := funcs[ast.Compress]
   376  	for _, test := range tests {
   377  		arg := types.NewCauset(test.in)
   378  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{arg}))
   379  		c.Assert(err, IsNil, Commentf("%v", test))
   380  		out, err := evalBuiltinFunc(f, chunk.Event{})
   381  		c.Assert(err, IsNil, Commentf("%v", test))
   382  		if test.expect == nil {
   383  			c.Assert(out.IsNull(), IsTrue, Commentf("%v", test))
   384  			continue
   385  		}
   386  		c.Assert(out, DeepEquals, types.NewDefCauslationStringCauset(test.expect.(string), charset.DefCauslationBin, defCauslate.DefaultLen), Commentf("%v", test))
   387  	}
   388  }
   389  
   390  func (s *testEvaluatorSuite) TestUncompress(c *C) {
   391  	tests := []struct {
   392  		in     interface{}
   393  		expect interface{}
   394  	}{
   395  		{decodeHex("0B000000789CCB48CDC9C95728CF2FCA4901001A0B045D"), "hello world"},         // zlib result from MyALLEGROSQL
   396  		{decodeHex("0B000000789CCA48CDC9C95728CF2FCA4901040000FFFF1A0B045D"), "hello world"}, // zlib result from MilevaDB
   397  		{decodeHex("02000000789CCB48CDC9C95728CF2FCA4901001A0B045D"), nil},                   // wrong length in the first four bytes
   398  		{decodeHex(""), ""},
   399  		{"1", nil},
   400  		{"1234", nil},
   401  		{"12345", nil},
   402  		{decodeHex("0B"), nil},
   403  		{decodeHex("0B000000"), nil},
   404  		{decodeHex("0B0000001234"), nil},
   405  		{12345, nil},
   406  		{nil, nil},
   407  	}
   408  
   409  	fc := funcs[ast.Uncompress]
   410  	for _, test := range tests {
   411  		arg := types.NewCauset(test.in)
   412  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{arg}))
   413  		c.Assert(err, IsNil, Commentf("%v", test))
   414  		out, err := evalBuiltinFunc(f, chunk.Event{})
   415  		c.Assert(err, IsNil, Commentf("%v", test))
   416  		if test.expect == nil {
   417  			c.Assert(out.IsNull(), IsTrue, Commentf("%v", test))
   418  			continue
   419  		}
   420  		c.Assert(out, DeepEquals, types.NewDefCauslationStringCauset(test.expect.(string), charset.DefCauslationBin, defCauslate.DefaultLen), Commentf("%v", test))
   421  	}
   422  }
   423  
   424  func (s *testEvaluatorSuite) TestUncompressLength(c *C) {
   425  	tests := []struct {
   426  		in     interface{}
   427  		expect interface{}
   428  	}{
   429  		{decodeHex("0B000000789CCB48CDC9C95728CF2FCA4901001A0B045D"), int64(11)},         // zlib result from MyALLEGROSQL
   430  		{decodeHex("0B000000789CCA48CDC9C95728CF2FCA4901040000FFFF1A0B045D"), int64(11)}, // zlib result from MilevaDB
   431  		{decodeHex(""), int64(0)},
   432  		{"1", int64(0)},
   433  		{"123", int64(0)},
   434  		{decodeHex("0B"), int64(0)},
   435  		{decodeHex("0B00"), int64(0)},
   436  		{decodeHex("0B000000"), int64(0x0)},
   437  		{decodeHex("0B0000001234"), int64(0x0B)},
   438  		{12345, int64(875770417)},
   439  		{nil, nil},
   440  	}
   441  
   442  	fc := funcs[ast.UncompressedLength]
   443  	for _, test := range tests {
   444  		arg := types.NewCauset(test.in)
   445  		f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Causet{arg}))
   446  		c.Assert(err, IsNil, Commentf("%v", test))
   447  		out, err := evalBuiltinFunc(f, chunk.Event{})
   448  		c.Assert(err, IsNil, Commentf("%v", test))
   449  		c.Assert(out, DeepEquals, types.NewCauset(test.expect), Commentf("%v", test))
   450  	}
   451  }
   452  
   453  func (s *testEvaluatorSuite) TestPassword(c *C) {
   454  	cases := []struct {
   455  		args     interface{}
   456  		expected string
   457  		isNil    bool
   458  		getErr   bool
   459  		getWarn  bool
   460  	}{
   461  		{nil, "", false, false, false},
   462  		{"", "", false, false, false},
   463  		{"abc", "*0D3CED9BEC10A777AEC23CCC353A8C08A633045E", false, false, true},
   464  		{123, "*23AE809DDACAF96AF0FD78ED04B6A265E05AA257", false, false, true},
   465  		{1.23, "*A589EEBA8D3F9E1A34A7EE518FAC4566BFAD5BB6", false, false, true},
   466  		{types.NewDecFromFloatForTest(123.123), "*B15B84262DB34BFB2C817A45A55C405DC7C52BB1", false, false, true},
   467  	}
   468  
   469  	warnCount := len(s.ctx.GetStochastikVars().StmtCtx.GetWarnings())
   470  	for _, t := range cases {
   471  		f, err := newFunctionForTest(s.ctx, ast.PasswordFunc, s.primitiveValsToConstants([]interface{}{t.args})...)
   472  		c.Assert(err, IsNil)
   473  		d, err := f.Eval(chunk.Event{})
   474  		c.Assert(err, IsNil)
   475  		if t.isNil {
   476  			c.Assert(d.HoTT(), Equals, types.HoTTNull)
   477  		} else {
   478  			c.Assert(d.GetString(), Equals, t.expected)
   479  		}
   480  
   481  		warnings := s.ctx.GetStochastikVars().StmtCtx.GetWarnings()
   482  		if t.getWarn {
   483  			c.Assert(len(warnings), Equals, warnCount+1)
   484  
   485  			lastWarn := warnings[len(warnings)-1]
   486  			c.Assert(terror.ErrorEqual(errDeprecatedSyntaxNoRememristed, lastWarn.Err), IsTrue, Commentf("err %v", lastWarn.Err))
   487  
   488  			warnCount = len(warnings)
   489  		} else {
   490  			c.Assert(len(warnings), Equals, warnCount)
   491  		}
   492  	}
   493  
   494  	_, err := funcs[ast.PasswordFunc].getFunction(s.ctx, []Expression{NewZero()})
   495  	c.Assert(err, IsNil)
   496  }