55package base
66
77import (
8+ "encoding/base64"
9+ "os"
810 "testing"
11+ "time"
912
1013 "github.com/stretchr/testify/assert"
1114)
@@ -53,11 +56,48 @@ func TestBasicAuthDecode(t *testing.T) {
5356
5457func TestBasicAuthEncode (t * testing.T ) {
5558 assert .Equal (t , "Zm9vOmJhcg==" , BasicAuthEncode ("foo" , "bar" ))
59+ assert .Equal (t , "MjM6IjotLS0t" , BasicAuthEncode ("23:\" " , "----" ))
5660}
5761
58- // TODO: Test PBKDF2()
59- // TODO: Test VerifyTimeLimitCode()
60- // TODO: Test CreateTimeLimitCode()
62+ func TestVerifyTimeLimitCode (t * testing.T ) {
63+ tc := []struct {
64+ data string
65+ minutes int
66+ code string
67+ valid bool
68+ }{{
69+ data : "data" ,
70+ minutes : 2 ,
71+ code : testCreateTimeLimitCode (t , "data" , 2 ),
72+ valid : true ,
73+ }, {
74+ data : "abc123-ß" ,
75+ minutes : 1 ,
76+ code : testCreateTimeLimitCode (t , "abc123-ß" , 1 ),
77+ valid : true ,
78+ }, {
79+ data : "data" ,
80+ minutes : 2 ,
81+ code : "2021012723240000005928251dac409d2c33a6eb82c63410aaad569bed" ,
82+ valid : false ,
83+ }}
84+ for _ , test := range tc {
85+ actualValid := VerifyTimeLimitCode (test .data , test .minutes , test .code )
86+ assert .Equal (t , test .valid , actualValid , "data: '%s' code: '%s' should be valid: %t" , test .data , test .code , test .valid )
87+ }
88+ }
89+
90+ func testCreateTimeLimitCode (t * testing.T , data string , m int ) string {
91+ result0 := CreateTimeLimitCode (data , m , nil )
92+ result1 := CreateTimeLimitCode (data , m , time .Now ().Format ("200601021504" ))
93+ result2 := CreateTimeLimitCode (data , m , time .Unix (time .Now ().Unix ()+ int64 (time .Minute )* int64 (m ), 0 ).Format ("200601021504" ))
94+
95+ assert .Equal (t , result0 , result1 )
96+ assert .NotEqual (t , result0 , result2 )
97+
98+ assert .True (t , len (result0 ) != 0 )
99+ return result0
100+ }
61101
62102func TestFileSize (t * testing.T ) {
63103 var size int64 = 512
@@ -76,6 +116,12 @@ func TestFileSize(t *testing.T) {
76116 assert .Equal (t , "2.0 EiB" , FileSize (size ))
77117}
78118
119+ func TestPrettyNumber (t * testing.T ) {
120+ assert .Equal (t , "23,342,432" , PrettyNumber (23342432 ))
121+ assert .Equal (t , "0" , PrettyNumber (0 ))
122+ assert .Equal (t , "-100,000" , PrettyNumber (- 100000 ))
123+ }
124+
79125func TestSubtract (t * testing.T ) {
80126 toFloat64 := func (n interface {}) float64 {
81127 switch v := n .(type ) {
@@ -168,6 +214,13 @@ func TestInt64sToMap(t *testing.T) {
168214 )
169215}
170216
217+ func TestInt64sContains (t * testing.T ) {
218+ assert .Equal (t , map [int64 ]bool {}, Int64sToMap ([]int64 {}))
219+ assert .Equal (t , true , Int64sContains ([]int64 {6 , 44324 , 4324 , 32 , 1 , 2323 }, 1 ))
220+ assert .Equal (t , true , Int64sContains ([]int64 {2323 }, 2323 ))
221+ assert .Equal (t , false , Int64sContains ([]int64 {6 , 44324 , 4324 , 32 , 1 , 2323 }, 232 ))
222+ }
223+
171224func TestIsLetter (t * testing.T ) {
172225 assert .True (t , IsLetter ('a' ))
173226 assert .True (t , IsLetter ('e' ))
@@ -181,6 +234,8 @@ func TestIsLetter(t *testing.T) {
181234 assert .False (t , IsLetter ('-' ))
182235 assert .False (t , IsLetter ('1' ))
183236 assert .False (t , IsLetter ('$' ))
237+ assert .False (t , IsLetter (0x00 ))
238+ assert .False (t , IsLetter (0x93 ))
184239}
185240
186241func TestDetectContentTypeLongerThanSniffLen (t * testing.T ) {
@@ -197,11 +252,19 @@ Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
197252Comment Comment Comment --><svg></svg>` )))
198253}
199254
255+ // IsRepresentableAsText
256+
200257func TestIsTextFile (t * testing.T ) {
201258 assert .True (t , IsTextFile ([]byte {}))
202259 assert .True (t , IsTextFile ([]byte ("lorem ipsum" )))
203260}
204261
262+ func TestIsImageFile (t * testing.T ) {
263+ png , _ := base64 .StdEncoding .DecodeString ("iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAG0lEQVQYlWN4+vTpf3SMDTAMBYXYBLFpHgoKAeiOf0SGE9kbAAAAAElFTkSuQmCC" )
264+ assert .True (t , IsImageFile (png ))
265+ assert .False (t , IsImageFile ([]byte ("plain text" )))
266+ }
267+
205268func TestIsSVGImageFile (t * testing.T ) {
206269 assert .True (t , IsSVGImageFile ([]byte ("<svg></svg>" )))
207270 assert .True (t , IsSVGImageFile ([]byte (" <svg></svg>" )))
@@ -248,13 +311,37 @@ func TestIsSVGImageFile(t *testing.T) {
248311 <foo></foo>` )))
249312}
250313
314+ func TestIsPDFFile (t * testing.T ) {
315+ pdf , _ := base64 .StdEncoding .DecodeString ("JVBERi0xLjYKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nF3NPwsCMQwF8D2f4s2CNYk1baF0EHRwOwg4iJt/NsFb/PpevUE4Mjwe" )
316+ assert .True (t , IsPDFFile (pdf ))
317+ assert .False (t , IsPDFFile ([]byte ("plain text" )))
318+ }
319+
320+ func TestIsVideoFile (t * testing.T ) {
321+ mp4 , _ := base64 .StdEncoding .DecodeString ("AAAAGGZ0eXBtcDQyAAAAAGlzb21tcDQyAAEI721vb3YAAABsbXZoZAAAAADaBlwX2gZcFwAAA+gA" )
322+ assert .True (t , IsVideoFile (mp4 ))
323+ assert .False (t , IsVideoFile ([]byte ("plain text" )))
324+ }
325+
326+ func TestIsAudioFile (t * testing.T ) {
327+ mp3 , _ := base64 .StdEncoding .DecodeString ("SUQzBAAAAAABAFRYWFgAAAASAAADbWFqb3JfYnJhbmQAbXA0MgBUWFhYAAAAEQAAA21pbm9yX3Zl" )
328+ assert .True (t , IsAudioFile (mp3 ))
329+ assert .False (t , IsAudioFile ([]byte ("plain text" )))
330+ }
331+
332+ // TODO: Test EntryIcon
333+
334+ func TestSetupGiteaRoot (t * testing.T ) {
335+ _ = os .Setenv ("GITEA_ROOT" , "test" )
336+ assert .EqualValues (t , "test" , SetupGiteaRoot ())
337+ _ = os .Setenv ("GITEA_ROOT" , "" )
338+ assert .NotEqual (t , "test" , SetupGiteaRoot ())
339+ }
340+
251341func TestFormatNumberSI (t * testing.T ) {
252342 assert .Equal (t , "125" , FormatNumberSI (int (125 )))
253343 assert .Equal (t , "1.3k" , FormatNumberSI (int64 (1317 )))
254344 assert .Equal (t , "21.3M" , FormatNumberSI (21317675 ))
255345 assert .Equal (t , "45.7G" , FormatNumberSI (45721317675 ))
256346 assert .Equal (t , "" , FormatNumberSI ("test" ))
257347}
258-
259- // TODO: IsImageFile(), currently no idea how to test
260- // TODO: IsPDFFile(), currently no idea how to test
0 commit comments