sigs.k8s.io/external-dns@v0.14.1/endpoint/crypto_test.go (about)

     1  /*
     2  Copyright 2017 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 endpoint
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestEncrypt(t *testing.T) {
    26  	// Verify that text encryption and decryption works
    27  	aesKey := []byte("s%zF`.*'5`9.AhI2!B,.~hmbs^.*TL?;")
    28  	plaintext := "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
    29  	encryptedtext, err := EncryptText(plaintext, aesKey, nil)
    30  	require.NoError(t, err)
    31  	decryptedtext, _, err := DecryptText(encryptedtext, aesKey)
    32  	require.NoError(t, err)
    33  	if plaintext != decryptedtext {
    34  		t.Errorf("Original plain text %#v differs from the resulting decrypted text %#v", plaintext, decryptedtext)
    35  	}
    36  
    37  	// Verify that decrypt returns an error and empty data if wrong AES encryption key is used
    38  	decryptedtext, _, err = DecryptText(encryptedtext, []byte("s'J!jD`].LC?g&Oa11AgTub,j48ts/96"))
    39  	require.Error(t, err)
    40  	if decryptedtext != "" {
    41  		t.Error("Data decryption failed, empty string should be as result")
    42  	}
    43  
    44  	// Verify that decrypt returns an error and empty data if unencrypted input is is supplied
    45  	decryptedtext, _, err = DecryptText(plaintext, aesKey)
    46  	require.Error(t, err)
    47  	if decryptedtext != "" {
    48  		t.Errorf("Data decryption failed, empty string should be as result")
    49  	}
    50  
    51  	// Verify that a known encrypted text is decrypted to what is expected
    52  	encryptedtext = "0Mfzf6wsN8llrfX0ucDZ6nlc2+QiQfKKedjPPLu5atb2I35L9nUZeJcCnuLVW7CVW3K0h94vSuBLdXnMrj8Vcm0M09shxaoF48IcCpD03XtQbKXqk2hPbsW6+JybvplHIQGr16/PcjUSObGmR9yjf38+qEltApkKvrPjsyw43BX4eE10rL0Bln33UJD7/w+zazRDPFlAcbGtkt0ETKHnvyB3/aCddLipvrhjCXj2ZY/ktRF6h716kJRgXU10dCIQHFYU45MIdxI+k10HK3yZqhI2V0Gp2xjrFV/LRQ7/OS9SFee4asPWUYxbCEsnOzp8qc0dCPFSo1dtADzWnUZnsAcbnjtudT4milfLJc5CxDk1v3ykqQ/ajejwHjWQ7b8U6AsTErbezfdcqrb5IzkLgHb5TosnfrdDmNc9GcKfpsrCHbVY8KgNwMVdtwavLv7d9WM6sooUlZ3t0sABGkzagXQmPRvwLnkSOlie5XrnzWo8/8/4UByLga29CaXO"
    53  	decryptedtext, _, err = DecryptText(encryptedtext, aesKey)
    54  	require.NoError(t, err)
    55  	if decryptedtext != plaintext {
    56  		t.Error("Decryption of text didn't result in expected plaintext result.")
    57  	}
    58  }