locator.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package main
  14. import (
  15. "bytes"
  16. "fmt"
  17. "zerotier/pkg/zerotier"
  18. )
  19. func TestLocator() bool {
  20. fmt.Printf("Creating Endpoint instances... ")
  21. ep0, err := zerotier.NewEndpointFromString("1.1.1.1/1")
  22. if err != nil {
  23. fmt.Printf("IPv4 FAILED (%s)\n",err.Error())
  24. return false
  25. }
  26. ep1, err := zerotier.NewEndpointFromString("2600:1901:0:4006::1234/2")
  27. if err != nil {
  28. fmt.Printf("IPv6 FAILED (%s)\n",err.Error())
  29. return false
  30. }
  31. eps := []*zerotier.Endpoint{ep0, ep1}
  32. fmt.Printf("OK\n")
  33. fmt.Printf("Creating signing Identity... ")
  34. signer, err := zerotier.NewIdentity(zerotier.IdentityTypeP384)
  35. if err != nil {
  36. fmt.Printf("FAILED (%s)\n", err.Error())
  37. return false
  38. }
  39. fmt.Printf("OK %s\n",signer.String())
  40. fmt.Printf("Creating Locator instance... ")
  41. loc, err := zerotier.NewLocator(zerotier.TimeMs(), eps, signer)
  42. if err != nil {
  43. fmt.Printf("FAILED (%s)\n",err.Error())
  44. return false
  45. }
  46. locStr := loc.String()
  47. locBytes := loc.Bytes()
  48. fmt.Printf("OK (%d bytes)\n",len(locBytes))
  49. fmt.Printf("Testing Locator Validate()... ")
  50. if !loc.Validate(signer) {
  51. fmt.Printf("FAILED (should have validated)\n")
  52. return false
  53. }
  54. fmt.Printf("OK\n")
  55. fmt.Printf("Testing Locator marshal/unmarshal... ")
  56. loc2, err := zerotier.NewLocatorFromString(locStr)
  57. if err != nil {
  58. fmt.Printf("FAILED (%s)\n",err.Error())
  59. return false
  60. }
  61. if !bytes.Equal(locBytes, loc2.Bytes()) {
  62. fmt.Printf("FAILED (not equal)\n")
  63. return false
  64. }
  65. loc2, err = zerotier.NewLocatorFromBytes(locBytes)
  66. if err != nil {
  67. fmt.Printf("FAILED (%s)\n",err.Error())
  68. return false
  69. }
  70. if !bytes.Equal(locBytes, loc2.Bytes()) {
  71. fmt.Printf("FAILED (not equal)\n")
  72. return false
  73. }
  74. fmt.Printf("OK\n")
  75. return true
  76. }