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.
 
 
 

72 lines
2.1 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. "context"
  17. "strings"
  18. "testing"
  19. )
  20. func TestCopyMissingFields(t *testing.T) {
  21. // Verify that copying checks for missing fields.a
  22. t.Parallel()
  23. var tests = []struct {
  24. srcBucket, srcName, destBucket, destName string
  25. errMsg string
  26. }{
  27. {
  28. "mybucket", "", "mybucket", "destname",
  29. "name is empty",
  30. },
  31. {
  32. "mybucket", "srcname", "mybucket", "",
  33. "name is empty",
  34. },
  35. {
  36. "", "srcfile", "mybucket", "destname",
  37. "name is empty",
  38. },
  39. {
  40. "mybucket", "srcfile", "", "destname",
  41. "name is empty",
  42. },
  43. }
  44. ctx := context.Background()
  45. client := mockClient(t, &mockTransport{})
  46. for i, test := range tests {
  47. src := client.Bucket(test.srcBucket).Object(test.srcName)
  48. dst := client.Bucket(test.destBucket).Object(test.destName)
  49. _, err := dst.CopierFrom(src).Run(ctx)
  50. if !strings.Contains(err.Error(), test.errMsg) {
  51. t.Errorf("CopyTo test #%v:\ngot err %q\nwant err %q", i, err, test.errMsg)
  52. }
  53. }
  54. }
  55. func TestCopyBothEncryptionKeys(t *testing.T) {
  56. // Test that using both a customer-supplied key and a KMS key is an error.
  57. ctx := context.Background()
  58. client := mockClient(t, &mockTransport{})
  59. dest := client.Bucket("b").Object("d").Key(testEncryptionKey)
  60. c := dest.CopierFrom(client.Bucket("b").Object("s"))
  61. c.DestinationKMSKeyName = "key"
  62. if _, err := c.Run(ctx); err == nil {
  63. t.Error("got nil, want error")
  64. } else if !strings.Contains(err.Error(), "KMS") {
  65. t.Errorf(`got %q, want it to contain "KMS"`, err)
  66. }
  67. }