...

Source file src/cloud.google.com/go/storage/writer_test.go

Documentation: cloud.google.com/go/storage

     1  // Copyright 2014 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  
    15  package storage
    16  
    17  import (
    18  	"context"
    19  	"crypto/sha256"
    20  	"encoding/base64"
    21  	"errors"
    22  	"net/http"
    23  	"strings"
    24  	"testing"
    25  
    26  	"cloud.google.com/go/internal/testutil"
    27  	"google.golang.org/api/googleapi"
    28  	"google.golang.org/api/option"
    29  )
    30  
    31  var testEncryptionKey = []byte("secret-key-that-is-32-bytes-long")
    32  
    33  func TestErrorOnObjectsInsertCall(t *testing.T) {
    34  	t.Parallel()
    35  	ctx := context.Background()
    36  	const contents = "hello world"
    37  
    38  	doWrite := func(mt *mockTransport) *Writer {
    39  		client := mockClient(t, mt)
    40  		wc := client.Bucket("bucketname").Object("filename1").If(Conditions{DoesNotExist: true}).NewWriter(ctx)
    41  		wc.ContentType = "text/plain"
    42  
    43  		// We can't check that the Write fails, since it depends on the write to the
    44  		// underling mockTransport failing which is racy.
    45  		wc.Write([]byte(contents))
    46  		return wc
    47  	}
    48  
    49  	wc := doWrite(&mockTransport{})
    50  	// Close must always return an error though since it waits for the transport to
    51  	// have closed.
    52  	if err := wc.Close(); err == nil {
    53  		t.Errorf("expected error on close, got nil")
    54  	}
    55  
    56  	// Retry on 5xx
    57  	mt := &mockTransport{}
    58  	mt.addResult(&http.Response{StatusCode: 503, Body: bodyReader("")}, nil)
    59  	mt.addResult(&http.Response{StatusCode: 200, Body: bodyReader("{}")}, nil)
    60  
    61  	wc = doWrite(mt)
    62  	if err := wc.Close(); err != nil {
    63  		t.Errorf("got %v, want nil", err)
    64  	}
    65  	got := string(mt.gotBody)
    66  	if !strings.Contains(got, contents) {
    67  		t.Errorf("got body %q, which does not contain %q", got, contents)
    68  	}
    69  }
    70  
    71  func TestEncryption(t *testing.T) {
    72  	t.Parallel()
    73  	ctx := context.Background()
    74  	mt := &mockTransport{}
    75  	mt.addResult(&http.Response{StatusCode: 200, Body: bodyReader("{}")}, nil)
    76  	client := mockClient(t, mt)
    77  	obj := client.Bucket("bucketname").Object("filename1")
    78  	wc := obj.Key(testEncryptionKey).NewWriter(ctx)
    79  	if _, err := wc.Write([]byte("hello world")); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if err := wc.Close(); err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	if got, want := mt.gotReq.Header.Get("x-goog-encryption-algorithm"), "AES256"; got != want {
    86  		t.Errorf("algorithm: got %q, want %q", got, want)
    87  	}
    88  	gotKey, err := base64.StdEncoding.DecodeString(mt.gotReq.Header.Get("x-goog-encryption-key"))
    89  	if err != nil {
    90  		t.Fatalf("decoding key: %v", err)
    91  	}
    92  	if !testutil.Equal(gotKey, testEncryptionKey) {
    93  		t.Errorf("key: got %v, want %v", gotKey, testEncryptionKey)
    94  	}
    95  	wantHash := sha256.Sum256(testEncryptionKey)
    96  	gotHash, err := base64.StdEncoding.DecodeString(mt.gotReq.Header.Get("x-goog-encryption-key-sha256"))
    97  	if err != nil {
    98  		t.Fatalf("decoding hash: %v", err)
    99  	}
   100  	if !testutil.Equal(gotHash, wantHash[:]) { // wantHash is an array
   101  		t.Errorf("hash: got\n%v, want\n%v", gotHash, wantHash)
   102  	}
   103  
   104  	// Using a customer-supplied encryption key and a KMS key together is an error.
   105  	checkKMSError := func(msg string, err error) {
   106  		if err == nil {
   107  			t.Errorf("%s: got nil, want error", msg)
   108  		} else if !strings.Contains(err.Error(), "KMS") {
   109  			t.Errorf(`%s: got %q, want it to contain "KMS"`, msg, err)
   110  		}
   111  	}
   112  
   113  	wc = obj.Key(testEncryptionKey).NewWriter(ctx)
   114  	wc.KMSKeyName = "key"
   115  	_, err = wc.Write([]byte{})
   116  	checkKMSError("Write", err)
   117  	checkKMSError("Close", wc.Close())
   118  }
   119  
   120  // This test demonstrates the data race on Writer.err that can happen when the
   121  // Writer's context is cancelled. To see the race, comment out the w.mu.Lock/Unlock
   122  // lines in writer.go and run this test with -race.
   123  func TestRaceOnCancelAfterWrite(t *testing.T) {
   124  	client := mockClient(t, &mockTransport{})
   125  	cctx, cancel := context.WithCancel(context.Background())
   126  	w := client.Bucket("b").Object("o").NewWriter(cctx)
   127  	w.ChunkSize = googleapi.MinUploadChunkSize
   128  	buf := make([]byte, w.ChunkSize)
   129  	// This Write starts the goroutine in Writer.open. That reads the first chunk in its entirety
   130  	// before sending the request (see google.golang.org/api/gensupport.PrepareUpload),
   131  	// so to exhibit the race we must provide ChunkSize bytes.  The goroutine then makes the RPC (L137).
   132  	w.Write(buf)
   133  	// Canceling the context causes the call to return context.Canceled, which makes the open goroutine
   134  	// write to w.err (L151).
   135  	cancel()
   136  	// This call to Write concurrently reads w.err (L169).
   137  	w.Write([]byte(nil))
   138  }
   139  
   140  // This test checks for the read-write race condition that can occur if
   141  // monitorCancel is launched before the pipe is opened. See
   142  // https://github.com/googleapis/google-cloud-go/issues/6816
   143  func TestRaceOnCancelBeforeWrite(t *testing.T) {
   144  	ctx, cancel := context.WithCancel(context.Background())
   145  	client := mockClient(t, &mockTransport{})
   146  	cancel()
   147  	writer := client.Bucket("foo").Object("bar").NewWriter(ctx)
   148  	writer.ChunkSize = googleapi.MinUploadChunkSize
   149  	writer.Write([]byte("data"))
   150  
   151  	// We expect Close to return a context cancelled error, and the Writer should
   152  	// not open the pipe and avoid triggering a race condition.
   153  	if err := writer.Close(); !errors.Is(err, context.Canceled) {
   154  		t.Errorf("Writer.Close: got %v, want %v", err, context.Canceled)
   155  	}
   156  }
   157  
   158  func TestCancelDoesNotLeak(t *testing.T) {
   159  	ctx, cancel := context.WithCancel(context.Background())
   160  	const contents = "hello world"
   161  	mt := mockTransport{}
   162  
   163  	client, err := NewClient(ctx, option.WithHTTPClient(&http.Client{Transport: &mt}))
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  
   168  	wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
   169  	wc.ContentType = "text/plain"
   170  
   171  	// We can't check that the Write fails, since it depends on the write to the
   172  	// underling mockTransport failing which is racy.
   173  	wc.Write([]byte(contents))
   174  
   175  	cancel()
   176  }
   177  
   178  func TestCloseDoesNotLeak(t *testing.T) {
   179  	ctx := context.Background()
   180  	const contents = "hello world"
   181  	mt := mockTransport{}
   182  
   183  	client, err := NewClient(ctx, option.WithHTTPClient(&http.Client{Transport: &mt}))
   184  	if err != nil {
   185  		t.Fatal(err)
   186  	}
   187  
   188  	wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
   189  	wc.ContentType = "text/plain"
   190  
   191  	// We can't check that the Write fails, since it depends on the write to the
   192  	// underling mockTransport failing which is racy.
   193  	wc.Write([]byte(contents))
   194  
   195  	wc.Close()
   196  }
   197  

View as plain text