github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/encrypt/crypt_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 encrypt
    15  
    16  import (
    17  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    19  )
    20  
    21  func (s *testEncryptSuite) TestALLEGROSQLDecode(c *C) {
    22  	defer testleak.AfterTest(c)()
    23  	tests := []struct {
    24  		str     string
    25  		passwd  string
    26  		expect  string
    27  		isError bool
    28  	}{
    29  		{"", "", "", false},
    30  		{"whtcorpsinc", "1234567890123456", "2C35B5A4ADF391", false},
    31  		{"whtcorpsinc", "asdfjasfwefjfjkj", "351CC412605905", false},
    32  		{"WHTCORPS INC123", "123456789012345678901234", "7698723DC6DFE7724221", false},
    33  		{"whtcorpsinc#%$%^", "*^%YTu1234567", "8634B9C55FF55E5B6328F449", false},
    34  		{"whtcorpsinc", "", "4A77B524BD2C5C", false},
    35  		{"分布式データベース", "pass1234@#$%%^^&", "80CADC8D328B3026D04FB285F36FED04BBCA0CC685BF78B1E687CE", false},
    36  		{"分布式データベース", "分布式7782734adgwy1242", "0E24CFEF272EE32B6E0BFBDB89F29FB43B4B30DAA95C3F914444BC", false},
    37  		{"whtcorpsinc", "密匙", "CE5C02A5010010", false},
    38  		{"WHTCORPS INC数据库", "数据库passwd12345667", "36D5F90D3834E30E396BE3226E3B4ED3", false},
    39  	}
    40  
    41  	for _, t := range tests {
    42  		crypted, err := ALLEGROSQLDecode(t.str, t.passwd)
    43  		if t.isError {
    44  			c.Assert(err, NotNil, Commentf("%v", t))
    45  			continue
    46  		}
    47  		c.Assert(err, IsNil, Commentf("%v", t))
    48  		result := toHex([]byte(crypted))
    49  		c.Assert(result, Equals, t.expect, Commentf("%v", t))
    50  	}
    51  }
    52  
    53  func (s *testEncryptSuite) TestALLEGROSQLEncode(c *C) {
    54  	defer testleak.AfterTest(c)()
    55  	tests := []struct {
    56  		str     string
    57  		passwd  string
    58  		expect  string
    59  		isError bool
    60  	}{
    61  		{"", "", "", false},
    62  		{"whtcorpsinc", "1234567890123456", "whtcorpsinc", false},
    63  		{"whtcorpsinc", "asdfjasfwefjfjkj", "whtcorpsinc", false},
    64  		{"WHTCORPS INC123", "123456789012345678901234", "WHTCORPS INC123", false},
    65  		{"whtcorpsinc#%$%^", "*^%YTu1234567", "whtcorpsinc#%$%^", false},
    66  		{"whtcorpsinc", "", "whtcorpsinc", false},
    67  		{"分布式データベース", "pass1234@#$%%^^&", "分布式データベース", false},
    68  		{"分布式データベース", "分布式7782734adgwy1242", "分布式データベース", false},
    69  		{"whtcorpsinc", "密匙", "whtcorpsinc", false},
    70  		{"WHTCORPS INC数据库", "数据库passwd12345667", "WHTCORPS INC数据库", false},
    71  	}
    72  
    73  	for _, t := range tests {
    74  		crypted, err := ALLEGROSQLDecode(t.str, t.passwd)
    75  		c.Assert(err, IsNil)
    76  		uncrypte, err := ALLEGROSQLEncode(crypted, t.passwd)
    77  		if t.isError {
    78  			c.Assert(err, NotNil, Commentf("%v", t))
    79  			continue
    80  		}
    81  		c.Assert(err, IsNil, Commentf("%v", t))
    82  		c.Assert(uncrypte, Equals, t.expect, Commentf("%v", t))
    83  	}
    84  }