ibingli_digest_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package signutil
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. )
  7. func TestIBINGLIDigest_Validate(t *testing.T) {
  8. type fields struct {
  9. AppId string
  10. NonceStr string
  11. Timestamp string
  12. Sign string
  13. TimeLimit time.Duration
  14. genSignFunc GenSignFunc
  15. }
  16. tests := []struct {
  17. name string
  18. fields fields
  19. wantErr bool
  20. }{
  21. {
  22. name: "lack of field",
  23. fields: fields{AppId: "123", NonceStr: "", Timestamp: "134134", Sign: ""},
  24. wantErr: true,
  25. },
  26. {
  27. name: "has field",
  28. fields: fields{AppId: "123", NonceStr: "123", Timestamp: "134134", Sign: "123"},
  29. wantErr: false,
  30. },
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. v := &IBINGLIDigest{
  35. AppId: tt.fields.AppId,
  36. NonceStr: tt.fields.NonceStr,
  37. Timestamp: tt.fields.Timestamp,
  38. Sign: tt.fields.Sign,
  39. TimeLimit: tt.fields.TimeLimit,
  40. genSignFunc: tt.fields.genSignFunc,
  41. }
  42. if err := v.Validate(); (err != nil) != tt.wantErr {
  43. t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
  44. }
  45. })
  46. }
  47. }
  48. func TestIBINGLIDigest_ValidateTimestamp(t *testing.T) {
  49. type fields struct {
  50. AppId string
  51. NonceStr string
  52. Timestamp string
  53. Sign string
  54. TimeLimit time.Duration
  55. genSignFunc GenSignFunc
  56. }
  57. tests := []struct {
  58. name string
  59. fields fields
  60. wantErr bool
  61. }{
  62. {
  63. name: "correct time",
  64. fields: fields{Timestamp: strconv.Itoa(int(time.Now().UnixMilli())), TimeLimit: time.Hour},
  65. wantErr: false,
  66. },
  67. {
  68. name: "error time",
  69. fields: fields{Timestamp: strconv.Itoa(int(time.Now().Add(-2 * time.Hour).UnixMilli())), TimeLimit: time.Hour},
  70. wantErr: true,
  71. },
  72. }
  73. for _, tt := range tests {
  74. t.Run(tt.name, func(t *testing.T) {
  75. v := &IBINGLIDigest{
  76. AppId: tt.fields.AppId,
  77. NonceStr: tt.fields.NonceStr,
  78. Timestamp: tt.fields.Timestamp,
  79. Sign: tt.fields.Sign,
  80. TimeLimit: tt.fields.TimeLimit,
  81. genSignFunc: tt.fields.genSignFunc,
  82. }
  83. if err := v.ValidateTimestamp(); (err != nil) != tt.wantErr {
  84. t.Errorf("ValidateTimestamp() error = %v, wantErr %v", err, tt.wantErr)
  85. }
  86. })
  87. }
  88. }
  89. func TestIBINGLIDigest_GenSign(t *testing.T) {
  90. type fields struct {
  91. AppId string
  92. NonceStr string
  93. Timestamp string
  94. Sign string
  95. TimeLimit time.Duration
  96. genSignFunc GenSignFunc
  97. }
  98. type args struct {
  99. secret string
  100. }
  101. tests := []struct {
  102. name string
  103. fields fields
  104. args args
  105. want string
  106. }{
  107. {
  108. name: "correct sign",
  109. fields: fields{AppId: "cc38c5ac086a3fc5", NonceStr: "d5f7d8x5z6ds9F8c", Timestamp: "1558507701959", genSignFunc: defaultGenSign},
  110. want: "ffe6d0b60c2b1585df653ddaf92e7104745e3a83",
  111. args: args{secret: "9c4759cb2b897722"},
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. v := &IBINGLIDigest{
  117. AppId: tt.fields.AppId,
  118. NonceStr: tt.fields.NonceStr,
  119. Timestamp: tt.fields.Timestamp,
  120. Sign: tt.fields.Sign,
  121. TimeLimit: tt.fields.TimeLimit,
  122. genSignFunc: tt.fields.genSignFunc,
  123. }
  124. got := v.GenSign(tt.args.secret)
  125. if got != tt.want {
  126. t.Errorf("GenSign() = %v, want %v", got, tt.want)
  127. }
  128. t.Logf("got: %v", got)
  129. })
  130. }
  131. }
  132. func TestIBINGLIDigest_CompareSign(t *testing.T) {
  133. type fields struct {
  134. AppId string
  135. NonceStr string
  136. Timestamp string
  137. Sign string
  138. TimeLimit time.Duration
  139. genSignFunc GenSignFunc
  140. }
  141. type args struct {
  142. secret string
  143. }
  144. tests := []struct {
  145. name string
  146. fields fields
  147. args args
  148. want bool
  149. }{
  150. {
  151. name: "correct secret",
  152. fields: fields{AppId: "cc38c5ac086a3fc5", NonceStr: "d5f7d8x5z6ds9F8c", Timestamp: "1558507701959", Sign: "ffe6d0b60c2b1585df653ddaf92e7104745e3a83", genSignFunc: defaultGenSign},
  153. want: true,
  154. args: args{secret: "9c4759cb2b897722"},
  155. },
  156. {
  157. name: "incorrect secret",
  158. fields: fields{AppId: "cc38c5ac086a3fc5", NonceStr: "d5f7d8x5z6ds9F8c", Timestamp: "1558507701959", Sign: "ffe6d0b60c2b1585df653ddaf92e7104745e3a83", genSignFunc: defaultGenSign},
  159. want: false,
  160. args: args{secret: "9c4759cb2b89772"},
  161. },
  162. }
  163. for _, tt := range tests {
  164. t.Run(tt.name, func(t *testing.T) {
  165. v := &IBINGLIDigest{
  166. AppId: tt.fields.AppId,
  167. NonceStr: tt.fields.NonceStr,
  168. Timestamp: tt.fields.Timestamp,
  169. Sign: tt.fields.Sign,
  170. TimeLimit: tt.fields.TimeLimit,
  171. genSignFunc: tt.fields.genSignFunc,
  172. }
  173. if got := v.CompareSign(tt.args.secret); got != tt.want {
  174. t.Errorf("CompareSign() = %v, want %v", got, tt.want)
  175. }
  176. })
  177. }
  178. }