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

upload_test.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package upload
  4. import (
  5. "bytes"
  6. "compress/gzip"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestUpload(t *testing.T) {
  11. testContent := []byte(`This is a plain text file.`)
  12. var b bytes.Buffer
  13. w := gzip.NewWriter(&b)
  14. w.Write(testContent)
  15. w.Close()
  16. kases := []struct {
  17. data []byte
  18. fileName string
  19. allowedTypes string
  20. err error
  21. }{
  22. {
  23. data: testContent,
  24. fileName: "test.txt",
  25. allowedTypes: "",
  26. err: nil,
  27. },
  28. {
  29. data: testContent,
  30. fileName: "dir/test.txt",
  31. allowedTypes: "",
  32. err: nil,
  33. },
  34. {
  35. data: testContent,
  36. fileName: "../../../test.txt",
  37. allowedTypes: "",
  38. err: nil,
  39. },
  40. {
  41. data: testContent,
  42. fileName: "test.txt",
  43. allowedTypes: "",
  44. err: nil,
  45. },
  46. {
  47. data: testContent,
  48. fileName: "test.txt",
  49. allowedTypes: ",",
  50. err: nil,
  51. },
  52. {
  53. data: testContent,
  54. fileName: "test.txt",
  55. allowedTypes: "|",
  56. err: nil,
  57. },
  58. {
  59. data: testContent,
  60. fileName: "test.txt",
  61. allowedTypes: "*/*",
  62. err: nil,
  63. },
  64. {
  65. data: testContent,
  66. fileName: "test.txt",
  67. allowedTypes: "*/*,",
  68. err: nil,
  69. },
  70. {
  71. data: testContent,
  72. fileName: "test.txt",
  73. allowedTypes: "*/*|",
  74. err: nil,
  75. },
  76. {
  77. data: testContent,
  78. fileName: "test.txt",
  79. allowedTypes: "text/plain",
  80. err: nil,
  81. },
  82. {
  83. data: testContent,
  84. fileName: "dir/test.txt",
  85. allowedTypes: "text/plain",
  86. err: nil,
  87. },
  88. {
  89. data: testContent,
  90. fileName: "/dir.txt/test.js",
  91. allowedTypes: ".js",
  92. err: nil,
  93. },
  94. {
  95. data: testContent,
  96. fileName: "test.txt",
  97. allowedTypes: " text/plain ",
  98. err: nil,
  99. },
  100. {
  101. data: testContent,
  102. fileName: "test.txt",
  103. allowedTypes: ".txt",
  104. err: nil,
  105. },
  106. {
  107. data: testContent,
  108. fileName: "test.txt",
  109. allowedTypes: " .txt,.js",
  110. err: nil,
  111. },
  112. {
  113. data: testContent,
  114. fileName: "test.txt",
  115. allowedTypes: " .txt|.js",
  116. err: nil,
  117. },
  118. {
  119. data: testContent,
  120. fileName: "../../test.txt",
  121. allowedTypes: " .txt|.js",
  122. err: nil,
  123. },
  124. {
  125. data: testContent,
  126. fileName: "test.txt",
  127. allowedTypes: " .txt ,.js ",
  128. err: nil,
  129. },
  130. {
  131. data: testContent,
  132. fileName: "test.txt",
  133. allowedTypes: "text/plain, .txt",
  134. err: nil,
  135. },
  136. {
  137. data: testContent,
  138. fileName: "test.txt",
  139. allowedTypes: "text/*",
  140. err: nil,
  141. },
  142. {
  143. data: testContent,
  144. fileName: "test.txt",
  145. allowedTypes: "text/*,.js",
  146. err: nil,
  147. },
  148. {
  149. data: testContent,
  150. fileName: "test.txt",
  151. allowedTypes: "text/**",
  152. err: ErrFileTypeForbidden{"text/plain; charset=utf-8"},
  153. },
  154. {
  155. data: testContent,
  156. fileName: "test.txt",
  157. allowedTypes: "application/x-gzip",
  158. err: ErrFileTypeForbidden{"text/plain; charset=utf-8"},
  159. },
  160. {
  161. data: testContent,
  162. fileName: "test.txt",
  163. allowedTypes: ".zip",
  164. err: ErrFileTypeForbidden{"text/plain; charset=utf-8"},
  165. },
  166. {
  167. data: testContent,
  168. fileName: "test.txt",
  169. allowedTypes: ".zip,.txtx",
  170. err: ErrFileTypeForbidden{"text/plain; charset=utf-8"},
  171. },
  172. {
  173. data: testContent,
  174. fileName: "test.txt",
  175. allowedTypes: ".zip|.txtx",
  176. err: ErrFileTypeForbidden{"text/plain; charset=utf-8"},
  177. },
  178. {
  179. data: b.Bytes(),
  180. fileName: "test.txt",
  181. allowedTypes: "application/x-gzip",
  182. err: nil,
  183. },
  184. }
  185. for _, kase := range kases {
  186. assert.Equal(t, kase.err, Verify(kase.data, kase.fileName, kase.allowedTypes))
  187. }
  188. }