您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

81 行
2.3 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "net/http"
  17. "strings"
  18. "testing"
  19. "golang.org/x/net/context"
  20. "google.golang.org/api/option"
  21. )
  22. func TestCopyMissingFields(t *testing.T) {
  23. // Verify that copying checks for missing fields.a
  24. t.Parallel()
  25. var tests = []struct {
  26. srcBucket, srcName, destBucket, destName string
  27. errMsg string
  28. }{
  29. {
  30. "mybucket", "", "mybucket", "destname",
  31. "name is empty",
  32. },
  33. {
  34. "mybucket", "srcname", "mybucket", "",
  35. "name is empty",
  36. },
  37. {
  38. "", "srcfile", "mybucket", "destname",
  39. "name is empty",
  40. },
  41. {
  42. "mybucket", "srcfile", "", "destname",
  43. "name is empty",
  44. },
  45. }
  46. ctx := context.Background()
  47. client, err := NewClient(ctx, option.WithHTTPClient(&http.Client{Transport: &fakeTransport{}}))
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. for i, test := range tests {
  52. src := client.Bucket(test.srcBucket).Object(test.srcName)
  53. dst := client.Bucket(test.destBucket).Object(test.destName)
  54. _, err := dst.CopierFrom(src).Run(ctx)
  55. if !strings.Contains(err.Error(), test.errMsg) {
  56. t.Errorf("CopyTo test #%v:\ngot err %q\nwant err %q", i, err, test.errMsg)
  57. }
  58. }
  59. }
  60. func TestCopyBothEncryptionKeys(t *testing.T) {
  61. // Test that using both a customer-supplied key and a KMS key is an error.
  62. ctx := context.Background()
  63. client, err := NewClient(ctx, option.WithHTTPClient(&http.Client{Transport: &fakeTransport{}}))
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. dest := client.Bucket("b").Object("d").Key(testEncryptionKey)
  68. c := dest.CopierFrom(client.Bucket("b").Object("s"))
  69. c.DestinationKMSKeyName = "key"
  70. if _, err := c.Run(ctx); err == nil {
  71. t.Error("got nil, want error")
  72. } else if !strings.Contains(err.Error(), "KMS") {
  73. t.Errorf(`got %q, want it to contain "KMS"`, err)
  74. }
  75. }