From b47e67f98e4b9295ba1b42ead95e3e2573566b81 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 10 Nov 2026 17:25:03 +0100 Subject: [PATCH 1/4] WIP: Net::FTP open_timeout/read_timeout (test still to add) Co-Authored-By: Claude Opus 5 --- internal/vm/net_ftp.go | 43 +++++++++++++++-- internal/vm/net_ftp_bind.go | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 3 deletions(-) diff --git a/internal/vm/net_ftp.go b/internal/vm/net_ftp.go index b7d26cc7..351276bd 100644 --- a/internal/vm/net_ftp.go +++ b/internal/vm/net_ftp.go @@ -78,14 +78,14 @@ func (vm *VM) registerFTPErrors(netMod, std *RClass) { // the ensured-close block form. func (vm *VM) registerFTPClassMethods(ftp *RClass) { newFn := func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - f := &ftpObj{cls: ftp, binary: true, passive: true} + f := newFTPObj(ftp) vm.ftpInit(f, args) return f } ftp.smethods["new"] = &Method{name: "new", owner: ftp, native: newFn} ftp.smethods["open"] = &Method{name: "open", owner: ftp, native: func(vm *VM, _ object.Value, args []object.Value, blk *Proc) object.Value { - f := &ftpObj{cls: ftp, binary: true, passive: true} + f := newFTPObj(ftp) vm.ftpInit(f, args) if blk == nil { return f @@ -142,6 +142,15 @@ func (vm *VM) ftpApplyOpts(f *ftpObj, h *object.Hash, port int) (int, string, bo if v, ok := ftpOptHash(h, "passive"); ok { f.passive = v.Truthy() } + // open_timeout and read_timeout are set from the options Hash exactly as + // given, nil included: `read_timeout: nil` is a request for no bound and + // must not fall back to the 60 s default. + if v, ok := ftpOptHash(h, "open_timeout"); ok { + f.openTimeout = v + } + if v, ok := ftpOptHash(h, "read_timeout"); ok { + f.readTimeout = v + } if v, ok := ftpOptHash(h, "use_pasv_ip"); ok { f.usePasvIP = v.Truthy() } @@ -175,8 +184,13 @@ func ftpOptHash(h *object.Hash, names ...string) (object.Value, bool) { // as welcome), and selects EPSV/EPRT when the connection is IPv6 // (Net::FTP#connect). func (vm *VM) ftpConnect(f *ftpObj, host string, port int) { - conn, err := net.Dial("tcp", net.JoinHostPort(host, strconv.Itoa(port))) + // open_timeout bounds the dial. MRI's default is nil — an unbounded connect + // — and DialTimeout(0) is Go's spelling of the same thing, so the default + // path is unchanged and only an explicit setting bounds it. + conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, strconv.Itoa(port)), + ftpTimeoutSeconds(f.openTimeout)) if err != nil { + ftpTimeoutErr(err, "open") ftpConnErr(err) } f.conn = conn @@ -251,6 +265,29 @@ func (vm *VM) registerFTPMethods(ftp *RClass) { }) // --- mode accessors --- + // Net::FTP#open_timeout / #read_timeout and their writers. They are plain + // attributes on the Ruby side: the value is handed back exactly as it was + // set, so `ftp.read_timeout = nil` reads back nil rather than 0. + d("open_timeout", func(_ *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + return self(v).openTimeout + }) + d("open_timeout=", func(_ *VM, v object.Value, a []object.Value, _ *Proc) object.Value { + self(v).openTimeout = a[0] + return a[0] + }) + d("read_timeout", func(_ *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { + return self(v).readTimeout + }) + d("read_timeout=", func(_ *VM, v object.Value, a []object.Value, _ *Proc) object.Value { + self(v).readTimeout = a[0] + // Apply it at once rather than at the next read: Net::FTP#close lowers + // read_timeout before the QUIT exchange precisely so a wedged server + // cannot hold the close, and a setter that only took effect later would + // not honour that. + self(v).armRead() + return a[0] + }) + d("passive", func(_ *VM, v object.Value, _ []object.Value, _ *Proc) object.Value { return object.Bool(self(v).passive) }) diff --git a/internal/vm/net_ftp_bind.go b/internal/vm/net_ftp_bind.go index 1ddac309..4cb19cc0 100644 --- a/internal/vm/net_ftp_bind.go +++ b/internal/vm/net_ftp_bind.go @@ -12,6 +12,7 @@ import ( "net" "strconv" "strings" + "time" netftp "github.com/go-ruby-net-ftp/net-ftp" @@ -68,6 +69,90 @@ type ftpObj struct { welcome string // closed reports whether the control connection has been closed. closed bool + // openTimeout and readTimeout are Net::FTP#open_timeout and #read_timeout in + // SECONDS, held as Ruby values because nil is a meaningful setting ("no + // bound") and distinct from 0. + // + // ⛔ THE DEFAULTS ARE NOT SYMMETRIC, AND THAT ASYMMETRY IS THE WHOLE POINT. + // ruby/spec pins them (library/net-ftp/initialize_spec.rb): open_timeout + // defaults to nil ("sets open_timeout to nil", :272) and read_timeout to 60 + // ("sets read_timeout to the default value", :291 — `should == 60`). So MRI + // leaves the connect unbounded and bounds every read. + // + // Without them this binding dialled with net.Dial and set no read deadline + // anywhere, so a peer that accepted the connection and then said nothing + // held the interpreter for ever: 39 of 56 library/net-ftp spec files had to + // be killed by the clock under rbgo and none under MRI (#916). That is a + // denial of service needing no exploit, only silence — which is why the + // default is a bound and not "whatever the caller remembers to set". + openTimeout object.Value + readTimeout object.Value +} + +// ftpDefaultReadTimeout is Net::FTP's documented default, asserted by +// library/net-ftp/initialize_spec.rb. +const ftpDefaultReadTimeout = 60 + +// newFTPObj builds a Net::FTP with MRI's timeout defaults in place. Every +// construction site goes through it so a new one cannot be born unbounded. +func newFTPObj(cls *RClass) *ftpObj { + return &ftpObj{ + cls: cls, + binary: true, + passive: true, + openTimeout: object.NilV, + readTimeout: object.IntValue(ftpDefaultReadTimeout), + } +} + +// timeoutSeconds reads a timeout value as a Duration; nil (or a non-number) +// means no bound, which is what MRI's nil open_timeout asks for. +func ftpTimeoutSeconds(v object.Value) time.Duration { + switch n := v.(type) { + case object.Integer: + if n <= 0 { + return 0 + } + return time.Duration(float64(n) * float64(time.Second)) + case object.Float: + if n <= 0 { + return 0 + } + return time.Duration(float64(n) * float64(time.Second)) + } + return 0 +} + +// armRead puts the read_timeout deadline on the control socket before a read. +// It is called before EVERY read rather than once at connect, because a +// deadline is absolute: set once, it would expire mid-session and turn every +// later read into a timeout. +func (f *ftpObj) armRead() { + if f.conn == nil { + return + } + d := ftpTimeoutSeconds(f.readTimeout) + if d <= 0 { + // nil read_timeout: MRI waits indefinitely, and so do we. Clear any + // deadline a previous setting left behind. + _ = f.conn.SetReadDeadline(time.Time{}) + return + } + _ = f.conn.SetReadDeadline(time.Now().Add(d)) +} + +// ftpTimeoutErr re-raises a net timeout as the MRI exception for the phase, +// following the shape nethttp_bind.go's raiseTransportErr already uses — the +// HTTP binding has bounded its reads all along, and this one simply never got +// the same treatment. +func ftpTimeoutErr(err error, phase string) { + var ne net.Error + if errors.As(err, &ne) && ne.Timeout() { + if phase == "open" { + raise("Net::OpenTimeout", "execution expired") + } + raise("Net::ReadTimeout", "execution expired") + } } func (f *ftpObj) ToS() string { return "#" } @@ -120,6 +205,10 @@ func ftpConnErr(err error) { // yields whole reply lines (terminator included — the codec strips it). func (f *ftpObj) reader() netftp.LineReader { return func() (string, error) { + // The deadline is armed per line, not per reply: GetMultiline calls this + // repeatedly for a multi-line reply, and read_timeout bounds each read + // rather than the whole exchange. + f.armRead() line, err := f.r.ReadString('\n') if err != nil && line == "" { return "", err @@ -146,6 +235,10 @@ func (f *ftpObj) writeLine(line string) { func (f *ftpObj) getResp() string { resp, err := netftp.GetMultiline(f.reader()) if err != nil { + // A deadline expiry is Net::ReadTimeout, not a connection error: the + // socket is fine, the peer simply said nothing in time, and the two are + // rescued by different classes. + ftpTimeoutErr(err, "read") ftpConnErr(err) } f.lastResponse = resp From 3d2f5cfc5af699c8fbb9b5286f0d1a4aaea5d9c6 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 11 Nov 2026 09:33:17 +0100 Subject: [PATCH 2/4] Net::FTP: the tests for the connect and read bounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The WIP commit below added open_timeout/read_timeout; this adds what it owed. A server that accepts and stays SILENT -- the production shape, and exactly what #916's SIGQUIT dump showed rbgo waiting on -- must raise Net::ReadTimeout. The connection is held OPEN on purpose: a server that closed it would give the reader an EOF, a different path (Net::FTPConnectionError), and the test would pass with no deadline at all. ⛔ THE ELAPSED TIME IS PART OF THE ASSERTION. Without it the test would also pass if the greeting read failed instantly for an unrelated reason, which is the one outcome that would NOT prove a deadline fired. It asserts the call took at least 150ms against a 0.2s read_timeout, and measures 0.21s. Control run in both directions: removing the single armRead() call makes that test HANG until the 40s wall instead of failing in 0.21s. The second test pins the defaults together -- open_timeout nil, read_timeout 60 -- because the ASYMMETRY is the point and is MRI's, from library/net-ftp/initialize_spec.rb:272 and :291. A tidier symmetric default would change behaviour MRI specifies. Refs #916 Co-Authored-By: Claude Opus 5 --- internal/vm/net_ftp_internal_test.go | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/internal/vm/net_ftp_internal_test.go b/internal/vm/net_ftp_internal_test.go index afe3d994..442d6c38 100644 --- a/internal/vm/net_ftp_internal_test.go +++ b/internal/vm/net_ftp_internal_test.go @@ -1539,3 +1539,87 @@ func freePort(t *testing.T) int { ln.Close() return p } + +// ⛔ THE DEFECT THIS GUARDS. Net::FTP dialled with net.Dial and set no read +// deadline anywhere, so a peer that accepted the connection and then said +// NOTHING held the interpreter for ever. Measured 2026-11-10: 39 of 56 +// library/net-ftp ruby/spec files had to be killed by the clock under rbgo and +// none under MRI (#916), and a SIGQUIT dump put the process in +// net.(*conn).Read beneath GetMultiline, waiting for the 220 greeting. +// +// An unbounded read on a socket the peer controls is a denial of service that +// needs no exploit, only silence — so the test is a silent server, which is +// exactly the production shape. +func TestFTPReadTimeoutBoundsASilentServer(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer ln.Close() + + // Accept and hold the connection open without writing a greeting. The + // connection must stay OPEN: a server that closed it would give the reader + // an EOF, which is a different path (Net::FTPConnectionError) and would let + // this test pass without any deadline at all. + accepted := make(chan net.Conn, 1) + go func() { + c, aerr := ln.Accept() + if aerr != nil { + return + } + accepted <- c + }() + defer func() { + select { + case c := <-accepted: + c.Close() + default: + } + }() + + port := ln.Addr().(*net.TCPAddr).Port + src := "require \"net/ftp\"\nPORT = " + strconv.Itoa(port) + "\n" + ` +begin + Net::FTP.new("127.0.0.1", port: PORT, read_timeout: 0.2) + puts "RETURNED WITHOUT RAISING" +rescue => e + puts [e.class, e.message].inspect +end` + + start := stdtime.Now() + got := runSrc(t, src) + elapsed := stdtime.Since(start) + + if want := `[Net::ReadTimeout, "execution expired"]`; got != want { + t.Errorf("got %q, want %q", got, want) + } + // ⛔ THE TIME IS PART OF THE ASSERTION. Without it this test would also pass + // if the greeting read failed instantly for some unrelated reason, which is + // the one outcome that would NOT prove a deadline fired. + if elapsed < 150*stdtime.Millisecond { + t.Errorf("came back in %v, too fast to have waited out a 0.2s read_timeout: "+ + "the raise did not come from the deadline", elapsed) + } +} + +// The defaults are MRI's, and ruby/spec pins both: open_timeout nil and +// read_timeout 60 (library/net-ftp/initialize_spec.rb). They are asserted +// together because the asymmetry is the whole point — MRI leaves the connect +// unbounded and bounds every read, and a "tidier" symmetric default would +// change behaviour MRI specifies. +func TestFTPTimeoutDefaultsAndAccessors(t *testing.T) { + s := newFTPServer(t) + got := s.run(t, ` +ftp = Net::FTP.new("127.0.0.1", port: PORT) +p ftp.open_timeout +p ftp.read_timeout +ftp.open_timeout = 5 +ftp.read_timeout = nil +p ftp.open_timeout +p ftp.read_timeout +ftp.close`) + want := "nil\n60\n5\nnil" + if got != want { + t.Errorf("got %q, want %q", got, want) + } +} From 3cd3b060847759ff0ee78c8e0aedebb93a4b71d7 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 11 Nov 2026 16:02:33 +0100 Subject: [PATCH 3/4] Net::FTP: cover every branch the timeout helpers added MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #921's first push dropped internal/vm to 98.8% and the coverage ratchet refused it. The uncovered lines were all branches no socket test can reach, and each is a behaviour worth asserting on its own rather than a coverage chore: - ftpTimeoutSeconds across every shape: nil (MRI's unbounded default), Integer, Float, and ⛔ the non-positive cases, which are UNBOUNDED and not "expire immediately" -- a deadline of now would fail every read at once -- plus a non-numeric, which is ignored rather than guessed at. - armRead's two early paths: an ftpObj with no connection yet, and a nil read_timeout that must CLEAR a deadline an earlier setting left behind. The second is asserted by showing the read goes back to BLOCKING, since a cleared deadline has no other visible effect. - ftpTimeoutErr returning for an error that is not a timeout. A version that raised unconditionally would turn every connection failure into Net::ReadTimeout, which is the opposite of what #916 needs to stay distinguishable. Refs #916 Co-Authored-By: Claude Opus 5 --- internal/vm/net_ftp_internal_test.go | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/internal/vm/net_ftp_internal_test.go b/internal/vm/net_ftp_internal_test.go index 442d6c38..15cad59c 100644 --- a/internal/vm/net_ftp_internal_test.go +++ b/internal/vm/net_ftp_internal_test.go @@ -8,6 +8,7 @@ package vm import ( "bufio" + "errors" "fmt" "net" "os" @@ -19,6 +20,8 @@ import ( stdtime "time" netftp "github.com/go-ruby-net-ftp/net-ftp" + + "github.com/go-embedded-ruby/ruby/internal/object" ) // The Net::FTP binding is exercised against an in-process FTP server (below) so @@ -1623,3 +1626,72 @@ ftp.close`) t.Errorf("got %q, want %q", got, want) } } + +// The timeout helpers' own branches. Each is a behaviour MRI specifies or a +// refusal this binding makes, so they are asserted directly rather than left +// to be reached by accident through a socket test. +func TestFTPTimeoutSecondsReadsEveryShape(t *testing.T) { + for _, tc := range []struct { + name string + v object.Value + want stdtime.Duration + }{ + // nil is MRI's "no bound" (open_timeout's default), and 0 is Go's + // spelling of the same thing for DialTimeout/SetReadDeadline. + {"nil means unbounded", object.NilV, 0}, + {"an Integer is seconds", object.IntValue(60), 60 * stdtime.Second}, + {"a Float is fractional seconds", object.Float(0.25), 250 * stdtime.Millisecond}, + // ⛔ Non-positive is NOT "expire immediately": MRI treats 0 as no + // timeout, and a deadline of now would make every read fail at once. + {"zero is unbounded", object.IntValue(0), 0}, + {"a negative Integer is unbounded", object.IntValue(-1), 0}, + {"a negative Float is unbounded", object.Float(-0.5), 0}, + // Anything that is not a number cannot be a duration; it is ignored + // rather than guessed at. + {"a String is not a duration", object.NewString("30"), 0}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := ftpTimeoutSeconds(tc.v); got != tc.want { + t.Errorf("ftpTimeoutSeconds(%v) = %v, want %v", tc.v, got, tc.want) + } + }) + } +} + +// armRead's two early paths, which no socket test reaches: an object with no +// connection yet, and a nil read_timeout that must CLEAR a deadline a previous +// setting left behind rather than leave it in place. +func TestFTPArmReadHandlesNoConnectionAndNoBound(t *testing.T) { + // No connection: must not panic, must not touch anything. + (&ftpObj{readTimeout: object.IntValue(5)}).armRead() + + srv, cli := net.Pipe() + defer srv.Close() + defer cli.Close() + f := &ftpObj{conn: cli, readTimeout: object.IntValue(5)} + f.armRead() // sets a deadline + f.readTimeout = object.NilV + f.armRead() // must clear it, not keep the old one + + // With the deadline cleared the pipe read blocks rather than failing, so a + // short wait that times out on OUR side proves no deadline is in force. + done := make(chan error, 1) + go func() { + b := make([]byte, 1) + _, err := cli.Read(b) + done <- err + }() + select { + case err := <-done: + t.Errorf("the read returned %v; a cleared deadline should leave it blocking", err) + case <-stdtime.After(100 * stdtime.Millisecond): + } +} + +// ftpTimeoutErr must RETURN for an error that is not a timeout, so the caller +// can go on to classify it. A version that raised unconditionally would turn +// every connection error into Net::ReadTimeout. +func TestFTPTimeoutErrIgnoresANonTimeout(t *testing.T) { + ftpTimeoutErr(errors.New("connection refused"), "read") + ftpTimeoutErr(nil, "open") +} From e92d9d2fe1f03387b109c59a4f2bca1797842393 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Thu, 12 Nov 2026 22:21:27 +0100 Subject: [PATCH 4/4] Net::FTP: the two doors the first coverage pass still had not opened 3cd3b06 was meant to answer the ratchet and did not move the measurement: CI came back with the same two functions, ftpApplyOpts at 95.0% and ftpTimeoutErr at 80.0%. A fix that does not move the number is not a fix, so this time the number is measured before pushing -- both now read 100.0% under `go tool cover -func` over the FTP tests. What was actually uncovered is the defect shape this repository keeps repeating: a mechanism wired at only SOME of its doors. - ftpApplyOpts reads open_timeout and read_timeout from the options Hash on two adjacent lines. Every socket test passes read_timeout ALONE, and the accessor tests go through the attr writer, not the Hash -- so the open_timeout line had never run. The new test goes through the Hash door for BOTH, and asserts the other half of that contract too: an explicit `read_timeout: nil` is a REQUEST for no bound and must not fall back to MRI's 60 s default, while a key absent from the Hash must leave the field untouched. Those are the two cases the key-presence check exists to tell apart. - ftpTimeoutErr has two raises and the existing test returns before reaching either: it only asserts that a NON-timeout is passed through. The new test drives a real net.Error timeout through both phases and asserts the CLASS -- Net::OpenTimeout for open, Net::ReadTimeout otherwise -- because MRI distinguishes them and ruby/spec matches on the class, so a version that collapsed the two would still satisfy any check that only asked "did it raise?". The error is WRAPPED, as the standard library hands it to the callers, so the test fails if the classification is ever narrowed to a type assertion. Refs #916 Co-Authored-By: Claude Opus 5 --- internal/vm/net_ftp_internal_test.go | 97 ++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/internal/vm/net_ftp_internal_test.go b/internal/vm/net_ftp_internal_test.go index 15cad59c..98b733fe 100644 --- a/internal/vm/net_ftp_internal_test.go +++ b/internal/vm/net_ftp_internal_test.go @@ -1695,3 +1695,100 @@ func TestFTPTimeoutErrIgnoresANonTimeout(t *testing.T) { ftpTimeoutErr(errors.New("connection refused"), "read") ftpTimeoutErr(nil, "open") } + +// ⛔ THE DEFECT SHAPE THIS REPOSITORY KEEPS REPEATING: a mechanism wired at only +// SOME of its doors. open_timeout and read_timeout each have three doors -- the +// constructor's options Hash, the attr writer, and the default -- and the Hash +// door is the one no socket test goes through, because every one of them passes +// read_timeout ALONE. The coverage ratchet named ftpApplyOpts at 95% for exactly +// that: the open_timeout assignment sitting beside a line that ran had never run +// itself. Covering it by hand is the point, not the percentage. +func TestFTPApplyOptsCarriesBothTimeoutsFromTheOptionsHash(t *testing.T) { + // ftpApplyOpts never touches its VM receiver, so a nil one keeps this test + // about the Hash and nothing else. + var vm *VM + + t.Run("both are read, not just read_timeout", func(t *testing.T) { + h := object.NewHash() + h.Set(object.Symbol("open_timeout"), object.IntValue(7)) + h.Set(object.Symbol("read_timeout"), object.Float(0.5)) + f := &ftpObj{} + port, _, _, _, _, _ := vm.ftpApplyOpts(f, h, 21) + if port != 21 { + t.Errorf("port = %d, want 21 left alone when the Hash carries none", port) + } + if got := ftpTimeoutSeconds(f.openTimeout); got != 7*stdtime.Second { + t.Errorf("open_timeout = %v, want 7s -- this door is not wired", got) + } + if got := ftpTimeoutSeconds(f.readTimeout); got != 500*stdtime.Millisecond { + t.Errorf("read_timeout = %v, want 500ms", got) + } + }) + + // ⛔ nil is a REQUEST, not an absence. `read_timeout: nil` asks for NO bound + // and must not fall back to MRI's 60 s default, which is the whole reason + // ftpApplyOpts tests the key's PRESENCE rather than its value. A key absent + // from the Hash is the other case, and must leave the field untouched. + t.Run("an explicit nil is carried, an absent key is not", func(t *testing.T) { + h := object.NewHash() + h.Set(object.Symbol("read_timeout"), object.NilV) + f := &ftpObj{readTimeout: object.IntValue(60)} + vm.ftpApplyOpts(f, h, 21) + if !object.IsNil(f.readTimeout) { + t.Errorf("read_timeout = %v, want nil: an explicit nil asks for no bound", f.readTimeout) + } + if f.openTimeout != nil { + t.Errorf("open_timeout = %v, want untouched: it was absent from the Hash", f.openTimeout) + } + }) +} + +// ftpTimeoutErr's two raises, which the non-timeout test above cannot reach +// because it returns before them. MRI distinguishes Net::OpenTimeout from +// Net::ReadTimeout and ruby/spec asserts the CLASS, so a version that collapsed +// them would still satisfy any check that only asked "did it raise?". +func TestFTPTimeoutErrNamesThePhase(t *testing.T) { + for _, tc := range []struct{ phase, want string }{ + {"open", "Net::OpenTimeout"}, + {"read", "Net::ReadTimeout"}, + // There is no third class: anything that is not the open phase is a read. + {"", "Net::ReadTimeout"}, + } { + t.Run(tc.phase+"=>"+tc.want, func(t *testing.T) { + // WRAPPED, because that is what the callers hand it: the standard + // library returns a *net.OpError around the timeout, and only + // errors.As sees through it. A type assertion would miss this. + err := fmt.Errorf("dial tcp 127.0.0.1:21: %w", ftpStubTimeout{}) + + got, raised := func() (re RubyError, ok bool) { + defer func() { + if r := recover(); r != nil { + re, ok = r.(RubyError) + } + }() + ftpTimeoutErr(err, tc.phase) + return RubyError{}, false + }() + + if !raised { + t.Fatalf("ftpTimeoutErr returned for a timeout in phase %q; it must raise", tc.phase) + } + if got.Class != tc.want { + t.Errorf("raised %s, want %s", got.Class, tc.want) + } + // MRI's message for both, which ruby/spec's Timeout specs match on. + if got.Message != "execution expired" { + t.Errorf("message = %q, want %q", got.Message, "execution expired") + } + }) + } +} + +// ftpStubTimeout is a net.Error that reports a timeout. A real socket can +// produce one only by waiting out a deadline, which is a second of test time per +// case; the classification under test is the same either way. +type ftpStubTimeout struct{} + +func (ftpStubTimeout) Error() string { return "i/o timeout" } +func (ftpStubTimeout) Timeout() bool { return true } +func (ftpStubTimeout) Temporary() bool { return true }