github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/utils/key_test.go (about) 1 // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. 2 3 package utils 4 5 import ( 6 "encoding/hex" 7 8 . "github.com/pingcap/check" 9 ) 10 11 type testKeySuite struct{} 12 13 var _ = Suite(&testKeySuite{}) 14 15 func (r *testKeySuite) TestParseKey(c *C) { 16 // test rawKey 17 testRawKey := []struct { 18 rawKey string 19 ans []byte 20 }{ 21 {"1234", []byte("1234")}, 22 {"abcd", []byte("abcd")}, 23 {"1a2b", []byte("1a2b")}, 24 {"AA", []byte("AA")}, 25 {"\a", []byte("\a")}, 26 {"\\'", []byte("\\'")}, 27 } 28 29 for _, tt := range testRawKey { 30 parsedKey, err := ParseKey("raw", tt.rawKey) 31 c.Assert(err, IsNil) 32 c.Assert(parsedKey, BytesEquals, tt.ans) 33 } 34 35 // test EscapedKey 36 testEscapedKey := []struct { 37 EscapedKey string 38 ans []byte 39 }{ 40 {"\\a\\x1", []byte("\a\x01")}, 41 {"\\b\\f", []byte("\b\f")}, 42 {"\\n\\r", []byte("\n\r")}, 43 {"\\t\\v", []byte("\t\v")}, 44 {"\\'", []byte("'")}, 45 } 46 47 for _, tt := range testEscapedKey { 48 parsedKey, err := ParseKey("escaped", tt.EscapedKey) 49 c.Assert(err, IsNil) 50 c.Assert(parsedKey, BytesEquals, tt.ans) 51 } 52 53 // test hexKey 54 testHexKey := []struct { 55 hexKey string 56 ans []byte 57 }{ 58 {"1234", []byte("1234")}, 59 {"abcd", []byte("abcd")}, 60 {"1a2b", []byte("1a2b")}, 61 {"AA", []byte("AA")}, 62 {"\a", []byte("\a")}, 63 {"\\'", []byte("\\'")}, 64 {"\x01", []byte("\x01")}, 65 {"\xAA", []byte("\xAA")}, 66 } 67 68 for _, tt := range testHexKey { 69 key := hex.EncodeToString([]byte(tt.hexKey)) 70 parsedKey, err := ParseKey("hex", key) 71 c.Assert(err, IsNil) 72 c.Assert(parsedKey, BytesEquals, tt.ans) 73 } 74 75 // test other 76 testNotSupportKey := []struct { 77 any string 78 ans []byte 79 }{ 80 {"1234", []byte("1234")}, 81 {"abcd", []byte("abcd")}, 82 {"1a2b", []byte("1a2b")}, 83 {"AA", []byte("AA")}, 84 {"\a", []byte("\a")}, 85 {"\\'", []byte("\\'")}, 86 {"\x01", []byte("\x01")}, 87 {"\xAA", []byte("\xAA")}, 88 } 89 90 for _, tt := range testNotSupportKey { 91 _, err := ParseKey("notSupport", tt.any) 92 c.Assert(err, ErrorMatches, "unknown format.*") 93 } 94 } 95 96 func (r *testKeySuite) TestCompareEndKey(c *C) { 97 // test endKey 98 testCase := []struct { 99 key1 []byte 100 key2 []byte 101 ans int 102 }{ 103 {[]byte("1"), []byte("2"), -1}, 104 {[]byte("1"), []byte("1"), 0}, 105 {[]byte("2"), []byte("1"), 1}, 106 {[]byte("1"), []byte(""), -1}, 107 {[]byte(""), []byte(""), 0}, 108 {[]byte(""), []byte("1"), 1}, 109 } 110 111 for _, tt := range testCase { 112 res := CompareEndKey(tt.key1, tt.key2) 113 c.Assert(res, Equals, tt.ans) 114 } 115 }