fp_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // fp_test.go — tests de ServerFP con dial y reloj inyectados (paquete
  2. // interno: necesita tocar dialFP/fpNow/fpCache, no exportables).
  3. package reinstall
  4. import (
  5. "errors"
  6. "testing"
  7. "time"
  8. )
  9. type fpTestHooks struct {
  10. dials []string
  11. fp string
  12. dialErr error
  13. now time.Time
  14. }
  15. // installFPHooks stubba dialFP y fpNow y vacía el cache. Devuelve un reset.
  16. func installFPHooks(t *testing.T, h *fpTestHooks) {
  17. t.Helper()
  18. origDial, origNow := dialFP, fpNow
  19. dialFP = func(host string) (string, error) {
  20. h.dials = append(h.dials, host)
  21. return h.fp, h.dialErr
  22. }
  23. fpNow = func() time.Time { return h.now }
  24. fpMu.Lock()
  25. fpCache = map[string]fpEntry{}
  26. fpMu.Unlock()
  27. t.Cleanup(func() {
  28. dialFP, fpNow = origDial, origNow
  29. fpMu.Lock()
  30. fpCache = map[string]fpEntry{}
  31. fpMu.Unlock()
  32. })
  33. }
  34. func TestServerFPHttpsDerivesAndCaches(t *testing.T) {
  35. h := &fpTestHooks{fp: "aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899"}
  36. installFPHooks(t, h)
  37. got := ServerFP("https://netpulse.example.org:3443")
  38. if got != h.fp {
  39. t.Fatalf("ServerFP = %q, quiero %q", got, h.fp)
  40. }
  41. if len(h.dials) != 1 || h.dials[0] != "netpulse.example.org:3443" {
  42. t.Fatalf("dial a %v, quiero [netpulse.example.org:3443]", h.dials)
  43. }
  44. // Dentro del TTL positivo NO re-diala.
  45. if again := ServerFP("https://netpulse.example.org:3443"); again != h.fp {
  46. t.Fatalf("cache positivo no devolvió el FP: %q", again)
  47. }
  48. if len(h.dials) != 1 {
  49. t.Fatalf("re-dial dentro del TTL positivo: %v", h.dials)
  50. }
  51. // Pasado el TTL re-dial.
  52. h.now = h.now.Add(fpPositiveTTL + time.Second)
  53. ServerFP("https://netpulse.example.org:3443")
  54. if len(h.dials) != 2 {
  55. t.Fatalf("sin re-dial tras expirar el cache positivo: %v", h.dials)
  56. }
  57. }
  58. func TestServerFPHttpsDefaultPort(t *testing.T) {
  59. h := &fpTestHooks{fp: "aa"}
  60. installFPHooks(t, h)
  61. ServerFP("https://netpulse.example.org")
  62. if len(h.dials) != 1 || h.dials[0] != "netpulse.example.org" {
  63. t.Fatalf("dial a %v, quiero el host tal cual (url.Host)", h.dials)
  64. }
  65. }
  66. func TestServerFPHttpSkipsDial(t *testing.T) {
  67. h := &fpTestHooks{fp: "aa"}
  68. installFPHooks(t, h)
  69. for _, u := range []string{"http://192.168.1.226:3000", "http://s:8080/x", "no-es-una-url", ""} {
  70. if got := ServerFP(u); got != "" {
  71. t.Errorf("ServerFP(%q) = %q, quiero \"\" para no-https", u, got)
  72. }
  73. }
  74. if len(h.dials) != 0 {
  75. t.Fatalf("dial para URL no-https: %v", h.dials)
  76. }
  77. }
  78. func TestServerFPNegativeCache(t *testing.T) {
  79. h := &fpTestHooks{dialErr: errors.New("red caida")}
  80. installFPHooks(t, h)
  81. if got := ServerFP("https://np.example.org"); got != "" {
  82. t.Fatalf("con dial fallido quiero \"\", tuve %q", got)
  83. }
  84. // Dentro del TTL negativo NO re-diala (anti-martilleo).
  85. ServerFP("https://np.example.org")
  86. if len(h.dials) != 1 {
  87. t.Fatalf("re-dial dentro del TTL negativo: %v", h.dials)
  88. }
  89. // Pasado el TTL negativo reintenta.
  90. h.now = h.now.Add(fpNegativeTTL + time.Second)
  91. h.dialErr = nil
  92. h.fp = "recuperado"
  93. if got := ServerFP("https://np.example.org"); got != "recuperado" {
  94. t.Fatalf("tras expirar el negativo quiero el FP nuevo, tuve %q", got)
  95. }
  96. }
  97. func TestServerFPEmptyPeerCerts(t *testing.T) {
  98. h := &fpTestHooks{fp: ""} // handshake sin leaf (dialFP devuelve "", nil)
  99. installFPHooks(t, h)
  100. if got := ServerFP("https://np.example.org"); got != "" {
  101. t.Fatalf("sin leaf quiero \"\", tuve %q", got)
  102. }
  103. }