utils_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package oss
  2. import (
  3. "strings"
  4. . "gopkg.in/check.v1"
  5. )
  6. type OssUtilsSuite struct{}
  7. var _ = Suite(&OssUtilsSuite{})
  8. func (s *OssUtilsSuite) TestUtilsTime(c *C) {
  9. c.Assert(GetNowSec() > 1448597674, Equals, true)
  10. c.Assert(GetNowNanoSec() > 1448597674000000000, Equals, true)
  11. c.Assert(len(GetNowGMT()), Equals, len("Fri, 27 Nov 2015 04:14:34 GMT"))
  12. }
  13. func (s *OssUtilsSuite) TestUtilsSplitFile(c *C) {
  14. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  15. // Num
  16. parts, err := SplitFileByPartNum(localFile, 4)
  17. c.Assert(err, IsNil)
  18. c.Assert(len(parts), Equals, 4)
  19. testLogger.Println("parts 4:", parts)
  20. for i, part := range parts {
  21. c.Assert(part.Number, Equals, i+1)
  22. c.Assert(part.Offset, Equals, int64(i*120512))
  23. c.Assert(part.Size, Equals, int64(120512))
  24. }
  25. parts, err = SplitFileByPartNum(localFile, 5)
  26. c.Assert(err, IsNil)
  27. c.Assert(len(parts), Equals, 5)
  28. testLogger.Println("parts 5:", parts)
  29. for i, part := range parts {
  30. c.Assert(part.Number, Equals, i+1)
  31. c.Assert(part.Offset, Equals, int64(i*96409))
  32. }
  33. _, err = SplitFileByPartNum(localFile, 10001)
  34. c.Assert(err, NotNil)
  35. _, err = SplitFileByPartNum(localFile, 0)
  36. c.Assert(err, NotNil)
  37. _, err = SplitFileByPartNum(localFile, -1)
  38. c.Assert(err, NotNil)
  39. _, err = SplitFileByPartNum("notexist", 1024)
  40. c.Assert(err, NotNil)
  41. // Size
  42. parts, err = SplitFileByPartSize(localFile, 120512)
  43. c.Assert(err, IsNil)
  44. c.Assert(len(parts), Equals, 4)
  45. testLogger.Println("parts 4:", parts)
  46. for i, part := range parts {
  47. c.Assert(part.Number, Equals, i+1)
  48. c.Assert(part.Offset, Equals, int64(i*120512))
  49. c.Assert(part.Size, Equals, int64(120512))
  50. }
  51. parts, err = SplitFileByPartSize(localFile, 96409)
  52. c.Assert(err, IsNil)
  53. c.Assert(len(parts), Equals, 6)
  54. testLogger.Println("parts 6:", parts)
  55. for i, part := range parts {
  56. c.Assert(part.Number, Equals, i+1)
  57. c.Assert(part.Offset, Equals, int64(i*96409))
  58. }
  59. _, err = SplitFileByPartSize(localFile, 0)
  60. c.Assert(err, NotNil)
  61. _, err = SplitFileByPartSize(localFile, -1)
  62. c.Assert(err, NotNil)
  63. _, err = SplitFileByPartSize(localFile, 10)
  64. c.Assert(err, NotNil)
  65. _, err = SplitFileByPartSize("noexist", 120512)
  66. c.Assert(err, NotNil)
  67. }
  68. func (s *OssUtilsSuite) TestUtilsFileExt(c *C) {
  69. c.Assert(strings.Contains(TypeByExtension("test.txt"), "text/plain"), Equals, true)
  70. c.Assert(TypeByExtension("test.jpg"), Equals, "image/jpeg")
  71. c.Assert(TypeByExtension("test.pdf"), Equals, "application/pdf")
  72. c.Assert(TypeByExtension("test"), Equals, "")
  73. c.Assert(strings.Contains(TypeByExtension("/root/dir/test.txt"), "text/plain"), Equals, true)
  74. c.Assert(strings.Contains(TypeByExtension("root/dir/test.txt"), "text/plain"), Equals, true)
  75. c.Assert(strings.Contains(TypeByExtension("root\\dir\\test.txt"), "text/plain"), Equals, true)
  76. c.Assert(strings.Contains(TypeByExtension("D:\\work\\dir\\test.txt"), "text/plain"), Equals, true)
  77. }
  78. func (s *OssUtilsSuite) TestGetPartEnd(c *C) {
  79. end := GetPartEnd(3, 10, 3)
  80. c.Assert(end, Equals, int64(5))
  81. end = GetPartEnd(9, 10, 3)
  82. c.Assert(end, Equals, int64(9))
  83. end = GetPartEnd(7, 10, 3)
  84. c.Assert(end, Equals, int64(9))
  85. }
  86. func (s *OssUtilsSuite) TestParseRange(c *C) {
  87. // InvalidRange bytes==M-N
  88. _, err := ParseRange("bytes==M-N")
  89. c.Assert(err, NotNil)
  90. c.Assert(err.Error(), Equals, "InvalidRange bytes==M-N")
  91. // InvalidRange ranges=M-N
  92. _, err = ParseRange("ranges=M-N")
  93. c.Assert(err, NotNil)
  94. c.Assert(err.Error(), Equals, "InvalidRange ranges=M-N")
  95. // InvalidRange ranges=M-N
  96. _, err = ParseRange("bytes=M-N")
  97. c.Assert(err, NotNil)
  98. c.Assert(err.Error(), Equals, "InvalidRange bytes=M-N")
  99. // InvalidRange ranges=M-
  100. _, err = ParseRange("bytes=M-")
  101. c.Assert(err, NotNil)
  102. c.Assert(err.Error(), Equals, "InvalidRange bytes=M-")
  103. // InvalidRange ranges=-N
  104. _, err = ParseRange("bytes=-N")
  105. c.Assert(err, NotNil)
  106. c.Assert(err.Error(), Equals, "InvalidRange bytes=-N")
  107. // InvalidRange ranges=-0
  108. _, err = ParseRange("bytes=-0")
  109. c.Assert(err, NotNil)
  110. c.Assert(err.Error(), Equals, "InvalidRange bytes=-0")
  111. // InvalidRange bytes=1-2-3
  112. _, err = ParseRange("bytes=1-2-3")
  113. c.Assert(err, NotNil)
  114. c.Assert(err.Error(), Equals, "InvalidRange bytes=1-2-3")
  115. // InvalidRange bytes=1-N
  116. _, err = ParseRange("bytes=1-N")
  117. c.Assert(err, NotNil)
  118. c.Assert(err.Error(), Equals, "InvalidRange bytes=1-N")
  119. // Ranges=M-N
  120. ur, err := ParseRange("bytes=1024-4096")
  121. c.Assert(err, IsNil)
  122. c.Assert(ur.Start, Equals, (int64)(1024))
  123. c.Assert(ur.End, Equals, (int64)(4096))
  124. c.Assert(ur.HasStart, Equals, true)
  125. c.Assert(ur.HasEnd, Equals, true)
  126. // Ranges=M-N,X-Y
  127. ur, err = ParseRange("bytes=1024-4096,2048-4096")
  128. c.Assert(err, IsNil)
  129. c.Assert(ur.Start, Equals, (int64)(1024))
  130. c.Assert(ur.End, Equals, (int64)(4096))
  131. c.Assert(ur.HasStart, Equals, true)
  132. c.Assert(ur.HasEnd, Equals, true)
  133. // Ranges=M-
  134. ur, err = ParseRange("bytes=1024-")
  135. c.Assert(err, IsNil)
  136. c.Assert(ur.Start, Equals, (int64)(1024))
  137. c.Assert(ur.End, Equals, (int64)(0))
  138. c.Assert(ur.HasStart, Equals, true)
  139. c.Assert(ur.HasEnd, Equals, false)
  140. // Ranges=-N
  141. ur, err = ParseRange("bytes=-4096")
  142. c.Assert(err, IsNil)
  143. c.Assert(ur.Start, Equals, (int64)(0))
  144. c.Assert(ur.End, Equals, (int64)(4096))
  145. c.Assert(ur.HasStart, Equals, false)
  146. c.Assert(ur.HasEnd, Equals, true)
  147. }
  148. func (s *OssUtilsSuite) TestAdjustRange(c *C) {
  149. // Nil
  150. start, end := AdjustRange(nil, 8192)
  151. c.Assert(start, Equals, (int64)(0))
  152. c.Assert(end, Equals, (int64)(8192))
  153. // 1024-4096
  154. ur := &UnpackedRange{true, true, 1024, 4095}
  155. start, end = AdjustRange(ur, 8192)
  156. c.Assert(start, Equals, (int64)(1024))
  157. c.Assert(end, Equals, (int64)(4096))
  158. // 1024-
  159. ur = &UnpackedRange{true, false, 1024, 4096}
  160. start, end = AdjustRange(ur, 8192)
  161. c.Assert(start, Equals, (int64)(1024))
  162. c.Assert(end, Equals, (int64)(8192))
  163. // -4096
  164. ur = &UnpackedRange{false, true, 1024, 4096}
  165. start, end = AdjustRange(ur, 8192)
  166. c.Assert(start, Equals, (int64)(4096))
  167. c.Assert(end, Equals, (int64)(8192))
  168. // Invalid range 4096-1024
  169. ur = &UnpackedRange{true, true, 4096, 1024}
  170. start, end = AdjustRange(ur, 8192)
  171. c.Assert(start, Equals, (int64)(0))
  172. c.Assert(end, Equals, (int64)(8192))
  173. // Invalid range -1-
  174. ur = &UnpackedRange{true, false, -1, 0}
  175. start, end = AdjustRange(ur, 8192)
  176. c.Assert(start, Equals, (int64)(0))
  177. c.Assert(end, Equals, (int64)(8192))
  178. // Invalid range -9999
  179. ur = &UnpackedRange{false, true, 0, 9999}
  180. start, end = AdjustRange(ur, 8192)
  181. c.Assert(start, Equals, (int64)(0))
  182. c.Assert(end, Equals, (int64)(8192))
  183. }
  184. func (s *OssUtilsSuite) TestUtilCheckBucketName(c *C) {
  185. err := CheckBucketName("a")
  186. c.Assert(err, NotNil)
  187. err = CheckBucketName("a11111111111111111111111111111nbbbbbbbbbbbbbbbbbbbbbbbbbbbqqqqqqqqqqqqqqqqqqqq")
  188. c.Assert(err, NotNil)
  189. err = CheckBucketName("-abcd")
  190. c.Assert(err, NotNil)
  191. err = CheckBucketName("abcd-")
  192. c.Assert(err, NotNil)
  193. err = CheckBucketName("abcD")
  194. c.Assert(err, NotNil)
  195. err = CheckBucketName("abc 1")
  196. c.Assert(err, NotNil)
  197. err = CheckBucketName("abc&1")
  198. c.Assert(err, NotNil)
  199. err = CheckBucketName("abc-1")
  200. c.Assert(err, IsNil)
  201. err = CheckBucketName("1bc-1")
  202. c.Assert(err, IsNil)
  203. err = CheckBucketName("111-1")
  204. c.Assert(err, IsNil)
  205. err = CheckBucketName("abc123-def1")
  206. c.Assert(err, IsNil)
  207. }