controlclient_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package controlclient
  4. import (
  5. "reflect"
  6. "testing"
  7. "tailscale.com/types/empty"
  8. )
  9. func fieldsOf(t reflect.Type) (fields []string) {
  10. for i := 0; i < t.NumField(); i++ {
  11. if name := t.Field(i).Name; name != "_" {
  12. fields = append(fields, name)
  13. }
  14. }
  15. return
  16. }
  17. func TestStatusEqual(t *testing.T) {
  18. // Verify that the Equal method stays in sync with reality
  19. equalHandles := []string{"LoginFinished", "LogoutFinished", "Err", "URL", "NetMap", "State", "Persist"}
  20. if have := fieldsOf(reflect.TypeOf(Status{})); !reflect.DeepEqual(have, equalHandles) {
  21. t.Errorf("Status.Equal check might be out of sync\nfields: %q\nhandled: %q\n",
  22. have, equalHandles)
  23. }
  24. tests := []struct {
  25. a, b *Status
  26. want bool
  27. }{
  28. {
  29. &Status{},
  30. nil,
  31. false,
  32. },
  33. {
  34. nil,
  35. &Status{},
  36. false,
  37. },
  38. {
  39. nil,
  40. nil,
  41. true,
  42. },
  43. {
  44. &Status{},
  45. &Status{},
  46. true,
  47. },
  48. {
  49. &Status{State: StateNew},
  50. &Status{State: StateNew},
  51. true,
  52. },
  53. {
  54. &Status{State: StateNew},
  55. &Status{State: StateAuthenticated},
  56. false,
  57. },
  58. {
  59. &Status{LoginFinished: nil},
  60. &Status{LoginFinished: new(empty.Message)},
  61. false,
  62. },
  63. }
  64. for i, tt := range tests {
  65. got := tt.a.Equal(tt.b)
  66. if got != tt.want {
  67. t.Errorf("%d. Equal = %v; want %v", i, got, tt.want)
  68. }
  69. }
  70. }