[INS-37] Fixed the issue of ftp connection ignoring the detector's co… (#4590)
* [INS-37] Fixed the issue of ftp connection ignoring the detector's context timeout * fix: ensure FTP connection is properly closed by adding c.Quit() * Add error handling for FTP connection cleanup (c.Quit) * Refactor code as a 3-part if else statement --------- Co-authored-by: Kashif Khan <[email protected]>
This commit is contained in:
co-authored by
Kashif Khan
parent
7583da5f26
commit
83235ddfbb
@@ -78,8 +78,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
|
||||
}
|
||||
|
||||
if verify {
|
||||
timeout := s.verificationTimeout
|
||||
if timeout == 0 {
|
||||
var timeout time.Duration
|
||||
if s.verificationTimeout > 0 {
|
||||
// Use to configure a simulate timeout for interation tests
|
||||
timeout = s.verificationTimeout
|
||||
} else if dl, ok := ctx.Deadline(); ok {
|
||||
// Here we assign the remaining time before our context expires
|
||||
// This help us cater the timelimit set through --detector-timeout flag
|
||||
timeout = time.Until(dl)
|
||||
} else {
|
||||
timeout = defaultVerificationTimeout
|
||||
}
|
||||
verificationErr := verifyFTP(timeout, parsedURL)
|
||||
@@ -126,6 +133,9 @@ func verifyFTP(timeout time.Duration, u *url.URL) error {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = c.Quit()
|
||||
}()
|
||||
password, _ := u.User.Password()
|
||||
return c.Login(u.User.Username(), password)
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ func TestFTP_FromChunk(t *testing.T) {
|
||||
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
|
||||
}
|
||||
}
|
||||
opts := cmpopts.IgnoreFields(detectors.Result{}, "verificationError")
|
||||
opts := cmpopts.IgnoreFields(detectors.Result{}, "verificationError", "primarySecret")
|
||||
if diff := cmp.Diff(got, tt.want, opts); diff != "" {
|
||||
t.Errorf("FTP.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user