@@ -5,6 +5,7 @@ package base
5
5
6
6
import (
7
7
"crypto/sha1"
8
+ "fmt"
8
9
"os"
9
10
"testing"
10
11
"time"
@@ -44,28 +45,53 @@ func TestBasicAuthDecode(t *testing.T) {
44
45
45
46
func TestVerifyTimeLimitCode (t * testing.T ) {
46
47
defer test .MockVariableValue (& setting .InstallLock , true )()
47
- setting .CfgProvider , _ = setting .NewConfigProviderFromData (`
48
+ initGeneralSecret := func (secret string ) {
49
+ setting .InstallLock = true
50
+ setting .CfgProvider , _ = setting .NewConfigProviderFromData (fmt .Sprintf (`
48
51
[oauth2]
49
- JWT_SECRET = KZb_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko
50
- ` )
51
- setting .LoadCommonSettings ()
52
- time2000 := time .Date (2000 , 1 , 2 , 3 , 4 , 5 , 0 , time .Local )
53
- // test generic parameter
54
- assert .Equal (t , "2000010203040000026fa5221b2731b7cf80b1b506f5e39e38c115fee5" , CreateTimeLimitCode ("test-sha1" , 2 , time2000 , sha1 .New ()))
55
- assert .Equal (t , "2000010203040000026fa5221b2731b7cf80b1b506f5e39e38c115fee5" , CreateTimeLimitCode ("test-sha1" , 2 , "200001020304" , sha1 .New ()))
56
- assert .Equal (t , "2000010203040000024842227a2f87041ff82025199c0187410a9297bf" , CreateTimeLimitCode ("test-hmac" , 2 , time2000 , nil ))
57
- assert .Equal (t , "2000010203040000024842227a2f87041ff82025199c0187410a9297bf" , CreateTimeLimitCode ("test-hmac" , 2 , "200001020304" , nil ))
52
+ JWT_SECRET = %s
53
+ ` , secret ))
54
+ setting .LoadCommonSettings ()
55
+ }
58
56
57
+ initGeneralSecret ("KZb_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko" )
59
58
now := time .Now ()
60
- assert .False (t , VerifyTimeLimitCode (now , "data" , 2 , "" ))
61
- assert .False (t , VerifyTimeLimitCode (now , "data" , 2 , "invalid code" ))
62
-
63
- code := CreateTimeLimitCode ("data" , 2 , now , nil )
64
- assert .False (t , VerifyTimeLimitCode (now .Add (- time .Minute ), "data" , 2 , code )) // not started yet
65
- assert .True (t , VerifyTimeLimitCode (now , "data" , 2 , code ))
66
- assert .True (t , VerifyTimeLimitCode (now .Add (time .Minute ), "data" , 2 , code ))
67
- assert .False (t , VerifyTimeLimitCode (now .Add (time .Minute ), "DATA" , 2 , code )) // invalid data
68
- assert .False (t , VerifyTimeLimitCode (now .Add (2 * time .Minute ), "data" , 2 , code )) // expired
59
+
60
+ t .Run ("TestGenericParameter" , func (t * testing.T ) {
61
+ time2000 := time .Date (2000 , 1 , 2 , 3 , 4 , 5 , 0 , time .Local )
62
+ assert .Equal (t , "2000010203040000026fa5221b2731b7cf80b1b506f5e39e38c115fee5" , CreateTimeLimitCode ("test-sha1" , 2 , time2000 , sha1 .New ()))
63
+ assert .Equal (t , "2000010203040000026fa5221b2731b7cf80b1b506f5e39e38c115fee5" , CreateTimeLimitCode ("test-sha1" , 2 , "200001020304" , sha1 .New ()))
64
+ assert .Equal (t , "2000010203040000024842227a2f87041ff82025199c0187410a9297bf" , CreateTimeLimitCode ("test-hmac" , 2 , time2000 , nil ))
65
+ assert .Equal (t , "2000010203040000024842227a2f87041ff82025199c0187410a9297bf" , CreateTimeLimitCode ("test-hmac" , 2 , "200001020304" , nil ))
66
+ })
67
+
68
+ t .Run ("TestInvalidCode" , func (t * testing.T ) {
69
+ assert .False (t , VerifyTimeLimitCode (now , "data" , 2 , "" ))
70
+ assert .False (t , VerifyTimeLimitCode (now , "data" , 2 , "invalid code" ))
71
+ })
72
+
73
+ t .Run ("TestCreateAndVerify" , func (t * testing.T ) {
74
+ code := CreateTimeLimitCode ("data" , 2 , now , nil )
75
+ assert .False (t , VerifyTimeLimitCode (now .Add (- time .Minute ), "data" , 2 , code )) // not started yet
76
+ assert .True (t , VerifyTimeLimitCode (now , "data" , 2 , code ))
77
+ assert .True (t , VerifyTimeLimitCode (now .Add (time .Minute ), "data" , 2 , code ))
78
+ assert .False (t , VerifyTimeLimitCode (now .Add (time .Minute ), "DATA" , 2 , code )) // invalid data
79
+ assert .False (t , VerifyTimeLimitCode (now .Add (2 * time .Minute ), "data" , 2 , code )) // expired
80
+ })
81
+
82
+ t .Run ("TestDifferentSecret" , func (t * testing.T ) {
83
+ // use another secret to ensure the code is invalid for different secret
84
+ verifyDataCode := func (c string ) bool {
85
+ return VerifyTimeLimitCode (now , "data" , 2 , c )
86
+ }
87
+ code1 := CreateTimeLimitCode ("data" , 2 , now , sha1 .New ())
88
+ code2 := CreateTimeLimitCode ("data" , 2 , now , nil )
89
+ assert .True (t , verifyDataCode (code1 ))
90
+ assert .True (t , verifyDataCode (code2 ))
91
+ initGeneralSecret ("000_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko" )
92
+ assert .False (t , verifyDataCode (code1 ))
93
+ assert .False (t , verifyDataCode (code2 ))
94
+ })
69
95
}
70
96
71
97
func TestFileSize (t * testing.T ) {
0 commit comments