srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_ftp.cpp
blob: 0015f42928f6dc91f06b3dba47229667a2e6bfe0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <doctest/doctest.h>

#include <vector>

#include "packeteer/l7/ftp.hpp"

using namespace packeteer::net;

namespace {

std::vector<unsigned char> to_bytes(const std::string& s) {
    return std::vector<unsigned char>(s.begin(), s.end());
}

}  // namespace

TEST_CASE("parse_ftp decodes a greeting response") {
    auto msg = parse_ftp(to_bytes("220 (vsFTPd 3.0.5)\r\n"));
    REQUIRE(msg.has_value());
    CHECK(msg->is_response);
    CHECK(msg->command_or_code == "220");
    CHECK(msg->argument == "(vsFTPd 3.0.5)");
}

TEST_CASE("parse_ftp decodes a USER command") {
    auto msg = parse_ftp(to_bytes("USER anonymous\r\n"));
    REQUIRE(msg.has_value());
    CHECK_FALSE(msg->is_response);
    CHECK(msg->command_or_code == "USER");
    CHECK(msg->argument == "anonymous");
}

TEST_CASE("parse_ftp decodes a command with no argument") {
    auto msg = parse_ftp(to_bytes("PASV\r\n"));
    REQUIRE(msg.has_value());
    CHECK(msg->command_or_code == "PASV");
    CHECK(msg->argument.empty());
}

TEST_CASE("parse_ftp rejects an unrecognized command word") {
    CHECK_FALSE(parse_ftp(to_bytes("BOGUS foo\r\n")).has_value());
}

TEST_CASE("FtpDissector claims port 21 and formats command and argument together") {
    FtpDissector dissector;
    CHECK(dissector.port() == kFtpPort);

    auto summary = dissector.summarize(to_bytes("RETR file.txt\r\n"));
    REQUIRE(summary.has_value());
    CHECK(*summary == "FTP RETR file.txt");
}