1
0

pcrypto_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  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 pcrypto
  15. import (
  16. "testing"
  17. )
  18. var (
  19. pp *Pcrypto
  20. )
  21. func init() {
  22. pp = &Pcrypto{}
  23. pp.Init([]byte("12234567890123451223456789012345321:wq"))
  24. }
  25. func TestEncrypt(t *testing.T) {
  26. testStr := "Test Encrypt!"
  27. res, err := pp.Encrypt([]byte(testStr))
  28. if err != nil {
  29. t.Fatalf("encrypt error: %v", err)
  30. }
  31. res, err = pp.Decrypt([]byte(res))
  32. if err != nil {
  33. t.Fatalf("decrypt error: %v", err)
  34. }
  35. if string(res) != testStr {
  36. t.Fatalf("test encrypt error, from [%s] to [%s]", testStr, string(res))
  37. }
  38. }
  39. func TestCompression(t *testing.T) {
  40. testStr := "Test Compression!"
  41. res, err := pp.Compression([]byte(testStr))
  42. if err != nil {
  43. t.Fatalf("compression error: %v", err)
  44. }
  45. res, err = pp.Decompression(res)
  46. if err != nil {
  47. t.Fatalf("decompression error: %v", err)
  48. }
  49. if string(res) != testStr {
  50. t.Fatalf("test compression error, from [%s] to [%s]", testStr, string(res))
  51. }
  52. }
  53. func BenchmarkEncrypt(b *testing.B) {
  54. testStr := "Test Encrypt!"
  55. for i := 0; i < b.N; i++ {
  56. pp.Encrypt([]byte(testStr))
  57. }
  58. }
  59. func BenchmarkDecrypt(b *testing.B) {
  60. testStr := "Test Encrypt!"
  61. res, _ := pp.Encrypt([]byte(testStr))
  62. for i := 0; i < b.N; i++ {
  63. pp.Decrypt([]byte(res))
  64. }
  65. }