controlclient_test.go 1.1 KB

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