Skip to content

Commit

Permalink
Move ValidateName to utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
stolowski committed Jun 28, 2018
1 parent f6c763b commit 29498b6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
5 changes: 3 additions & 2 deletions interfaces/builtin/all.go
Expand Up @@ -24,6 +24,7 @@ import (
"sort"

"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/utils"
"github.com/snapcore/snapd/snap"
)

Expand Down Expand Up @@ -68,7 +69,7 @@ func SanitizePlugsSlots(snapInfo *snap.Info) {
continue
}
// Reject plug with invalid name
if err := interfaces.ValidateName(plugName); err != nil {
if err := utils.ValidateName(plugName); err != nil {
snapInfo.BadInterfaces[plugName] = err.Error()
badPlugs = append(badPlugs, plugName)
continue
Expand All @@ -88,7 +89,7 @@ func SanitizePlugsSlots(snapInfo *snap.Info) {
continue
}
// Reject slot with invalid name
if err := interfaces.ValidateName(slotName); err != nil {
if err := utils.ValidateName(slotName); err != nil {
snapInfo.BadInterfaces[slotName] = err.Error()
badSlots = append(badSlots, slotName)
continue
Expand Down
12 changes: 0 additions & 12 deletions interfaces/core.go
Expand Up @@ -216,18 +216,6 @@ const (
SecuritySystemd SecuritySystem = "systemd"
)

// Regular expression describing correct identifiers.
var validName = regexp.MustCompile("^[a-z](?:-?[a-z0-9])*$")

// ValidateName checks if a string can be used as a plug or slot name.
func ValidateName(name string) error {
valid := validName.MatchString(name)
if !valid {
return fmt.Errorf("invalid interface name: %q", name)
}
return nil
}

// ValidateDBusBusName checks if a string conforms to
// https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
func ValidateDBusBusName(busName string) error {
Expand Down
5 changes: 3 additions & 2 deletions interfaces/core_test.go
Expand Up @@ -27,6 +27,7 @@ import (

. "github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/ifacetest"
"github.com/snapcore/snapd/interfaces/utils"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (s *CoreSuite) TestValidateName(c *C) {
"a0", "a-0", "a-0a",
}
for _, name := range validNames {
err := ValidateName(name)
err := utils.ValidateName(name)
c.Assert(err, IsNil)
}
invalidNames := []string{
Expand All @@ -78,7 +79,7 @@ func (s *CoreSuite) TestValidateName(c *C) {
"日本語", "한글", "ру́сский язы́к",
}
for _, name := range invalidNames {
err := ValidateName(name)
err := utils.ValidateName(name)
c.Assert(err, ErrorMatches, `invalid interface name: ".*"`)
}
}
Expand Down
7 changes: 4 additions & 3 deletions interfaces/repo.go
Expand Up @@ -25,6 +25,7 @@ import (
"strings"
"sync"

"github.com/snapcore/snapd/interfaces/utils"
"github.com/snapcore/snapd/snap"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func (r *Repository) AddInterface(i Interface) error {
defer r.m.Unlock()

interfaceName := i.Name()
if err := ValidateName(interfaceName); err != nil {
if err := utils.ValidateName(interfaceName); err != nil {
return err
}
if _, ok := r.ifaces[interfaceName]; ok {
Expand Down Expand Up @@ -269,7 +270,7 @@ func (r *Repository) AddPlug(plug *snap.PlugInfo) error {
return err
}
// Reject plug with invalid names
if err := ValidateName(plug.Name); err != nil {
if err := utils.ValidateName(plug.Name); err != nil {
return err
}
i := r.ifaces[plug.Interface]
Expand Down Expand Up @@ -364,7 +365,7 @@ func (r *Repository) AddSlot(slot *snap.SlotInfo) error {
return err
}
// Reject plug with invalid names
if err := ValidateName(slot.Name); err != nil {
if err := utils.ValidateName(slot.Name); err != nil {
return err
}
// TODO: ensure that apps are correct
Expand Down
17 changes: 17 additions & 0 deletions interfaces/utils/utils.go
Expand Up @@ -19,6 +19,11 @@

package utils

import (
"fmt"
"regexp"
)

// NormalizeInterfaceAttributes normalises types of an attribute values.
// The following transformations are applied: int -> int64, float32 -> float64.
// The normalisation proceeds recursively through maps and slices.
Expand All @@ -40,3 +45,15 @@ func NormalizeInterfaceAttributes(value interface{}) interface{} {
}
return value
}

// Regular expression describing correct identifiers.
var validName = regexp.MustCompile("^[a-z](?:-?[a-z0-9])*$")

// ValidateName checks if a string can be used as a plug or slot name.
func ValidateName(name string) error {
valid := validName.MatchString(name)
if !valid {
return fmt.Errorf("invalid interface name: %q", name)
}
return nil
}

0 comments on commit 29498b6

Please sign in to comment.