vitess.io/vitess@v0.16.2/go/mysql/collations/integration/wildcard_test.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package integration
    18  
    19  import (
    20  	"encoding/hex"
    21  	"testing"
    22  
    23  	"vitess.io/vitess/go/mysql/collations"
    24  	"vitess.io/vitess/go/mysql/collations/internal/charset"
    25  	"vitess.io/vitess/go/mysql/collations/remote"
    26  )
    27  
    28  func TestRemoteWildcardMatches(t *testing.T) {
    29  	conn := mysqlconn(t)
    30  	defer conn.Close()
    31  
    32  	var cases = []struct {
    33  		in, pat string
    34  	}{
    35  		{"abc", "abc"},
    36  		{"Abc", "aBc"},
    37  		{"abc", "_bc"},
    38  		{"abc", "a_c"},
    39  		{"abc", "ab_"},
    40  		{"abc", "%c"},
    41  		{"abc", "a%c"},
    42  		{"abc", "a%"},
    43  		{"abcdef", "a%d_f"},
    44  		{"abcdefg", "a%d%g"},
    45  		{"a\\", "a\\"},
    46  		{"aa\\", "a%\\"},
    47  		{"Y", "\u00dd"},
    48  		{"abcd", "abcde"},
    49  		{"abcde", "abcd"},
    50  		{"abcde", "a%f"},
    51  		{"abcdef", "a%%f"},
    52  		{"abcd", "a__d"},
    53  		{"abcd", "a\\bcd"},
    54  		{"a\\bcd", "abcd"},
    55  		{"abdbcd", "a%cd"},
    56  		{"abecd", "a%bd"},
    57  		{"ǎḄÇ", "Ǎḅç"},
    58  		{"ÁḆĈ", "Ǎḅç"},
    59  		{"ǍBc", "_bc"},
    60  		{"Aḅc", "a_c"},
    61  		{"Abç", "ab_"},
    62  		{"Ǎḅç", "%ç"},
    63  		{"Ǎḅç", "ǎ%Ç"},
    64  		{"aḅç", "a%"},
    65  		{"Ǎḅçdef", "ǎ%d_f"},
    66  		{"Ǎḅçdefg", "ǎ%d%g"},
    67  		{"ǎ\\", "Ǎ\\"},
    68  		{"ǎa\\", "Ǎ%\\"},
    69  		{"Y", "\u00dd"},
    70  		{"abcd", "Ǎḅçde"},
    71  		{"abcde", "Ǎḅçd"},
    72  		{"Ǎḅçde", "a%f"},
    73  		{"Ǎḅçdef", "ǎ%%f"},
    74  		{"Ǎḅçd", "ǎ__d"},
    75  		{"Ǎḅçd", "ǎ\\ḄÇd"},
    76  		{"a\\bcd", "Ǎḅçd"},
    77  		{"Ǎḅdbçd", "ǎ%Çd"},
    78  		{"Ǎḅeçd", "a%bd"},
    79  	}
    80  
    81  	for _, local := range collations.Local().AllCollations() {
    82  		t.Run(local.Name(), func(t *testing.T) {
    83  			var remote = remote.NewCollation(conn, local.Name())
    84  			var err error
    85  			var chEscape = '\\'
    86  
    87  			if !charset.IsBackslashSafe(local.Charset()) {
    88  				chEscape = '/'
    89  			}
    90  
    91  			for _, tc := range cases {
    92  				input, pat := []byte(tc.in), []byte(tc.pat)
    93  
    94  				if chEscape != '\\' {
    95  					for i := range pat {
    96  						if pat[i] == '\\' {
    97  							pat[i] = byte(chEscape)
    98  						}
    99  					}
   100  				}
   101  
   102  				input, err = charset.ConvertFromUTF8(nil, local.Charset(), input)
   103  				if err != nil {
   104  					continue
   105  				}
   106  				pat, err = charset.ConvertFromUTF8(nil, local.Charset(), pat)
   107  				if err != nil {
   108  					continue
   109  				}
   110  
   111  				localResult := local.Wildcard(pat, 0, 0, chEscape).Match(input)
   112  				remoteResult := remote.Wildcard(pat, 0, 0, chEscape).Match(input)
   113  				if err := remote.LastError(); err != nil {
   114  					t.Fatalf("remote collation failed: %v", err)
   115  				}
   116  				if localResult != remoteResult {
   117  					t.Errorf("expected %q LIKE %q = %v (got %v)", tc.in, tc.pat, remoteResult, localResult)
   118  
   119  					printDebugData(t, []string{
   120  						"wildcmp",
   121  						"--collation", local.Name(),
   122  						"--input", hex.EncodeToString(input),
   123  						"--pattern", hex.EncodeToString(pat),
   124  					})
   125  				}
   126  			}
   127  		})
   128  	}
   129  }