Make restricted artifact reads cooperatively cancellable
This commit is contained in:
56
internal/adapter/http/artifact_reader_linux_test.go
Normal file
56
internal/adapter/http/artifact_reader_linux_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package httpadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestRestrictedArtifactReaderRejectsFIFO(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "pipe")
|
||||
if err := syscall.Mkfifo(path, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reader, err := NewRestrictedArtifactReader(root, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := reader.Read(context.Background(), promptkit.File("pipe"))
|
||||
done <- err
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err == nil {
|
||||
t.Fatal("expected FIFO rejection")
|
||||
}
|
||||
return
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
// Release an old blocking FIFO open before joining the worker.
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
fd, err := syscall.Open(path, syscall.O_WRONLY|syscall.O_NONBLOCK, 0)
|
||||
if err == nil {
|
||||
_ = syscall.Close(fd)
|
||||
break
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
t.Fatal("FIFO read did not reject promptly")
|
||||
case <-time.After(10 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
t.Fatal("FIFO read did not reject promptly")
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("FIFO reader could not be released")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user