direct_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package controlclient
  5. import (
  6. "encoding/json"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "time"
  11. "inet.af/netaddr"
  12. "tailscale.com/hostinfo"
  13. "tailscale.com/ipn/ipnstate"
  14. "tailscale.com/tailcfg"
  15. "tailscale.com/types/key"
  16. )
  17. func TestNewDirect(t *testing.T) {
  18. hi := hostinfo.New()
  19. ni := tailcfg.NetInfo{LinkType: "wired"}
  20. hi.NetInfo = &ni
  21. k := key.NewMachine()
  22. opts := Options{
  23. ServerURL: "https://example.com",
  24. Hostinfo: hi,
  25. GetMachinePrivateKey: func() (key.MachinePrivate, error) {
  26. return k, nil
  27. },
  28. }
  29. c, err := NewDirect(opts)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. if c.serverURL != opts.ServerURL {
  34. t.Errorf("c.serverURL got %v want %v", c.serverURL, opts.ServerURL)
  35. }
  36. if !hi.Equal(c.hostinfo) {
  37. t.Errorf("c.hostinfo got %v want %v", c.hostinfo, hi)
  38. }
  39. changed := c.SetNetInfo(&ni)
  40. if changed {
  41. t.Errorf("c.SetNetInfo(ni) want false got %v", changed)
  42. }
  43. ni = tailcfg.NetInfo{LinkType: "wifi"}
  44. changed = c.SetNetInfo(&ni)
  45. if !changed {
  46. t.Errorf("c.SetNetInfo(ni) want true got %v", changed)
  47. }
  48. changed = c.SetHostinfo(hi)
  49. if changed {
  50. t.Errorf("c.SetHostinfo(hi) want false got %v", changed)
  51. }
  52. hi = hostinfo.New()
  53. hi.Hostname = "different host name"
  54. changed = c.SetHostinfo(hi)
  55. if !changed {
  56. t.Errorf("c.SetHostinfo(hi) want true got %v", changed)
  57. }
  58. endpoints := fakeEndpoints(1, 2, 3)
  59. changed = c.newEndpoints(12, endpoints)
  60. if !changed {
  61. t.Errorf("c.newEndpoints(12) want true got %v", changed)
  62. }
  63. changed = c.newEndpoints(12, endpoints)
  64. if changed {
  65. t.Errorf("c.newEndpoints(12) want false got %v", changed)
  66. }
  67. changed = c.newEndpoints(13, endpoints)
  68. if !changed {
  69. t.Errorf("c.newEndpoints(13) want true got %v", changed)
  70. }
  71. endpoints = fakeEndpoints(4, 5, 6)
  72. changed = c.newEndpoints(13, endpoints)
  73. if !changed {
  74. t.Errorf("c.newEndpoints(13) want true got %v", changed)
  75. }
  76. }
  77. func fakeEndpoints(ports ...uint16) (ret []tailcfg.Endpoint) {
  78. for _, port := range ports {
  79. ret = append(ret, tailcfg.Endpoint{
  80. Addr: netaddr.IPPortFrom(netaddr.IP{}, port),
  81. })
  82. }
  83. return
  84. }
  85. func TestTsmpPing(t *testing.T) {
  86. hi := hostinfo.New()
  87. ni := tailcfg.NetInfo{LinkType: "wired"}
  88. hi.NetInfo = &ni
  89. k := key.NewMachine()
  90. opts := Options{
  91. ServerURL: "https://example.com",
  92. Hostinfo: hi,
  93. GetMachinePrivateKey: func() (key.MachinePrivate, error) {
  94. return k, nil
  95. },
  96. }
  97. c, err := NewDirect(opts)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. pingRes := &ipnstate.PingResult{
  102. IP: "123.456.7890",
  103. Err: "",
  104. NodeName: "testnode",
  105. }
  106. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  107. defer r.Body.Close()
  108. body := new(ipnstate.PingResult)
  109. if err := json.NewDecoder(r.Body).Decode(body); err != nil {
  110. t.Fatal(err)
  111. }
  112. if pingRes.IP != body.IP {
  113. t.Fatalf("PingResult did not have the correct IP : got %v, expected : %v", body.IP, pingRes.IP)
  114. }
  115. w.WriteHeader(200)
  116. }))
  117. defer ts.Close()
  118. now := time.Now()
  119. pr := &tailcfg.PingRequest{
  120. URL: ts.URL,
  121. }
  122. err = postPingResult(now, t.Logf, c.httpc, pr, pingRes)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. }