github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/security/sasl_test.go (about) 1 // Copyright 2022 PingCAP, 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 security 15 16 import ( 17 "testing" 18 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestSASLMechanismFromString(t *testing.T) { 23 t.Parallel() 24 25 tests := []struct { 26 name string 27 s string 28 expectedMechanism string 29 expectErr string 30 }{ 31 { 32 name: "random mechanism", 33 s: "random", 34 expectedMechanism: "", 35 expectErr: "unknown random SASL mechanism", 36 }, 37 { 38 name: "lower case plain mechanism", 39 s: "plain", 40 expectedMechanism: "PLAIN", 41 }, 42 { 43 name: "upper case plain mechanism", 44 s: "PLAIN", 45 expectedMechanism: "PLAIN", 46 }, 47 { 48 name: "lower case scram-sha-256 mechanism", 49 s: "scram-sha-256", 50 expectedMechanism: "SCRAM-SHA-256", 51 }, 52 { 53 name: "upper case SCRAM-SHA-256 mechanism", 54 s: "SCRAM-SHA-256", 55 expectedMechanism: "SCRAM-SHA-256", 56 }, 57 { 58 name: "lower case scram-sha-512 mechanism", 59 s: "scram-sha-512", 60 expectedMechanism: "SCRAM-SHA-512", 61 }, 62 { 63 name: "upper case SCRAM-SHA-512 mechanism", 64 s: "SCRAM-SHA-512", 65 expectedMechanism: "SCRAM-SHA-512", 66 }, 67 { 68 name: "lower case gssapi mechanism", 69 s: "gssapi", 70 expectedMechanism: "GSSAPI", 71 }, 72 { 73 name: "upper case GSSAPI mechanism", 74 s: "GSSAPI", 75 expectedMechanism: "GSSAPI", 76 }, 77 } 78 for _, test := range tests { 79 test := test 80 t.Run(test.name, func(t *testing.T) { 81 t.Parallel() 82 mechanism, err := SASLMechanismFromString(test.s) 83 if test.expectErr != "" { 84 require.Error(t, err) 85 require.Regexp(t, test.expectErr, err.Error()) 86 } else { 87 require.NoError(t, err) 88 require.Equal(t, test.expectedMechanism, string(mechanism)) 89 } 90 }) 91 } 92 } 93 94 func TestAuthTypeFromString(t *testing.T) { 95 t.Parallel() 96 97 tests := []struct { 98 name string 99 s string 100 expectedType int 101 expectErr string 102 }{ 103 { 104 name: "unknown", 105 s: "a", 106 expectedType: 0, 107 expectErr: "unknown a auth type", 108 }, 109 { 110 name: "lower case user", 111 s: "user", 112 expectedType: 1, 113 }, 114 { 115 name: "upper case user", 116 s: "USER", 117 expectedType: 1, 118 }, 119 { 120 name: "lower case keytab", 121 s: "keytab", 122 expectedType: 2, 123 }, 124 { 125 name: "upper case keytab", 126 s: "KEYTAB", 127 expectedType: 2, 128 }, 129 } 130 131 for _, test := range tests { 132 test := test 133 t.Run(test.name, func(t *testing.T) { 134 t.Parallel() 135 136 authType, err := AuthTypeFromString(test.s) 137 if test.expectErr != "" { 138 require.Error(t, err) 139 require.Regexp(t, test.expectErr, err.Error()) 140 } else { 141 require.NoError(t, err) 142 require.Equal(t, test.expectedType, int(authType)) 143 } 144 }) 145 } 146 }