Skip to content

Commit

Permalink
Rewrite parse functions
Browse files Browse the repository at this point in the history
  • Loading branch information
eoan-ermine committed Mar 22, 2024
1 parent 90502ff commit 500ba72
Show file tree
Hide file tree
Showing 3 changed files with 352 additions and 305 deletions.
48 changes: 24 additions & 24 deletions tests/parse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ TEST(Request, Parse) {
0x6e, 0x65, 0x74, 0x61, 0x73, 0x63, 0x69, 0x69, 0x00};
auto Length = sizeof(PacketBytes) / sizeof(std::uint8_t);

Request Packet;
auto [success, bytesRead] = parse(PacketBytes, Length, Packet);
auto Res = Parser<Request>::parse(PacketBytes, Length);
auto [Packet, BytesRead] = Res.get();

ASSERT_EQ(Packet.getType(), types::ReadRequest);
ASSERT_EQ(Packet.getFilename(), "/srv/tftp/ReadFile");
ASSERT_EQ(Packet.getMode(), "netascii");

ASSERT_EQ(success, true);
ASSERT_EQ(bytesRead, Length);
ASSERT_EQ(Res.isSuccess(), true);
ASSERT_EQ(BytesRead, Length);
}

/// Test that Request packet with options parsing is going fine
Expand Down Expand Up @@ -50,8 +50,8 @@ TEST(Request, OptionParse) {
0x7A, 0x31, 0x67, 0x53, 0x30, 0x58, 0x78, 0x4A, 0x57, 0x33, 0x00};
auto Length = sizeof(PacketBytes) / sizeof(std::uint8_t);

Request Packet;
auto [success, bytesRead] = parse(PacketBytes, Length, Packet);
auto Res = Parser<Request>::parse(PacketBytes, Length);
auto [Packet, BytesRead] = Res.get();

ASSERT_EQ(Packet.getType(), types::ReadRequest);
ASSERT_EQ(Packet.getFilename(), "/srv/tftp/ReadFile");
Expand All @@ -64,8 +64,8 @@ TEST(Request, OptionParse) {
ASSERT_EQ(Packet.getOptionValue(Idx), OptionsValues[Idx]);
}

ASSERT_EQ(success, true);
ASSERT_EQ(bytesRead, Length);
ASSERT_EQ(Res.isSuccess(), true);
ASSERT_EQ(BytesRead, Length);
}

/// Test that Data packet parsing is going fine
Expand All @@ -79,8 +79,8 @@ TEST(Data, Parse) {
0x2e, 0x2e, 0x0d, 0x0a};
auto Length = sizeof(PacketBytes) / sizeof(std::uint8_t);

Data Packet;
auto [success, bytesRead] = parse(PacketBytes, Length, Packet);
auto Res = Parser<Data>::parse(PacketBytes, Length);
auto [Packet, BytesRead] = Res.get();

ASSERT_EQ(Packet.getType(), types::DataPacket);
ASSERT_EQ(Packet.getBlock(), 0x01);
Expand All @@ -91,8 +91,8 @@ TEST(Data, Parse) {
ASSERT_EQ(Data[Idx], PacketBytes[BaseOffset + Idx]);
}

ASSERT_EQ(success, true);
ASSERT_EQ(bytesRead, Length);
ASSERT_EQ(Res.isSuccess(), true);
ASSERT_EQ(BytesRead, Length);
}

/// Test that Acknowledgment packet parsing is going fine
Expand All @@ -103,14 +103,14 @@ TEST(Acknowledgment, Parse) {
0x00, 0x01};
auto Length = sizeof(PacketBytes) / sizeof(std::uint8_t);

Acknowledgment Packet;
auto [success, bytesRead] = parse(PacketBytes, Length, Packet);
auto Res = Parser<Acknowledgment>::parse(PacketBytes, Length);
auto [Packet, BytesRead] = Res.get();

ASSERT_EQ(Packet.getType(), types::AcknowledgmentPacket);
ASSERT_EQ(Packet.getBlock(), 0x01);

