-
-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move validateDomains to management.domain pkg to reuse
- Loading branch information
Showing
10 changed files
with
180 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package domain | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const maxDomains = 32 | ||
|
||
// ValidateDomains checks if each domain in the list is valid and returns a punycode-encoded DomainList. | ||
func ValidateDomains(domains []string) (List, error) { | ||
if len(domains) == 0 { | ||
return nil, fmt.Errorf("domains list is empty") | ||
} | ||
if len(domains) > maxDomains { | ||
return nil, fmt.Errorf("domains list exceeds maximum allowed domains: %d", maxDomains) | ||
} | ||
|
||
domainRegex := regexp.MustCompile(`^(?:\*\.)?(?:(?:xn--)?[a-zA-Z0-9_](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?\.)*(?:xn--)?[a-zA-Z0-9](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?$`) | ||
|
||
var domainList List | ||
|
||
for _, d := range domains { | ||
d := strings.ToLower(d) | ||
|
||
// handles length and idna conversion | ||
punycode, err := FromString(d) | ||
if err != nil { | ||
return domainList, fmt.Errorf("failed to convert domain to punycode: %s: %v", d, err) | ||
} | ||
|
||
if !domainRegex.MatchString(string(punycode)) { | ||
return domainList, fmt.Errorf("invalid domain format: %s", d) | ||
} | ||
|
||
domainList = append(domainList, punycode) | ||
} | ||
return domainList, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package domain | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/magiconair/properties/assert" | ||
) | ||
|
||
func TestValidateDomains(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
domains []string | ||
expected List | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Empty list", | ||
domains: nil, | ||
expected: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Valid ASCII domain", | ||
domains: []string{"sub.ex-ample.com"}, | ||
expected: List{"sub.ex-ample.com"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Valid Unicode domain", | ||
domains: []string{"münchen.de"}, | ||
expected: List{"xn--mnchen-3ya.de"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Valid Unicode, all labels", | ||
domains: []string{"中国.中国.中国"}, | ||
expected: List{"xn--fiqs8s.xn--fiqs8s.xn--fiqs8s"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "With underscores", | ||
domains: []string{"_jabber._tcp.gmail.com"}, | ||
expected: List{"_jabber._tcp.gmail.com"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Invalid domain format", | ||
domains: []string{"-example.com"}, | ||
expected: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Invalid domain format 2", | ||
domains: []string{"example.com-"}, | ||
expected: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Multiple domains valid and invalid", | ||
domains: []string{"google.com", "invalid,nbdomain.com", "münchen.de"}, | ||
expected: List{"google.com"}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Valid wildcard domain", | ||
domains: []string{"*.example.com"}, | ||
expected: List{"*.example.com"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Wildcard with dot domain", | ||
domains: []string{".*.example.com"}, | ||
expected: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Wildcard with dot domain", | ||
domains: []string{".*.example.com"}, | ||
expected: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Invalid wildcard domain", | ||
domains: []string{"a.*.example.com"}, | ||
expected: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ValidateDomains(tt.domains) | ||
assert.Equal(t, tt.wantErr, err != nil) | ||
assert.Equal(t, got, tt.expected) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.