github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/encryption/fallback_strategy_test.go (about)

     1  package encryption_test
     2  
     3  import (
     4  	"github.com/pf-qiu/concourse/v6/atc/db/encryption"
     5  	"github.com/pf-qiu/concourse/v6/atc/db/encryption/encryptionfakes"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("Encryption Key with Fallback", func() {
    11  	var (
    12  		key       *encryption.FallbackStrategy
    13  		strategy1 *encryptionfakes.FakeStrategy
    14  		strategy2 *encryptionfakes.FakeStrategy
    15  	)
    16  
    17  	BeforeEach(func() {
    18  		strategy1 = &encryptionfakes.FakeStrategy{}
    19  		strategy2 = &encryptionfakes.FakeStrategy{}
    20  
    21  		key = encryption.NewFallbackStrategy(strategy1, strategy2)
    22  	})
    23  
    24  	Context("when the main key is valid", func() {
    25  		It("decrypts plaintext using the main key", func() {
    26  			strategy1.DecryptReturns([]byte("plaintext"), nil)
    27  
    28  			decryptedText, err := key.Decrypt("ciphertext", nil)
    29  			Expect(err).ToNot(HaveOccurred())
    30  			Expect(decryptedText).To(Equal([]byte("plaintext")))
    31  		})
    32  	})
    33  
    34  	Context("when the main key is invalid", func() {
    35  		It("decrypts plaintext using the fallback key", func() {
    36  			strategy1.DecryptReturns(nil, encryption.ErrDataIsEncrypted)
    37  			strategy2.DecryptReturns([]byte("plaintext"), nil)
    38  
    39  			decryptedText, err := key.Decrypt("ciphertext", nil)
    40  			Expect(err).ToNot(HaveOccurred())
    41  			Expect(decryptedText).To(Equal([]byte("plaintext")))
    42  		})
    43  	})
    44  
    45  	Context("when both keys to decrypt are invalid", func() {
    46  		It("return with an error", func() {
    47  			strategy1.DecryptReturns(nil, encryption.ErrDataIsEncrypted)
    48  			strategy2.DecryptReturns(nil, encryption.ErrDataIsEncrypted)
    49  
    50  			_, err := key.Decrypt("ciphertext", nil)
    51  			Expect(err).To(HaveOccurred())
    52  		})
    53  	})
    54  })
    55