crypto_multipart_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. // multipart test
  2. package osscrypto
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. . "gopkg.in/check.v1"
  8. )
  9. func (s *OssCryptoBucketSuite) TestMultipartUpload(c *C) {
  10. // create a bucket with default proprety
  11. client, err := oss.New(endpoint, accessID, accessKey)
  12. c.Assert(err, IsNil)
  13. bucketName := bucketNamePrefix + RandLowStr(6)
  14. err = client.CreateBucket(bucketName)
  15. c.Assert(err, IsNil)
  16. // crypto bucket
  17. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  18. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  19. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  20. objectName := objectNamePrefix + RandStr(8)
  21. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  22. srcMD5, err := GetFileMD5(fileName)
  23. c.Assert(err, IsNil)
  24. fileInfo, err := os.Stat(fileName)
  25. dataSize := fileInfo.Size()
  26. c.Assert(err, IsNil)
  27. options := []oss.Option{oss.Meta("my", "myprop")}
  28. var cryptoContext PartCryptoContext
  29. cryptoContext.DataSize = dataSize
  30. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  31. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  32. c.Assert(err, IsNil)
  33. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  34. c.Assert(err, IsNil)
  35. fd, err := os.Open(fileName)
  36. c.Assert(err, IsNil)
  37. defer fd.Close()
  38. var parts []oss.UploadPart
  39. for _, chunk := range chunks {
  40. fd.Seek(chunk.Offset, os.SEEK_SET)
  41. part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
  42. c.Assert(err, IsNil)
  43. parts = append(parts, part)
  44. }
  45. _, err = bucket.CompleteMultipartUpload(imur, parts)
  46. c.Assert(err, IsNil)
  47. meta, err := bucket.GetObjectDetailedMeta(objectName)
  48. c.Assert(err, IsNil)
  49. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  50. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  51. downfileName := "test-go-sdk-file-" + RandLowStr(5)
  52. err = bucket.GetObjectToFile(objectName, downfileName)
  53. c.Assert(err, IsNil)
  54. downFileMD5, err := GetFileMD5(downfileName)
  55. c.Assert(err, IsNil)
  56. c.Assert(downFileMD5, Equals, srcMD5)
  57. os.Remove(downfileName)
  58. ForceDeleteBucket(client, bucketName, c)
  59. }
  60. func (s *OssCryptoBucketSuite) TestMultipartUploadFromFile(c *C) {
  61. // create a bucket with default proprety
  62. client, err := oss.New(endpoint, accessID, accessKey)
  63. c.Assert(err, IsNil)
  64. bucketName := bucketNamePrefix + RandLowStr(6)
  65. err = client.CreateBucket(bucketName)
  66. c.Assert(err, IsNil)
  67. // crypto bucket
  68. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  69. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  70. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  71. objectName := objectNamePrefix + RandStr(8)
  72. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  73. srcMD5, err := GetFileMD5(fileName)
  74. c.Assert(err, IsNil)
  75. fileInfo, err := os.Stat(fileName)
  76. dataSize := fileInfo.Size()
  77. c.Assert(err, IsNil)
  78. options := []oss.Option{oss.Meta("my", "myprop")}
  79. var cryptoContext PartCryptoContext
  80. cryptoContext.DataSize = dataSize
  81. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  82. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  83. c.Assert(err, IsNil)
  84. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  85. c.Assert(err, IsNil)
  86. var parts []oss.UploadPart
  87. for _, chunk := range chunks {
  88. part, err := bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
  89. c.Assert(err, IsNil)
  90. parts = append(parts, part)
  91. }
  92. _, err = bucket.CompleteMultipartUpload(imur, parts)
  93. c.Assert(err, IsNil)
  94. meta, err := bucket.GetObjectDetailedMeta(objectName)
  95. c.Assert(err, IsNil)
  96. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  97. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  98. downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
  99. err = bucket.GetObjectToFile(objectName, downfileName)
  100. c.Assert(err, IsNil)
  101. downFileMD5, err := GetFileMD5(downfileName)
  102. c.Assert(err, IsNil)
  103. c.Assert(downFileMD5, Equals, srcMD5)
  104. os.Remove(downfileName)
  105. ForceDeleteBucket(client, bucketName, c)
  106. }
  107. func (s *OssCryptoBucketSuite) TestMultipartUploadFromSmallSizeFile(c *C) {
  108. // create a bucket with default proprety
  109. client, err := oss.New(endpoint, accessID, accessKey)
  110. c.Assert(err, IsNil)
  111. bucketName := bucketNamePrefix + RandLowStr(6)
  112. err = client.CreateBucket(bucketName)
  113. c.Assert(err, IsNil)
  114. // crypto bucket
  115. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  116. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  117. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  118. objectName := objectNamePrefix + RandStr(8)
  119. fileName := "oss-go-sdk-test-file-" + RandStr(5)
  120. fo, err := os.Create(fileName)
  121. c.Assert(err, IsNil)
  122. _, err = fo.Write([]byte("123"))
  123. c.Assert(err, IsNil)
  124. fo.Close()
  125. srcMD5, err := GetFileMD5(fileName)
  126. c.Assert(err, IsNil)
  127. fileInfo, err := os.Stat(fileName)
  128. dataSize := fileInfo.Size()
  129. c.Assert(err, IsNil)
  130. options := []oss.Option{oss.Meta("my", "myprop")}
  131. var cryptoContext PartCryptoContext
  132. cryptoContext.DataSize = dataSize
  133. cryptoContext.PartSize = 16
  134. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  135. c.Assert(err, IsNil)
  136. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  137. c.Assert(err, IsNil)
  138. var parts []oss.UploadPart
  139. for _, chunk := range chunks {
  140. part, err := bucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
  141. c.Assert(err, IsNil)
  142. parts = append(parts, part)
  143. }
  144. _, err = bucket.CompleteMultipartUpload(imur, parts)
  145. c.Assert(err, IsNil)
  146. meta, err := bucket.GetObjectDetailedMeta(objectName)
  147. c.Assert(err, IsNil)
  148. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  149. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Multipart")
  150. downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
  151. err = bucket.GetObjectToFile(objectName, downfileName)
  152. c.Assert(err, IsNil)
  153. downFileMD5, err := GetFileMD5(downfileName)
  154. c.Assert(err, IsNil)
  155. c.Assert(downFileMD5, Equals, srcMD5)
  156. os.Remove(fileName)
  157. os.Remove(downfileName)
  158. ForceDeleteBucket(client, bucketName, c)
  159. }
  160. func (s *OssCryptoBucketSuite) TestListUploadedPartsNormal(c *C) {
  161. // create a bucket with default proprety
  162. client, err := oss.New(endpoint, accessID, accessKey)
  163. c.Assert(err, IsNil)
  164. bucketName := bucketNamePrefix + RandLowStr(6)
  165. err = client.CreateBucket(bucketName)
  166. c.Assert(err, IsNil)
  167. // crypto bucket
  168. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  169. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  170. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  171. objectName := objectNamePrefix + RandStr(8)
  172. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  173. srcMD5, err := GetFileMD5(fileName)
  174. c.Assert(err, IsNil)
  175. fileInfo, err := os.Stat(fileName)
  176. dataSize := fileInfo.Size()
  177. c.Assert(err, IsNil)
  178. // Upload
  179. var cryptoContext PartCryptoContext
  180. cryptoContext.DataSize = dataSize
  181. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  182. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  183. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  184. c.Assert(err, IsNil)
  185. var partsUpload []oss.UploadPart
  186. for _, chunk := range chunks {
  187. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number), cryptoContext)
  188. c.Assert(err, IsNil)
  189. partsUpload = append(partsUpload, part)
  190. }
  191. // List
  192. lupr, err := bucket.ListUploadedParts(imurUpload)
  193. c.Assert(err, IsNil)
  194. c.Assert(len(lupr.UploadedParts), Equals, len(chunks))
  195. // Complete
  196. _, err = bucket.CompleteMultipartUpload(imurUpload, partsUpload)
  197. c.Assert(err, IsNil)
  198. // Download
  199. downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
  200. err = bucket.GetObjectToFile(objectName, downfileName)
  201. downFileMD5, err := GetFileMD5(downfileName)
  202. c.Assert(err, IsNil)
  203. c.Assert(downFileMD5, Equals, srcMD5)
  204. os.Remove(downfileName)
  205. ForceDeleteBucket(client, bucketName, c)
  206. }
  207. func (s *OssCryptoBucketSuite) TestListUploadedPartsComplete(c *C) {
  208. // create a bucket with default proprety
  209. client, err := oss.New(endpoint, accessID, accessKey)
  210. c.Assert(err, IsNil)
  211. bucketName := bucketNamePrefix + RandLowStr(6)
  212. err = client.CreateBucket(bucketName)
  213. c.Assert(err, IsNil)
  214. // crypto bucket
  215. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  216. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  217. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  218. objectName := objectNamePrefix + RandStr(8)
  219. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  220. srcMD5, err := GetFileMD5(fileName)
  221. c.Assert(err, IsNil)
  222. fileInfo, err := os.Stat(fileName)
  223. dataSize := fileInfo.Size()
  224. c.Assert(err, IsNil)
  225. // Upload
  226. var cryptoContext PartCryptoContext
  227. cryptoContext.DataSize = dataSize
  228. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  229. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  230. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  231. c.Assert(err, IsNil)
  232. var partsUpload []oss.UploadPart
  233. i := 0
  234. // upload excepted the last part
  235. for ; i < len(chunks)-1; i++ {
  236. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  237. c.Assert(err, IsNil)
  238. partsUpload = append(partsUpload, part)
  239. }
  240. // List
  241. lupr, err := bucket.ListUploadedParts(imurUpload)
  242. c.Assert(err, IsNil)
  243. c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
  244. lmur, err := bucket.ListMultipartUploads()
  245. c.Assert(err, IsNil)
  246. c.Assert(len(lmur.Uploads), Equals, 1)
  247. // upload the last part with list part result
  248. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  249. c.Assert(err, IsNil)
  250. partsUpload = append(partsUpload, part)
  251. // Complete
  252. _, err = bucket.CompleteMultipartUpload(imurUpload, partsUpload)
  253. c.Assert(err, IsNil)
  254. // Download
  255. downfileName := "test-go-sdk-file-" + RandLowStr(5) + ".jpg"
  256. err = bucket.GetObjectToFile(objectName, downfileName)
  257. downFileMD5, err := GetFileMD5(downfileName)
  258. c.Assert(err, IsNil)
  259. c.Assert(downFileMD5, Equals, srcMD5)
  260. os.Remove(downfileName)
  261. ForceDeleteBucket(client, bucketName, c)
  262. }
  263. func (s *OssCryptoBucketSuite) TestListUploadedPartsAbortUseInit(c *C) {
  264. // create a bucket with default proprety
  265. client, err := oss.New(endpoint, accessID, accessKey)
  266. c.Assert(err, IsNil)
  267. bucketName := bucketNamePrefix + RandLowStr(6)
  268. err = client.CreateBucket(bucketName)
  269. c.Assert(err, IsNil)
  270. // crypto bucket
  271. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  272. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  273. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  274. objectName := objectNamePrefix + RandStr(8)
  275. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  276. fileInfo, err := os.Stat(fileName)
  277. dataSize := fileInfo.Size()
  278. c.Assert(err, IsNil)
  279. // Upload
  280. var cryptoContext PartCryptoContext
  281. cryptoContext.DataSize = dataSize
  282. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  283. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  284. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  285. c.Assert(err, IsNil)
  286. c.Assert(cryptoContext.Valid(), Equals, true)
  287. var partsUpload []oss.UploadPart
  288. i := 0
  289. // upload excepted the last part
  290. for ; i < len(chunks)-1; i++ {
  291. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  292. c.Assert(err, IsNil)
  293. partsUpload = append(partsUpload, part)
  294. }
  295. // List
  296. lupr, err := bucket.ListUploadedParts(imurUpload)
  297. c.Assert(err, IsNil)
  298. c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
  299. lmur, err := bucket.ListMultipartUploads()
  300. c.Assert(err, IsNil)
  301. c.Assert(len(lmur.Uploads), Equals, 1)
  302. // abort upload
  303. err = bucket.AbortMultipartUpload(imurUpload)
  304. c.Assert(err, IsNil)
  305. // list again
  306. lupr, err = bucket.ListUploadedParts(imurUpload)
  307. c.Assert(err, NotNil)
  308. ForceDeleteBucket(client, bucketName, c)
  309. }
  310. func (s *OssCryptoBucketSuite) TestListUploadedPartsAbortUseList(c *C) {
  311. // create a bucket with default proprety
  312. client, err := oss.New(endpoint, accessID, accessKey)
  313. c.Assert(err, IsNil)
  314. bucketName := bucketNamePrefix + RandLowStr(6)
  315. err = client.CreateBucket(bucketName)
  316. c.Assert(err, IsNil)
  317. // crypto bucket
  318. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  319. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  320. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  321. objectName := objectNamePrefix + RandStr(8)
  322. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  323. fileInfo, err := os.Stat(fileName)
  324. dataSize := fileInfo.Size()
  325. c.Assert(err, IsNil)
  326. // Upload
  327. var cryptoContext PartCryptoContext
  328. cryptoContext.DataSize = dataSize
  329. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  330. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  331. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  332. c.Assert(err, IsNil)
  333. var partsUpload []oss.UploadPart
  334. i := 0
  335. // upload excepted the last part
  336. for ; i < len(chunks)-1; i++ {
  337. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  338. c.Assert(err, IsNil)
  339. partsUpload = append(partsUpload, part)
  340. }
  341. // List
  342. lupr, err := bucket.ListUploadedParts(imurUpload)
  343. c.Assert(err, IsNil)
  344. c.Assert(len(lupr.UploadedParts), Equals, len(chunks)-1)
  345. // abort upload
  346. err = bucket.AbortMultipartUpload(imurUpload)
  347. c.Assert(err, IsNil)
  348. // list again
  349. lupr, err = bucket.ListUploadedParts(imurUpload)
  350. c.Assert(err, NotNil)
  351. ForceDeleteBucket(client, bucketName, c)
  352. }
  353. func (s *OssCryptoBucketSuite) TestInitiateMultipartUpload(c *C) {
  354. // create a bucket with default proprety
  355. client, err := oss.New(endpoint, accessID, accessKey)
  356. c.Assert(err, IsNil)
  357. bucketName := bucketNamePrefix + RandLowStr(6)
  358. err = client.CreateBucket(bucketName)
  359. c.Assert(err, IsNil)
  360. // crypto bucket
  361. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  362. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  363. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  364. objectName := objectNamePrefix + RandStr(8)
  365. context := RandStr(ivSize * 1024 * 10)
  366. fileName := "test-go-sdk-file-" + RandStr(5)
  367. err = ioutil.WriteFile(fileName, []byte(context), 0666)
  368. c.Assert(err, IsNil)
  369. fileInfo, err := os.Stat(fileName)
  370. dataSize := fileInfo.Size()
  371. c.Assert(err, IsNil)
  372. var cryptoContext PartCryptoContext
  373. cryptoContext.DataSize = dataSize
  374. cryptoContext.PartSize = ivSize * 1024
  375. imurUpload, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  376. c.Assert(err, IsNil)
  377. err = bucket.AbortMultipartUpload(imurUpload)
  378. c.Assert(err, IsNil)
  379. cryptoContext.DataSize = dataSize
  380. cryptoContext.PartSize = ivSize / 2
  381. imurUpload, err = bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  382. c.Assert(err, NotNil)
  383. os.Remove(fileName)
  384. ForceDeleteBucket(client, bucketName, c)
  385. }
  386. func (s *OssCryptoBucketSuite) TestUploadPartError(c *C) {
  387. // create a bucket with default proprety
  388. client, err := oss.New(endpoint, accessID, accessKey)
  389. c.Assert(err, IsNil)
  390. bucketName := bucketNamePrefix + RandLowStr(6)
  391. err = client.CreateBucket(bucketName)
  392. c.Assert(err, IsNil)
  393. // crypto bucket
  394. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  395. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  396. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  397. objectName := objectNamePrefix + RandStr(8)
  398. context := RandStr(ivSize * 1024 * 10)
  399. fileName := "test-go-sdk-file-" + RandStr(5)
  400. err = ioutil.WriteFile(fileName, []byte(context), 0666)
  401. c.Assert(err, IsNil)
  402. fileInfo, err := os.Stat(fileName)
  403. dataSize := fileInfo.Size()
  404. c.Assert(err, IsNil)
  405. var cryptoContext PartCryptoContext
  406. cryptoContext.DataSize = dataSize
  407. cryptoContext.PartSize = ivSize * 1024
  408. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  409. c.Assert(err, IsNil)
  410. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  411. c.Assert(err, IsNil)
  412. fd, err := os.Open(fileName)
  413. c.Assert(err, IsNil)
  414. for _, chunk := range chunks {
  415. fd.Seek(chunk.Offset, os.SEEK_SET)
  416. _, err := bucket.UploadPart(imur, fd, chunk.Size+1, chunk.Number, cryptoContext)
  417. c.Assert(err, NotNil)
  418. }
  419. for _, chunk := range chunks {
  420. fd.Seek(chunk.Offset, os.SEEK_SET)
  421. _, err := bucket.UploadPart(imur, fd, chunk.Size, 0, cryptoContext)
  422. c.Assert(err, NotNil)
  423. }
  424. fd.Close()
  425. err = bucket.AbortMultipartUpload(imur)
  426. c.Assert(err, IsNil)
  427. os.Remove(fileName)
  428. ForceDeleteBucket(client, bucketName, c)
  429. }
  430. func (s *OssCryptoBucketSuite) TestUploadPartFromFileError(c *C) {
  431. // create a bucket with default proprety
  432. client, err := oss.New(endpoint, accessID, accessKey)
  433. c.Assert(err, IsNil)
  434. bucketName := bucketNamePrefix + RandLowStr(6)
  435. err = client.CreateBucket(bucketName)
  436. c.Assert(err, IsNil)
  437. // crypto bucket
  438. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  439. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  440. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  441. objectName := objectNamePrefix + RandStr(8)
  442. context := RandStr(ivSize * 1024 * 10)
  443. fileName := "test-go-sdk-file-" + RandStr(5)
  444. err = ioutil.WriteFile(fileName, []byte(context), 0666)
  445. c.Assert(err, IsNil)
  446. fileInfo, err := os.Stat(fileName)
  447. dataSize := fileInfo.Size()
  448. c.Assert(err, IsNil)
  449. var cryptoContext PartCryptoContext
  450. cryptoContext.DataSize = dataSize
  451. cryptoContext.PartSize = ivSize * 1024
  452. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  453. c.Assert(err, IsNil)
  454. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  455. c.Assert(err, IsNil)
  456. for _, chunk := range chunks {
  457. _, err := bucket.UploadPartFromFile(imur, fileName+".test", chunk.Offset, chunk.Size, chunk.Number, cryptoContext)
  458. c.Assert(err, NotNil)
  459. }
  460. _, err = bucket.UploadPartFromFile(imur, fileName+".test", chunks[0].Offset, chunks[0].Size+1, chunks[0].Number, cryptoContext)
  461. c.Assert(err, NotNil)
  462. _, err = bucket.UploadPartFromFile(imur, fileName+".test", chunks[0].Offset, chunks[0].Size, 0, cryptoContext)
  463. c.Assert(err, NotNil)
  464. err = bucket.AbortMultipartUpload(imur)
  465. c.Assert(err, IsNil)
  466. os.Remove(fileName)
  467. ForceDeleteBucket(client, bucketName, c)
  468. }
  469. func (s *OssCryptoBucketSuite) TestUploadPartCopyError(c *C) {
  470. // create a bucket with default proprety
  471. client, err := oss.New(endpoint, accessID, accessKey)
  472. c.Assert(err, IsNil)
  473. bucketName := bucketNamePrefix + RandLowStr(6)
  474. err = client.CreateBucket(bucketName)
  475. c.Assert(err, IsNil)
  476. // crypto bucket
  477. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  478. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  479. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  480. objectName := objectNamePrefix + RandStr(8)
  481. context := RandStr(ivSize * 1024 * 10)
  482. fileName := "test-go-sdk-file-" + RandStr(5)
  483. err = ioutil.WriteFile(fileName, []byte(context), 0666)
  484. c.Assert(err, IsNil)
  485. fileInfo, err := os.Stat(fileName)
  486. dataSize := fileInfo.Size()
  487. c.Assert(err, IsNil)
  488. var cryptoContext PartCryptoContext
  489. cryptoContext.DataSize = dataSize
  490. cryptoContext.PartSize = ivSize * 1024
  491. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext)
  492. c.Assert(err, IsNil)
  493. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  494. c.Assert(err, IsNil)
  495. _, err = bucket.UploadPartCopy(imur, bucketName, objectName, 0, chunks[0].Size, 1, cryptoContext)
  496. c.Assert(err, NotNil)
  497. err = bucket.AbortMultipartUpload(imur)
  498. c.Assert(err, IsNil)
  499. os.Remove(fileName)
  500. ForceDeleteBucket(client, bucketName, c)
  501. }
  502. func (s *OssCryptoBucketSuite) TestMultipartUploadFromFileError(c *C) {
  503. // create a bucket with default proprety
  504. client, err := oss.New(endpoint, accessID, accessKey)
  505. c.Assert(err, IsNil)
  506. bucketName := bucketNamePrefix + RandLowStr(6)
  507. err = client.CreateBucket(bucketName)
  508. c.Assert(err, IsNil)
  509. // crypto bucket
  510. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  511. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  512. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  513. objectName := objectNamePrefix + RandStr(8)
  514. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  515. fileInfo, err := os.Stat(fileName)
  516. dataSize := fileInfo.Size()
  517. c.Assert(err, IsNil)
  518. options := []oss.Option{oss.Meta("my", "myprop")}
  519. var cryptoContext PartCryptoContext
  520. cryptoContext.DataSize = dataSize
  521. cryptoContext.PartSize = -1
  522. _, err = bucket.InitiateMultipartUpload(objectName, nil, options...)
  523. c.Assert(err, NotNil)
  524. _, err = bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  525. c.Assert(err, NotNil)
  526. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  527. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  528. c.Assert(err, IsNil)
  529. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  530. c.Assert(err, IsNil)
  531. bakCC := cryptoContext.ContentCipher
  532. cryptoContext.ContentCipher = nil
  533. i := 0
  534. // upload excepted the last part
  535. for ; i < len(chunks); i++ {
  536. _, err = bucket.UploadPartFromFile(imur, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  537. c.Assert(err, NotNil)
  538. }
  539. i = 0
  540. cryptoContext.ContentCipher = bakCC
  541. cryptoContext.PartSize -= 1
  542. for ; i < len(chunks); i++ {
  543. _, err = bucket.UploadPartFromFile(imur, fileName, chunks[i].Offset, chunks[i].Size, (int)(chunks[i].Number), cryptoContext)
  544. c.Assert(err, NotNil)
  545. }
  546. ForceDeleteBucket(client, bucketName, c)
  547. }
  548. func (s *OssCryptoBucketSuite) TestMultipartUploadPartError(c *C) {
  549. // create a bucket with default proprety
  550. client, err := oss.New(endpoint, accessID, accessKey)
  551. c.Assert(err, IsNil)
  552. bucketName := bucketNamePrefix + RandLowStr(6)
  553. err = client.CreateBucket(bucketName)
  554. c.Assert(err, IsNil)
  555. // crypto bucket
  556. masterRsaCipher, _ := CreateMasterRsa(matDesc, rsaPublicKey, rsaPrivateKey)
  557. contentProvider := CreateAesCtrCipher(masterRsaCipher)
  558. bucket, err := GetCryptoBucket(client, bucketName, contentProvider)
  559. objectName := objectNamePrefix + RandStr(8)
  560. fileName := "../../sample/BingWallpaper-2015-11-07.jpg"
  561. fileInfo, err := os.Stat(fileName)
  562. dataSize := fileInfo.Size()
  563. c.Assert(err, IsNil)
  564. options := []oss.Option{oss.Meta("my", "myprop")}
  565. var cryptoContext PartCryptoContext
  566. cryptoContext.DataSize = dataSize
  567. cryptoContext.PartSize = (dataSize / 16 / 3) * 16
  568. imur, err := bucket.InitiateMultipartUpload(objectName, &cryptoContext, options...)
  569. c.Assert(err, IsNil)
  570. chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
  571. c.Assert(err, IsNil)
  572. bakCC := cryptoContext.ContentCipher
  573. cryptoContext.ContentCipher = nil
  574. fd, err := os.Open(fileName)
  575. c.Assert(err, IsNil)
  576. for _, chunk := range chunks {
  577. fd.Seek(chunk.Offset, os.SEEK_SET)
  578. _, err = bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
  579. c.Assert(err, NotNil)
  580. }
  581. cryptoContext.ContentCipher = bakCC
  582. cryptoContext.PartSize -= 1
  583. for _, chunk := range chunks {
  584. _, err = bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, cryptoContext)
  585. c.Assert(err, NotNil)
  586. }
  587. ForceDeleteBucket(client, bucketName, c)
  588. }