Nettacker icon indicating copy to clipboard operation
Nettacker copied to clipboard

[Testing] Add unit tests for core protocol libraries (HTTP, SSH, FTP, SMTP, Telnet, POP3)

Open adity1raut opened this issue 8 months ago • 4 comments

Summary

The protocol handler libraries under nettacker/core/lib/ are almost entirely untested. Out of 13 protocol files, only 3 have test coverage (SMB, SSL, Socket). The remaining 10 — including HTTP, SSH, FTP, FTPS, SMTP, SMTPS, POP3, POP3S, Telnet, and Base — have zero unit tests.

Affected Files

  • nettacker/core/lib/base.py — base connection class (parent of all protocols)
  • nettacker/core/lib/http.py — used by all scan/vuln modules
  • nettacker/core/lib/ssh.py
  • nettacker/core/lib/ftp.py / ftps.py
  • nettacker/core/lib/smtp.py / smtps.py
  • nettacker/core/lib/pop3.py / pop3s.py
  • nettacker/core/lib/telnet.py

What Needs to Be Done

  • [ ] Write unit tests for base.py covering connection lifecycle, timeout handling, and error states
  • [ ] Write unit tests for http.py covering request building, response parsing, and condition matching (mock requests)
  • [ ] Write unit tests for ssh.py covering auth attempts and error handling (mock paramiko)
  • [ ] Write unit tests for ftp.py, ftps.py covering login flows and error handling (mock ftplib)
  • [ ] Write unit tests for smtp.py, smtps.py covering connection and auth flows
  • [ ] Write unit tests for pop3.py, pop3s.py
  • [ ] Write unit tests for telnet.py
  • [ ] Place tests in tests/core/lib/test_<protocol>.py matching the existing pattern

Acceptance Criteria

  • All network calls must be mocked (no real connections)
  • Each protocol file achieves ≥ 80% line coverage
  • Tests run successfully with pytest tests/core/lib/

Context

This is part of the GSoC 2026 goal to raise overall test coverage to 85%. The existing tests/core/lib/test_socket.py and test_smb.py can serve as reference implementations.

adity1raut avatar Mar 10 '26 07:03 adity1raut

@securestep9, Please Guide me !

adity1raut avatar Mar 10 '26 07:03 adity1raut

I'm starting with the base.py tests since BaseEngine is the parent class for all protocol engines and its methods are the foundation of the scanning pipeline.

My PR adds 25 unit tests covering:

  • BaseLibrary: default state, brute_force noop
  • filter_large_content(): space-boundary truncation, custom filter rates, edge cases
  • process_conditions(): successful/failed conditions, temp event storage, port extraction from URLs and ports field, log field cleanup, sensitive header removal
  • BaseEngine.run(): library method dispatch, ports-to-port renaming, retry logic (partial failure + all-retries-fail)
  • get_dependent_results_from_database(): event retrieval, polling until available, multi-event parsing
  • find_and_replace_dependent_values(): passthrough for non-dependent data
  • replace_dependent_values(): delegation verification

Achieved 87% line coverage for base.py with all network calls mocked.

xovishnukosuri avatar Mar 14 '26 05:03 xovishnukosuri

Following up from my base.py tests in PR #1401, I'm picking up the remaining brute-force protocol libraries: SSH, FTP, FTPS, SMTP, SMTPS, POP3, POP3S, and Telnet.

All eight share the same structure (BaseLibrary subclass with a brute_force method that connects, authenticates, and returns credentials). I'll cover:

  • Successful authentication returning the expected credential dict
  • Connection/auth failures propagating the correct exceptions
  • TLS-specific behavior (starttls for SMTPS, FTP_TLS for FTPS, POP3_SSL for POP3S)
  • SSH's conditional auth strategy selection (Password vs NoneAuth based on whether a password is supplied)
  • Telnet's interactive read/write login sequence

All network calls mocked.

xovishnukosuri avatar Mar 14 '26 05:03 xovishnukosuri

Hi! I'm a GSoC 2026 applicant and would like to work on this.

I've already written unit tests for smtp.py locally on WSL2 (Ubuntu 24.04, Python 3.12.3) and they pass with 100% coverage. I plan to cover smtp.py, smtps.py, ftp.py, ftps.py, pop3.py, pop3s.py, and telnet.py in a single PR.

Could a maintainer confirm this issue is available?

hemant795 avatar Mar 24 '26 19:03 hemant795