demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 42 minutes ago
History
tlspin_test.go · 1023 bytes · 41 lines
| 1 | package tlspin |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestPinning(t *testing.T) { |
| 12 | srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 13 | w.Write([]byte("ok")) |
| 14 | })) |
| 15 | defer srv.Close() |
| 16 | sum := sha256.Sum256(srv.Certificate().Raw) |
| 17 | good := hex.EncodeToString(sum[:]) |
| 18 | |
| 19 | if _, err := Client(good).Get(srv.URL); err != nil { |
| 20 | t.Fatalf("correct pin rejected: %v", err) |
| 21 | } |
| 22 | // uppercase with colons, as people copy it |
| 23 | colons := "" |
| 24 | for i := 0; i < len(good); i += 2 { |
| 25 | if i > 0 { |
| 26 | colons += ":" |
| 27 | } |
| 28 | colons += good[i : i+2] |
| 29 | } |
| 30 | if _, err := Client("SHA256 " + colons).Get(srv.URL); err != nil { |
| 31 | t.Fatalf("normalised pin rejected: %v", err) |
| 32 | } |
| 33 | bad := "00" + good[2:] |
| 34 | if _, err := Client(bad).Get(srv.URL); err == nil { |
| 35 | t.Fatal("wrong pin accepted") |
| 36 | } |
| 37 | // unpinned client must still refuse a self-signed cert |
| 38 | if _, err := Client("").Get(srv.URL); err == nil { |
| 39 | t.Fatal("unpinned client accepted a self-signed cert") |
| 40 | } |
| 41 | } |