utils_test.go 7.0 KB

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