package webread import ( "encoding/json" "fmt" "os" "os/exec" "runtime" "testing" "golang.org/x/sys/unix" ) type limitsReport struct{ Data, AS, CPU, Core uint64 } // limitsMain runs in a child of the test binary: it limits itself as the // fetcher does, then shows the limits hold and that Go still runs. func limitsMain(over bool) int { if err := limitSelf(); err != nil { fmt.Fprintln(os.Stderr, err) return 1 } size := 128 << 20 // a decoded 16-megapixel image, twice over if over { size = 1200 << 20 } buf := make([]byte, size) buf[len(buf)-1] = 1 var r limitsReport var l unix.Rlimit unix.Getrlimit(unix.RLIMIT_DATA, &l) r.Data = l.Max unix.Getrlimit(unix.RLIMIT_AS, &l) r.AS = l.Max unix.Getrlimit(unix.RLIMIT_CPU, &l) r.CPU = l.Max unix.Getrlimit(unix.RLIMIT_CORE, &l) r.Core = l.Max json.NewEncoder(os.Stdout).Encode(r) return 0 } func TestFetcherLimitsItself(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("the fetcher runs only on Linux") } self, err := os.Executable() if err != nil { t.Fatal(err) } out, err := exec.Command(self, "webread-limits").Output() if err != nil { t.Fatalf("child: %v", err) } var r limitsReport if err = json.Unmarshal(out, &r); err != nil { t.Fatalf("%v: %s", err, out) } if r.Data != 1<<30 || r.AS != 4<<30 || r.CPU != 120 || r.Core != 0 { t.Fatalf("limits %+v", r) } // The data bound holds: more than 1 GB cannot be had. if out, err := exec.Command(self, "webread-limits-over").CombinedOutput(); err == nil { t.Fatalf("allocated past the bound: %s", out) } }