You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

540 lines
20 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package coscrypto_test
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/aes"
  6. "crypto/cipher"
  7. "crypto/md5"
  8. "crypto/rand"
  9. "encoding/base64"
  10. "fmt"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/suite"
  13. "github.com/tencentyun/cos-go-sdk-v5"
  14. "github.com/tencentyun/cos-go-sdk-v5/crypto"
  15. "io"
  16. "io/ioutil"
  17. math_rand "math/rand"
  18. "net/http"
  19. "net/url"
  20. "os"
  21. "testing"
  22. "time"
  23. )
  24. const (
  25. kAppid = 1259654469
  26. kBucket = "cosgosdktest-1259654469"
  27. kRegion = "ap-guangzhou"
  28. )
  29. type CosTestSuite struct {
  30. suite.Suite
  31. Client *cos.Client
  32. CClient *coscrypto.CryptoClient
  33. Master coscrypto.MasterCipher
  34. }
  35. func (s *CosTestSuite) SetupSuite() {
  36. u, _ := url.Parse("https://" + kBucket + ".cos." + kRegion + ".myqcloud.com")
  37. b := &cos.BaseURL{BucketURL: u}
  38. s.Client = cos.NewClient(b, &http.Client{
  39. Transport: &cos.AuthorizationTransport{
  40. SecretID: os.Getenv("COS_SECRETID"),
  41. SecretKey: os.Getenv("COS_SECRETKEY"),
  42. },
  43. })
  44. material := make(map[string]string)
  45. material["desc"] = "cos crypto suite test"
  46. kmsclient, _ := coscrypto.NewKMSClient(s.Client.GetCredential(), kRegion)
  47. s.Master, _ = coscrypto.CreateMasterKMS(kmsclient, os.Getenv("KMSID"), material)
  48. s.CClient = coscrypto.NewCryptoClient(s.Client, s.Master)
  49. opt := &cos.BucketPutOptions{
  50. XCosACL: "public-read",
  51. }
  52. r, err := s.Client.Bucket.Put(context.Background(), opt)
  53. if err != nil && r != nil && r.StatusCode == 409 {
  54. fmt.Println("BucketAlreadyOwnedByYou")
  55. } else if err != nil {
  56. assert.Nil(s.T(), err, "PutBucket Failed")
  57. }
  58. }
  59. func (s *CosTestSuite) TestPutGetDeleteObject_DecryptWithKey_10MB() {
  60. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  61. originData := make([]byte, 1024*1024*10+1)
  62. _, err := rand.Read(originData)
  63. f := bytes.NewReader(originData)
  64. // 加密存储
  65. _, err = s.CClient.Object.Put(context.Background(), name, f, nil)
  66. assert.Nil(s.T(), err, "PutObject Failed")
  67. // 获取解密信息
  68. resp, err := s.CClient.Object.Head(context.Background(), name, nil)
  69. assert.Nil(s.T(), err, "HeadObject Failed")
  70. cipherKey := resp.Header.Get(coscrypto.COSClientSideEncryptionKey)
  71. cipherIV := resp.Header.Get(coscrypto.COSClientSideEncryptionStart)
  72. key, err := s.Master.Decrypt([]byte(cipherKey))
  73. assert.Nil(s.T(), err, "Master Decrypt Failed")
  74. iv, err := s.Master.Decrypt([]byte(cipherIV))
  75. assert.Nil(s.T(), err, "Master Decrypt Failed")
  76. // 正常读取
  77. resp, err = s.Client.Object.Get(context.Background(), name, nil)
  78. assert.Nil(s.T(), err, "GetObject Failed")
  79. defer resp.Body.Close()
  80. encryptedData, _ := ioutil.ReadAll(resp.Body)
  81. assert.NotEqual(s.T(), bytes.Compare(encryptedData, originData), 0, "encryptedData == originData")
  82. // 手动解密
  83. block, err := aes.NewCipher(key)
  84. assert.Nil(s.T(), err, "NewCipher Failed")
  85. decrypter := cipher.NewCTR(block, iv)
  86. decryptedData := make([]byte, len(originData))
  87. decrypter.XORKeyStream(decryptedData, encryptedData)
  88. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  89. _, err = s.CClient.Object.Delete(context.Background(), name)
  90. assert.Nil(s.T(), err, "DeleteObject Failed")
  91. }
  92. func (s *CosTestSuite) TestPutGetDeleteObject_Normal_10MB() {
  93. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  94. originData := make([]byte, 1024*1024*10+1)
  95. _, err := rand.Read(originData)
  96. f := bytes.NewReader(originData)
  97. // 加密存储
  98. _, err = s.CClient.Object.Put(context.Background(), name, f, nil)
  99. assert.Nil(s.T(), err, "PutObject Failed")
  100. // 解密读取
  101. resp, err := s.CClient.Object.Get(context.Background(), name, nil)
  102. assert.Nil(s.T(), err, "GetObject Failed")
  103. defer resp.Body.Close()
  104. decryptedData, _ := ioutil.ReadAll(resp.Body)
  105. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  106. _, err = s.CClient.Object.Delete(context.Background(), name)
  107. assert.Nil(s.T(), err, "DeleteObject Failed")
  108. }
  109. func (s *CosTestSuite) TestPutGetDeleteObject_VersionID() {
  110. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  111. originData := make([]byte, 1024*1024*10+1)
  112. _, err := rand.Read(originData)
  113. f := bytes.NewReader(originData)
  114. opt := &cos.BucketPutVersionOptions{
  115. Status: "Enabled",
  116. }
  117. _, err = s.CClient.Bucket.PutVersioning(context.Background(), opt)
  118. assert.Nil(s.T(), err, "PutVersioning Failed")
  119. time.Sleep(3 * time.Second)
  120. // 加密存储
  121. resp, err := s.CClient.Object.Put(context.Background(), name, f, nil)
  122. assert.Nil(s.T(), err, "PutObject Failed")
  123. versionId := resp.Header.Get("x-cos-version-id")
  124. _, err = s.CClient.Object.Delete(context.Background(), name)
  125. assert.Nil(s.T(), err, "DeleteObject Failed")
  126. // 解密读取
  127. resp, err = s.CClient.Object.Get(context.Background(), name, nil, versionId)
  128. assert.Nil(s.T(), err, "GetObject Failed")
  129. defer resp.Body.Close()
  130. decryptedData, _ := ioutil.ReadAll(resp.Body)
  131. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  132. delopt := &cos.ObjectDeleteOptions{
  133. VersionId: versionId,
  134. }
  135. _, err = s.CClient.Object.Delete(context.Background(), name, delopt)
  136. assert.Nil(s.T(), err, "DeleteObject Failed")
  137. opt.Status = "Suspended"
  138. _, err = s.CClient.Bucket.PutVersioning(context.Background(), opt)
  139. assert.Nil(s.T(), err, "PutVersioning Failed")
  140. }
  141. func (s *CosTestSuite) TestPutGetDeleteObject_ZeroFile() {
  142. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  143. // 加密存储
  144. _, err := s.CClient.Object.Put(context.Background(), name, bytes.NewReader([]byte("")), nil)
  145. assert.Nil(s.T(), err, "PutObject Failed")
  146. // 解密读取
  147. resp, err := s.CClient.Object.Get(context.Background(), name, nil)
  148. assert.Nil(s.T(), err, "GetObject Failed")
  149. defer resp.Body.Close()
  150. decryptedData, _ := ioutil.ReadAll(resp.Body)
  151. assert.Equal(s.T(), bytes.Compare([]byte(""), decryptedData), 0, "decryptData != originData")
  152. _, err = s.CClient.Object.Delete(context.Background(), name)
  153. assert.Nil(s.T(), err, "DeleteObject Failed")
  154. }
  155. func (s *CosTestSuite) TestPutGetDeleteObject_WithMetaData() {
  156. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  157. originData := make([]byte, 1024*1024*10+1)
  158. _, err := rand.Read(originData)
  159. f := bytes.NewReader(originData)
  160. m := md5.New()
  161. m.Write(originData)
  162. contentMD5 := m.Sum(nil)
  163. opt := &cos.ObjectPutOptions{
  164. &cos.ACLHeaderOptions{
  165. XCosACL: "private",
  166. },
  167. &cos.ObjectPutHeaderOptions{
  168. ContentLength: 1024*1024*10 + 1,
  169. ContentMD5: base64.StdEncoding.EncodeToString(contentMD5),
  170. XCosMetaXXX: &http.Header{},
  171. },
  172. }
  173. opt.XCosMetaXXX.Add("x-cos-meta-isEncrypted", "true")
  174. // 加密存储
  175. _, err = s.CClient.Object.Put(context.Background(), name, f, opt)
  176. assert.Nil(s.T(), err, "PutObject Failed")
  177. // 解密读取
  178. resp, err := s.CClient.Object.Get(context.Background(), name, nil)
  179. assert.Nil(s.T(), err, "GetObject Failed")
  180. defer resp.Body.Close()
  181. decryptedData, _ := ioutil.ReadAll(resp.Body)
  182. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  183. assert.Equal(s.T(), resp.Header.Get("x-cos-meta-isEncrypted"), "true", "meta data isn't consistent")
  184. assert.Equal(s.T(), resp.Header.Get(coscrypto.COSClientSideEncryptionCekAlg), coscrypto.AesCtrAlgorithm, "meta data isn't consistent")
  185. assert.Equal(s.T(), resp.Header.Get(coscrypto.COSClientSideEncryptionWrapAlg), coscrypto.CosKmsCryptoWrap, "meta data isn't consistent")
  186. assert.Equal(s.T(), resp.Header.Get(coscrypto.COSClientSideEncryptionUnencryptedContentMD5), base64.StdEncoding.EncodeToString(contentMD5), "meta data isn't consistent")
  187. _, err = s.CClient.Object.Delete(context.Background(), name)
  188. assert.Nil(s.T(), err, "DeleteObject Failed")
  189. }
  190. func (s *CosTestSuite) TestPutGetDeleteObject_ByFile() {
  191. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  192. filepath := "tmpfile" + time.Now().Format(time.RFC3339)
  193. newfile, err := os.Create(filepath)
  194. assert.Nil(s.T(), err, "Create File Failed")
  195. defer os.Remove(filepath)
  196. originData := make([]byte, 1024*1024*10+1)
  197. _, err = rand.Read(originData)
  198. newfile.Write(originData)
  199. newfile.Close()
  200. m := md5.New()
  201. m.Write(originData)
  202. contentMD5 := m.Sum(nil)
  203. opt := &cos.ObjectPutOptions{
  204. &cos.ACLHeaderOptions{
  205. XCosACL: "private",
  206. },
  207. &cos.ObjectPutHeaderOptions{
  208. ContentLength: 1024*1024*10 + 1,
  209. ContentMD5: base64.StdEncoding.EncodeToString(contentMD5),
  210. XCosMetaXXX: &http.Header{},
  211. },
  212. }
  213. opt.XCosMetaXXX.Add("x-cos-meta-isEncrypted", "true")
  214. // 加密存储
  215. _, err = s.CClient.Object.PutFromFile(context.Background(), name, filepath, opt)
  216. assert.Nil(s.T(), err, "PutFromFile Failed")
  217. // 解密读取
  218. downfile := "downfile" + time.Now().Format(time.RFC3339)
  219. resp, err := s.CClient.Object.GetToFile(context.Background(), name, downfile, nil)
  220. assert.Nil(s.T(), err, "GetToFile Failed")
  221. assert.Equal(s.T(), resp.Header.Get("x-cos-meta-isEncrypted"), "true", "meta data isn't consistent")
  222. assert.Equal(s.T(), resp.Header.Get(coscrypto.COSClientSideEncryptionCekAlg), coscrypto.AesCtrAlgorithm, "meta data isn't consistent")
  223. assert.Equal(s.T(), resp.Header.Get(coscrypto.COSClientSideEncryptionWrapAlg), coscrypto.CosKmsCryptoWrap, "meta data isn't consistent")
  224. assert.Equal(s.T(), resp.Header.Get(coscrypto.COSClientSideEncryptionUnencryptedContentMD5), base64.StdEncoding.EncodeToString(contentMD5), "meta data isn't consistent")
  225. fd, err := os.Open(downfile)
  226. assert.Nil(s.T(), err, "Open File Failed")
  227. defer os.Remove(downfile)
  228. defer fd.Close()
  229. m = md5.New()
  230. io.Copy(m, fd)
  231. downContentMD5 := m.Sum(nil)
  232. assert.Equal(s.T(), bytes.Compare(contentMD5, downContentMD5), 0, "decryptData != originData")
  233. _, err = s.CClient.Object.Delete(context.Background(), name)
  234. assert.Nil(s.T(), err, "DeleteObject Failed")
  235. }
  236. func (s *CosTestSuite) TestPutGetDeleteObject_DecryptWithNewClient_10MB() {
  237. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  238. originData := make([]byte, 1024*1024*10+1)
  239. _, err := rand.Read(originData)
  240. f := bytes.NewReader(originData)
  241. // 加密存储
  242. _, err = s.CClient.Object.Put(context.Background(), name, f, nil)
  243. assert.Nil(s.T(), err, "PutObject Failed")
  244. u, _ := url.Parse("https://" + kBucket + ".cos." + kRegion + ".myqcloud.com")
  245. b := &cos.BaseURL{BucketURL: u}
  246. c := cos.NewClient(b, &http.Client{
  247. Transport: &cos.AuthorizationTransport{
  248. SecretID: os.Getenv("COS_SECRETID"),
  249. SecretKey: os.Getenv("COS_SECRETKEY"),
  250. },
  251. })
  252. {
  253. // 使用不同的MatDesc客户端读取, 期待错误
  254. material := make(map[string]string)
  255. material["desc"] = "cos crypto suite test 2"
  256. kmsclient, _ := coscrypto.NewKMSClient(c.GetCredential(), kRegion)
  257. master, _ := coscrypto.CreateMasterKMS(kmsclient, os.Getenv("KMSID"), material)
  258. client := coscrypto.NewCryptoClient(c, master)
  259. resp, err := client.Object.Get(context.Background(), name, nil)
  260. assert.Nil(s.T(), resp, "Get Object Failed")
  261. assert.NotNil(s.T(), err, "Get Object Failed")
  262. }
  263. {
  264. // 使用相同的MatDesc客户端读取, 但KMSID不一样,期待正确,kms解密是不需要KMSID
  265. material := make(map[string]string)
  266. material["desc"] = "cos crypto suite test"
  267. kmsclient, _ := coscrypto.NewKMSClient(s.Client.GetCredential(), kRegion)
  268. master, _ := coscrypto.CreateMasterKMS(kmsclient, "KMSID", material)
  269. client := coscrypto.NewCryptoClient(c, master)
  270. resp, err := client.Object.Get(context.Background(), name, nil)
  271. assert.Nil(s.T(), err, "Get Object Failed")
  272. defer resp.Body.Close()
  273. decryptedData, _ := ioutil.ReadAll(resp.Body)
  274. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  275. }
  276. {
  277. // 使用相同的MatDesc和KMSID客户端读取, 期待正确
  278. material := make(map[string]string)
  279. material["desc"] = "cos crypto suite test"
  280. kmsclient, _ := coscrypto.NewKMSClient(s.Client.GetCredential(), kRegion)
  281. master, _ := coscrypto.CreateMasterKMS(kmsclient, os.Getenv("KMSID"), material)
  282. client := coscrypto.NewCryptoClient(c, master)
  283. resp, err := client.Object.Get(context.Background(), name, nil)
  284. assert.Nil(s.T(), err, "Get Object Failed")
  285. defer resp.Body.Close()
  286. decryptedData, _ := ioutil.ReadAll(resp.Body)
  287. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  288. }
  289. _, err = s.CClient.Object.Delete(context.Background(), name)
  290. assert.Nil(s.T(), err, "DeleteObject Failed")
  291. }
  292. func (s *CosTestSuite) TestPutGetDeleteObject_RangeGet() {
  293. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  294. contentLength := 1024*1024*10 + 1
  295. originData := make([]byte, contentLength)
  296. _, err := rand.Read(originData)
  297. f := bytes.NewReader(originData)
  298. // 加密存储
  299. _, err = s.CClient.Object.Put(context.Background(), name, f, nil)
  300. assert.Nil(s.T(), err, "PutObject Failed")
  301. // Range解密读取
  302. for i := 0; i < 10; i++ {
  303. math_rand.Seed(time.Now().UnixNano())
  304. rangeStart := math_rand.Intn(contentLength)
  305. rangeEnd := rangeStart + math_rand.Intn(contentLength-rangeStart)
  306. if rangeEnd == rangeStart || rangeStart >= contentLength-1 {
  307. continue
  308. }
  309. opt := &cos.ObjectGetOptions{
  310. Range: fmt.Sprintf("bytes=%v-%v", rangeStart, rangeEnd),
  311. }
  312. resp, err := s.CClient.Object.Get(context.Background(), name, opt)
  313. assert.Nil(s.T(), err, "GetObject Failed")
  314. defer resp.Body.Close()
  315. decryptedData, _ := ioutil.ReadAll(resp.Body)
  316. assert.Equal(s.T(), bytes.Compare(originData[rangeStart:rangeEnd+1], decryptedData), 0, "decryptData != originData")
  317. }
  318. // 解密读取
  319. resp, err := s.CClient.Object.Get(context.Background(), name, nil)
  320. assert.Nil(s.T(), err, "GetObject Failed")
  321. defer resp.Body.Close()
  322. decryptedData, _ := ioutil.ReadAll(resp.Body)
  323. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  324. _, err = s.CClient.Object.Delete(context.Background(), name)
  325. assert.Nil(s.T(), err, "DeleteObject Failed")
  326. }
  327. func (s *CosTestSuite) TestPutGetDeleteObject_WithListenerAndRange() {
  328. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  329. contentLength := 1024*1024*10 + 1
  330. originData := make([]byte, contentLength)
  331. _, err := rand.Read(originData)
  332. f := bytes.NewReader(originData)
  333. // 加密存储
  334. popt := &cos.ObjectPutOptions{
  335. nil,
  336. &cos.ObjectPutHeaderOptions{
  337. Listener: &cos.DefaultProgressListener{},
  338. },
  339. }
  340. _, err = s.CClient.Object.Put(context.Background(), name, f, popt)
  341. assert.Nil(s.T(), err, "PutObject Failed")
  342. // Range解密读取
  343. for i := 0; i < 10; i++ {
  344. math_rand.Seed(time.Now().UnixNano())
  345. rangeStart := math_rand.Intn(contentLength)
  346. rangeEnd := rangeStart + math_rand.Intn(contentLength-rangeStart)
  347. if rangeEnd == rangeStart || rangeStart >= contentLength-1 {
  348. continue
  349. }
  350. opt := &cos.ObjectGetOptions{
  351. Range: fmt.Sprintf("bytes=%v-%v", rangeStart, rangeEnd),
  352. Listener: &cos.DefaultProgressListener{},
  353. }
  354. resp, err := s.CClient.Object.Get(context.Background(), name, opt)
  355. assert.Nil(s.T(), err, "GetObject Failed")
  356. defer resp.Body.Close()
  357. decryptedData, _ := ioutil.ReadAll(resp.Body)
  358. assert.Equal(s.T(), bytes.Compare(originData[rangeStart:rangeEnd+1], decryptedData), 0, "decryptData != originData")
  359. }
  360. // 解密读取
  361. opt := &cos.ObjectGetOptions{
  362. Listener: &cos.DefaultProgressListener{},
  363. }
  364. resp, err := s.CClient.Object.Get(context.Background(), name, opt)
  365. assert.Nil(s.T(), err, "GetObject Failed")
  366. defer resp.Body.Close()
  367. decryptedData, _ := ioutil.ReadAll(resp.Body)
  368. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  369. _, err = s.CClient.Object.Delete(context.Background(), name)
  370. assert.Nil(s.T(), err, "DeleteObject Failed")
  371. }
  372. func (s *CosTestSuite) TestPutGetDeleteObject_Copy() {
  373. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  374. contentLength := 1024*1024*10 + 1
  375. originData := make([]byte, contentLength)
  376. _, err := rand.Read(originData)
  377. f := bytes.NewReader(originData)
  378. // 加密存储
  379. popt := &cos.ObjectPutOptions{
  380. nil,
  381. &cos.ObjectPutHeaderOptions{
  382. Listener: &cos.DefaultProgressListener{},
  383. },
  384. }
  385. resp, err := s.CClient.Object.Put(context.Background(), name, f, popt)
  386. assert.Nil(s.T(), err, "PutObject Failed")
  387. encryptedDataCRC := resp.Header.Get("x-cos-hash-crc64ecma")
  388. time.Sleep(3 * time.Second)
  389. sourceURL := fmt.Sprintf("%s/%s", s.CClient.BaseURL.BucketURL.Host, name)
  390. {
  391. // x-cos-metadata-directive必须为Copy,否则丢失加密信息,无法解密
  392. dest := "test/ObjectCopy1" + time.Now().Format(time.RFC3339)
  393. res, _, err := s.CClient.Object.Copy(context.Background(), dest, sourceURL, nil)
  394. assert.Nil(s.T(), err, "ObjectCopy Failed")
  395. assert.Equal(s.T(), encryptedDataCRC, res.CRC64, "CRC isn't consistent, return:%v, want:%v", res.CRC64, encryptedDataCRC)
  396. // Range解密读取
  397. for i := 0; i < 3; i++ {
  398. math_rand.Seed(time.Now().UnixNano())
  399. rangeStart := math_rand.Intn(contentLength)
  400. rangeEnd := rangeStart + math_rand.Intn(contentLength-rangeStart)
  401. if rangeEnd == rangeStart || rangeStart >= contentLength-1 {
  402. continue
  403. }
  404. opt := &cos.ObjectGetOptions{
  405. Range: fmt.Sprintf("bytes=%v-%v", rangeStart, rangeEnd),
  406. Listener: &cos.DefaultProgressListener{},
  407. }
  408. resp, err := s.CClient.Object.Get(context.Background(), dest, opt)
  409. assert.Nil(s.T(), err, "GetObject Failed")
  410. defer resp.Body.Close()
  411. decryptedData, _ := ioutil.ReadAll(resp.Body)
  412. assert.Equal(s.T(), bytes.Compare(originData[rangeStart:rangeEnd+1], decryptedData), 0, "decryptData != originData")
  413. }
  414. // 解密读取
  415. resp, err := s.CClient.Object.Get(context.Background(), dest, nil)
  416. assert.Nil(s.T(), err, "GetObject Failed")
  417. defer resp.Body.Close()
  418. decryptedData, _ := ioutil.ReadAll(resp.Body)
  419. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  420. _, err = s.CClient.Object.Delete(context.Background(), dest)
  421. assert.Nil(s.T(), err, "DeleteObject Failed")
  422. }
  423. {
  424. // x-cos-metadata-directive必须为Copy,否则丢失加密信息,无法解密
  425. opt := &cos.ObjectCopyOptions{
  426. &cos.ObjectCopyHeaderOptions{
  427. XCosMetadataDirective: "Replaced",
  428. },
  429. nil,
  430. }
  431. dest := "test/ObjectCopy2" + time.Now().Format(time.RFC3339)
  432. res, _, err := s.CClient.Object.Copy(context.Background(), dest, sourceURL, opt)
  433. assert.Nil(s.T(), err, "ObjectCopy Failed")
  434. assert.Equal(s.T(), encryptedDataCRC, res.CRC64, "CRC isn't consistent, return:%v, want:%v", res.CRC64, encryptedDataCRC)
  435. // 解密读取
  436. resp, err := s.CClient.Object.Get(context.Background(), dest, nil)
  437. assert.Nil(s.T(), err, "GetObject Failed")
  438. defer resp.Body.Close()
  439. decryptedData, _ := ioutil.ReadAll(resp.Body)
  440. assert.NotEqual(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  441. _, err = s.CClient.Object.Delete(context.Background(), dest)
  442. assert.Nil(s.T(), err, "DeleteObject Failed")
  443. }
  444. {
  445. // MultiCopy若是分块拷贝,则无法拷贝元数据
  446. dest := "test/ObjectCopy3" + time.Now().Format(time.RFC3339)
  447. res, _, err := s.CClient.Object.MultiCopy(context.Background(), dest, sourceURL, nil)
  448. assert.Nil(s.T(), err, "ObjectMultiCopy Failed")
  449. assert.Equal(s.T(), encryptedDataCRC, res.CRC64, "CRC isn't consistent, return:%v, want:%v", res.CRC64, encryptedDataCRC)
  450. // Range解密读取
  451. for i := 0; i < 3; i++ {
  452. math_rand.Seed(time.Now().UnixNano())
  453. rangeStart := math_rand.Intn(contentLength)
  454. rangeEnd := rangeStart + math_rand.Intn(contentLength-rangeStart)
  455. if rangeEnd == rangeStart || rangeStart >= contentLength-1 {
  456. continue
  457. }
  458. opt := &cos.ObjectGetOptions{
  459. Range: fmt.Sprintf("bytes=%v-%v", rangeStart, rangeEnd),
  460. Listener: &cos.DefaultProgressListener{},
  461. }
  462. resp, err := s.CClient.Object.Get(context.Background(), dest, opt)
  463. assert.Nil(s.T(), err, "GetObject Failed")
  464. defer resp.Body.Close()
  465. decryptedData, _ := ioutil.ReadAll(resp.Body)
  466. assert.Equal(s.T(), bytes.Compare(originData[rangeStart:rangeEnd+1], decryptedData), 0, "decryptData != originData")
  467. }
  468. // 解密读取
  469. resp, err := s.CClient.Object.Get(context.Background(), dest, nil)
  470. assert.Nil(s.T(), err, "GetObject Failed")
  471. defer resp.Body.Close()
  472. decryptedData, _ := ioutil.ReadAll(resp.Body)
  473. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  474. _, err = s.CClient.Object.Delete(context.Background(), dest)
  475. assert.Nil(s.T(), err, "DeleteObject Failed")
  476. }
  477. _, err = s.CClient.Object.Delete(context.Background(), name)
  478. assert.Nil(s.T(), err, "DeleteObject Failed")
  479. }
  480. func TestCosTestSuite(t *testing.T) {
  481. suite.Run(t, new(CosTestSuite))
  482. }
  483. func (s *CosTestSuite) TearDownSuite() {
  484. }