|
4 | 4 | package base |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "crypto/sha1" |
| 8 | + "fmt" |
7 | 9 | "os" |
8 | 10 | "testing" |
9 | 11 | "time" |
10 | 12 |
|
| 13 | + "code.gitea.io/gitea/modules/setting" |
| 14 | + "code.gitea.io/gitea/modules/test" |
| 15 | + |
11 | 16 | "github.com/stretchr/testify/assert" |
12 | 17 | ) |
13 | 18 |
|
14 | | -func TestEncodeSha1(t *testing.T) { |
15 | | - assert.Equal(t, |
16 | | - "8843d7f92416211de9ebb963ff4ce28125932878", |
17 | | - EncodeSha1("foobar"), |
18 | | - ) |
19 | | -} |
20 | | - |
21 | 19 | func TestEncodeSha256(t *testing.T) { |
22 | 20 | assert.Equal(t, |
23 | 21 | "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2", |
@@ -46,43 +44,54 @@ func TestBasicAuthDecode(t *testing.T) { |
46 | 44 | } |
47 | 45 |
|
48 | 46 | func TestVerifyTimeLimitCode(t *testing.T) { |
49 | | - tc := []struct { |
50 | | - data string |
51 | | - minutes int |
52 | | - code string |
53 | | - valid bool |
54 | | - }{{ |
55 | | - data: "data", |
56 | | - minutes: 2, |
57 | | - code: testCreateTimeLimitCode(t, "data", 2), |
58 | | - valid: true, |
59 | | - }, { |
60 | | - data: "abc123-ß", |
61 | | - minutes: 1, |
62 | | - code: testCreateTimeLimitCode(t, "abc123-ß", 1), |
63 | | - valid: true, |
64 | | - }, { |
65 | | - data: "data", |
66 | | - minutes: 2, |
67 | | - code: "2021012723240000005928251dac409d2c33a6eb82c63410aaad569bed", |
68 | | - valid: false, |
69 | | - }} |
70 | | - for _, test := range tc { |
71 | | - actualValid := VerifyTimeLimitCode(test.data, test.minutes, test.code) |
72 | | - assert.Equal(t, test.valid, actualValid, "data: '%s' code: '%s' should be valid: %t", test.data, test.code, test.valid) |
| 47 | + defer test.MockVariableValue(&setting.InstallLock, true)() |
| 48 | + initGeneralSecret := func(secret string) { |
| 49 | + setting.InstallLock = true |
| 50 | + setting.CfgProvider, _ = setting.NewConfigProviderFromData(fmt.Sprintf(` |
| 51 | +[oauth2] |
| 52 | +JWT_SECRET = %s |
| 53 | +`, secret)) |
| 54 | + setting.LoadCommonSettings() |
73 | 55 | } |
74 | | -} |
75 | | - |
76 | | -func testCreateTimeLimitCode(t *testing.T, data string, m int) string { |
77 | | - result0 := CreateTimeLimitCode(data, m, nil) |
78 | | - result1 := CreateTimeLimitCode(data, m, time.Now().Format("200601021504")) |
79 | | - result2 := CreateTimeLimitCode(data, m, time.Unix(time.Now().Unix()+int64(time.Minute)*int64(m), 0).Format("200601021504")) |
80 | | - |
81 | | - assert.Equal(t, result0, result1) |
82 | | - assert.NotEqual(t, result0, result2) |
83 | 56 |
|
84 | | - assert.True(t, len(result0) != 0) |
85 | | - return result0 |
| 57 | + initGeneralSecret("KZb_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko") |
| 58 | + now := time.Now() |
| 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 | + }) |
86 | 95 | } |
87 | 96 |
|
88 | 97 | func TestFileSize(t *testing.T) { |
|
0 commit comments