From b92103068892cda2e94255bceaecf6f71857c4af Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 24 Apr 2016 14:30:17 +0800 Subject: [PATCH 1/3] check android message. Signed-off-by: Bo-Yi Wu --- gorush.go | 6 ++++ gorush/notification.go | 38 ++++++++++++++++++++ gorush/notification_test.go | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/gorush.go b/gorush.go index 93dfe6d..fe135b6 100644 --- a/gorush.go +++ b/gorush.go @@ -82,6 +82,12 @@ func main() { Message: *message, } + err := gorush.CheckGCMMessage(req) + + if err != nil { + gorush.LogError.Fatal(err) + } + gorush.InitAppStatus() gorush.PushToAndroid(req) diff --git a/gorush/notification.go b/gorush/notification.go index 182bb65..271d740 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -77,6 +77,37 @@ type PushNotification struct { Alert Alert `json:"alert,omitempty"` } +// CheckGCMMessage for check GCM Message +func CheckGCMMessage(req PushNotification) error { + var msg string + if req.Message == "" { + msg = "the message must not be empty" + LogAccess.Debug(msg) + return errors.New(msg) + } + + if len(req.Tokens) == 0 { + msg = "the message must specify at least one registration ID" + LogAccess.Debug(msg) + return errors.New(msg) + } + + if len(req.Tokens) > 1000 { + msg = "the message may specify at most 1000 registration IDs" + LogAccess.Debug(msg) + return errors.New(msg) + } + + if req.TimeToLive < 0 || 2419200 < req.TimeToLive { + msg = "the message's TimeToLive field must be an integer " + + "between 0 and 2419200 (4 weeks)" + LogAccess.Debug(msg) + return errors.New(msg) + } + + return nil +} + // CheckPushConf provide check your yml config. func CheckPushConf() error { if !PushConf.Ios.Enabled && !PushConf.Android.Enabled { @@ -348,6 +379,13 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage { func PushToAndroid(req PushNotification) bool { var APIKey string + // check message + err := CheckGCMMessage(req) + + if err != nil { + return false + } + notification := GetAndroidNotification(req) if APIKey = PushConf.Android.APIKey; req.APIKey != "" { diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 746587c..5555937 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -454,3 +454,72 @@ func TestAPNSClientProdHost(t *testing.T) { assert.Equal(t, apns2.HostProduction, ApnsClient.Host) } + +func TestGCMMessage(t *testing.T) { + var req PushNotification + var err error + + // the message must not be empty + req = PushNotification{ + Message: "", + } + + err = CheckGCMMessage(req) + assert.Error(t, err) + + // the message must specify at least one registration ID + req = PushNotification{ + Message: "Test", + Tokens: []string{}, + } + + err = CheckGCMMessage(req) + assert.Error(t, err) + + // the message may specify at most 1000 registration IDs + req = PushNotification{ + Message: "Test", + Tokens: make([]string, 1001), + } + + err = CheckGCMMessage(req) + assert.Error(t, err) + + // the message's TimeToLive field must be an integer + // between 0 and 2419200 (4 weeks) + req = PushNotification{ + Message: "Test", + Tokens: []string{"XXXXXXXXX"}, + TimeToLive: 2419201, + } + + err = CheckGCMMessage(req) + assert.Error(t, err) + + // Pass + req = PushNotification{ + Message: "Test", + Tokens: []string{"XXXXXXXXX"}, + TimeToLive: 86400, + } + + err = CheckGCMMessage(req) + assert.NoError(t, err) +} + +func TestCheckAndroidMessage(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Android.Enabled = true + PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") + + req := PushNotification{ + Tokens: []string{"aaaaaa", "bbbbb"}, + Platform: 2, + Message: "Welcome", + TimeToLive: 2419201, + } + + success := PushToAndroid(req) + assert.False(t, success) +} From 28bdc4a45660e9ff120e30bf26a635ddb6dd8b9d Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 24 Apr 2016 15:54:41 +0800 Subject: [PATCH 2/3] fix constant variable. Signed-off-by: Bo-Yi Wu --- gorush.go | 13 ++++++----- gorush/notification.go | 15 ++++++++---- gorush/notification_test.go | 46 +++++++++++++++++++++++-------------- gorush/server_test.go | 6 ++--- 4 files changed, 50 insertions(+), 30 deletions(-) diff --git a/gorush.go b/gorush.go index fe135b6..7e48168 100644 --- a/gorush.go +++ b/gorush.go @@ -72,9 +72,6 @@ func main() { // send android notification if *android { - - checkInput(*token, *message) - gorush.PushConf.Android.Enabled = true req := gorush.PushNotification{ Tokens: []string{*token}, @@ -82,7 +79,7 @@ func main() { Message: *message, } - err := gorush.CheckGCMMessage(req) + err := gorush.CheckMessage(req) if err != nil { gorush.LogError.Fatal(err) @@ -96,8 +93,6 @@ func main() { // send android notification if *ios { - checkInput(*token, *message) - if *production { gorush.PushConf.Ios.Production = true } @@ -109,6 +104,12 @@ func main() { Message: *message, } + err := gorush.CheckMessage(req) + + if err != nil { + gorush.LogError.Fatal(err) + } + gorush.InitAppStatus() gorush.InitAPNSClient() gorush.PushToIOS(req) diff --git a/gorush/notification.go b/gorush/notification.go index 271d740..2dc8949 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -78,7 +78,7 @@ type PushNotification struct { } // CheckGCMMessage for check GCM Message -func CheckGCMMessage(req PushNotification) error { +func CheckMessage(req PushNotification) error { var msg string if req.Message == "" { msg = "the message must not be empty" @@ -92,13 +92,19 @@ func CheckGCMMessage(req PushNotification) error { return errors.New(msg) } - if len(req.Tokens) > 1000 { + if len(req.Tokens) == PlatFormIos && len(req.Tokens[0]) == 0 { + msg = "the token must not be empty" + LogAccess.Debug(msg) + return errors.New(msg) + } + + if req.Platform == PlatFormAndroid && len(req.Tokens) > 1000 { msg = "the message may specify at most 1000 registration IDs" LogAccess.Debug(msg) return errors.New(msg) } - if req.TimeToLive < 0 || 2419200 < req.TimeToLive { + if req.Platform == PlatFormAndroid && (req.TimeToLive < 0 || 2419200 < req.TimeToLive) { msg = "the message's TimeToLive field must be an integer " + "between 0 and 2419200 (4 weeks)" LogAccess.Debug(msg) @@ -380,9 +386,10 @@ func PushToAndroid(req PushNotification) bool { var APIKey string // check message - err := CheckGCMMessage(req) + err := CheckMessage(req) if err != nil { + LogError.Error("request error: " + err.Error()) return false } diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 5555937..d4af358 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -241,7 +241,7 @@ func TestPushToAndroidWrongAPIKey(t *testing.T) { req := PushNotification{ Tokens: []string{"aaaaaa", "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", } @@ -257,7 +257,7 @@ func TestPushToAndroidWrongToken(t *testing.T) { req := PushNotification{ Tokens: []string{"aaaaaa", "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", } @@ -277,7 +277,7 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) { req := PushNotification{ Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", } @@ -295,7 +295,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) { req := PushNotification{ Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", } @@ -313,7 +313,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) { req := PushNotification{ Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", // overwrite android api key APIKey: "1234", @@ -342,13 +342,13 @@ func TestSenMultipleNotifications(t *testing.T) { //ios { Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, - Platform: 1, + Platform: PlatFormIos, Message: "Welcome", }, // android { Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", }, }, @@ -375,13 +375,13 @@ func TestDisabledAndroidNotifications(t *testing.T) { //ios { Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, - Platform: 1, + Platform: PlatFormIos, Message: "Welcome", }, // android { Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", }, }, @@ -408,13 +408,13 @@ func TestDisabledIosNotifications(t *testing.T) { //ios { Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, - Platform: 1, + Platform: PlatFormIos, Message: "Welcome", }, // android { Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", }, }, @@ -464,7 +464,7 @@ func TestGCMMessage(t *testing.T) { Message: "", } - err = CheckGCMMessage(req) + err = CheckMessage(req) assert.Error(t, err) // the message must specify at least one registration ID @@ -473,37 +473,49 @@ func TestGCMMessage(t *testing.T) { Tokens: []string{}, } - err = CheckGCMMessage(req) + err = CheckMessage(req) + assert.Error(t, err) + + // the token must not be empty + req = PushNotification{ + Message: "Test", + Tokens: []string{""}, + } + + err = CheckMessage(req) assert.Error(t, err) // the message may specify at most 1000 registration IDs req = PushNotification{ Message: "Test", + Platform: PlatFormAndroid, Tokens: make([]string, 1001), } - err = CheckGCMMessage(req) + err = CheckMessage(req) assert.Error(t, err) // the message's TimeToLive field must be an integer // between 0 and 2419200 (4 weeks) req = PushNotification{ Message: "Test", + Platform: PlatFormAndroid, Tokens: []string{"XXXXXXXXX"}, TimeToLive: 2419201, } - err = CheckGCMMessage(req) + err = CheckMessage(req) assert.Error(t, err) // Pass req = PushNotification{ Message: "Test", + Platform: PlatFormAndroid, Tokens: []string{"XXXXXXXXX"}, TimeToLive: 86400, } - err = CheckGCMMessage(req) + err = CheckMessage(req) assert.NoError(t, err) } @@ -515,7 +527,7 @@ func TestCheckAndroidMessage(t *testing.T) { req := PushNotification{ Tokens: []string{"aaaaaa", "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", TimeToLive: 2419201, } diff --git a/gorush/server_test.go b/gorush/server_test.go index c0946b4..68b578b 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -156,12 +156,12 @@ func TestOutOfRangeMaxNotifications(t *testing.T) { "notifications": []gofight.D{ { "tokens": []string{"aaaaa", "bbbbb"}, - "platform": 2, + "platform": PlatFormAndroid, "message": "Welcome", }, { "tokens": []string{"aaaaa", "bbbbb"}, - "platform": 2, + "platform": PlatFormAndroid, "message": "Welcome", }, }, @@ -187,7 +187,7 @@ func TestSuccessPushHandler(t *testing.T) { "notifications": []gofight.D{ { "tokens": []string{androidToken, "bbbbb"}, - "platform": 2, + "platform": PlatFormAndroid, "message": "Welcome", }, }, From 6c1c509a5c81dd420cbb2d3193156b84b2ff9159 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 24 Apr 2016 15:57:38 +0800 Subject: [PATCH 3/3] fix golint. Signed-off-by: Bo-Yi Wu --- gorush/notification.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gorush/notification.go b/gorush/notification.go index 2dc8949..5a7ffec 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -77,7 +77,7 @@ type PushNotification struct { Alert Alert `json:"alert,omitempty"` } -// CheckGCMMessage for check GCM Message +// CheckMessage for check request message func CheckMessage(req PushNotification) error { var msg string if req.Message == "" { @@ -104,6 +104,7 @@ func CheckMessage(req PushNotification) error { return errors.New(msg) } + // ref: https://developers.google.com/cloud-messaging/http-server-ref if req.Platform == PlatFormAndroid && (req.TimeToLive < 0 || 2419200 < req.TimeToLive) { msg = "the message's TimeToLive field must be an integer " + "between 0 and 2419200 (4 weeks)"