server_fp_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package httpapi
  2. // FORK: tests for the live server fingerprint (Deps.ServerFPFunc).
  3. import (
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. )
  9. // Pairing hands agents the key the server serves now, not the one it served
  10. // at start-up: a user-supplied certificate can change key on renewal.
  11. func TestPairingHandsOutTheLiveFingerprint(t *testing.T) {
  12. s := panelTestServer(t)
  13. s.serverFP = "stale-static-value"
  14. current := "aa11"
  15. s.serverFPFunc = func() string { return current }
  16. read := func() string {
  17. rec := httptest.NewRecorder()
  18. s.handlePairingToken(rec, httptest.NewRequest(http.MethodGet, "/api/pairing/token", nil))
  19. var body map[string]string
  20. if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
  21. t.Fatalf("decode: %v (%s)", err, rec.Body.String())
  22. }
  23. return body["server_fp"]
  24. }
  25. if got := read(); got != "aa11" {
  26. t.Fatalf("server_fp = %q, want the live value", got)
  27. }
  28. current = "bb22"
  29. if got := read(); got != "bb22" {
  30. t.Fatalf("server_fp = %q after a renewal, want the new key", got)
  31. }
  32. }
  33. // Without a live source the static value is used, as before.
  34. func TestPairingFallsBackToTheStaticFingerprint(t *testing.T) {
  35. s := panelTestServer(t)
  36. s.serverFP = "cc33"
  37. if got := s.fingerprint(); got != "cc33" {
  38. t.Fatalf("fingerprint = %q, want the static value", got)
  39. }
  40. }