interfaces_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 interfaces
  5. import (
  6. "encoding/json"
  7. "testing"
  8. "inet.af/netaddr"
  9. )
  10. func TestGetState(t *testing.T) {
  11. st, err := GetState()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. j, err := json.MarshalIndent(st, "", "\t")
  16. if err != nil {
  17. t.Errorf("JSON: %v", err)
  18. }
  19. t.Logf("Got: %s", j)
  20. t.Logf("As string: %s", st)
  21. st2, err := GetState()
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. if !st.EqualFiltered(st2, FilterAll) {
  26. // let's assume nobody was changing the system network interfaces between
  27. // the two GetState calls.
  28. t.Fatal("two States back-to-back were not equal")
  29. }
  30. t.Logf("As string:\n\t%s", st)
  31. }
  32. func TestLikelyHomeRouterIP(t *testing.T) {
  33. gw, my, ok := LikelyHomeRouterIP()
  34. if !ok {
  35. t.Logf("no result")
  36. return
  37. }
  38. t.Logf("myIP = %v; gw = %v", my, gw)
  39. }
  40. func TestIsUsableV6(t *testing.T) {
  41. tests := []struct {
  42. name string
  43. ip string
  44. want bool
  45. }{
  46. {"first ULA", "fc00::1", true},
  47. {"Tailscale", "fd7a:115c:a1e0::1", false},
  48. {"Cloud Run", "fddf:3978:feb1:d745::1", true},
  49. {"zeros", "0000:0000:0000:0000:0000:0000:0000:0000", false},
  50. {"Link Local", "fe80::1", false},
  51. {"Global", "2602::1", true},
  52. {"IPv4 public", "192.0.2.1", false},
  53. {"IPv4 private", "192.168.1.1", false},
  54. }
  55. for _, test := range tests {
  56. if got := isUsableV6(netaddr.MustParseIP(test.ip)); got != test.want {
  57. t.Errorf("isUsableV6(%s) = %v, want %v", test.name, got, test.want)
  58. }
  59. }
  60. }