download_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package oss
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "time"
  7. . "gopkg.in/check.v1"
  8. )
  9. type OssDownloadSuite struct {
  10. client *Client
  11. bucket *Bucket
  12. }
  13. var _ = Suite(&OssDownloadSuite{})
  14. // SetUpSuite runs once when the suite starts running
  15. func (s *OssDownloadSuite) SetUpSuite(c *C) {
  16. client, err := New(endpoint, accessID, accessKey)
  17. c.Assert(err, IsNil)
  18. s.client = client
  19. s.client.CreateBucket(bucketName)
  20. time.Sleep(5 * time.Second)
  21. bucket, err := s.client.Bucket(bucketName)
  22. c.Assert(err, IsNil)
  23. s.bucket = bucket
  24. testLogger.Println("test download started")
  25. }
  26. // TearDownSuite runs before each test or benchmark starts running
  27. func (s *OssDownloadSuite) TearDownSuite(c *C) {
  28. // Delete part
  29. lmur, err := s.bucket.ListMultipartUploads()
  30. c.Assert(err, IsNil)
  31. for _, upload := range lmur.Uploads {
  32. var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
  33. Key: upload.Key, UploadID: upload.UploadID}
  34. err = s.bucket.AbortMultipartUpload(imur)
  35. c.Assert(err, IsNil)
  36. }
  37. // Delete objects
  38. lor, err := s.bucket.ListObjects()
  39. c.Assert(err, IsNil)
  40. for _, object := range lor.Objects {
  41. err = s.bucket.DeleteObject(object.Key)
  42. c.Assert(err, IsNil)
  43. }
  44. testLogger.Println("test download completed")
  45. }
  46. // SetUpTest runs after each test or benchmark runs
  47. func (s *OssDownloadSuite) SetUpTest(c *C) {
  48. err := removeTempFiles("../oss", ".jpg")
  49. c.Assert(err, IsNil)
  50. }
  51. // TearDownTest runs once after all tests or benchmarks have finished running
  52. func (s *OssDownloadSuite) TearDownTest(c *C) {
  53. err := removeTempFiles("../oss", ".jpg")
  54. c.Assert(err, IsNil)
  55. err = removeTempFiles("../oss", ".temp")
  56. c.Assert(err, IsNil)
  57. }
  58. // TestDownloadRoutineWithoutRecovery multipart downloads without checkpoint
  59. func (s *OssDownloadSuite) TestDownloadRoutineWithoutRecovery(c *C) {
  60. objectName := objectNamePrefix + "tdrwr"
  61. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  62. newFile := "down-new-file.jpg"
  63. // Upload a file
  64. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  65. c.Assert(err, IsNil)
  66. // Download the file by default
  67. err = s.bucket.DownloadFile(objectName, newFile, 100*1024)
  68. c.Assert(err, IsNil)
  69. // Check
  70. eq, err := compareFiles(fileName, newFile)
  71. c.Assert(err, IsNil)
  72. c.Assert(eq, Equals, true)
  73. // Use 2 coroutines to download the file and total parts count is 5
  74. os.Remove(newFile)
  75. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(2))
  76. c.Assert(err, IsNil)
  77. // Check
  78. eq, err = compareFiles(fileName, newFile)
  79. c.Assert(err, IsNil)
  80. c.Assert(eq, Equals, true)
  81. // Use 5 coroutines to download the file and the total parts count is 5.
  82. os.Remove(newFile)
  83. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(5))
  84. c.Assert(err, IsNil)
  85. // Check
  86. eq, err = compareFiles(fileName, newFile)
  87. c.Assert(err, IsNil)
  88. c.Assert(eq, Equals, true)
  89. // Use 10 coroutines to download the file and the total parts count is 5.
  90. os.Remove(newFile)
  91. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(10))
  92. c.Assert(err, IsNil)
  93. // Check
  94. eq, err = compareFiles(fileName, newFile)
  95. c.Assert(err, IsNil)
  96. c.Assert(eq, Equals, true)
  97. err = s.bucket.DeleteObject(objectName)
  98. c.Assert(err, IsNil)
  99. }
  100. // DownErrorHooker requests hook by downloadPart
  101. func DownErrorHooker(part downloadPart) error {
  102. if part.Index == 4 {
  103. time.Sleep(time.Second)
  104. return fmt.Errorf("ErrorHooker")
  105. }
  106. return nil
  107. }
  108. // TestDownloadRoutineWithRecovery multi-routine resumable download
  109. func (s *OssDownloadSuite) TestDownloadRoutineWithRecovery(c *C) {
  110. objectName := objectNamePrefix + "tdrtr"
  111. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  112. newFile := "down-new-file-2.jpg"
  113. // Upload a file
  114. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  115. c.Assert(err, IsNil)
  116. // Download a file with default checkpoint
  117. downloadPartHooker = DownErrorHooker
  118. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, ""))
  119. c.Assert(err, NotNil)
  120. c.Assert(err.Error(), Equals, "ErrorHooker")
  121. downloadPartHooker = defaultDownloadPartHook
  122. // Check
  123. dcp := downloadCheckpoint{}
  124. err = dcp.load(newFile + ".cp")
  125. c.Assert(err, IsNil)
  126. c.Assert(dcp.Magic, Equals, downloadCpMagic)
  127. c.Assert(len(dcp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  128. c.Assert(dcp.FilePath, Equals, newFile)
  129. c.Assert(dcp.ObjStat.Size, Equals, int64(482048))
  130. c.Assert(len(dcp.ObjStat.LastModified) > 0, Equals, true)
  131. c.Assert(dcp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  132. c.Assert(dcp.Object, Equals, objectName)
  133. c.Assert(len(dcp.Parts), Equals, 5)
  134. c.Assert(len(dcp.todoParts()), Equals, 1)
  135. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, ""))
  136. c.Assert(err, IsNil)
  137. err = dcp.load(newFile + ".cp")
  138. c.Assert(err, NotNil)
  139. eq, err := compareFiles(fileName, newFile)
  140. c.Assert(err, IsNil)
  141. c.Assert(eq, Equals, true)
  142. // Resumable download with checkpoint
  143. os.Remove(newFile)
  144. downloadPartHooker = DownErrorHooker
  145. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, objectName+".cp"))
  146. c.Assert(err, NotNil)
  147. c.Assert(err.Error(), Equals, "ErrorHooker")
  148. downloadPartHooker = defaultDownloadPartHook
  149. // Check
  150. dcp = downloadCheckpoint{}
  151. err = dcp.load(objectName + ".cp")
  152. c.Assert(err, IsNil)
  153. c.Assert(dcp.Magic, Equals, downloadCpMagic)
  154. c.Assert(len(dcp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  155. c.Assert(dcp.FilePath, Equals, newFile)
  156. c.Assert(dcp.ObjStat.Size, Equals, int64(482048))
  157. c.Assert(len(dcp.ObjStat.LastModified) > 0, Equals, true)
  158. c.Assert(dcp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  159. c.Assert(dcp.Object, Equals, objectName)
  160. c.Assert(len(dcp.Parts), Equals, 5)
  161. c.Assert(len(dcp.todoParts()), Equals, 1)
  162. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, objectName+".cp"))
  163. c.Assert(err, IsNil)
  164. err = dcp.load(objectName + ".cp")
  165. c.Assert(err, NotNil)
  166. eq, err = compareFiles(fileName, newFile)
  167. c.Assert(err, IsNil)
  168. c.Assert(eq, Equals, true)
  169. // Resumable download with checkpoint at a time. No error is expected in the download procedure.
  170. os.Remove(newFile)
  171. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, ""))
  172. c.Assert(err, IsNil)
  173. err = dcp.load(newFile + ".cp")
  174. c.Assert(err, NotNil)
  175. eq, err = compareFiles(fileName, newFile)
  176. c.Assert(err, IsNil)
  177. c.Assert(eq, Equals, true)
  178. // Resumable download with checkpoint at a time. No error is expected in the download procedure.
  179. os.Remove(newFile)
  180. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(10), Checkpoint(true, ""))
  181. c.Assert(err, IsNil)
  182. err = dcp.load(newFile + ".cp")
  183. c.Assert(err, NotNil)
  184. eq, err = compareFiles(fileName, newFile)
  185. c.Assert(err, IsNil)
  186. c.Assert(eq, Equals, true)
  187. err = s.bucket.DeleteObject(objectName)
  188. c.Assert(err, IsNil)
  189. }
  190. // TestDownloadOption options
  191. func (s *OssDownloadSuite) TestDownloadOption(c *C) {
  192. objectName := objectNamePrefix + "tdmo"
  193. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  194. newFile := "down-new-file-3.jpg"
  195. // Upload the file
  196. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  197. c.Assert(err, IsNil)
  198. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  199. c.Assert(err, IsNil)
  200. // IfMatch
  201. os.Remove(newFile)
  202. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfMatch(meta.Get("Etag")))
  203. c.Assert(err, IsNil)
  204. eq, err := compareFiles(fileName, newFile)
  205. c.Assert(err, IsNil)
  206. c.Assert(eq, Equals, true)
  207. // IfNoneMatch
  208. os.Remove(newFile)
  209. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfNoneMatch(meta.Get("Etag")))
  210. c.Assert(err, NotNil)
  211. // IfMatch
  212. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Checkpoint(true, ""), IfMatch(meta.Get("Etag")))
  213. c.Assert(err, IsNil)
  214. eq, err = compareFiles(fileName, newFile)
  215. c.Assert(err, IsNil)
  216. c.Assert(eq, Equals, true)
  217. // IfNoneMatch
  218. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Checkpoint(true, ""), IfNoneMatch(meta.Get("Etag")))
  219. c.Assert(err, NotNil)
  220. }
  221. // TestDownloadObjectChange tests the file is updated during the upload
  222. func (s *OssDownloadSuite) TestDownloadObjectChange(c *C) {
  223. objectName := objectNamePrefix + "tdloc"
  224. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  225. newFile := "down-new-file-4.jpg"
  226. // Upload a file
  227. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  228. c.Assert(err, IsNil)
  229. // Download with default checkpoint
  230. downloadPartHooker = DownErrorHooker
  231. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, ""))
  232. c.Assert(err, NotNil)
  233. c.Assert(err.Error(), Equals, "ErrorHooker")
  234. downloadPartHooker = defaultDownloadPartHook
  235. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  236. c.Assert(err, IsNil)
  237. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, ""))
  238. c.Assert(err, IsNil)
  239. eq, err := compareFiles(fileName, newFile)
  240. c.Assert(err, IsNil)
  241. c.Assert(eq, Equals, true)
  242. }
  243. // TestDownloadNegative tests downloading negative
  244. func (s *OssDownloadSuite) TestDownloadNegative(c *C) {
  245. objectName := objectNamePrefix + "tdn"
  246. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  247. newFile := "down-new-file-3.jpg"
  248. // Upload a file
  249. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  250. c.Assert(err, IsNil)
  251. // Worker routine error
  252. downloadPartHooker = DownErrorHooker
  253. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(2))
  254. c.Assert(err, NotNil)
  255. c.Assert(err.Error(), Equals, "ErrorHooker")
  256. downloadPartHooker = defaultDownloadPartHook
  257. // Local file does not exist
  258. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Routines(2))
  259. c.Assert(err, NotNil)
  260. // Invalid part size
  261. err = s.bucket.DownloadFile(objectName, newFile, 0, Routines(2))
  262. c.Assert(err, NotNil)
  263. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Routines(2))
  264. c.Assert(err, IsNil)
  265. err = s.bucket.DeleteObject(objectName)
  266. c.Assert(err, IsNil)
  267. // Local file does not exist
  268. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Checkpoint(true, ""))
  269. c.Assert(err, NotNil)
  270. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Routines(2), Checkpoint(true, ""))
  271. c.Assert(err, NotNil)
  272. // Invalid part size
  273. err = s.bucket.DownloadFile(objectName, newFile, -1, Checkpoint(true, ""))
  274. c.Assert(err, NotNil)
  275. err = s.bucket.DownloadFile(objectName, newFile, 0, Routines(2), Checkpoint(true, ""))
  276. c.Assert(err, NotNil)
  277. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Checkpoint(true, ""))
  278. c.Assert(err, NotNil)
  279. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Routines(2), Checkpoint(true, ""))
  280. c.Assert(err, NotNil)
  281. }
  282. // TestDownloadWithRange tests concurrent downloading with range specified and checkpoint enabled
  283. func (s *OssDownloadSuite) TestDownloadWithRange(c *C) {
  284. objectName := objectNamePrefix + "tdwr"
  285. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  286. newFile := "down-new-file-tdwr.jpg"
  287. newFileGet := "down-new-file-tdwr-2.jpg"
  288. // Upload a file
  289. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  290. c.Assert(err, IsNil)
  291. fileSize, err := getFileSize(fileName)
  292. c.Assert(err, IsNil)
  293. // Download with range, from 1024 to 4096
  294. os.Remove(newFile)
  295. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Range(1024, 4095))
  296. c.Assert(err, IsNil)
  297. // Check
  298. eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  299. c.Assert(err, IsNil)
  300. c.Assert(eq, Equals, true)
  301. os.Remove(newFileGet)
  302. err = s.bucket.GetObjectToFile(objectName, newFileGet, Range(1024, 4095))
  303. c.Assert(err, IsNil)
  304. // Compare get and download
  305. eq, err = compareFiles(newFile, newFileGet)
  306. c.Assert(err, IsNil)
  307. c.Assert(eq, Equals, true)
  308. // Download with range, from 1024 to 4096
  309. os.Remove(newFile)
  310. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), NormalizedRange("1024-4095"))
  311. c.Assert(err, IsNil)
  312. // Check
  313. eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  314. c.Assert(err, IsNil)
  315. c.Assert(eq, Equals, true)
  316. os.Remove(newFileGet)
  317. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("1024-4095"))
  318. c.Assert(err, IsNil)
  319. // Compare get and download
  320. eq, err = compareFiles(newFile, newFileGet)
  321. c.Assert(err, IsNil)
  322. c.Assert(eq, Equals, true)
  323. // Download with range, from 2048 to the end
  324. os.Remove(newFile)
  325. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), NormalizedRange("2048-"))
  326. c.Assert(err, IsNil)
  327. // Check
  328. eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
  329. c.Assert(err, IsNil)
  330. c.Assert(eq, Equals, true)
  331. os.Remove(newFileGet)
  332. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("2048-"))
  333. c.Assert(err, IsNil)
  334. // Compare get and download
  335. eq, err = compareFiles(newFile, newFileGet)
  336. c.Assert(err, IsNil)
  337. c.Assert(eq, Equals, true)
  338. // Download with range, the last 4096
  339. os.Remove(newFile)
  340. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), NormalizedRange("-4096"))
  341. c.Assert(err, IsNil)
  342. // Check
  343. eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
  344. c.Assert(err, IsNil)
  345. c.Assert(eq, Equals, true)
  346. os.Remove(newFileGet)
  347. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("-4096"))
  348. c.Assert(err, IsNil)
  349. // Compare get and download
  350. eq, err = compareFiles(newFile, newFileGet)
  351. c.Assert(err, IsNil)
  352. c.Assert(eq, Equals, true)
  353. err = s.bucket.DeleteObject(objectName)
  354. c.Assert(err, IsNil)
  355. }
  356. // TestDownloadWithCheckoutAndRange tests concurrent downloading with range specified and checkpoint enabled
  357. func (s *OssDownloadSuite) TestDownloadWithCheckoutAndRange(c *C) {
  358. objectName := objectNamePrefix + "tdwcr"
  359. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  360. newFile := "down-new-file-tdwcr.jpg"
  361. newFileGet := "down-new-file-tdwcr-2.jpg"
  362. // Upload a file
  363. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  364. c.Assert(err, IsNil)
  365. fileSize, err := getFileSize(fileName)
  366. c.Assert(err, IsNil)
  367. // Download with range, from 1024 to 4096
  368. os.Remove(newFile)
  369. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Checkpoint(true, ""), Range(1024, 4095))
  370. c.Assert(err, IsNil)
  371. // Check
  372. eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  373. c.Assert(err, IsNil)
  374. c.Assert(eq, Equals, true)
  375. os.Remove(newFileGet)
  376. err = s.bucket.GetObjectToFile(objectName, newFileGet, Range(1024, 4095))
  377. c.Assert(err, IsNil)
  378. // Compare get and download
  379. eq, err = compareFiles(newFile, newFileGet)
  380. c.Assert(err, IsNil)
  381. c.Assert(eq, Equals, true)
  382. // Download with range, from 1024 to 4096
  383. os.Remove(newFile)
  384. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), Checkpoint(true, ""), NormalizedRange("1024-4095"))
  385. c.Assert(err, IsNil)
  386. // Check
  387. eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  388. c.Assert(err, IsNil)
  389. c.Assert(eq, Equals, true)
  390. os.Remove(newFileGet)
  391. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("1024-4095"))
  392. c.Assert(err, IsNil)
  393. // Compare get and download
  394. eq, err = compareFiles(newFile, newFileGet)
  395. c.Assert(err, IsNil)
  396. c.Assert(eq, Equals, true)
  397. // Download with range, from 2048 to the end
  398. os.Remove(newFile)
  399. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), Checkpoint(true, ""), NormalizedRange("2048-"))
  400. c.Assert(err, IsNil)
  401. // Check
  402. eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
  403. c.Assert(err, IsNil)
  404. c.Assert(eq, Equals, true)
  405. os.Remove(newFileGet)
  406. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("2048-"))
  407. c.Assert(err, IsNil)
  408. // Compare get and download
  409. eq, err = compareFiles(newFile, newFileGet)
  410. c.Assert(err, IsNil)
  411. c.Assert(eq, Equals, true)
  412. // Download with range, the last 4096 bytes
  413. os.Remove(newFile)
  414. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), Checkpoint(true, ""), NormalizedRange("-4096"))
  415. c.Assert(err, IsNil)
  416. // Check
  417. eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
  418. c.Assert(err, IsNil)
  419. c.Assert(eq, Equals, true)
  420. os.Remove(newFileGet)
  421. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("-4096"))
  422. c.Assert(err, IsNil)
  423. // Compare get and download
  424. eq, err = compareFiles(newFile, newFileGet)
  425. c.Assert(err, IsNil)
  426. c.Assert(eq, Equals, true)
  427. err = s.bucket.DeleteObject(objectName)
  428. c.Assert(err, IsNil)
  429. }
  430. // TestCombineCRCInDownloadParts tests combineCRCInParts
  431. func (s *OssDownloadSuite) TestCombineCRCInDownloadParts(c *C) {
  432. crc := combineCRCInParts(nil)
  433. c.Assert(crc == 0, Equals, true)
  434. crc = combineCRCInParts(make([]downloadPart, 0))
  435. c.Assert(crc == 0, Equals, true)
  436. parts := make([]downloadPart, 1)
  437. parts[0].CRC64 = 10278880121275185425
  438. crc = combineCRCInParts(parts)
  439. c.Assert(crc == 10278880121275185425, Equals, true)
  440. parts = make([]downloadPart, 2)
  441. parts[0].CRC64 = 6748440630437108969
  442. parts[0].Start = 0
  443. parts[0].End = 4
  444. parts[1].CRC64 = 10278880121275185425
  445. parts[1].Start = 5
  446. parts[1].End = 8
  447. crc = combineCRCInParts(parts)
  448. c.Assert(crc == 11051210869376104954, Equals, true)
  449. }
  450. func getFileSize(fileName string) (int64, error) {
  451. file, err := os.Open(fileName)
  452. if err != nil {
  453. return 0, err
  454. }
  455. defer file.Close()
  456. stat, err := file.Stat()
  457. if err != nil {
  458. return 0, err
  459. }
  460. return stat.Size(), nil
  461. }
  462. // compareFilesWithRange compares the content between fileL and fileR with specified range
  463. func compareFilesWithRange(fileL string, offsetL int64, fileR string, offsetR int64, size int64) (bool, error) {
  464. finL, err := os.Open(fileL)
  465. if err != nil {
  466. return false, err
  467. }
  468. defer finL.Close()
  469. finL.Seek(offsetL, os.SEEK_SET)
  470. finR, err := os.Open(fileR)
  471. if err != nil {
  472. return false, err
  473. }
  474. defer finR.Close()
  475. finR.Seek(offsetR, os.SEEK_SET)
  476. statL, err := finL.Stat()
  477. if err != nil {
  478. return false, err
  479. }
  480. statR, err := finR.Stat()
  481. if err != nil {
  482. return false, err
  483. }
  484. if (offsetL+size > statL.Size()) || (offsetR+size > statR.Size()) {
  485. return false, nil
  486. }
  487. part := statL.Size() - offsetL
  488. if part > 16*1024 {
  489. part = 16 * 1024
  490. }
  491. bufL := make([]byte, part)
  492. bufR := make([]byte, part)
  493. for readN := int64(0); readN < size; {
  494. n, _ := finL.Read(bufL)
  495. if 0 == n {
  496. break
  497. }
  498. n, _ = finR.Read(bufR)
  499. if 0 == n {
  500. break
  501. }
  502. tailer := part
  503. if tailer > size-readN {
  504. tailer = size - readN
  505. }
  506. readN += tailer
  507. if !bytes.Equal(bufL[0:tailer], bufR[0:tailer]) {
  508. return false, nil
  509. }
  510. }
  511. return true, nil
  512. }