authtoken_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 clientman
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestEncoeDecode(t *testing.T) {
  20. SetupTest()
  21. token := SAuthToken{
  22. token: `gAAAAABe-gUMAawOPrP-mA4jY6-b1UPalPJw9WlZJVqHZMtc3IBKUOvHTbKm60YyZQtnVBa3O3QDfS2ss5_Xwi_n0L-jfuUstguLHfDyztAvT_IAKupw8YNK0FvJg25LKC4IR3bmDzCNzTwMO-rEeb4ha2e1vkGOwko9GT1Bn-xN7UM2qeEsm5PiLBg0ZTMuv4Jm5RWIXk2K`,
  23. verifyTotp: true,
  24. enableTotp: false,
  25. }
  26. et := token.encodeBytes()
  27. plainEt := compressString(et)
  28. encEt := EncryptString(et)
  29. t.Logf("origin token: %s", token.token)
  30. t.Logf("plain token: %s (%d)", plainEt, len(plainEt))
  31. t.Logf("encrypt token: %s (%d)", encEt, len(encEt))
  32. decBytes, err := decompressString(plainEt)
  33. if err != nil {
  34. t.Fatalf("decompressString fail %s", err)
  35. }
  36. decBytes2, err := DecryptString(encEt)
  37. if err != nil {
  38. t.Fatalf("decryptString fail %s", err)
  39. }
  40. if !reflect.DeepEqual(decBytes, decBytes2) {
  41. t.Fatalf("decompress and decrypt bytes not equal!")
  42. }
  43. token2, err := decodeBytes(decBytes)
  44. if err != nil {
  45. t.Fatalf("decodeBytes fail %s", err)
  46. }
  47. if *token2 != token {
  48. t.Fatalf("token2 != token")
  49. }
  50. }