fernet_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fernetool
  15. import (
  16. "crypto/rand"
  17. "testing"
  18. )
  19. func TestFernetKeys(t *testing.T) {
  20. m := SFernetKeyManager{}
  21. err := m.InitKeys("", 2)
  22. if err != nil {
  23. t.Fatalf("fail to initkeys %s", err)
  24. }
  25. buf := make([]byte, 128)
  26. for i := 0; i < 10; i += 1 {
  27. msgLen, err := rand.Read(buf)
  28. if err != nil {
  29. t.Fatalf("rand.Read fail %s", err)
  30. }
  31. msg, err := m.Encrypt(buf[:msgLen])
  32. if err != nil {
  33. t.Fatalf("fail to encrypt %s", err)
  34. }
  35. omsg := m.Decrypt(msg)
  36. if len(omsg) != msgLen {
  37. t.Fatalf("descrupt fail %s", err)
  38. }
  39. for i := 0; i < msgLen; i += 1 {
  40. if omsg[i] != buf[i] {
  41. t.Fatalf("not identical message!!")
  42. }
  43. }
  44. }
  45. }