download_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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, newFile+".cp"))
  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, newFile+".cp"))
  136. c.Assert(err, IsNil)
  137. //download success, checkpoint file has been deleted
  138. err = dcp.load(newFile + ".cp")
  139. c.Assert(err, NotNil)
  140. eq, err := compareFiles(fileName, newFile)
  141. c.Assert(err, IsNil)
  142. c.Assert(eq, Equals, true)
  143. // Resumable download with empty checkpoint file path
  144. downloadPartHooker = DownErrorHooker
  145. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, ""))
  146. c.Assert(err, NotNil)
  147. c.Assert(err.Error(), Equals, "ErrorHooker")
  148. downloadPartHooker = defaultDownloadPartHook
  149. dcp = downloadCheckpoint{}
  150. err = dcp.load(newFile + ".cp")
  151. c.Assert(err, NotNil)
  152. // Resumable download with checkpoint dir
  153. os.Remove(newFile)
  154. downloadPartHooker = DownErrorHooker
  155. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, CheckpointDir(true, "./"))
  156. c.Assert(err, NotNil)
  157. c.Assert(err.Error(), Equals, "ErrorHooker")
  158. downloadPartHooker = defaultDownloadPartHook
  159. // Check
  160. dcp = downloadCheckpoint{}
  161. cpConf := cpConfig{IsEnable: true, DirPath: "./"}
  162. cpFilePath := getDownloadCpFilePath(&cpConf, s.bucket.BucketName, objectName, newFile)
  163. err = dcp.load(cpFilePath)
  164. c.Assert(err, IsNil)
  165. c.Assert(dcp.Magic, Equals, downloadCpMagic)
  166. c.Assert(len(dcp.MD5), Equals, len("LC34jZU5xK4hlxi3Qn3XGQ=="))
  167. c.Assert(dcp.FilePath, Equals, newFile)
  168. c.Assert(dcp.ObjStat.Size, Equals, int64(482048))
  169. c.Assert(len(dcp.ObjStat.LastModified) > 0, Equals, true)
  170. c.Assert(dcp.ObjStat.Etag, Equals, "\"2351E662233817A7AE974D8C5B0876DD-5\"")
  171. c.Assert(dcp.Object, Equals, objectName)
  172. c.Assert(len(dcp.Parts), Equals, 5)
  173. c.Assert(len(dcp.todoParts()), Equals, 1)
  174. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, CheckpointDir(true, "./"))
  175. c.Assert(err, IsNil)
  176. //download success, checkpoint file has been deleted
  177. err = dcp.load(cpFilePath)
  178. c.Assert(err, NotNil)
  179. eq, err = compareFiles(fileName, newFile)
  180. c.Assert(err, IsNil)
  181. c.Assert(eq, Equals, true)
  182. // Resumable download with checkpoint at a time. No error is expected in the download procedure.
  183. os.Remove(newFile)
  184. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
  185. c.Assert(err, IsNil)
  186. err = dcp.load(newFile + ".cp")
  187. c.Assert(err, NotNil)
  188. eq, err = compareFiles(fileName, newFile)
  189. c.Assert(err, IsNil)
  190. c.Assert(eq, Equals, true)
  191. // Resumable download with checkpoint at a time. No error is expected in the download procedure.
  192. os.Remove(newFile)
  193. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(10), Checkpoint(true, newFile+".cp"))
  194. c.Assert(err, IsNil)
  195. err = dcp.load(newFile + ".cp")
  196. c.Assert(err, NotNil)
  197. eq, err = compareFiles(fileName, newFile)
  198. c.Assert(err, IsNil)
  199. c.Assert(eq, Equals, true)
  200. err = s.bucket.DeleteObject(objectName)
  201. c.Assert(err, IsNil)
  202. }
  203. // TestDownloadOption options
  204. func (s *OssDownloadSuite) TestDownloadOption(c *C) {
  205. objectName := objectNamePrefix + "tdmo"
  206. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  207. newFile := "down-new-file-3.jpg"
  208. // Upload the file
  209. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  210. c.Assert(err, IsNil)
  211. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  212. c.Assert(err, IsNil)
  213. // IfMatch
  214. os.Remove(newFile)
  215. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfMatch(meta.Get("Etag")))
  216. c.Assert(err, IsNil)
  217. eq, err := compareFiles(fileName, newFile)
  218. c.Assert(err, IsNil)
  219. c.Assert(eq, Equals, true)
  220. // IfNoneMatch
  221. os.Remove(newFile)
  222. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfNoneMatch(meta.Get("Etag")))
  223. c.Assert(err, NotNil)
  224. // IfMatch
  225. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfMatch(meta.Get("Etag")))
  226. c.Assert(err, IsNil)
  227. eq, err = compareFiles(fileName, newFile)
  228. c.Assert(err, IsNil)
  229. c.Assert(eq, Equals, true)
  230. // IfNoneMatch
  231. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), IfNoneMatch(meta.Get("Etag")))
  232. c.Assert(err, NotNil)
  233. }
  234. // TestDownloadObjectChange tests the file is updated during the upload
  235. func (s *OssDownloadSuite) TestDownloadObjectChange(c *C) {
  236. objectName := objectNamePrefix + "tdloc"
  237. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  238. newFile := "down-new-file-4.jpg"
  239. // Upload a file
  240. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  241. c.Assert(err, IsNil)
  242. // Download with default checkpoint
  243. downloadPartHooker = DownErrorHooker
  244. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
  245. c.Assert(err, NotNil)
  246. c.Assert(err.Error(), Equals, "ErrorHooker")
  247. downloadPartHooker = defaultDownloadPartHook
  248. err = s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  249. c.Assert(err, IsNil)
  250. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Checkpoint(true, newFile+".cp"))
  251. c.Assert(err, IsNil)
  252. eq, err := compareFiles(fileName, newFile)
  253. c.Assert(err, IsNil)
  254. c.Assert(eq, Equals, true)
  255. }
  256. // TestDownloadNegative tests downloading negative
  257. func (s *OssDownloadSuite) TestDownloadNegative(c *C) {
  258. objectName := objectNamePrefix + "tdn"
  259. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  260. newFile := "down-new-file-3.jpg"
  261. // Upload a file
  262. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  263. c.Assert(err, IsNil)
  264. // Worker routine error
  265. downloadPartHooker = DownErrorHooker
  266. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(2))
  267. c.Assert(err, NotNil)
  268. c.Assert(err.Error(), Equals, "ErrorHooker")
  269. downloadPartHooker = defaultDownloadPartHook
  270. // Local file does not exist
  271. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Routines(2))
  272. c.Assert(err, NotNil)
  273. // Invalid part size
  274. err = s.bucket.DownloadFile(objectName, newFile, 0, Routines(2))
  275. c.Assert(err, NotNil)
  276. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Routines(2))
  277. c.Assert(err, IsNil)
  278. err = s.bucket.DeleteObject(objectName)
  279. c.Assert(err, IsNil)
  280. // Local file does not exist
  281. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024)
  282. c.Assert(err, NotNil)
  283. err = s.bucket.DownloadFile(objectName, "/tmp/", 100*1024, Routines(2))
  284. c.Assert(err, NotNil)
  285. // Invalid part size
  286. err = s.bucket.DownloadFile(objectName, newFile, -1)
  287. c.Assert(err, NotNil)
  288. err = s.bucket.DownloadFile(objectName, newFile, 0, Routines(2))
  289. c.Assert(err, NotNil)
  290. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100)
  291. c.Assert(err, NotNil)
  292. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024*1024*100, Routines(2))
  293. c.Assert(err, NotNil)
  294. }
  295. // TestDownloadWithRange tests concurrent downloading with range specified and checkpoint enabled
  296. func (s *OssDownloadSuite) TestDownloadWithRange(c *C) {
  297. objectName := objectNamePrefix + "tdwr"
  298. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  299. newFile := "down-new-file-tdwr.jpg"
  300. newFileGet := "down-new-file-tdwr-2.jpg"
  301. // Upload a file
  302. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3))
  303. c.Assert(err, IsNil)
  304. fileSize, err := getFileSize(fileName)
  305. c.Assert(err, IsNil)
  306. // Download with range, from 1024 to 4096
  307. os.Remove(newFile)
  308. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Range(1024, 4095))
  309. c.Assert(err, IsNil)
  310. // Check
  311. eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  312. c.Assert(err, IsNil)
  313. c.Assert(eq, Equals, true)
  314. os.Remove(newFileGet)
  315. err = s.bucket.GetObjectToFile(objectName, newFileGet, Range(1024, 4095))
  316. c.Assert(err, IsNil)
  317. // Compare get and download
  318. eq, err = compareFiles(newFile, newFileGet)
  319. c.Assert(err, IsNil)
  320. c.Assert(eq, Equals, true)
  321. // Download with range, from 1024 to 4096
  322. os.Remove(newFile)
  323. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), NormalizedRange("1024-4095"))
  324. c.Assert(err, IsNil)
  325. // Check
  326. eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  327. c.Assert(err, IsNil)
  328. c.Assert(eq, Equals, true)
  329. os.Remove(newFileGet)
  330. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("1024-4095"))
  331. c.Assert(err, IsNil)
  332. // Compare get and download
  333. eq, err = compareFiles(newFile, newFileGet)
  334. c.Assert(err, IsNil)
  335. c.Assert(eq, Equals, true)
  336. // Download with range, from 2048 to the end
  337. os.Remove(newFile)
  338. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), NormalizedRange("2048-"))
  339. c.Assert(err, IsNil)
  340. // Check
  341. eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
  342. c.Assert(err, IsNil)
  343. c.Assert(eq, Equals, true)
  344. os.Remove(newFileGet)
  345. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("2048-"))
  346. c.Assert(err, IsNil)
  347. // Compare get and download
  348. eq, err = compareFiles(newFile, newFileGet)
  349. c.Assert(err, IsNil)
  350. c.Assert(eq, Equals, true)
  351. // Download with range, the last 4096
  352. os.Remove(newFile)
  353. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), NormalizedRange("-4096"))
  354. c.Assert(err, IsNil)
  355. // Check
  356. eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
  357. c.Assert(err, IsNil)
  358. c.Assert(eq, Equals, true)
  359. os.Remove(newFileGet)
  360. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("-4096"))
  361. c.Assert(err, IsNil)
  362. // Compare get and download
  363. eq, err = compareFiles(newFile, newFileGet)
  364. c.Assert(err, IsNil)
  365. c.Assert(eq, Equals, true)
  366. err = s.bucket.DeleteObject(objectName)
  367. c.Assert(err, IsNil)
  368. }
  369. // TestDownloadWithCheckoutAndRange tests concurrent downloading with range specified and checkpoint enabled
  370. func (s *OssDownloadSuite) TestDownloadWithCheckoutAndRange(c *C) {
  371. objectName := objectNamePrefix + "tdwcr"
  372. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  373. newFile := "down-new-file-tdwcr.jpg"
  374. newFileGet := "down-new-file-tdwcr-2.jpg"
  375. // Upload a file
  376. err := s.bucket.UploadFile(objectName, fileName, 100*1024, Routines(3), Checkpoint(true, fileName+".cp"))
  377. c.Assert(err, IsNil)
  378. fileSize, err := getFileSize(fileName)
  379. c.Assert(err, IsNil)
  380. // Download with range, from 1024 to 4096
  381. os.Remove(newFile)
  382. err = s.bucket.DownloadFile(objectName, newFile, 100*1024, Routines(3), Checkpoint(true, newFile+".cp"), Range(1024, 4095))
  383. c.Assert(err, IsNil)
  384. // Check
  385. eq, err := compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  386. c.Assert(err, IsNil)
  387. c.Assert(eq, Equals, true)
  388. os.Remove(newFileGet)
  389. err = s.bucket.GetObjectToFile(objectName, newFileGet, Range(1024, 4095))
  390. c.Assert(err, IsNil)
  391. // Compare get and download
  392. eq, err = compareFiles(newFile, newFileGet)
  393. c.Assert(err, IsNil)
  394. c.Assert(eq, Equals, true)
  395. // Download with range, from 1024 to 4096
  396. os.Remove(newFile)
  397. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("1024-4095"))
  398. c.Assert(err, IsNil)
  399. // Check
  400. eq, err = compareFilesWithRange(fileName, 1024, newFile, 0, 3072)
  401. c.Assert(err, IsNil)
  402. c.Assert(eq, Equals, true)
  403. os.Remove(newFileGet)
  404. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("1024-4095"))
  405. c.Assert(err, IsNil)
  406. // Compare get and download
  407. eq, err = compareFiles(newFile, newFileGet)
  408. c.Assert(err, IsNil)
  409. c.Assert(eq, Equals, true)
  410. // Download with range, from 2048 to the end
  411. os.Remove(newFile)
  412. err = s.bucket.DownloadFile(objectName, newFile, 1024*1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("2048-"))
  413. c.Assert(err, IsNil)
  414. // Check
  415. eq, err = compareFilesWithRange(fileName, 2048, newFile, 0, fileSize-2048)
  416. c.Assert(err, IsNil)
  417. c.Assert(eq, Equals, true)
  418. os.Remove(newFileGet)
  419. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("2048-"))
  420. c.Assert(err, IsNil)
  421. // Compare get and download
  422. eq, err = compareFiles(newFile, newFileGet)
  423. c.Assert(err, IsNil)
  424. c.Assert(eq, Equals, true)
  425. // Download with range, the last 4096 bytes
  426. os.Remove(newFile)
  427. err = s.bucket.DownloadFile(objectName, newFile, 1024, Routines(3), Checkpoint(true, newFile+".cp"), NormalizedRange("-4096"))
  428. c.Assert(err, IsNil)
  429. // Check
  430. eq, err = compareFilesWithRange(fileName, fileSize-4096, newFile, 0, 4096)
  431. c.Assert(err, IsNil)
  432. c.Assert(eq, Equals, true)
  433. os.Remove(newFileGet)
  434. err = s.bucket.GetObjectToFile(objectName, newFileGet, NormalizedRange("-4096"))
  435. c.Assert(err, IsNil)
  436. // Compare get and download
  437. eq, err = compareFiles(newFile, newFileGet)
  438. c.Assert(err, IsNil)
  439. c.Assert(eq, Equals, true)
  440. err = s.bucket.DeleteObject(objectName)
  441. c.Assert(err, IsNil)
  442. }
  443. // TestCombineCRCInDownloadParts tests combineCRCInParts
  444. func (s *OssDownloadSuite) TestCombineCRCInDownloadParts(c *C) {
  445. crc := combineCRCInParts(nil)
  446. c.Assert(crc == 0, Equals, true)
  447. crc = combineCRCInParts(make([]downloadPart, 0))
  448. c.Assert(crc == 0, Equals, true)
  449. parts := make([]downloadPart, 1)
  450. parts[0].CRC64 = 10278880121275185425
  451. crc = combineCRCInParts(parts)
  452. c.Assert(crc == 10278880121275185425, Equals, true)
  453. parts = make([]downloadPart, 2)
  454. parts[0].CRC64 = 6748440630437108969
  455. parts[0].Start = 0
  456. parts[0].End = 4
  457. parts[1].CRC64 = 10278880121275185425
  458. parts[1].Start = 5
  459. parts[1].End = 8
  460. crc = combineCRCInParts(parts)
  461. c.Assert(crc == 11051210869376104954, Equals, true)
  462. }
  463. func getFileSize(fileName string) (int64, error) {
  464. file, err := os.Open(fileName)
  465. if err != nil {
  466. return 0, err
  467. }
  468. defer file.Close()
  469. stat, err := file.Stat()
  470. if err != nil {
  471. return 0, err
  472. }
  473. return stat.Size(), nil
  474. }
  475. // compareFilesWithRange compares the content between fileL and fileR with specified range
  476. func compareFilesWithRange(fileL string, offsetL int64, fileR string, offsetR int64, size int64) (bool, error) {
  477. finL, err := os.Open(fileL)
  478. if err != nil {
  479. return false, err
  480. }
  481. defer finL.Close()
  482. finL.Seek(offsetL, os.SEEK_SET)
  483. finR, err := os.Open(fileR)
  484. if err != nil {
  485. return false, err
  486. }
  487. defer finR.Close()
  488. finR.Seek(offsetR, os.SEEK_SET)
  489. statL, err := finL.Stat()
  490. if err != nil {
  491. return false, err
  492. }
  493. statR, err := finR.Stat()
  494. if err != nil {
  495. return false, err
  496. }
  497. if (offsetL+size > statL.Size()) || (offsetR+size > statR.Size()) {
  498. return false, nil
  499. }
  500. part := statL.Size() - offsetL
  501. if part > 16*1024 {
  502. part = 16 * 1024
  503. }
  504. bufL := make([]byte, part)
  505. bufR := make([]byte, part)
  506. for readN := int64(0); readN < size; {
  507. n, _ := finL.Read(bufL)
  508. if 0 == n {
  509. break
  510. }
  511. n, _ = finR.Read(bufR)
  512. if 0 == n {
  513. break
  514. }
  515. tailer := part
  516. if tailer > size-readN {
  517. tailer = size - readN
  518. }
  519. readN += tailer
  520. if !bytes.Equal(bufL[0:tailer], bufR[0:tailer]) {
  521. return false, nil
  522. }
  523. }
  524. return true, nil
  525. }