MCPcopy Create free account
hub / github.com/docker/secrets-engine / Test_hijacking

Function Test_hijacking

x/ipc/hijack_test.go:36–81  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

34)
35
36func Test_hijacking(t *testing.T) {
37 socketPath := filepath.Join(os.TempDir(), "hijack.sock")
38 l := newListener(t, socketPath)
39 t.Cleanup(func() { l.Close() })
40
41 httpMux := http.NewServeMux()
42 ch := make(chan io.ReadWriter)
43 wait := make(chan struct{})
44 once := sync.OnceFunc(func() { close(wait) })
45 defer once()
46 httpMux.Handle(NewHijackAcceptor(testhelper.TestLogger(t), func(_ context.Context, closer io.ReadWriteCloser) {
47 ch <- closer
48 <-wait
49 }))
50 httpMux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
51 w.WriteHeader(http.StatusOK)
52 _, _ = w.Write([]byte("ok"))
53 })
54 serverErr := make(chan error, 1)
55 server := &http.Server{Handler: httpMux}
56 go func() {
57 serverErr <- server.Serve(l)
58 }()
59 connClient, err := net.Dial("unix", socketPath)
60 require.NoError(t, err)
61 t.Cleanup(func() { connClient.Close() })
62 connHijacked, err := Hijackify(connClient, hijackTimeout)
63 require.NoError(t, err)
64 connServer, err := testhelper.WaitForWithTimeoutV(ch)
65 require.NoError(t, err)
66
67 assert.NoError(t, writeLine(connHijacked, "ping"))
68 assert.Equal(t, "ping", readLine(connServer))
69 assert.NoError(t, writeLine(connServer, "pong"))
70 assert.Equal(t, "pong", readLine(connHijacked))
71 assert.NoError(t, connHijacked.Close())
72 once()
73
74 // The server should still be up and also be available for normal/non-hijack stuff
75 health, err := requestHealthCheck(socketPath)
76 assert.NoError(t, err)
77 assert.Equal(t, "ok", health)
78
79 assert.NoError(t, server.Close())
80 assert.ErrorIs(t, testhelper.WaitForErrorWithTimeout(serverErr), http.ErrServerClosed)
81}
82
83func TestHijackify_hijackRequest_timeout(t *testing.T) {
84 socket := testhelper.RandomShortSocketName()

Callers

nothing calls this directly

Calls 9

newListenerFunction · 0.85
NewHijackAcceptorFunction · 0.85
HijackifyFunction · 0.85
writeLineFunction · 0.85
readLineFunction · 0.85
requestHealthCheckFunction · 0.85
HandleMethod · 0.80
CloseMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected