| 248 | } |
| 249 | |
| 250 | func TestCheckerWithTLSVerifySuccess(t *testing.T) { |
| 251 | // Listen on localhost, random port |
| 252 | certPair, err := tls.LoadX509KeyPair("testdata/leaf.pem", "testdata/leaf.key") |
| 253 | if err != nil { |
| 254 | t.Error("Failed to load certificate.", err) |
| 255 | } |
| 256 | config := tls.Config{ |
| 257 | Certificates: []tls.Certificate{certPair}, |
| 258 | } |
| 259 | srv, err := tls.Listen("tcp", "localhost:0", &config) |
| 260 | if err != nil { |
| 261 | t.Errorf("There was an error while starting TLS: %v", err) |
| 262 | } |
| 263 | defer srv.Close() |
| 264 | |
| 265 | // Accept a future connection |
| 266 | go func() { |
| 267 | for { |
| 268 | conn, err := srv.Accept() |
| 269 | if err != nil { |
| 270 | return |
| 271 | } |
| 272 | // Keep connection open for enough time to complete test |
| 273 | conn.SetDeadline(time.Now().Add(100 * time.Millisecond)) |
| 274 | tmp := make([]byte, 1) |
| 275 | conn.Read(tmp) |
| 276 | conn.Close() |
| 277 | } |
| 278 | }() |
| 279 | |
| 280 | // Should know the host:port by now |
| 281 | endpt := srv.Addr().String() |
| 282 | testName := "TestWithTLSNoVerify" |
| 283 | hc := Checker{Name: testName, URL: endpt, TLSEnabled: true, TLSCAFile: "testdata/root.pem", Attempts: 2} |
| 284 | |
| 285 | // Try an up server |
| 286 | result, err := hc.Check() |
| 287 | if err != nil { |
| 288 | t.Errorf("Didn't expect an error: %v", err) |
| 289 | } |
| 290 | for _, run := range result.Times { |
| 291 | if got, want := run.Error, ""; got != want { |
| 292 | t.Fatalf("Expected no errors, got %s", got) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if got, want := result.Title, testName; got != want { |
| 297 | t.Errorf("Expected result.Title='%s', got '%s'", want, got) |
| 298 | } |
| 299 | if got, want := result.Endpoint, endpt; got != want { |
| 300 | t.Errorf("Expected result.Endpoint='%s', got '%s'", want, got) |
| 301 | } |
| 302 | if got, want := result.Down, false; got != want { |
| 303 | t.Errorf("Expected result.Down=%v, got %v", want, got) |
| 304 | } |
| 305 | if got, want := result.Degraded, false; got != want { |
| 306 | t.Errorf("Expected result.Degraded=%v, got %v", want, got) |
| 307 | } |