Skip to content
quarry Artifact [280eeb3bae]
Login

Artifact [280eeb3bae]

Artifact 280eeb3bae722e6063677e80df98c509af39eeb7dba2c108f17cfb976a484b5a:


package main

import (
	"io"
	"net"
	"net/http"
	"strings"
	"testing"
)

func TestDebugRefusesNonLoopback(t *testing.T) {
	for _, addr := range []string{"0.0.0.0:0", ":0", "[::]:0", "8.8.8.8:6060", "example.org:6060"} {
		if err := startDebug(addr); err == nil {
			t.Errorf("startDebug(%q) = nil, want a refusal", addr)
		}
	}
	if err := startDebug(""); err != nil {
		t.Errorf("startDebug(\"\") = %v, want off and no error", err)
	}
}

func TestDebugServesHeapOnLoopback(t *testing.T) {
	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	addr := ln.Addr().String()
	ln.Close()
	if err := startDebug(addr); err != nil {
		t.Fatal(err)
	}
	resp, err := http.Get("http://" + addr + "/debug/pprof/heap?debug=1")
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK || !strings.Contains(string(body), "heap profile") {
		t.Fatalf("heap profile: status %d, body %.80q", resp.StatusCode, body)
	}
}