Add IsValid method to *Manifest struct (#13609)

Этот коммит содержится в:
Shobhit Gupta
2020-01-17 12:08:55 -08:00
коммит произвёл Ben Schumacher
родитель b71a6b9f8d
Коммит 7d99d8fba7
5 изменённых файлов: 275 добавлений и 12 удалений

Просмотреть файл

@@ -1,39 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"regexp"
"unicode/utf8"
)
const (
MinIdLength = 3
MaxIdLength = 190
ValidIdRegex = `^[a-zA-Z0-9-_\.]+$`
)
// ValidId constrains the set of valid plugin identifiers:
// ^[a-zA-Z0-9-_\.]+
var validId *regexp.Regexp
func init() {
validId = regexp.MustCompile(ValidIdRegex)
}
// IsValidId verifies that the plugin id has a minimum length of 3, maximum length of 190, and
// contains only alphanumeric characters, dashes, underscores and periods.
//
// These constraints are necessary since the plugin id is used as part of a filesystem path.
func IsValidId(id string) bool {
if utf8.RuneCountInString(id) < MinIdLength {
return false
}
if utf8.RuneCountInString(id) > MaxIdLength {
return false
}
return validId.MatchString(id)
}

Просмотреть файл

@@ -1,35 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/v5/plugin"
)
func TestIsValid(t *testing.T) {
t.Parallel()
testCases := map[string]bool{
"": false,
"a": false,
"ab": false,
"abc": true,
"abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij": true,
"abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij1": false,
"../path": false,
"/etc/passwd": false,
"com.mattermost.plugin_with_features-0.9": true,
"PLUGINS-THAT-YELL-ARE-OK-2": true,
}
for id, valid := range testCases {
t.Run(id, func(t *testing.T) {
assert.Equal(t, valid, plugin.IsValidId(id))
})
}
}