123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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)
- }
- })
- }
- }
|