...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/util/crypto/crypto_test.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/util/crypto

     1  /*
     2  Copyright 2019 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 crypto
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/lithammer/dedent"
    23  
    24  	kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
    25  )
    26  
    27  func TestEncryptAndDecryptData(t *testing.T) {
    28  	key1, err := CreateRandBytes(kubeadmconstants.CertificateKeySize)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	key2, err := CreateRandBytes(kubeadmconstants.CertificateKeySize)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	testData := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
    37  
    38  	tests := map[string]struct {
    39  		encryptKey       []byte
    40  		decryptKey       []byte
    41  		data             []byte
    42  		expectDecryptErr bool
    43  	}{
    44  		"can decrypt using the correct key": {
    45  			encryptKey:       key1,
    46  			decryptKey:       key1,
    47  			data:             testData,
    48  			expectDecryptErr: false,
    49  		},
    50  		"can't decrypt using incorrect key": {
    51  			encryptKey:       key1,
    52  			decryptKey:       key2,
    53  			data:             testData,
    54  			expectDecryptErr: true,
    55  		},
    56  		"can't decrypt without a key": {
    57  			encryptKey:       key1,
    58  			decryptKey:       []byte{},
    59  			data:             testData,
    60  			expectDecryptErr: true,
    61  		},
    62  	}
    63  
    64  	for name, test := range tests {
    65  		t.Run(name, func(t2 *testing.T) {
    66  			encryptedData, err := EncryptBytes(test.data, test.encryptKey)
    67  			if err != nil {
    68  				t2.Fatalf(dedent.Dedent(
    69  					"EncryptBytes failed\nerror: %v"),
    70  					err,
    71  				)
    72  			}
    73  
    74  			decryptedData, err := DecryptBytes(encryptedData, test.decryptKey)
    75  			if (err != nil) != test.expectDecryptErr {
    76  				t2.Fatalf(dedent.Dedent(
    77  					"DecryptBytes failed\nexpected error: %t\n\tgot: %t\nerror: %v"),
    78  					test.expectDecryptErr,
    79  					(err != nil),
    80  					err,
    81  				)
    82  			}
    83  
    84  			if (string(decryptedData) != string(test.data)) && !test.expectDecryptErr {
    85  				t2.Fatalf(dedent.Dedent(
    86  					"EncryptDecryptBytes failed\nexpected decryptedData equal to data\n\tgot: data=%q decryptedData=%q"),
    87  					test.data,
    88  					string(decryptedData),
    89  				)
    90  			}
    91  		})
    92  	}
    93  }
    94  

View as plain text