Skip to content

Commit 0e747f1

Browse files
elianddbclaude
andcommitted
Add comprehensive wildcard matching tests to TestGetUserWithWildcardAuthentication
Enhanced the integration test with 17 additional test cases covering: - IP wildcard patterns (127.0.0.%, 192.168.1.%) - Hostname wildcard patterns (%.example.com) - Global wildcard pattern (%) - Customer scenario reproduction - Both positive and negative test cases Tests verify that wildcard user authentication works correctly for various patterns that were previously failing with "No authentication methods available". 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 994120a commit 0e747f1

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

sql/mysql_db/mysql_db.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,9 @@ func (db *MySQLDb) AddLockedSuperUser(ed *Editor, username string, host string,
592592
}
593593
}
594594

595-
// matchesHostPattern checks if a host matches a MySQL host pattern.
596-
// This function handles wildcard patterns like '127.0.0.%' which should match '127.0.0.1', '127.0.0.255', etc.
595+
// matchesHostPattern checks if a host matches a host pattern with wildcards.
597596
func matchesHostPattern(host, pattern string) bool {
598-
// If pattern doesn't contain %, it's not a wildcard pattern
597+
// No wildcard, not a pattern
599598
if !strings.Contains(pattern, "%") {
600599
return false
601600
}

sql/mysql_db/mysql_db_test.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,15 @@ func TestGetUserWithWildcardAuthentication(t *testing.T) {
138138
db := CreateEmptyMySQLDb()
139139
p := &capturingPersistence{}
140140
db.SetPersister(p)
141-
141+
142142
// Add test users with various host patterns
143143
ed := db.Editor()
144144
db.AddSuperUser(ed, "testuser", "127.0.0.1", "password")
145145
db.AddSuperUser(ed, "localhost_user", "localhost", "password")
146+
db.AddSuperUser(ed, "wildcard_user", "127.0.0.%", "password")
147+
db.AddSuperUser(ed, "subnet_user", "192.168.1.%", "password")
148+
db.AddSuperUser(ed, "hostname_user", "%.example.com", "password")
149+
db.AddSuperUser(ed, "any_user", "%", "password")
146150
db.Persist(ctx, ed)
147151
ed.Close()
148152

@@ -156,11 +160,38 @@ func TestGetUserWithWildcardAuthentication(t *testing.T) {
156160
expectedUser string
157161
shouldFind bool
158162
}{
159-
// Test specific IP matching (existing functionality)
163+
// Specific IP tests
160164
{"Specific IP - exact match", "testuser", "127.0.0.1", "testuser", true},
161165
{"Localhost user - normalized", "localhost_user", "127.0.0.1", "localhost_user", true},
162166
{"Localhost user - ::1", "localhost_user", "::1", "localhost_user", true},
163167
{"Non-existent user", "nonexistent", "127.0.0.1", "", false},
168+
169+
// IP wildcard tests
170+
{"Wildcard IP - 127.0.0.% matches 127.0.0.1", "wildcard_user", "127.0.0.1", "wildcard_user", true},
171+
{"Wildcard IP - 127.0.0.% matches 127.0.0.100", "wildcard_user", "127.0.0.100", "wildcard_user", true},
172+
{"Wildcard IP - 127.0.0.% matches 127.0.0.255", "wildcard_user", "127.0.0.255", "wildcard_user", true},
173+
{"Wildcard IP - 127.0.0.% does not match 127.0.1.1", "wildcard_user", "127.0.1.1", "", false},
174+
{"Wildcard IP - 127.0.0.% does not match 192.168.1.1", "wildcard_user", "192.168.1.1", "", false},
175+
176+
// Subnet wildcard tests
177+
{"Subnet wildcard - 192.168.1.% matches 192.168.1.1", "subnet_user", "192.168.1.1", "subnet_user", true},
178+
{"Subnet wildcard - 192.168.1.% matches 192.168.1.100", "subnet_user", "192.168.1.100", "subnet_user", true},
179+
{"Subnet wildcard - 192.168.1.% does not match 192.168.2.1", "subnet_user", "192.168.2.1", "", false},
180+
{"Subnet wildcard - 192.168.1.% does not match 10.0.0.1", "subnet_user", "10.0.0.1", "", false},
181+
182+
// Hostname wildcard tests
183+
{"Hostname wildcard - %.example.com matches host.example.com", "hostname_user", "host.example.com", "hostname_user", true},
184+
{"Hostname wildcard - %.example.com matches www.example.com", "hostname_user", "www.example.com", "hostname_user", true},
185+
{"Hostname wildcard - %.example.com does not match example.com", "hostname_user", "example.com", "", false},
186+
{"Hostname wildcard - %.example.com does not match host.other.com", "hostname_user", "host.other.com", "", false},
187+
188+
// Global wildcard tests
189+
{"Global wildcard - % matches any IP", "any_user", "10.0.0.1", "any_user", true},
190+
{"Global wildcard - % matches any hostname", "any_user", "any.hostname.com", "any_user", true},
191+
{"Global wildcard - % matches localhost", "any_user", "localhost", "any_user", true},
192+
193+
// Issue #9624 scenario
194+
{"Customer scenario - matches connecting IP in range", "wildcard_user", "127.0.0.50", "wildcard_user", true},
164195
}
165196

166197
for _, tt := range tests {

0 commit comments

Comments
 (0)