key_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 utils
  15. import (
  16. "testing"
  17. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  18. )
  19. func TestKey(t *testing.T) {
  20. t.Run("parse", func(t *testing.T) {
  21. keyStr := "WJYVsrTtAae1QS9YzefV4OmVM6mkJglR+GEgxQpTs2g="
  22. pubkeyStr := "mOX0S5AuRqd8lQZWcqTlzOS+veo404gE7NyV4u3xVkg="
  23. pubkeyX, err := wgtypes.ParseKey(pubkeyStr)
  24. if err != nil {
  25. t.Fatalf("parse public key failed: %v", err)
  26. }
  27. key, err := wgtypes.ParseKey(keyStr)
  28. if err != nil {
  29. t.Fatalf("parse private key failed: %v", err)
  30. }
  31. pubkey := key.PublicKey()
  32. if pubkey != pubkeyX {
  33. t.Errorf("derived public key does not match expected.\ngot %#v\nwant %#v", pubkey, pubkeyX)
  34. }
  35. })
  36. t.Run("generate", func(t *testing.T) {
  37. key, err := wgtypes.GeneratePrivateKey()
  38. if err != nil {
  39. t.Fatalf("generate private key failed: %v", err)
  40. }
  41. pubkey := key.PublicKey()
  42. t.Logf("private key: %s", key)
  43. t.Logf(" public key: %s", pubkey)
  44. })
  45. }