...

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

Documentation: cloud.google.com/go/storage

     1  // Copyright 2021 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  	"reflect"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  )
    24  
    25  func TestPostPolicyV4OptionsClone(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	opts := &PostPolicyV4Options{
    29  		GoogleAccessID: "accessID",
    30  		PrivateKey:     []byte{},
    31  		SignBytes: func(b []byte) ([]byte, error) {
    32  			return b, nil
    33  		},
    34  		SignRawBytes: func(bytes []byte) ([]byte, error) {
    35  			return bytes, nil
    36  		},
    37  		Expires:             time.Now(),
    38  		Style:               VirtualHostedStyle(),
    39  		Insecure:            true,
    40  		Fields:              &PolicyV4Fields{ACL: "test-acl"},
    41  		Conditions:          []PostPolicyV4Condition{},
    42  		shouldHashSignBytes: true,
    43  		Hostname:            "localhost:9000",
    44  	}
    45  
    46  	// Check that all fields are set to a non-zero value, so we can check that
    47  	// clone accurately clones all fields and catch newly added fields not cloned
    48  	reflectOpts := reflect.ValueOf(*opts)
    49  	for i := 0; i < reflectOpts.NumField(); i++ {
    50  		zero, err := isZeroValue(reflectOpts.Field(i))
    51  		if err != nil {
    52  			t.Errorf("IsZero: %v", err)
    53  		}
    54  		if zero {
    55  			t.Errorf("PostPolicyV4Options field %d not set", i)
    56  		}
    57  	}
    58  
    59  	// Check that fields are properly cloned
    60  	optsClone := opts.clone()
    61  
    62  	// We need a special comparer for functions
    63  	signBytesComp := func(a func([]byte) ([]byte, error), b func([]byte) ([]byte, error)) bool {
    64  		return reflect.ValueOf(a) == reflect.ValueOf(b)
    65  	}
    66  
    67  	if diff := cmp.Diff(opts, optsClone, cmp.Comparer(signBytesComp),
    68  		cmp.AllowUnexported(PostPolicyV4Options{})); diff != "" {
    69  		t.Errorf("clone does not match (original: -, cloned: +):\n%s", diff)
    70  	}
    71  }
    72  

View as plain text