diff --git a/utils/utils.go b/utils/utils.go index 6be787d..51b4ce2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -15,7 +15,11 @@ import ( "strings" ) -var validChannelName *regexp.Regexp = regexp.MustCompile("^[A-Za-z0-9_\\-=@,.;]+$") +var validChannelName *regexp.Regexp + +func init() { + validChannelName = regexp.MustCompile("^[A-Za-z0-9_\\-=@,.;]+$") +} // HashMAC Calculates the MAC signing with the given key and returns the hexadecimal encoded Result func HashMAC(message, key []byte) string { diff --git a/utils/utils_test.go b/utils/utils_test.go index b210500..31600e5 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -30,19 +30,80 @@ func TestGenerateSession(t *testing.T) { } func TestIsValidChannelName(t *testing.T) { - if IsChannelNameValid("#@#hhh**sasas") { - t.Errorf("Invalid Channel Name") + name := "#@#hhh**sasas" + ok := IsChannelNameValid(name) + + if ok { + t.Errorf("IsChannelNameValid(%s) == %t, wants %t", name, ok, false) } - if !IsChannelNameValid("private-hello") { - t.Errorf("Must be Valid Channel Name") + name = "private-hello" + ok = IsChannelNameValid(name) + + if !ok { + t.Errorf("IsChannelNameValid(%s) == %t, wants %t", name, ok, true) } - if !IsChannelNameValid("presence-hello") { - t.Errorf("Must be Valid Channel Name") + name = "presence-hello" + ok = IsChannelNameValid(name) + + if !ok { + t.Errorf("IsChannelNameValid(%s) == %t, wants %t", name, ok, true) } - if !IsChannelNameValid("public") { - t.Errorf("Must be Valid Channel Name") + name = "public" + ok = IsChannelNameValid(name) + + if !ok { + t.Errorf("IsChannelNameValid(%s) == %t, wants %t", name, ok, true) + } +} + +func TestIsPrivateChannel_valid(t *testing.T) { + name := "private-hello" + ok := IsPrivateChannel(name) + + if !ok { + t.Errorf("IsPrivateChannel(%s) == %t, wants %t", name, ok, true) + } +} + +func TestIsPrivateChannel_invalid(t *testing.T) { + name := "hello" + ok := IsPrivateChannel(name) + + if ok { + t.Errorf("IsPrivateChannel(%s) == %t, wants %t", name, ok, false) + } +} + +func TestIsIsPresenceChannel_valid(t *testing.T) { + name := "presence-hello" + ok := IsPresenceChannel(name) + + if !ok { + t.Errorf("IsPresenceChannel(%s) == %t, wants %t", name, ok, true) + } +} + +func TestIsPresenceChannel_invalid(t *testing.T) { + name := "hello" + ok := IsPresenceChannel(name) + + if ok { + t.Errorf("IsPresenceChannel(%s) == %t, wants %t", name, ok, false) + } +} + +func TestHashMAC(t *testing.T) { + message := []byte("hello world") + key := []byte("my super secret key") + digest := HashMAC(message, key) + + // See: http://www.freeformatter.com/hmac-generator.html + expected := "0811b8affc185a01e1a65b80089ebb1f7f68d287fc3b64581da9ec99136ad1db" + + if digest != expected { + t.Errorf("HashMAC(%s, %q) == %s, wants %s", message, key, digest, expected) } }