Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
is device token valid
Browse files Browse the repository at this point in the history
  • Loading branch information
nathany committed Jan 9, 2016
1 parent dbb02fc commit 947f5b8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions push/device_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package push

import "encoding/hex"

// IsDeviceTokenValid checks if s is a hexadecimal token of the correct length.
func IsDeviceTokenValid(s string) bool {
// TODO: In 2016, they may be growing up to 100 bytes (200 hexadecimal digits).
if len(s) != 64 {
return false
}
_, err := hex.DecodeString(s)
return err == nil
}
35 changes: 35 additions & 0 deletions push/device_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package push_test

import (
"testing"

"github.com/RobotsAndPencils/buford/push"
)

func TestInvalidDeviceTokens(t *testing.T) {
tokens := []string{
"invalid-token",
"f00f",
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl",
"50c4afb5 9197d2ba d1794be4 e63f2532 ee18c660 0ee655fa 38b0b380 94fd8847",
"<50c4afb5 9197d2ba d1794be4 e63f2532 ee18c660 0ee655fa 38b0b380 94fd8847>",
}

for _, token := range tokens {
if push.IsDeviceTokenValid(token) {
t.Errorf("Expected device token %q to be invalid.", token)
}
}
}

func TestValidDeviceToken(t *testing.T) {
tokens := []string{
"c2732227a1d8021cfaf781d71fb2f908c61f5861079a00954a5453f1d0281433",
}

for _, token := range tokens {
if !push.IsDeviceTokenValid(token) {
t.Errorf("Expected device token %q to be valid.", token)
}
}
}

0 comments on commit 947f5b8

Please sign in to comment.