ASSERT_EQ(success, true);
ASSERT_EQ(bytesRead, Length);
ASSERT_EQ(Res.isSuccess(), true);
ASSERT_EQ(BytesRead, Length);
}

/// Test that Error packet parsing is going fine
Expand All @@ -124,15 +124,15 @@ TEST(Error, Parse) {
0x00};
auto Length = sizeof(PacketBytes) / sizeof(std::uint8_t);

Error Packet;
auto [success, bytesRead] = parse(PacketBytes, Length, Packet);
auto Res = Parser<Error>::parse(PacketBytes, Length);
auto [Packet, BytesRead] = Res.get();

ASSERT_EQ(Packet.getType(), types::ErrorPacket);
ASSERT_EQ(Packet.getErrorCode(), 0x01);
ASSERT_EQ(Packet.getErrorMessage(), "File not found");

ASSERT_EQ(success, true);
ASSERT_EQ(bytesRead, Length);
ASSERT_EQ(Res.isSuccess(), true);
ASSERT_EQ(BytesRead, Length);
}

/// Test that Option Acknowledgment packet parsing is going fine
Expand All @@ -155,8 +155,8 @@ TEST(OptionAcknowledgment, Parse) {
0x7A, 0x31, 0x67, 0x53, 0x30, 0x58, 0x78, 0x4A, 0x57, 0x33, 0x00};
auto Length = sizeof(PacketBytes) / sizeof(std::uint8_t);

OptionAcknowledgment Packet;
auto [success, bytesRead] = parse(PacketBytes, Length, Packet);
auto Res = Parser<OptionAcknowledgment>::parse(PacketBytes, Length);
auto [Packet, BytesRead] = Res.get();

ASSERT_EQ(Packet.getType(), types::OptionAcknowledgmentPacket);

Expand All @@ -166,8 +166,8 @@ TEST(OptionAcknowledgment, Parse) {
ASSERT_EQ(Packet.getOptionValue(Key), Value);
}

ASSERT_EQ(success, true);
ASSERT_EQ(bytesRead, Length);
ASSERT_EQ(Res.isSuccess(), true);
ASSERT_EQ(BytesRead, Length);
}

int main(int argc, char **argv) {
Expand Down
12 changes: 0 additions & 12 deletions tftp_common/details/packets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

namespace tftp_common::packets {

struct ParseResult;

namespace types {

/// Trivial File Transfer Protocol packet type
Expand Down Expand Up @@ -153,8 +151,6 @@ class Request final {
}

private:
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, Request &Packet);

std::uint16_t Type_;
std::string Filename;
std::string Mode;
Expand Down Expand Up @@ -208,8 +204,6 @@ class Data final {
const std::vector<std::uint8_t> &getData() const noexcept { return DataBuffer; }

private:
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, Data &Packet);

std::uint16_t Type_ = types::DataPacket;
std::uint16_t Block;
std::vector<std::uint8_t> DataBuffer;
Expand Down Expand Up @@ -243,8 +237,6 @@ class Acknowledgment final {
}

private:
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, Acknowledgment &Packet);

std::uint16_t Type_ = types::AcknowledgmentPacket;
std::uint16_t Block;
};
Expand Down Expand Up @@ -290,8 +282,6 @@ class Error final {
}

private:
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, Error &Packet);

std::uint16_t Type_ = types::ErrorPacket;
std::uint16_t ErrorCode;
std::string ErrorMessage;
Expand Down Expand Up @@ -352,8 +342,6 @@ class OptionAcknowledgment final {
std::string_view getOptionValue(const std::string &OptionName) const noexcept { return Options.at(OptionName); }

private:
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, OptionAcknowledgment &Packet);

std::uint16_t Type_ = types::OptionAcknowledgmentPacket;
// According to the RFC, the order in which options are specified is not significant, so it's fine
std::unordered_map<std::string, std::string> Options;
Expand Down
Loading

0 comments on commit 500ba72

Please sign in to comment.