github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/base/tool_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package base
     7  
     8  import (
     9  	"os"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestEncodeMD5(t *testing.T) {
    17  	assert.Equal(t,
    18  		"3858f62230ac3c915f300c664312c63f",
    19  		EncodeMD5("foobar"),
    20  	)
    21  }
    22  
    23  func TestEncodeSha1(t *testing.T) {
    24  	assert.Equal(t,
    25  		"8843d7f92416211de9ebb963ff4ce28125932878",
    26  		EncodeSha1("foobar"),
    27  	)
    28  }
    29  
    30  func TestEncodeSha256(t *testing.T) {
    31  	assert.Equal(t,
    32  		"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
    33  		EncodeSha256("foobar"),
    34  	)
    35  }
    36  
    37  func TestShortSha(t *testing.T) {
    38  	assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
    39  }
    40  
    41  func TestBasicAuthDecode(t *testing.T) {
    42  	_, _, err := BasicAuthDecode("?")
    43  	assert.Equal(t, "illegal base64 data at input byte 0", err.Error())
    44  
    45  	user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
    46  	assert.NoError(t, err)
    47  	assert.Equal(t, "foo", user)
    48  	assert.Equal(t, "bar", pass)
    49  
    50  	_, _, err = BasicAuthDecode("aW52YWxpZA==")
    51  	assert.Error(t, err)
    52  
    53  	_, _, err = BasicAuthDecode("invalid")
    54  	assert.Error(t, err)
    55  }
    56  
    57  func TestBasicAuthEncode(t *testing.T) {
    58  	assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
    59  	assert.Equal(t, "MjM6IjotLS0t", BasicAuthEncode("23:\"", "----"))
    60  }
    61  
    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  }
   101  
   102  func TestFileSize(t *testing.T) {
   103  	var size int64 = 512
   104  	assert.Equal(t, "512 B", FileSize(size))
   105  	size *= 1024
   106  	assert.Equal(t, "512 KiB", FileSize(size))
   107  	size *= 1024
   108  	assert.Equal(t, "512 MiB", FileSize(size))
   109  	size *= 1024
   110  	assert.Equal(t, "512 GiB", FileSize(size))
   111  	size *= 1024
   112  	assert.Equal(t, "512 TiB", FileSize(size))
   113  	size *= 1024
   114  	assert.Equal(t, "512 PiB", FileSize(size))
   115  	size *= 4
   116  	assert.Equal(t, "2.0 EiB", FileSize(size))
   117  }
   118  
   119  func TestPrettyNumber(t *testing.T) {
   120  	assert.Equal(t, "23,342,432", PrettyNumber(23342432))
   121  	assert.Equal(t, "23,342,432", PrettyNumber(int32(23342432)))
   122  	assert.Equal(t, "0", PrettyNumber(0))
   123  	assert.Equal(t, "-100,000", PrettyNumber(-100000))
   124  }
   125  
   126  func TestSubtract(t *testing.T) {
   127  	toFloat64 := func(n interface{}) float64 {
   128  		switch v := n.(type) {
   129  		case int:
   130  			return float64(v)
   131  		case int8:
   132  			return float64(v)
   133  		case int16:
   134  			return float64(v)
   135  		case int32:
   136  			return float64(v)
   137  		case int64:
   138  			return float64(v)
   139  		case float32:
   140  			return float64(v)
   141  		case float64:
   142  			return v
   143  		default:
   144  			return 0.0
   145  		}
   146  	}
   147  	values := []interface{}{
   148  		int(-3),
   149  		int8(14),
   150  		int16(81),
   151  		int32(-156),
   152  		int64(1528),
   153  		float32(3.5),
   154  		float64(-15.348),
   155  	}
   156  	for _, left := range values {
   157  		for _, right := range values {
   158  			expected := toFloat64(left) - toFloat64(right)
   159  			sub := Subtract(left, right)
   160  			assert.InDelta(t, expected, sub, 1e-3)
   161  		}
   162  	}
   163  }
   164  
   165  func TestEllipsisString(t *testing.T) {
   166  	assert.Equal(t, "...", EllipsisString("foobar", 0))
   167  	assert.Equal(t, "...", EllipsisString("foobar", 1))
   168  	assert.Equal(t, "...", EllipsisString("foobar", 2))
   169  	assert.Equal(t, "...", EllipsisString("foobar", 3))
   170  	assert.Equal(t, "f...", EllipsisString("foobar", 4))
   171  	assert.Equal(t, "fo...", EllipsisString("foobar", 5))
   172  	assert.Equal(t, "foobar", EllipsisString("foobar", 6))
   173  	assert.Equal(t, "foobar", EllipsisString("foobar", 10))
   174  	assert.Equal(t, "测...", EllipsisString("测试文本一二三四", 4))
   175  	assert.Equal(t, "测试...", EllipsisString("测试文本一二三四", 5))
   176  	assert.Equal(t, "测试文...", EllipsisString("测试文本一二三四", 6))
   177  	assert.Equal(t, "测试文本一二三四", EllipsisString("测试文本一二三四", 10))
   178  }
   179  
   180  func TestTruncateString(t *testing.T) {
   181  	assert.Equal(t, "", TruncateString("foobar", 0))
   182  	assert.Equal(t, "f", TruncateString("foobar", 1))
   183  	assert.Equal(t, "fo", TruncateString("foobar", 2))
   184  	assert.Equal(t, "foo", TruncateString("foobar", 3))
   185  	assert.Equal(t, "foob", TruncateString("foobar", 4))
   186  	assert.Equal(t, "fooba", TruncateString("foobar", 5))
   187  	assert.Equal(t, "foobar", TruncateString("foobar", 6))
   188  	assert.Equal(t, "foobar", TruncateString("foobar", 7))
   189  	assert.Equal(t, "测试文本", TruncateString("测试文本一二三四", 4))
   190  	assert.Equal(t, "测试文本一", TruncateString("测试文本一二三四", 5))
   191  	assert.Equal(t, "测试文本一二", TruncateString("测试文本一二三四", 6))
   192  	assert.Equal(t, "测试文本一二三", TruncateString("测试文本一二三四", 7))
   193  }
   194  
   195  func TestStringsToInt64s(t *testing.T) {
   196  	testSuccess := func(input []string, expected []int64) {
   197  		result, err := StringsToInt64s(input)
   198  		assert.NoError(t, err)
   199  		assert.Equal(t, expected, result)
   200  	}
   201  	testSuccess([]string{}, []int64{})
   202  	testSuccess([]string{"-1234"}, []int64{-1234})
   203  	testSuccess([]string{"1", "4", "16", "64", "256"},
   204  		[]int64{1, 4, 16, 64, 256})
   205  
   206  	_, err := StringsToInt64s([]string{"-1", "a", "$"})
   207  	assert.Error(t, err)
   208  }
   209  
   210  func TestInt64sToStrings(t *testing.T) {
   211  	assert.Equal(t, []string{}, Int64sToStrings([]int64{}))
   212  	assert.Equal(t,
   213  		[]string{"1", "4", "16", "64", "256"},
   214  		Int64sToStrings([]int64{1, 4, 16, 64, 256}),
   215  	)
   216  }
   217  
   218  func TestInt64sToMap(t *testing.T) {
   219  	assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
   220  	assert.Equal(t,
   221  		map[int64]bool{1: true, 4: true, 16: true},
   222  		Int64sToMap([]int64{1, 4, 16}),
   223  	)
   224  }
   225  
   226  func TestInt64sContains(t *testing.T) {
   227  	assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
   228  	assert.True(t, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 1))
   229  	assert.True(t, Int64sContains([]int64{2323}, 2323))
   230  	assert.False(t, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 232))
   231  }
   232  
   233  func TestIsLetter(t *testing.T) {
   234  	assert.True(t, IsLetter('a'))
   235  	assert.True(t, IsLetter('e'))
   236  	assert.True(t, IsLetter('q'))
   237  	assert.True(t, IsLetter('z'))
   238  	assert.True(t, IsLetter('A'))
   239  	assert.True(t, IsLetter('E'))
   240  	assert.True(t, IsLetter('Q'))
   241  	assert.True(t, IsLetter('Z'))
   242  	assert.True(t, IsLetter('_'))
   243  	assert.False(t, IsLetter('-'))
   244  	assert.False(t, IsLetter('1'))
   245  	assert.False(t, IsLetter('$'))
   246  	assert.False(t, IsLetter(0x00))
   247  	assert.False(t, IsLetter(0x93))
   248  }
   249  
   250  // TODO: Test EntryIcon
   251  
   252  func TestSetupGiteaRoot(t *testing.T) {
   253  	_ = os.Setenv("GITBUNDLE_ROOT", "test")
   254  	assert.Equal(t, "test", SetupGiteaRoot())
   255  	_ = os.Setenv("GITBUNDLE_ROOT", "")
   256  	assert.NotEqual(t, "test", SetupGiteaRoot())
   257  }
   258  
   259  func TestFormatNumberSI(t *testing.T) {
   260  	assert.Equal(t, "125", FormatNumberSI(int(125)))
   261  	assert.Equal(t, "1.3k", FormatNumberSI(int64(1317)))
   262  	assert.Equal(t, "21.3M", FormatNumberSI(21317675))
   263  	assert.Equal(t, "45.7G", FormatNumberSI(45721317675))
   264  	assert.Equal(t, "", FormatNumberSI("test"))
   265  }