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.

125 lines
4.1 KiB

  1. package cos
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func TestNewAuthorization(t *testing.T) {
  13. expectAuthorization := `q-sign-algorithm=sha1&q-ak=QmFzZTY0IGlzIGEgZ2VuZXJp&q-sign-time=1480932292;1481012292&q-key-time=1480932292;1481012292&q-header-list=host;x-cos-content-sha1;x-cos-stroage-class&q-url-param-list=&q-signature=ce4ac0ecbcdb30538b3fee0a97cc6389694ce53a`
  14. secretID := "QmFzZTY0IGlzIGEgZ2VuZXJp"
  15. secretKey := "AKIDZfbOA78asKUYBcXFrJD0a1ICvR98JM"
  16. host := "testbucket-125000000.cos.ap-guangzhou.myqcloud.com"
  17. uri := "http://testbucket-125000000.cos.ap-guangzhou.myqcloud.com/testfile2"
  18. startTime := time.Unix(int64(1480932292), 0)
  19. endTime := time.Unix(int64(1481012292), 0)
  20. req, _ := http.NewRequest("PUT", uri, nil)
  21. req.Header.Add("Host", host)
  22. req.Header.Add("x-cos-content-sha1", "db8ac1c259eb89d4a131b253bacfca5f319d54f2")
  23. req.Header.Add("x-cos-stroage-class", "nearline")
  24. authTime := &AuthTime{
  25. SignStartTime: startTime,
  26. SignEndTime: endTime,
  27. KeyStartTime: startTime,
  28. KeyEndTime: endTime,
  29. }
  30. auth := newAuthorization(secretID, secretKey, req, authTime)
  31. if auth != expectAuthorization {
  32. t.Errorf("NewAuthorization returned \n%#v, want \n%#v", auth, expectAuthorization)
  33. }
  34. }
  35. func TestAuthorizationTransport(t *testing.T) {
  36. setup()
  37. defer teardown()
  38. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  39. auth := r.Header.Get("Authorization")
  40. if auth == "" {
  41. t.Error("AuthorizationTransport didn't add Authorization header")
  42. }
  43. })
  44. client.client.Transport = &AuthorizationTransport{
  45. SecretID: "test",
  46. SecretKey: "test",
  47. }
  48. req, _ := http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
  49. client.doAPI(context.Background(), req, nil, true)
  50. }
  51. func TestCVMCredentialsTransport(t *testing.T) {
  52. setup()
  53. defer teardown()
  54. uri := client.BaseURL.BucketURL.String()
  55. ak := "test_ak"
  56. sk := "test_sk"
  57. token := "test_token"
  58. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  59. if r.Header.Get("x-cos-security-token") != token {
  60. t.Errorf("CVMCredentialsTransport x-cos-security-token error, want:%v, return:%v\n", token, r.Header.Get("x-cos-security-token"))
  61. }
  62. auth := r.Header.Get("Authorization")
  63. if auth == "" {
  64. t.Error("CVMCredentialsTransport didn't add Authorization header")
  65. }
  66. field := strings.Split(auth, "&")
  67. if len(field) != 7 {
  68. t.Errorf("CVMCredentialsTransport Authorization header format error: %v\n", auth)
  69. }
  70. st_et := strings.Split(strings.Split(field[2], "=")[1], ";")
  71. st, _ := strconv.ParseInt(st_et[0], 10, 64)
  72. et, _ := strconv.ParseInt(st_et[1], 10, 64)
  73. authTime := &AuthTime{
  74. SignStartTime: time.Unix(st, 0),
  75. SignEndTime: time.Unix(et, 0),
  76. KeyStartTime: time.Unix(st, 0),
  77. KeyEndTime: time.Unix(et, 0),
  78. }
  79. host := strings.TrimLeft(uri, "http://")
  80. req, _ := http.NewRequest("GET", uri, nil)
  81. req.Header.Add("Host", host)
  82. expect := newAuthorization(ak, sk, req, authTime)
  83. if expect != auth {
  84. t.Errorf("CVMCredentialsTransport Authorization error, want:%v, return:%v\n", expect, auth)
  85. }
  86. })
  87. // CVM http server
  88. cvm_mux := http.NewServeMux()
  89. cvm_server := httptest.NewServer(cvm_mux)
  90. defer cvm_server.Close()
  91. // 将默认 CVM Host 修改成测试IP:PORT
  92. defaultCVMMetaHost = strings.TrimLeft(cvm_server.URL, "http://")
  93. cvm_mux.HandleFunc("/"+defaultCVMCredURI, func(w http.ResponseWriter, r *http.Request) {
  94. fmt.Fprint(w, "cvm_read_cos_only")
  95. })
  96. cvm_mux.HandleFunc("/"+defaultCVMCredURI+"/cvm_read_cos_only", func(w http.ResponseWriter, r *http.Request) {
  97. fmt.Fprint(w, fmt.Sprintf(`{
  98. "TmpSecretId": "%s",
  99. "TmpSecretKey": "%s",
  100. "ExpiredTime": %v,
  101. "Expiration": "now",
  102. "Token": "%s",
  103. "Code": "Success"
  104. }`, ak, sk, time.Now().Unix()+3600, token))
  105. })
  106. client.client.Transport = &CVMCredentialsTransport{}
  107. req, _ := http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
  108. client.doAPI(context.Background(), req, nil, true)
  109. req, _ = http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
  110. client.doAPI(context.Background(), req, nil, true)
  111. }