From 04caa0489906073f04e3659baf125ef9bfb9fa2b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 6 Apr 2017 10:52:57 +0800 Subject: [PATCH] fix: check certificate file exist. (#209) * fix: check certificate file exist. * fix: certificate key path error --- gorush/notification.go | 6 ++++++ gorush/notification_test.go | 10 ++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gorush/notification.go b/gorush/notification.go index db8f412..37bfe35 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/url" + "os" "path/filepath" "time" @@ -142,6 +143,11 @@ func CheckPushConf() error { if PushConf.Ios.KeyPath == "" { return errors.New("Missing iOS certificate path") } + + // check certificate file exist + if _, err := os.Stat(PushConf.Ios.KeyPath); os.IsNotExist(err) { + return errors.New("certificate file does not exist") + } } if PushConf.Android.Enabled { diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 4a9b1da..1490cb5 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -28,11 +28,17 @@ func TestMissingIOSCertificate(t *testing.T) { PushConf.Ios.Enabled = true PushConf.Ios.KeyPath = "" - err := CheckPushConf() assert.Error(t, err) assert.Equal(t, "Missing iOS certificate path", err.Error()) + + PushConf.Ios.KeyPath = "test.pem" + err = CheckPushConf() + + assert.Error(t, err) + assert.Equal(t, "certificate file does not exist", err.Error()) + } func TestMissingAndroidAPIKey(t *testing.T) { @@ -54,7 +60,7 @@ func TestCorrectConf(t *testing.T) { PushConf.Android.APIKey = "xxxxx" PushConf.Ios.Enabled = true - PushConf.Ios.KeyPath = "xxxxx" + PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem" err := CheckPushConf()