srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_smtp.cpp
blob: 610ed4ec7e37f6d7338d6bc93d05c0b7300ff67a (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/smtp.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_smtp decodes a greeting response") {
    auto msg = parse_smtp(to_bytes("220 mail.example.com ESMTP\r\n"));
    REQUIRE(msg.has_value());
    CHECK(msg->is_response);
    CHECK(msg->command_or_code == "220");
    CHECK(msg->argument == "mail.example.com ESMTP");
}

TEST_CASE("parse_smtp decodes a MAIL FROM command") {
    auto msg = parse_smtp(to_bytes("MAIL FROM:<[email protected]>\r\n"));
    REQUIRE(msg.has_value());
    CHECK_FALSE(msg->is_response);
    CHECK(msg->command_or_code == "MAIL");
    CHECK(msg->argument == "FROM:<[email protected]>");
}

TEST_CASE("parse_smtp decodes a multi-line response's first line, dash included") {
    auto msg = parse_smtp(to_bytes("250-mail.example.com Hello\r\n250-PIPELINING\r\n"));
    REQUIRE(msg.has_value());
    CHECK(msg->command_or_code == "250");
    CHECK(msg->argument == "mail.example.com Hello");
}

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

TEST_CASE("SmtpDissector claims port 25") {
    SmtpDissector dissector;
    CHECK(dissector.port() == kSmtpPort);

    auto summary = dissector.summarize(to_bytes("EHLO client.example.com\r\n"));
    REQUIRE(summary.has_value());
    CHECK(*summary == "SMTP EHLO client.example.com");
}