summaryrefslogtreecommitdiffstats
path: root/modules/base/tool_test.go
blob: e92786b35b6070e1fd99cfc08ed6aa2f1a24031c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package base

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestEncodeMD5(t *testing.T) {
	if checksum := EncodeMD5("foobar"); checksum != "3858f62230ac3c915f300c664312c63f" {
		t.Errorf("got the wrong md5sum for string foobar: %s", checksum)
	}

}

func TestEncodeSha1(t *testing.T) {
	if checksum := EncodeSha1("foobar"); checksum != "8843d7f92416211de9ebb963ff4ce28125932878" {
		t.Errorf("got the wrong sha1sum for string foobar: %s", checksum)
	}
}

func TestShortSha(t *testing.T) {
	if result := ShortSha("veryverylong"); result != "veryverylo" {
		t.Errorf("got the wrong sha1sum for string foobar: %s", result)
	}
}

// TODO: Test DetectEncoding()

func TestBasicAuthDecode(t *testing.T) {
	if _, _, err := BasicAuthDecode("?"); err.Error() != "illegal base64 data at input byte 0" {
		t.Errorf("BasicAuthDecode should fail due to illeagl data: %v", err)
	}

	user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
	if err != nil {
		t.Errorf("err should be nil but is: %v", err)
	}
	if user != "foo" {
		t.Errorf("user should be foo but is: %s", user)
	}
	if pass != "bar" {
		t.Errorf("pass should be foo but is: %s", pass)
	}
}

func TestBasicAuthEncode(t *testing.T) {
	if auth := BasicAuthEncode("foo", "bar"); auth != "Zm9vOmJhcg==" {
		t.Errorf("auth should be Zm9vOmJhcg== but is: %s", auth)
	}
}

func TestGetRandomString(t *testing.T) {
	if len(GetRandomString(4)) != 4 {
		t.Error("expected GetRandomString to be of len 4")
	}
}

// TODO: Test PBKDF2()
// TODO: Test VerifyTimeLimitCode()
// TODO: Test CreateTimeLimitCode()

func TestHashEmail(t *testing.T) {
	if hash := HashEmail("lunny@gitea.io"); hash != "1b6d0c0e124d47ded12cd7115addeb11" {
		t.Errorf("unexpected email hash: %s", hash)
	}
}

// TODO: AvatarLink()
// TODO: computeTimeDiff()
// TODO: TimeSincePro()
// TODO: timeSince()
// TODO: RawTimeSince()
// TODO: TimeSince()
// TODO: logn()
// TODO: humanateBytes()

func TestFileSize(t *testing.T) {
	var size int64
	size = 512
	assert.Equal(t, "512B", FileSize(size))
	size = size * 1024
	assert.Equal(t, "512KB", FileSize(size))
	size = size * 1024
	assert.Equal(t, "512MB", FileSize(size))
	size = size * 1024
	assert.Equal(t, "512GB", FileSize(size))
	size = size * 1024
	assert.Equal(t, "512TB", FileSize(size))
	size = size * 1024
	assert.Equal(t, "512PB", FileSize(size))
	//size = size * 1024 TODO: Fix bug for EB
	//assert.Equal(t, "512EB", FileSize(size))
}

// TODO: Subtract()
// TODO: EllipsisString()
// TODO: TruncateString()
// TODO: StringsToInt64s()
// TODO: Int64sToStrings()
// TODO: Int64sToMap()
// TODO: IsLetter()
// TODO: IsTextFile()
// TODO: IsImageFile()
// TODO: IsPDFFile()