crc_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. package oss
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "hash/crc64"
  6. "io"
  7. "io/ioutil"
  8. "math/rand"
  9. "os"
  10. "strings"
  11. "time"
  12. . "gopkg.in/check.v1"
  13. )
  14. type OssCrcSuite struct {
  15. client *Client
  16. bucket *Bucket
  17. }
  18. var _ = Suite(&OssCrcSuite{})
  19. // Run once when the suite starts running
  20. func (s *OssCrcSuite) SetUpSuite(c *C) {
  21. client, err := New(endpoint, accessID, accessKey)
  22. c.Assert(err, IsNil)
  23. s.client = client
  24. s.client.CreateBucket(bucketName)
  25. time.Sleep(5 * time.Second)
  26. bucket, err := s.client.Bucket(bucketName)
  27. c.Assert(err, IsNil)
  28. s.bucket = bucket
  29. testLogger.Println("test crc started")
  30. }
  31. // Run before each test or benchmark starts running
  32. func (s *OssCrcSuite) TearDownSuite(c *C) {
  33. // Delete Part
  34. lmur, err := s.bucket.ListMultipartUploads()
  35. c.Assert(err, IsNil)
  36. for _, upload := range lmur.Uploads {
  37. var imur = InitiateMultipartUploadResult{Bucket: s.bucket.BucketName,
  38. Key: upload.Key, UploadID: upload.UploadID}
  39. err = s.bucket.AbortMultipartUpload(imur)
  40. c.Assert(err, IsNil)
  41. }
  42. // Delete Objects
  43. lor, err := s.bucket.ListObjects()
  44. c.Assert(err, IsNil)
  45. for _, object := range lor.Objects {
  46. err = s.bucket.DeleteObject(object.Key)
  47. c.Assert(err, IsNil)
  48. }
  49. testLogger.Println("test crc completed")
  50. }
  51. // Run after each test or benchmark runs
  52. func (s *OssCrcSuite) SetUpTest(c *C) {
  53. err := removeTempFiles("../oss", ".jpg")
  54. c.Assert(err, IsNil)
  55. }
  56. // Run once after all tests or benchmarks have finished running
  57. func (s *OssCrcSuite) TearDownTest(c *C) {
  58. err := removeTempFiles("../oss", ".jpg")
  59. c.Assert(err, IsNil)
  60. }
  61. // TestCRCGolden 测试OSS实现的CRC64
  62. func (s *OssCrcSuite) TestCRCGolden(c *C) {
  63. type crcTest struct {
  64. out uint64
  65. in string
  66. }
  67. var crcGolden = []crcTest{
  68. {0x0, ""},
  69. {0x3420000000000000, "a"},
  70. {0x36c4200000000000, "ab"},
  71. {0x3776c42000000000, "abc"},
  72. {0x336776c420000000, "abcd"},
  73. {0x32d36776c4200000, "abcde"},
  74. {0x3002d36776c42000, "abcdef"},
  75. {0x31b002d36776c420, "abcdefg"},
  76. {0xe21b002d36776c4, "abcdefgh"},
  77. {0x8b6e21b002d36776, "abcdefghi"},
  78. {0x7f5b6e21b002d367, "abcdefghij"},
  79. {0x8ec0e7c835bf9cdf, "Discard medicine more than two years old."},
  80. {0xc7db1759e2be5ab4, "He who has a shady past knows that nice guys finish last."},
  81. {0xfbf9d9603a6fa020, "I wouldn't marry him with a ten foot pole."},
  82. {0xeafc4211a6daa0ef, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
  83. {0x3e05b21c7a4dc4da, "The days of the digital watch are numbered. -Tom Stoppard"},
  84. {0x5255866ad6ef28a6, "Nepal premier won't resign."},
  85. {0x8a79895be1e9c361, "For every action there is an equal and opposite government program."},
  86. {0x8878963a649d4916, "His money is twice tainted: 'taint yours and 'taint mine."},
  87. {0xa7b9d53ea87eb82f, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
  88. {0xdb6805c0966a2f9c, "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
  89. {0xf3553c65dacdadd2, "size: a.out: bad magic"},
  90. {0x9d5e034087a676b9, "The major problem is with sendmail. -Mark Horton"},
  91. {0xa6db2d7f8da96417, "Give me a rock, paper and scissors and I will move the world. CCFestoon"},
  92. {0x325e00cd2fe819f9, "If the enemy is within range, then so are you."},
  93. {0x88c6600ce58ae4c6, "It's well we cannot hear the screams/That we create in others' dreams."},
  94. {0x28c4a3f3b769e078, "You remind me of a TV show, but that's all right: I watch it anyway."},
  95. {0xa698a34c9d9f1dca, "C is as portable as Stonehedge!!"},
  96. {0xf6c1e2a8c26c5cfc, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
  97. {0xd402559dfe9b70c, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"},
  98. {0xdb6efff26aa94946, "How can you write a big system without C++? -Paul Glick"},
  99. }
  100. var tab = crc64.MakeTable(crc64.ISO)
  101. for i := 0; i < len(crcGolden); i++ {
  102. golden := crcGolden[i]
  103. crc := NewCRC(tab, 0)
  104. io.WriteString(crc, golden.in)
  105. sum := crc.Sum64()
  106. c.Assert(sum, Equals, golden.out)
  107. }
  108. }
  109. // testCRC64Combine test crc64 on vector[0..pos] which should have CRC-64 crc.
  110. // Also test CRC64Combine on vector[] split in two.
  111. func testCRC64Combine(c *C, str string, pos int, crc uint64) {
  112. tabECMA := crc64.MakeTable(crc64.ECMA)
  113. // test crc64
  114. hash := crc64.New(tabECMA)
  115. io.WriteString(hash, str)
  116. crc1 := hash.Sum64()
  117. c.Assert(crc1, Equals, crc)
  118. // test crc64 combine
  119. hash = crc64.New(tabECMA)
  120. io.WriteString(hash, str[0:pos])
  121. crc1 = hash.Sum64()
  122. hash = crc64.New(tabECMA)
  123. io.WriteString(hash, str[pos:len(str)])
  124. crc2 := hash.Sum64()
  125. crc1 = CRC64Combine(crc1, crc2, uint64(len(str)-pos))
  126. c.Assert(crc1, Equals, crc)
  127. }
  128. // TestCRCGolden 测试CRC64Combine
  129. func (s *OssCrcSuite) TestCRCCombine(c *C) {
  130. str := "123456789"
  131. testCRC64Combine(c, str, (len(str)+1)>>1, 0x995DC9BBDF1939FA)
  132. str = "This is a test of the emergency broadcast system."
  133. testCRC64Combine(c, str, (len(str)+1)>>1, 0x27DB187FC15BBC72)
  134. }
  135. // TestCRCGolden 测试CRC64Combine
  136. func (s *OssCrcSuite) TestCRCRepeatedCombine(c *C) {
  137. tab := crc64.MakeTable(crc64.ECMA)
  138. str := "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"
  139. for i := 0; i <= len(str); i++ {
  140. hash := crc64.New(tab)
  141. io.WriteString(hash, string(str[0:i]))
  142. prev := hash.Sum64()
  143. hash = crc64.New(tab)
  144. io.WriteString(hash, string(str[i:len(str)]))
  145. post := hash.Sum64()
  146. crc := CRC64Combine(prev, post, uint64(len(str)-i))
  147. testLogger.Println("TestCRCRepeatedCombine:", prev, post, crc, i, len(str))
  148. c.Assert(crc == 0x7AD25FAFA1710407, Equals, true)
  149. }
  150. }
  151. // TestCRCGolden 测试CRC64Combine
  152. func (s *OssCrcSuite) TestCRCRandomCombine(c *C) {
  153. tab := crc64.MakeTable(crc64.ECMA)
  154. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  155. body, err := ioutil.ReadFile(fileName)
  156. c.Assert(err, IsNil)
  157. for i := 0; i < 10; i++ {
  158. fileParts, err := SplitFileByPartNum(fileName, 1+rand.Intn(9999))
  159. c.Assert(err, IsNil)
  160. var crc uint64
  161. for _, part := range fileParts {
  162. calc := NewCRC(tab, 0)
  163. calc.Write(body[part.Offset : part.Offset+part.Size])
  164. crc = CRC64Combine(crc, calc.Sum64(), (uint64)(part.Size))
  165. }
  166. testLogger.Println("TestCRCRandomCombine:", crc, i, fileParts)
  167. c.Assert(crc == 0x2B612D24FFF64222, Equals, true)
  168. }
  169. }
  170. // TestEnableCRCAndMD5 开启MD5和CRC校验
  171. func (s *OssCrcSuite) TestEnableCRCAndMD5(c *C) {
  172. objectName := objectNamePrefix + "tecam"
  173. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  174. newFileName := "BingWallpaper-2015-11-07-2.jpg"
  175. objectValue := "空山新雨后,天气晚来秋。明月松间照,清泉石上流。竹喧归浣女,莲动下渔舟。随意春芳歇,王孙自可留。"
  176. client, err := New(endpoint, accessID, accessKey, EnableCRC(true), EnableMD5(true), MD5ThresholdCalcInMemory(200*1024))
  177. c.Assert(err, IsNil)
  178. bucket, err := client.Bucket(bucketName)
  179. c.Assert(err, IsNil)
  180. // PutObject
  181. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  182. c.Assert(err, IsNil)
  183. // GetObject
  184. body, err := bucket.GetObject(objectName)
  185. c.Assert(err, IsNil)
  186. _, err = ioutil.ReadAll(body)
  187. c.Assert(err, IsNil)
  188. body.Close()
  189. // GetObjectWithCRC
  190. getResult, err := bucket.DoGetObject(&GetObjectRequest{objectName}, nil)
  191. c.Assert(err, IsNil)
  192. str, err := readBody(getResult.Response.Body)
  193. c.Assert(err, IsNil)
  194. c.Assert(str, Equals, objectValue)
  195. c.Assert(getResult.ClientCRC.Sum64(), Equals, getResult.ServerCRC)
  196. // PutObjectFromFile
  197. err = bucket.PutObjectFromFile(objectName, fileName)
  198. c.Assert(err, IsNil)
  199. // GetObjectToFile
  200. err = bucket.GetObjectToFile(objectName, newFileName)
  201. c.Assert(err, IsNil)
  202. eq, err := compareFiles(fileName, newFileName)
  203. c.Assert(err, IsNil)
  204. c.Assert(eq, Equals, true)
  205. // DeleteObject
  206. err = bucket.DeleteObject(objectName)
  207. c.Assert(err, IsNil)
  208. // AppendObject
  209. var nextPos int64
  210. nextPos, err = bucket.AppendObject(objectName, strings.NewReader(objectValue), nextPos)
  211. c.Assert(err, IsNil)
  212. nextPos, err = bucket.AppendObject(objectName, strings.NewReader(objectValue), nextPos)
  213. c.Assert(err, IsNil)
  214. err = bucket.DeleteObject(objectName)
  215. c.Assert(err, IsNil)
  216. request := &AppendObjectRequest{
  217. ObjectKey: objectName,
  218. Reader: strings.NewReader(objectValue),
  219. Position: 0,
  220. }
  221. appendResult, err := bucket.DoAppendObject(request, []Option{InitCRC(0)})
  222. c.Assert(err, IsNil)
  223. request.Position = appendResult.NextPosition
  224. appendResult, err = bucket.DoAppendObject(request, []Option{InitCRC(appendResult.CRC)})
  225. c.Assert(err, IsNil)
  226. err = s.bucket.DeleteObject(objectName)
  227. c.Assert(err, IsNil)
  228. // MultipartUpload
  229. chunks, err := SplitFileByPartSize(fileName, 100*1024)
  230. imurUpload, err := bucket.InitiateMultipartUpload(objectName)
  231. c.Assert(err, IsNil)
  232. var partsUpload []UploadPart
  233. for _, chunk := range chunks {
  234. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
  235. c.Assert(err, IsNil)
  236. partsUpload = append(partsUpload, part)
  237. }
  238. _, err = bucket.CompleteMultipartUpload(imurUpload, partsUpload)
  239. c.Assert(err, IsNil)
  240. // Check MultipartUpload
  241. err = bucket.GetObjectToFile(objectName, newFileName)
  242. c.Assert(err, IsNil)
  243. eq, err = compareFiles(fileName, newFileName)
  244. c.Assert(err, IsNil)
  245. c.Assert(eq, Equals, true)
  246. // DeleteObjects
  247. _, err = bucket.DeleteObjects([]string{objectName})
  248. c.Assert(err, IsNil)
  249. }
  250. // TestDisableCRCAndMD5 关闭MD5和CRC校验
  251. func (s *OssCrcSuite) TestDisableCRCAndMD5(c *C) {
  252. objectName := objectNamePrefix + "tdcam"
  253. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  254. newFileName := "BingWallpaper-2015-11-07-3.jpg"
  255. objectValue := "中岁颇好道,晚家南山陲。兴来每独往,胜事空自知。行到水穷处,坐看云起时。偶然值林叟,谈笑无还期。"
  256. client, err := New(endpoint, accessID, accessKey, EnableCRC(false), EnableMD5(false))
  257. c.Assert(err, IsNil)
  258. bucket, err := client.Bucket(bucketName)
  259. c.Assert(err, IsNil)
  260. // PutObject
  261. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  262. c.Assert(err, IsNil)
  263. // GetObject
  264. body, err := bucket.GetObject(objectName)
  265. c.Assert(err, IsNil)
  266. _, err = ioutil.ReadAll(body)
  267. c.Assert(err, IsNil)
  268. body.Close()
  269. // GetObjectWithCRC
  270. getResult, err := bucket.DoGetObject(&GetObjectRequest{objectName}, nil)
  271. c.Assert(err, IsNil)
  272. str, err := readBody(getResult.Response.Body)
  273. c.Assert(err, IsNil)
  274. c.Assert(str, Equals, objectValue)
  275. // PutObjectFromFile
  276. err = bucket.PutObjectFromFile(objectName, fileName)
  277. c.Assert(err, IsNil)
  278. // GetObjectToFile
  279. err = bucket.GetObjectToFile(objectName, newFileName)
  280. c.Assert(err, IsNil)
  281. eq, err := compareFiles(fileName, newFileName)
  282. c.Assert(err, IsNil)
  283. c.Assert(eq, Equals, true)
  284. // DeleteObject
  285. err = bucket.DeleteObject(objectName)
  286. c.Assert(err, IsNil)
  287. // AppendObject
  288. var nextPos int64
  289. nextPos, err = bucket.AppendObject(objectName, strings.NewReader(objectValue), nextPos)
  290. c.Assert(err, IsNil)
  291. nextPos, err = bucket.AppendObject(objectName, strings.NewReader(objectValue), nextPos)
  292. c.Assert(err, IsNil)
  293. err = bucket.DeleteObject(objectName)
  294. c.Assert(err, IsNil)
  295. request := &AppendObjectRequest{
  296. ObjectKey: objectName,
  297. Reader: strings.NewReader(objectValue),
  298. Position: 0,
  299. }
  300. appendResult, err := bucket.DoAppendObject(request, []Option{InitCRC(0)})
  301. c.Assert(err, IsNil)
  302. request.Position = appendResult.NextPosition
  303. appendResult, err = bucket.DoAppendObject(request, []Option{InitCRC(appendResult.CRC)})
  304. c.Assert(err, IsNil)
  305. err = s.bucket.DeleteObject(objectName)
  306. c.Assert(err, IsNil)
  307. // MultipartUpload
  308. chunks, err := SplitFileByPartSize(fileName, 100*1024)
  309. imurUpload, err := bucket.InitiateMultipartUpload(objectName)
  310. c.Assert(err, IsNil)
  311. var partsUpload []UploadPart
  312. for _, chunk := range chunks {
  313. part, err := bucket.UploadPartFromFile(imurUpload, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number))
  314. c.Assert(err, IsNil)
  315. partsUpload = append(partsUpload, part)
  316. }
  317. _, err = bucket.CompleteMultipartUpload(imurUpload, partsUpload)
  318. c.Assert(err, IsNil)
  319. // Check MultipartUpload
  320. err = bucket.GetObjectToFile(objectName, newFileName)
  321. c.Assert(err, IsNil)
  322. eq, err = compareFiles(fileName, newFileName)
  323. c.Assert(err, IsNil)
  324. c.Assert(eq, Equals, true)
  325. // DeleteObjects
  326. _, err = bucket.DeleteObjects([]string{objectName})
  327. c.Assert(err, IsNil)
  328. }
  329. // TestSpecifyContentMD5 指定MD5
  330. func (s *OssCrcSuite) TestSpecifyContentMD5(c *C) {
  331. objectName := objectNamePrefix + "tdcam"
  332. fileName := "../sample/BingWallpaper-2015-11-07.jpg"
  333. objectValue := "积雨空林烟火迟,蒸藜炊黍饷东菑。漠漠水田飞白鹭,阴阴夏木啭黄鹂。山中习静观朝槿,松下清斋折露葵。野老与人争席罢,海鸥何事更相疑。"
  334. mh := md5.Sum([]byte(objectValue))
  335. md5B64 := base64.StdEncoding.EncodeToString(mh[:])
  336. // PutObject
  337. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue), ContentMD5(md5B64))
  338. c.Assert(err, IsNil)
  339. // PutObjectFromFile
  340. file, err := os.Open(fileName)
  341. md5 := md5.New()
  342. io.Copy(md5, file)
  343. mdHex := base64.StdEncoding.EncodeToString(md5.Sum(nil)[:])
  344. err = s.bucket.PutObjectFromFile(objectName, fileName, ContentMD5(mdHex))
  345. c.Assert(err, IsNil)
  346. err = s.bucket.DeleteObject(objectName)
  347. c.Assert(err, IsNil)
  348. // AppendObject
  349. var nextPos int64
  350. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader(objectValue), nextPos)
  351. c.Assert(err, IsNil)
  352. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader(objectValue), nextPos)
  353. c.Assert(err, IsNil)
  354. err = s.bucket.DeleteObject(objectName)
  355. c.Assert(err, IsNil)
  356. request := &AppendObjectRequest{
  357. ObjectKey: objectName,
  358. Reader: strings.NewReader(objectValue),
  359. Position: 0,
  360. }
  361. appendResult, err := s.bucket.DoAppendObject(request, []Option{InitCRC(0)})
  362. c.Assert(err, IsNil)
  363. request.Position = appendResult.NextPosition
  364. appendResult, err = s.bucket.DoAppendObject(request, []Option{InitCRC(appendResult.CRC)})
  365. c.Assert(err, IsNil)
  366. err = s.bucket.DeleteObject(objectName)
  367. c.Assert(err, IsNil)
  368. // MultipartUpload
  369. imurUpload, err := s.bucket.InitiateMultipartUpload(objectName)
  370. c.Assert(err, IsNil)
  371. var partsUpload []UploadPart
  372. part, err := s.bucket.UploadPart(imurUpload, strings.NewReader(objectValue), (int64)(len([]byte(objectValue))), 1)
  373. c.Assert(err, IsNil)
  374. partsUpload = append(partsUpload, part)
  375. _, err = s.bucket.CompleteMultipartUpload(imurUpload, partsUpload)
  376. c.Assert(err, IsNil)
  377. // DeleteObject
  378. err = s.bucket.DeleteObject(objectName)
  379. c.Assert(err, IsNil)
  380. }
  381. // TestCopyObjectToOrFromNegative
  382. func (s *OssCrcSuite) TestAppendObjectNegative(c *C) {
  383. objectName := objectNamePrefix + "taoncrc"
  384. objectValue := "空山不见人,但闻人语响。返影入深林,复照青苔上。"
  385. nextPos, err := s.bucket.AppendObject(objectName, strings.NewReader(objectValue), 0, InitCRC(0))
  386. c.Assert(err, IsNil)
  387. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader(objectValue), nextPos, InitCRC(0))
  388. c.Assert(err, NotNil)
  389. c.Assert(strings.HasPrefix(err.Error(), "oss: the crc"), Equals, true)
  390. }