From 3ce68d6a306990fffb356ce6d19af3537318065b Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Tue, 23 Jun 2020 21:18:58 +0800 Subject: [PATCH] Move many PR's change here since the original authors are responseless --- config.go | 4 +- session/sess_file.go | 5 +- toolbox/task.go | 7 ++- validation/validation_test.go | 98 ++++++++++++++++++++++------------- validation/validators.go | 2 +- 5 files changed, 73 insertions(+), 43 deletions(-) diff --git a/config.go b/config.go index 72b8a333..6fcc4c59 100644 --- a/config.go +++ b/config.go @@ -420,9 +420,9 @@ func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, err func (b *beegoAppConfig) Set(key, val string) error { if err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val); err != nil { - return err + return b.innerConfig.Set(key, val) } - return b.innerConfig.Set(key, val) + return nil } func (b *beegoAppConfig) String(key string) string { diff --git a/session/sess_file.go b/session/sess_file.go index 9f8ccaed..c6dbf209 100644 --- a/session/sess_file.go +++ b/session/sess_file.go @@ -129,8 +129,9 @@ func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error { // if file is not exist, create it. // the file path is generated from sid string. func (fp *FileProvider) SessionRead(sid string) (Store, error) { - if strings.ContainsAny(sid, "./") { - return nil, nil + invalidChars := "./" + if strings.ContainsAny(sid, invalidChars) { + return nil, errors.New("the sid shouldn't have following characters: " + invalidChars) } if len(sid) < 2 { return nil, errors.New("length of the sid is less than 2") diff --git a/toolbox/task.go b/toolbox/task.go index d1343023..d2a94ba9 100644 --- a/toolbox/task.go +++ b/toolbox/task.go @@ -33,7 +33,7 @@ type bounds struct { // The bounds for each field. var ( AdminTaskList map[string]Tasker - taskLock sync.Mutex + taskLock sync.RWMutex stop chan bool changed chan bool isstart bool @@ -408,7 +408,10 @@ func run() { } for { + // we only use RLock here because NewMapSorter copy the reference, do not change any thing + taskLock.RLock() sortList := NewMapSorter(AdminTaskList) + taskLock.RUnlock() sortList.Sort() var effective time.Time if len(AdminTaskList) == 0 || sortList.Vals[0].GetNext().IsZero() { @@ -432,9 +435,11 @@ func run() { continue case <-changed: now = time.Now().Local() + taskLock.Lock() for _, t := range AdminTaskList { t.SetNext(now) } + taskLock.Unlock() continue case <-stop: return diff --git a/validation/validation_test.go b/validation/validation_test.go index 845bed67..b4b5b1b6 100644 --- a/validation/validation_test.go +++ b/validation/validation_test.go @@ -253,44 +253,68 @@ func TestBase64(t *testing.T) { func TestMobile(t *testing.T) { valid := Validation{} - if valid.Mobile("19800008888", "mobile").Ok { - t.Error("\"19800008888\" is a valid mobile phone number should be false") + validMobiles := []string{ + "19800008888", + "18800008888", + "18000008888", + "8618300008888", + "+8614700008888", + "17300008888", + "+8617100008888", + "8617500008888", + "8617400008888", + "16200008888", + "16500008888", + "16600008888", + "16700008888", + "13300008888", + "14900008888", + "15300008888", + "17300008888", + "17700008888", + "18000008888", + "18900008888", + "19100008888", + "19900008888", + "19300008888", + "13000008888", + "13100008888", + "13200008888", + "14500008888", + "15500008888", + "15600008888", + "16600008888", + "17100008888", + "17500008888", + "17600008888", + "18500008888", + "18600008888", + "13400008888", + "13500008888", + "13600008888", + "13700008888", + "13800008888", + "13900008888", + "14700008888", + "15000008888", + "15100008888", + "15200008888", + "15800008888", + "15900008888", + "17200008888", + "17800008888", + "18200008888", + "18300008888", + "18400008888", + "18700008888", + "18800008888", + "19800008888", } - if !valid.Mobile("18800008888", "mobile").Ok { - t.Error("\"18800008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("18000008888", "mobile").Ok { - t.Error("\"18000008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("8618300008888", "mobile").Ok { - t.Error("\"8618300008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("+8614700008888", "mobile").Ok { - t.Error("\"+8614700008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("17300008888", "mobile").Ok { - t.Error("\"17300008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("+8617100008888", "mobile").Ok { - t.Error("\"+8617100008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("8617500008888", "mobile").Ok { - t.Error("\"8617500008888\" is a valid mobile phone number should be true") - } - if valid.Mobile("8617400008888", "mobile").Ok { - t.Error("\"8617400008888\" is a valid mobile phone number should be false") - } - if !valid.Mobile("16200008888", "mobile").Ok { - t.Error("\"16200008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("16500008888", "mobile").Ok { - t.Error("\"16500008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("16600008888", "mobile").Ok { - t.Error("\"16600008888\" is a valid mobile phone number should be true") - } - if !valid.Mobile("16700008888", "mobile").Ok { - t.Error("\"16700008888\" is a valid mobile phone number should be true") + + for _, m := range validMobiles { + if !valid.Mobile(m, "mobile").Ok { + t.Error(m + " is a valid mobile phone number should be true") + } } } diff --git a/validation/validators.go b/validation/validators.go index ac00a72c..0caa7aef 100644 --- a/validation/validators.go +++ b/validation/validators.go @@ -632,7 +632,7 @@ func (b Base64) GetLimitValue() interface{} { } // just for chinese mobile phone number -var mobilePattern = regexp.MustCompile(`^((\+86)|(86))?(1(([35][0-9])|[8][0-9]|[7][01356789]|[4][579]|[6][2567]))\d{8}$`) +var mobilePattern = regexp.MustCompile(`^((\+86)|(86))?1([356789][0-9]|4[579]|6[67]|7[0135678]|9[189])[0-9]{8}$`) // Mobile check struct type Mobile struct {