controlclient_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "reflect"
  7. "testing"
  8. "tailscale.com/types/empty"
  9. )
  10. func fieldsOf(t reflect.Type) (fields []string) {
  11. for i := 0; i < t.NumField(); i++ {
  12. if name := t.Field(i).Name; name != "_" {
  13. fields = append(fields, name)
  14. }
  15. }
  16. return
  17. }
  18. func TestStatusEqual(t *testing.T) {
  19. // Verify that the Equal method stays in sync with reality
  20. equalHandles := []string{"LoginFinished", "LogoutFinished", "Err", "URL", "NetMap", "State", "Persist", "Hostinfo"}
  21. if have := fieldsOf(reflect.TypeOf(Status{})); !reflect.DeepEqual(have, equalHandles) {
  22. t.Errorf("Status.Equal check might be out of sync\nfields: %q\nhandled: %q\n",
  23. have, equalHandles)
  24. }
  25. tests := []struct {
  26. a, b *Status
  27. want bool
  28. }{
  29. {
  30. &Status{},
  31. nil,
  32. false,
  33. },
  34. {
  35. nil,
  36. &Status{},
  37. false,
  38. },
  39. {
  40. nil,
  41. nil,
  42. true,
  43. },
  44. {
  45. &Status{},
  46. &Status{},
  47. true,
  48. },
  49. {
  50. &Status{State: StateNew},
  51. &Status{State: StateNew},
  52. true,
  53. },
  54. {
  55. &Status{State: StateNew},
  56. &Status{State: StateAuthenticated},
  57. false,
  58. },
  59. {
  60. &Status{LoginFinished: nil},
  61. &Status{LoginFinished: new(empty.Message)},
  62. false,
  63. },
  64. }
  65. for i, tt := range tests {
  66. got := tt.a.Equal(tt.b)
  67. if got != tt.want {
  68. t.Errorf("%d. Equal = %v; want %v", i, got, tt.want)
  69. }
  70. }
  71. }
  72. func TestOSVersion(t *testing.T) {
  73. if osVersion == nil {
  74. t.Skip("not available for OS")
  75. }
  76. t.Logf("Got: %#q", osVersion())
  77. }