sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/internal/bytes/bytes_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package bytes
    18  
    19  import (
    20  	crand "crypto/rand"
    21  	"encoding/base64"
    22  	"fmt"
    23  	"math"
    24  	"math/rand"
    25  	"testing"
    26  	"time"
    27  
    28  	. "github.com/onsi/gomega"
    29  )
    30  
    31  func init() {
    32  	rand.Seed(time.Now().Unix())
    33  }
    34  
    35  func TestSplitBytes(t *testing.T) {
    36  	g := NewWithT(t)
    37  
    38  	t.Run("should call 1 time if it fits", func(t *testing.T) {
    39  		maxSize := 100
    40  		input := make([]byte, 100)
    41  		_, err := crand.Read(input)
    42  		g.Expect(err).To(BeNil())
    43  
    44  		count := 0
    45  		Split(input, false, maxSize, func(split []byte) {
    46  			g.Expect(split).To(BeEquivalentTo(input))
    47  			count++
    48  		})
    49  		g.Expect(count).To(BeEquivalentTo(1))
    50  	})
    51  
    52  	t.Run("should properly encode and split given random input and maxsize", func(t *testing.T) {
    53  		maxSize := 1 + rand.Intn(1024)
    54  		input := make([]byte, rand.Intn(24576))
    55  		_, err := crand.Read(input)
    56  		g.Expect(err).To(BeNil())
    57  
    58  		data := []byte{}
    59  		Split(input, true, maxSize, func(split []byte) {
    60  			data = append(data, split...)
    61  		})
    62  
    63  		decoded := make([]byte, base64.StdEncoding.DecodedLen(len(data)))
    64  		l, err := base64.StdEncoding.Decode(decoded, data)
    65  		g.Expect(err).To(BeNil())
    66  
    67  		decoded = decoded[:l]
    68  
    69  		g.Expect(decoded).To(BeEquivalentTo(input))
    70  	})
    71  
    72  	t.Run("should properly split given random input and maxsize", func(t *testing.T) {
    73  		maxSize := 1 + rand.Intn(1024)
    74  		input := make([]byte, rand.Intn(24576))
    75  		_, err := crand.Read(input)
    76  		g.Expect(err).To(BeNil())
    77  
    78  		expected := len(input) / maxSize
    79  		if math.Mod(float64(len(input)), float64(maxSize)) > 0 {
    80  			// Add 1 to expected if there is remaining bytes left at the end of all the splits.
    81  			expected++
    82  		}
    83  
    84  		data := []byte{}
    85  		count := 0
    86  		Split(input, false, maxSize, func(split []byte) {
    87  			data = append(data, split...)
    88  			count++
    89  		})
    90  
    91  		g.Expect(data).To(BeEquivalentTo(input))
    92  		g.Expect(expected).To(BeEquivalentTo(count), fmt.Sprintf("input=%d, maxsize=%d", len(input), maxSize))
    93  	})
    94  
    95  	t.Run("should call 0 times if there is no data", func(t *testing.T) {
    96  		maxSize := 100
    97  		input := []byte{}
    98  
    99  		count := 0
   100  		Split(input, false, maxSize, func(split []byte) {
   101  			t.Fail()
   102  		})
   103  		g.Expect(count).To(BeEquivalentTo(0))
   104  	})
   105  }