You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tool_test.go 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package base
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. )
  6. func TestEncodeMD5(t *testing.T) {
  7. if checksum := EncodeMD5("foobar"); checksum != "3858f62230ac3c915f300c664312c63f" {
  8. t.Errorf("got the wrong md5sum for string foobar: %s", checksum)
  9. }
  10. }
  11. func TestEncodeSha1(t *testing.T) {
  12. if checksum := EncodeSha1("foobar"); checksum != "8843d7f92416211de9ebb963ff4ce28125932878" {
  13. t.Errorf("got the wrong sha1sum for string foobar: %s", checksum)
  14. }
  15. }
  16. func TestShortSha(t *testing.T) {
  17. if result := ShortSha("veryverylong"); result != "veryverylo" {
  18. t.Errorf("got the wrong sha1sum for string foobar: %s", result)
  19. }
  20. }
  21. // TODO: Test DetectEncoding()
  22. func TestBasicAuthDecode(t *testing.T) {
  23. if _, _, err := BasicAuthDecode("?"); err.Error() != "illegal base64 data at input byte 0" {
  24. t.Errorf("BasicAuthDecode should fail due to illeagl data: %v", err)
  25. }
  26. user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
  27. if err != nil {
  28. t.Errorf("err should be nil but is: %v", err)
  29. }
  30. if user != "foo" {
  31. t.Errorf("user should be foo but is: %s", user)
  32. }
  33. if pass != "bar" {
  34. t.Errorf("pass should be foo but is: %s", pass)
  35. }
  36. }
  37. func TestBasicAuthEncode(t *testing.T) {
  38. if auth := BasicAuthEncode("foo", "bar"); auth != "Zm9vOmJhcg==" {
  39. t.Errorf("auth should be Zm9vOmJhcg== but is: %s", auth)
  40. }
  41. }
  42. func TestGetRandomString(t *testing.T) {
  43. if len(GetRandomString(4)) != 4 {
  44. t.Error("expected GetRandomString to be of len 4")
  45. }
  46. }
  47. // TODO: Test PBKDF2()
  48. // TODO: Test VerifyTimeLimitCode()
  49. // TODO: Test CreateTimeLimitCode()
  50. func TestHashEmail(t *testing.T) {
  51. if hash := HashEmail("lunny@gitea.io"); hash != "1b6d0c0e124d47ded12cd7115addeb11" {
  52. t.Errorf("unexpected email hash: %s", hash)
  53. }
  54. }
  55. // TODO: AvatarLink()
  56. // TODO: computeTimeDiff()
  57. // TODO: TimeSincePro()
  58. // TODO: timeSince()
  59. // TODO: RawTimeSince()
  60. // TODO: TimeSince()
  61. // TODO: logn()
  62. // TODO: humanateBytes()
  63. func TestFileSize(t *testing.T) {
  64. var size int64
  65. size = 512
  66. assert.Equal(t, "512B", FileSize(size))
  67. size = size * 1024
  68. assert.Equal(t, "512KB", FileSize(size))
  69. size = size * 1024
  70. assert.Equal(t, "512MB", FileSize(size))
  71. size = size * 1024
  72. assert.Equal(t, "512GB", FileSize(size))
  73. size = size * 1024
  74. assert.Equal(t, "512TB", FileSize(size))
  75. size = size * 1024
  76. assert.Equal(t, "512PB", FileSize(size))
  77. //size = size * 1024 TODO: Fix bug for EB
  78. //assert.Equal(t, "512EB", FileSize(size))
  79. }
  80. // TODO: Subtract()
  81. // TODO: EllipsisString()
  82. // TODO: TruncateString()
  83. // TODO: StringsToInt64s()
  84. // TODO: Int64sToStrings()
  85. // TODO: Int64sToMap()
  86. // TODO: IsLetter()
  87. // TODO: IsTextFile()
  88. // TODO: IsImageFile()
  89. // TODO: IsPDFFile()