firmware_transport_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. package runtime
  2. // FORK: tests for firmwareTransport. Addresses are from the documentation
  3. // ranges.
  4. import (
  5. "net/http"
  6. "testing"
  7. )
  8. // The pinned transport accepts only the server's own key, so it is kept for
  9. // an image on the server itself and never used for a mirror.
  10. func TestFirmwareTransportKeepsThePinOnlyForTheServer(t *testing.T) {
  11. pinned := &http.Transport{}
  12. for _, tc := range []struct {
  13. server string
  14. target string
  15. wantPin bool
  16. }{
  17. {"https://192.0.2.10:3443", "https://192.0.2.10:3443/firmware/image.bin", true},
  18. {"https://192.0.2.10:3443", "https://downloads.example.org/releases/image.bin", false},
  19. {"https://192.0.2.10:3443", "https://192.0.2.10:8443/image.bin", false},
  20. {"https://192.0.2.10:3443", "http://192.0.2.10:3443/image.bin", false},
  21. // The default port, written out or not, is the same server.
  22. {"https://192.0.2.10", "https://192.0.2.10:443/image.bin", true},
  23. {"https://192.0.2.10:443", "https://192.0.2.10/image.bin", true},
  24. } {
  25. got := firmwareTransport(tc.server, tc.target, pinned)
  26. if (got == http.RoundTripper(pinned)) != tc.wantPin {
  27. t.Errorf("server %s, image %s: pinned transport used = %v, want %v",
  28. tc.server, tc.target, got == http.RoundTripper(pinned), tc.wantPin)
  29. }
  30. }
  31. }