Andrew Geissler | 6f01edc | 2024-05-24 10:21:05 -0500 | [diff] [blame] | 1 | #include "slp.hpp" |
| 2 | #include "slp_meta.hpp" |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | // Header |
| 7 | /* 0 1 2 3 |
| 8 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 9 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 10 | | Version | Function-ID | Length | |
| 11 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 12 | | Length, contd.|O|F|R| reserved |Next Ext Offset| |
| 13 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 14 | | Next Extension Offset, contd.| XID | |
| 15 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 16 | | Language Tag Length | Language Tag \ |
| 17 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ |
| 18 | |
| 19 | // Error response |
| 20 | /* 0 1 2 3 |
| 21 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 22 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 23 | | Service Location header (function = SrvRply = 2) | |
| 24 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 25 | | Error Code | URL Entry count | |
| 26 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 27 | | <URL Entry 1> ... <URL Entry N> \ |
| 28 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*/ |
| 29 | |
| 30 | TEST(processError, BasicGoodPath) |
| 31 | { |
| 32 | // Basic buffer with valid Function-ID |
| 33 | slp::buffer testData{0x02, 0x01, 0x00, 0x00, 0x0E, 0x00, 0x00, |
| 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 35 | |
| 36 | slp::Message req; |
| 37 | int rc = slp::SUCCESS; |
| 38 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 39 | |
| 40 | EXPECT_EQ(rc, 0); |
| 41 | |
| 42 | // Verify all expected fields show up in response buffer |
| 43 | std::vector<uint8_t> resp = slp::handler::processError( |
| 44 | req, static_cast<uint8_t>(slp::Error::MSG_NOT_SUPPORTED)); |
| 45 | |
| 46 | EXPECT_EQ(resp.size(), slp::header::MIN_LEN + slp::response::SIZE_ERROR); |
| 47 | EXPECT_EQ(resp[slp::header::OFFSET_VERSION], 2); |
| 48 | EXPECT_EQ(resp[slp::header::OFFSET_FUNCTION], 2); |
| 49 | EXPECT_EQ(resp[slp::header::MIN_LEN + 1], |
| 50 | static_cast<uint8_t>(slp::Error::MSG_NOT_SUPPORTED)); |
| 51 | } |
| 52 | |
| 53 | TEST(processError, InvalidLangTag) |
| 54 | { |
| 55 | // Basic buffer with valid Function-ID |
| 56 | slp::buffer testData{0x02, 0x01, 0x00, 0x00, 0x0E, 0x00, 0x00, |
| 57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, |
| 58 | 0x00, 0x01, 0x02, 0x03, 0x04}; |
| 59 | |
| 60 | slp::Message req; |
| 61 | int rc = slp::SUCCESS; |
| 62 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 63 | |
| 64 | EXPECT_NE(rc, 0); |
| 65 | |
| 66 | // Verify all expected fields show up in response buffer even |
| 67 | // with an inavlid langugage tag size in the header |
| 68 | std::vector<uint8_t> resp = slp::handler::processError( |
| 69 | req, static_cast<uint8_t>(slp::Error::MSG_NOT_SUPPORTED)); |
| 70 | |
| 71 | EXPECT_EQ(resp.size(), slp::header::MIN_LEN + slp::response::SIZE_ERROR); |
| 72 | EXPECT_EQ(resp[slp::header::OFFSET_VERSION], 2); |
| 73 | EXPECT_EQ(resp[slp::header::OFFSET_FUNCTION], 2); |
| 74 | EXPECT_EQ(resp[slp::header::MIN_LEN + 1], |
| 75 | static_cast<uint8_t>(slp::Error::MSG_NOT_SUPPORTED)); |
| 76 | } |
| 77 | |
| 78 | TEST(processError, InvalidEverything) |
| 79 | { |
| 80 | // Basic buffer with valid Function-ID |
| 81 | slp::buffer testData{0x03, 0x99, 0x99, 0x99, 0xA0, 0xA0, 0xA1, |
| 82 | 0xA2, 0xB9, 0x55, 0x44, 0x33, 0x21, 0x90, |
| 83 | 0x78, 0x1, 0x02, 0x03, 0x04}; |
| 84 | |
| 85 | slp::Message req; |
| 86 | int rc = slp::SUCCESS; |
| 87 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 88 | |
| 89 | EXPECT_NE(rc, 0); |
| 90 | |
| 91 | // Verify all expected fields show up in response buffer even |
| 92 | // with an inavlid langugage tag size in the header |
| 93 | std::vector<uint8_t> resp = slp::handler::processError( |
| 94 | req, static_cast<uint8_t>(slp::Error::MSG_NOT_SUPPORTED)); |
| 95 | |
| 96 | EXPECT_EQ(resp.size(), slp::header::MIN_LEN + slp::response::SIZE_ERROR); |
| 97 | EXPECT_EQ(resp[slp::header::OFFSET_VERSION], 3); |
| 98 | EXPECT_EQ(resp[slp::header::OFFSET_FUNCTION], 0x9A); |
| 99 | EXPECT_EQ(resp[slp::header::MIN_LEN + 1], |
| 100 | static_cast<uint8_t>(slp::Error::MSG_NOT_SUPPORTED)); |
| 101 | } |