error_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package oss
  2. import (
  3. "math"
  4. "net/http"
  5. . "gopkg.in/check.v1"
  6. )
  7. type OssErrorSuite struct{}
  8. var _ = Suite(&OssErrorSuite{})
  9. func (s *OssErrorSuite) TestCheckCRCHasCRCInResp(c *C) {
  10. headers := http.Header{
  11. "Expires": {"-1"},
  12. "Content-Length": {"0"},
  13. "Content-Encoding": {"gzip"},
  14. "X-Oss-Hash-Crc64ecma": {"0"},
  15. }
  16. resp := &Response{
  17. StatusCode: 200,
  18. Headers: headers,
  19. Body: nil,
  20. ClientCRC: math.MaxUint64,
  21. ServerCRC: math.MaxUint64,
  22. }
  23. err := checkCRC(resp, "test")
  24. c.Assert(err, IsNil)
  25. }
  26. func (s *OssErrorSuite) TestCheckCRCNotHasCRCInResp(c *C) {
  27. headers := http.Header{
  28. "Expires": {"-1"},
  29. "Content-Length": {"0"},
  30. "Content-Encoding": {"gzip"},
  31. }
  32. resp := &Response{
  33. StatusCode: 200,
  34. Headers: headers,
  35. Body: nil,
  36. ClientCRC: math.MaxUint64,
  37. ServerCRC: math.MaxUint32,
  38. }
  39. err := checkCRC(resp, "test")
  40. c.Assert(err, IsNil)
  41. }
  42. func (s *OssErrorSuite) TestCheckCRCCNegative(c *C) {
  43. headers := http.Header{
  44. "Expires": {"-1"},
  45. "Content-Length": {"0"},
  46. "Content-Encoding": {"gzip"},
  47. "X-Oss-Hash-Crc64ecma": {"0"},
  48. }
  49. resp := &Response{
  50. StatusCode: 200,
  51. Headers: headers,
  52. Body: nil,
  53. ClientCRC: 0,
  54. ServerCRC: math.MaxUint64,
  55. }
  56. err := checkCRC(resp, "test")
  57. c.Assert(err, NotNil)
  58. testLogger.Println("error:", err)
  59. }
  60. func (s *OssErrorSuite) TestCheckDownloadCRC(c *C) {
  61. err := checkDownloadCRC(0xFBF9D9603A6FA020, 0xFBF9D9603A6FA020)
  62. c.Assert(err, IsNil)
  63. err = checkDownloadCRC(0, 0)
  64. c.Assert(err, IsNil)
  65. err = checkDownloadCRC(0xDB6EFFF26AA94946, 0)
  66. c.Assert(err, NotNil)
  67. testLogger.Println("error:", err)
  68. }