util_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package util
  2. import (
  3. "io"
  4. "net/netip"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestRandomString(t *testing.T) {
  12. s1 := RandomString(10)
  13. s2 := RandomString(10)
  14. s3 := RandomString(12)
  15. require.Equal(t, 10, len(s1))
  16. require.Equal(t, 10, len(s2))
  17. require.Equal(t, 12, len(s3))
  18. require.NotEqual(t, s1, s2)
  19. }
  20. func TestFileExists(t *testing.T) {
  21. filename := filepath.Join(t.TempDir(), "somefile.txt")
  22. require.Nil(t, os.WriteFile(filename, []byte{0x25, 0x86}, 0600))
  23. require.True(t, FileExists(filename))
  24. require.False(t, FileExists(filename+".doesnotexist"))
  25. }
  26. func TestInStringList(t *testing.T) {
  27. s := []string{"one", "two"}
  28. require.True(t, Contains(s, "two"))
  29. require.False(t, Contains(s, "three"))
  30. }
  31. func TestInStringListAll(t *testing.T) {
  32. s := []string{"one", "two", "three", "four"}
  33. require.True(t, ContainsAll(s, []string{"two", "four"}))
  34. require.False(t, ContainsAll(s, []string{"three", "five"}))
  35. }
  36. func TestContains(t *testing.T) {
  37. s := []int{1, 2}
  38. require.True(t, Contains(s, 2))
  39. require.False(t, Contains(s, 3))
  40. }
  41. func TestContainsIP(t *testing.T) {
  42. require.True(t, ContainsIP([]netip.Prefix{netip.MustParsePrefix("fd00::/8"), netip.MustParsePrefix("1.1.0.0/16")}, netip.MustParseAddr("1.1.1.1")))
  43. require.True(t, ContainsIP([]netip.Prefix{netip.MustParsePrefix("fd00::/8"), netip.MustParsePrefix("1.1.0.0/16")}, netip.MustParseAddr("fd12:1234:5678::9876")))
  44. require.False(t, ContainsIP([]netip.Prefix{netip.MustParsePrefix("fd00::/8"), netip.MustParsePrefix("1.1.0.0/16")}, netip.MustParseAddr("1.2.0.1")))
  45. require.False(t, ContainsIP([]netip.Prefix{netip.MustParsePrefix("fd00::/8"), netip.MustParsePrefix("1.1.0.0/16")}, netip.MustParseAddr("fc00::1")))
  46. }
  47. func TestSplitNoEmpty(t *testing.T) {
  48. require.Equal(t, []string{}, SplitNoEmpty("", ","))
  49. require.Equal(t, []string{}, SplitNoEmpty(",,,", ","))
  50. require.Equal(t, []string{"tag1", "tag2"}, SplitNoEmpty("tag1,tag2", ","))
  51. require.Equal(t, []string{"tag1", "tag2"}, SplitNoEmpty("tag1,tag2,", ","))
  52. }
  53. func TestParsePriority(t *testing.T) {
  54. priorities := []string{"", "1", "2", "3", "4", "5", "min", "LOW", " default ", "HIgh", "max", "urgent"}
  55. expected := []int{0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 5}
  56. for i, priority := range priorities {
  57. actual, err := ParsePriority(priority)
  58. require.Nil(t, err)
  59. require.Equal(t, expected[i], actual)
  60. }
  61. }
  62. func TestParsePriority_Invalid(t *testing.T) {
  63. priorities := []string{"-1", "6", "aa", "-", "o=1"}
  64. for _, priority := range priorities {
  65. _, err := ParsePriority(priority)
  66. require.Equal(t, errInvalidPriority, err)
  67. }
  68. }
  69. func TestParsePriority_HTTPSpecPriority(t *testing.T) {
  70. priorities := []string{"u=1", "u=3", "u=7, i"} // see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-priority
  71. for _, priority := range priorities {
  72. actual, err := ParsePriority(priority)
  73. require.Nil(t, err)
  74. require.Equal(t, 3, actual) // Always expect 3!
  75. }
  76. }
  77. func TestPriorityString(t *testing.T) {
  78. priorities := []int{0, 1, 2, 3, 4, 5}
  79. expected := []string{"default", "min", "low", "default", "high", "max"}
  80. for i, priority := range priorities {
  81. actual, err := PriorityString(priority)
  82. require.Nil(t, err)
  83. require.Equal(t, expected[i], actual)
  84. }
  85. }
  86. func TestPriorityString_Invalid(t *testing.T) {
  87. _, err := PriorityString(99)
  88. require.Equal(t, err, errInvalidPriority)
  89. }
  90. func TestShortTopicURL(t *testing.T) {
  91. require.Equal(t, "ntfy.sh/mytopic", ShortTopicURL("https://ntfy.sh/mytopic"))
  92. require.Equal(t, "ntfy.sh/mytopic", ShortTopicURL("http://ntfy.sh/mytopic"))
  93. require.Equal(t, "lalala", ShortTopicURL("lalala"))
  94. }
  95. func TestParseSize_10GSuccess(t *testing.T) {
  96. s, err := ParseSize("10G")
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. require.Equal(t, int64(10*1024*1024*1024), s)
  101. }
  102. func TestParseSize_10MUpperCaseSuccess(t *testing.T) {
  103. s, err := ParseSize("10M")
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. require.Equal(t, int64(10*1024*1024), s)
  108. }
  109. func TestParseSize_10kLowerCaseSuccess(t *testing.T) {
  110. s, err := ParseSize("10k")
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. require.Equal(t, int64(10*1024), s)
  115. }
  116. func TestParseSize_FailureInvalid(t *testing.T) {
  117. _, err := ParseSize("not a size")
  118. if err == nil {
  119. t.Fatalf("expected error, but got none")
  120. }
  121. }
  122. func TestSplitKV(t *testing.T) {
  123. key, value := SplitKV(" key = value ", "=")
  124. require.Equal(t, "key", key)
  125. require.Equal(t, "value", value)
  126. key, value = SplitKV(" value ", "=")
  127. require.Equal(t, "", key)
  128. require.Equal(t, "value", value)
  129. key, value = SplitKV("mykey=value=with=separator ", "=")
  130. require.Equal(t, "mykey", key)
  131. require.Equal(t, "value=with=separator", value)
  132. }
  133. func TestLastString(t *testing.T) {
  134. require.Equal(t, "last", LastString([]string{"first", "second", "last"}, "default"))
  135. require.Equal(t, "default", LastString([]string{}, "default"))
  136. }
  137. func TestQuoteCommand(t *testing.T) {
  138. require.Equal(t, `ls -al "Document Folder"`, QuoteCommand([]string{"ls", "-al", "Document Folder"}))
  139. require.Equal(t, `rsync -av /home/phil/ root@example.com:/home/phil/`, QuoteCommand([]string{"rsync", "-av", "/home/phil/", "root@example.com:/home/phil/"}))
  140. require.Equal(t, `/home/sweet/home "Äöü this is a test" "\a\b"`, QuoteCommand([]string{"/home/sweet/home", "Äöü this is a test", "\\a\\b"}))
  141. }
  142. func TestBasicAuth(t *testing.T) {
  143. require.Equal(t, "Basic cGhpbDpwaGls", BasicAuth("phil", "phil"))
  144. }
  145. func TestBearerAuth(t *testing.T) {
  146. require.Equal(t, "Bearer sometoken", BearerAuth("sometoken"))
  147. }
  148. type testJSON struct {
  149. Name string `json:"name"`
  150. Something int `json:"something"`
  151. }
  152. func TestReadJSON_Success(t *testing.T) {
  153. v, err := UnmarshalJSON[testJSON](io.NopCloser(strings.NewReader(`{"name":"some name","something":99}`)))
  154. require.Nil(t, err)
  155. require.Equal(t, "some name", v.Name)
  156. require.Equal(t, 99, v.Something)
  157. }
  158. func TestReadJSON_Failure(t *testing.T) {
  159. _, err := UnmarshalJSON[testJSON](io.NopCloser(strings.NewReader(`{"na`)))
  160. require.Equal(t, ErrUnmarshalJSON, err)
  161. }
  162. func TestReadJSONWithLimit_Success(t *testing.T) {
  163. v, err := UnmarshalJSONWithLimit[testJSON](io.NopCloser(strings.NewReader(`{"name":"some name","something":99}`)), 100)
  164. require.Nil(t, err)
  165. require.Equal(t, "some name", v.Name)
  166. require.Equal(t, 99, v.Something)
  167. }
  168. func TestReadJSONWithLimit_FailureTooLong(t *testing.T) {
  169. _, err := UnmarshalJSONWithLimit[testJSON](io.NopCloser(strings.NewReader(`{"name":"some name","something":99}`)), 10)
  170. require.Equal(t, ErrTooLargeJSON, err)
  171. }