2
0

sealer_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright © 2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package crypto_test
  16. import (
  17. . "github.com/mudler/edgevpn/pkg/utils"
  18. . "github.com/onsi/ginkgo/v2"
  19. . "github.com/onsi/gomega"
  20. . "github.com/mudler/edgevpn/pkg/crypto"
  21. )
  22. var _ = Describe("Crypto utilities", func() {
  23. Context("AESSealer", func() {
  24. It("Encode/decode", func() {
  25. key := RandStringRunes(32)
  26. message := "foo"
  27. s := &AESSealer{}
  28. encoded, err := s.Seal(message, key)
  29. Expect(err).ToNot(HaveOccurred())
  30. Expect(encoded).ToNot(Equal(key))
  31. Expect(len(encoded)).To(Equal(62))
  32. // Encode again
  33. encoded2, err := s.Seal(message, key)
  34. Expect(err).ToNot(HaveOccurred())
  35. // should differ
  36. Expect(encoded2).ToNot(Equal(encoded))
  37. // Decrypt and check
  38. decoded, err := s.Unseal(encoded, key)
  39. Expect(err).ToNot(HaveOccurred())
  40. Expect(decoded).To(Equal(message))
  41. decoded, err = s.Unseal(encoded2, key)
  42. Expect(err).ToNot(HaveOccurred())
  43. Expect(decoded).To(Equal(message))
  44. })
  45. })
  46. })