package signutil import ( "strconv" "testing" "time" ) func TestIBINGLIDigest_Validate(t *testing.T) { type fields struct { AppId string NonceStr string Timestamp string Sign string TimeLimit time.Duration genSignFunc GenSignFunc } tests := []struct { name string fields fields wantErr bool }{ { name: "lack of field", fields: fields{AppId: "123", NonceStr: "", Timestamp: "134134", Sign: ""}, wantErr: true, }, { name: "has field", fields: fields{AppId: "123", NonceStr: "123", Timestamp: "134134", Sign: "123"}, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := &IBINGLIDigest{ AppId: tt.fields.AppId, NonceStr: tt.fields.NonceStr, Timestamp: tt.fields.Timestamp, Sign: tt.fields.Sign, TimeLimit: tt.fields.TimeLimit, genSignFunc: tt.fields.genSignFunc, } if err := v.Validate(); (err != nil) != tt.wantErr { t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestIBINGLIDigest_ValidateTimestamp(t *testing.T) { type fields struct { AppId string NonceStr string Timestamp string Sign string TimeLimit time.Duration genSignFunc GenSignFunc } tests := []struct { name string fields fields wantErr bool }{ { name: "correct time", fields: fields{Timestamp: strconv.Itoa(int(time.Now().UnixMilli())), TimeLimit: time.Hour}, wantErr: false, }, { name: "error time", fields: fields{Timestamp: strconv.Itoa(int(time.Now().Add(-2 * time.Hour).UnixMilli())), TimeLimit: time.Hour}, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := &IBINGLIDigest{ AppId: tt.fields.AppId, NonceStr: tt.fields.NonceStr, Timestamp: tt.fields.Timestamp, Sign: tt.fields.Sign, TimeLimit: tt.fields.TimeLimit, genSignFunc: tt.fields.genSignFunc, } if err := v.ValidateTimestamp(); (err != nil) != tt.wantErr { t.Errorf("ValidateTimestamp() error = %v, wantErr %v", err, tt.wantErr) } }) } } func TestIBINGLIDigest_GenSign(t *testing.T) { type fields struct { AppId string NonceStr string Timestamp string Sign string TimeLimit time.Duration genSignFunc GenSignFunc } type args struct { secret string } tests := []struct { name string fields fields args args want string }{ { name: "correct sign", fields: fields{AppId: "cc38c5ac086a3fc5", NonceStr: "d5f7d8x5z6ds9F8c", Timestamp: "1558507701959", genSignFunc: defaultGenSign}, want: "ffe6d0b60c2b1585df653ddaf92e7104745e3a83", args: args{secret: "9c4759cb2b897722"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := &IBINGLIDigest{ AppId: tt.fields.AppId, NonceStr: tt.fields.NonceStr, Timestamp: tt.fields.Timestamp, Sign: tt.fields.Sign, TimeLimit: tt.fields.TimeLimit, genSignFunc: tt.fields.genSignFunc, } got := v.GenSign(tt.args.secret) if got != tt.want { t.Errorf("GenSign() = %v, want %v", got, tt.want) } t.Logf("got: %v", got) }) } } func TestIBINGLIDigest_CompareSign(t *testing.T) { type fields struct { AppId string NonceStr string Timestamp string Sign string TimeLimit time.Duration genSignFunc GenSignFunc } type args struct { secret string } tests := []struct { name string fields fields args args want bool }{ { name: "correct secret", fields: fields{AppId: "cc38c5ac086a3fc5", NonceStr: "d5f7d8x5z6ds9F8c", Timestamp: "1558507701959", Sign: "ffe6d0b60c2b1585df653ddaf92e7104745e3a83", genSignFunc: defaultGenSign}, want: true, args: args{secret: "9c4759cb2b897722"}, }, { name: "incorrect secret", fields: fields{AppId: "cc38c5ac086a3fc5", NonceStr: "d5f7d8x5z6ds9F8c", Timestamp: "1558507701959", Sign: "ffe6d0b60c2b1585df653ddaf92e7104745e3a83", genSignFunc: defaultGenSign}, want: false, args: args{secret: "9c4759cb2b89772"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := &IBINGLIDigest{ AppId: tt.fields.AppId, NonceStr: tt.fields.NonceStr, Timestamp: tt.fields.Timestamp, Sign: tt.fields.Sign, TimeLimit: tt.fields.TimeLimit, genSignFunc: tt.fields.genSignFunc, } if got := v.CompareSign(tt.args.secret); got != tt.want { t.Errorf("CompareSign() = %v, want %v", got, tt.want) } }) } }