util_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package util
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. )
  8. func TestRandomString(t *testing.T) {
  9. s1 := RandomString(10)
  10. s2 := RandomString(10)
  11. s3 := RandomString(12)
  12. require.Equal(t, 10, len(s1))
  13. require.Equal(t, 10, len(s2))
  14. require.Equal(t, 12, len(s3))
  15. require.NotEqual(t, s1, s2)
  16. }
  17. func TestFileExists(t *testing.T) {
  18. filename := filepath.Join(t.TempDir(), "somefile.txt")
  19. require.Nil(t, os.WriteFile(filename, []byte{0x25, 0x86}, 0600))
  20. require.True(t, FileExists(filename))
  21. require.False(t, FileExists(filename+".doesnotexist"))
  22. }
  23. func TestInStringList(t *testing.T) {
  24. s := []string{"one", "two"}
  25. require.True(t, Contains(s, "two"))
  26. require.False(t, Contains(s, "three"))
  27. }
  28. func TestInStringListAll(t *testing.T) {
  29. s := []string{"one", "two", "three", "four"}
  30. require.True(t, ContainsAll(s, []string{"two", "four"}))
  31. require.False(t, ContainsAll(s, []string{"three", "five"}))
  32. }
  33. func TestContains(t *testing.T) {
  34. s := []int{1, 2}
  35. require.True(t, Contains(s, 2))
  36. require.False(t, Contains(s, 3))
  37. }
  38. func TestSplitNoEmpty(t *testing.T) {
  39. require.Equal(t, []string{}, SplitNoEmpty("", ","))
  40. require.Equal(t, []string{}, SplitNoEmpty(",,,", ","))
  41. require.Equal(t, []string{"tag1", "tag2"}, SplitNoEmpty("tag1,tag2", ","))
  42. require.Equal(t, []string{"tag1", "tag2"}, SplitNoEmpty("tag1,tag2,", ","))
  43. }
  44. func TestParsePriority(t *testing.T) {
  45. priorities := []string{"", "1", "2", "3", "4", "5", "min", "LOW", " default ", "HIgh", "max", "urgent"}
  46. expected := []int{0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 5}
  47. for i, priority := range priorities {
  48. actual, err := ParsePriority(priority)
  49. require.Nil(t, err)
  50. require.Equal(t, expected[i], actual)
  51. }
  52. }
  53. func TestParsePriority_Invalid(t *testing.T) {
  54. priorities := []string{"-1", "6", "aa", "-", "o=1"}
  55. for _, priority := range priorities {
  56. _, err := ParsePriority(priority)
  57. require.Equal(t, errInvalidPriority, err)
  58. }
  59. }
  60. func TestParsePriority_HTTPSpecPriority(t *testing.T) {
  61. priorities := []string{"u=1", "u=3", "u=7, i"} // see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-priority
  62. for _, priority := range priorities {
  63. actual, err := ParsePriority(priority)
  64. require.Nil(t, err)
  65. require.Equal(t, 3, actual) // Always expect 3!
  66. }
  67. }
  68. func TestPriorityString(t *testing.T) {
  69. priorities := []int{0, 1, 2, 3, 4, 5}
  70. expected := []string{"default", "min", "low", "default", "high", "max"}
  71. for i, priority := range priorities {
  72. actual, err := PriorityString(priority)
  73. require.Nil(t, err)
  74. require.Equal(t, expected[i], actual)
  75. }
  76. }
  77. func TestPriorityString_Invalid(t *testing.T) {
  78. _, err := PriorityString(99)
  79. require.Equal(t, err, errInvalidPriority)
  80. }
  81. func TestShortTopicURL(t *testing.T) {
  82. require.Equal(t, "ntfy.sh/mytopic", ShortTopicURL("https://ntfy.sh/mytopic"))
  83. require.Equal(t, "ntfy.sh/mytopic", ShortTopicURL("http://ntfy.sh/mytopic"))
  84. require.Equal(t, "lalala", ShortTopicURL("lalala"))
  85. }
  86. func TestParseSize_10GSuccess(t *testing.T) {
  87. s, err := ParseSize("10G")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. require.Equal(t, int64(10*1024*1024*1024), s)
  92. }
  93. func TestParseSize_10MUpperCaseSuccess(t *testing.T) {
  94. s, err := ParseSize("10M")
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. require.Equal(t, int64(10*1024*1024), s)
  99. }
  100. func TestParseSize_10kLowerCaseSuccess(t *testing.T) {
  101. s, err := ParseSize("10k")
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. require.Equal(t, int64(10*1024), s)
  106. }
  107. func TestParseSize_FailureInvalid(t *testing.T) {
  108. _, err := ParseSize("not a size")
  109. if err == nil {
  110. t.Fatalf("expected error, but got none")
  111. }
  112. }
  113. func TestSplitKV(t *testing.T) {
  114. key, value := SplitKV(" key = value ", "=")
  115. require.Equal(t, "key", key)
  116. require.Equal(t, "value", value)
  117. key, value = SplitKV(" value ", "=")
  118. require.Equal(t, "", key)
  119. require.Equal(t, "value", value)
  120. key, value = SplitKV("mykey=value=with=separator ", "=")
  121. require.Equal(t, "mykey", key)
  122. require.Equal(t, "value=with=separator", value)
  123. }
  124. func TestLastString(t *testing.T) {
  125. require.Equal(t, "last", LastString([]string{"first", "second", "last"}, "default"))
  126. require.Equal(t, "default", LastString([]string{}, "default"))
  127. }
  128. func TestQuoteCommand(t *testing.T) {
  129. require.Equal(t, `ls -al "Document Folder"`, QuoteCommand([]string{"ls", "-al", "Document Folder"}))
  130. require.Equal(t, `rsync -av /home/phil/ root@example.com:/home/phil/`, QuoteCommand([]string{"rsync", "-av", "/home/phil/", "root@example.com:/home/phil/"}))
  131. require.Equal(t, `/home/sweet/home "Äöü this is a test" "\a\b"`, QuoteCommand([]string{"/home/sweet/home", "Äöü this is a test", "\\a\\b"}))
  132. }