diff --git a/push/device_token.go b/push/device_token.go new file mode 100644 index 0000000..62c663e --- /dev/null +++ b/push/device_token.go @@ -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 +} diff --git a/push/device_token_test.go b/push/device_token_test.go new file mode 100644 index 0000000..0fac765 --- /dev/null +++ b/push/device_token_test.go @@ -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) + } + } +}