Andrew Geissler | 0e948dc | 2024-05-23 09:51:48 -0500 | [diff] [blame] | 1 | #include "slp.hpp" |
| 2 | #include "slp_meta.hpp" |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | /* 0 1 2 3 |
| 7 | 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 |
| 8 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 9 | | Version | Function-ID | Length | |
| 10 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 11 | | Length, contd.|O|F|R| reserved |Next Ext Offset| |
| 12 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 13 | | Next Extension Offset, contd.| XID | |
| 14 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 15 | | Language Tag Length | Language Tag \ |
| 16 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ |
| 17 | |
| 18 | TEST(parseHeaderTest, BasicGoodPath) |
| 19 | { |
| 20 | // Basic buffer with valid Function-ID |
| 21 | slp::buffer testData{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 22 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 23 | |
| 24 | slp::Message req; |
| 25 | int rc = slp::SUCCESS; |
| 26 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 27 | |
| 28 | EXPECT_EQ(rc, 0); |
| 29 | } |
| 30 | |
| 31 | TEST(parseHeaderTest, InvalidBufferSize) |
| 32 | { |
| 33 | // 1 byte too small |
| 34 | slp::buffer testData{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 36 | |
| 37 | slp::Message req; |
| 38 | int rc = slp::SUCCESS; |
| 39 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 40 | |
| 41 | EXPECT_NE(rc, 0); |
| 42 | } |
Andrew Geissler | c668313 | 2024-05-23 10:21:26 -0500 | [diff] [blame] | 43 | |
| 44 | TEST(parseHeaderTest, ValidLangTagLength) |
| 45 | { |
| 46 | // Language Tag Length that is valid |
| 47 | slp::buffer testData{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 48 | 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}; |
| 49 | |
| 50 | slp::Message req; |
| 51 | int rc = slp::SUCCESS; |
| 52 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 53 | |
| 54 | EXPECT_EQ(rc, 0); |
| 55 | } |
| 56 | |
| 57 | TEST(parseHeaderTest, InvalidLangTagLength) |
| 58 | { |
| 59 | // Language Tag Length that is too big |
| 60 | slp::buffer testData{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 61 | 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00}; |
| 62 | |
| 63 | slp::Message req; |
| 64 | int rc = slp::SUCCESS; |
| 65 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 66 | |
| 67 | EXPECT_NE(rc, 0); |
| 68 | } |
Andrew Geissler | f60e710 | 2024-05-24 12:39:36 -0500 | [diff] [blame^] | 69 | |
| 70 | /* 0 1 2 3 |
| 71 | 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 |
| 72 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 73 | | Service Location header (function = SrvTypeRqst = 9) | |
| 74 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 75 | | length of PRList | <PRList> String \ |
| 76 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 77 | | length of Naming Authority | <Naming Authority String> \ |
| 78 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 79 | | length of <scope-list> | <scope-list> String \ |
| 80 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*/ |
| 81 | TEST(parseSrvTypeRqst, BasicGoodPath) |
| 82 | { |
| 83 | // Basic buffer with 0 for all lengths |
| 84 | slp::buffer testData{0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 85 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 86 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 87 | slp::Message req; |
| 88 | int rc = slp::SUCCESS; |
| 89 | rc = slp::parser::internal::parseSrvTypeRqst(testData, req); |
| 90 | |
| 91 | EXPECT_EQ(rc, 0); |
| 92 | } |
| 93 | |
| 94 | TEST(parseSrvTypeRqst, GoodPathWithData) |
| 95 | { |
| 96 | // Basic buffer with some valid lengths and data |
| 97 | slp::buffer |
| 98 | testData{0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 99 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, /* Lang Length */ |
| 100 | 'L', 'A', 'N', 'G', 0x00, 0x04, /* PRlist length*/ |
| 101 | 'P', 'R', 'L', 'T', 0x00, 0x08, /* Naming auth length */ |
| 102 | 'N', 'A', 'M', 'E', 'A', 'U', 'T', |
| 103 | 'H', 0x00, 0x05, /* Scope length*/ |
| 104 | 'S', 'C', 'O', 'P', 'E'}; |
| 105 | slp::Message req; |
| 106 | int rc = slp::SUCCESS; |
| 107 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 108 | EXPECT_EQ(rc, 0); |
| 109 | EXPECT_EQ(req.header.langtag, "LANG"); |
| 110 | EXPECT_EQ(req.header.langtagLen, 4); |
| 111 | |
| 112 | rc = slp::parser::internal::parseSrvTypeRqst(testData, req); |
| 113 | EXPECT_EQ(rc, 0); |
| 114 | EXPECT_EQ(req.body.srvtyperqst.prList, "PRLT"); |
| 115 | EXPECT_EQ(req.body.srvtyperqst.namingAuth, "NAMEAUTH"); |
| 116 | EXPECT_EQ(req.body.srvtyperqst.scopeList, "SCOPE"); |
| 117 | } |
| 118 | |
| 119 | TEST(parseSrvTypeRqst, GoodPathMatchSlptoolFindSrvTypes) |
| 120 | { |
| 121 | // This matches what "slptool -u <server> findsrvtypes" sends |
| 122 | slp::buffer testData{0x02, 0x09, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, |
| 123 | 0x00, 0x00, 0x74, 0xe2, 0x00, 0x02, /* Lang Length */ |
| 124 | 'e', 'n', 0x00, 0x00, /* PRlist length*/ |
| 125 | 0xff, 0xff, /* Naming auth length */ |
| 126 | 0x00, 0x07, /* Scope length*/ |
| 127 | 'D', 'E', 'F', 'A', 'U', 'L', 'T'}; |
| 128 | slp::Message req; |
| 129 | int rc = slp::SUCCESS; |
| 130 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 131 | EXPECT_EQ(rc, 0); |
| 132 | EXPECT_EQ(req.header.langtag, "en"); |
| 133 | EXPECT_EQ(req.header.langtagLen, 2); |
| 134 | |
| 135 | rc = slp::parser::internal::parseSrvTypeRqst(testData, req); |
| 136 | EXPECT_EQ(rc, 0); |
| 137 | EXPECT_EQ(req.body.srvtyperqst.namingAuth, ""); |
| 138 | EXPECT_EQ(req.body.srvtyperqst.scopeList, "DEFAULT"); |
| 139 | } |
| 140 | |
| 141 | TEST(parseSrvTypeRqst, BadPathSizes) |
| 142 | { |
| 143 | // Basic buffer with some valid lengths and data |
| 144 | slp::buffer |
| 145 | testData{0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 146 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, /* Lang Length */ |
| 147 | 'L', 'A', 'N', 'G', 0x00, 0x05, /* PRlist length*/ |
| 148 | 'P', 'R', 'L', 'T', 0x00, 0x08, /* Naming auth length */ |
| 149 | 'N', 'A', 'M', 'E', 'A', 'U', 'T', |
| 150 | 'H', 0x00, 0x05, /* Scope length*/ |
| 151 | 'S', 'C', 'O', 'P', 'E'}; |
| 152 | slp::Message req; |
| 153 | int rc = slp::SUCCESS; |
| 154 | std::tie(rc, req) = slp::parser::internal::parseHeader(testData); |
| 155 | EXPECT_EQ(rc, 0); |
| 156 | EXPECT_EQ(req.header.langtag, "LANG"); |
| 157 | EXPECT_EQ(req.header.langtagLen, 4); |
| 158 | |
| 159 | rc = slp::parser::internal::parseSrvTypeRqst(testData, req); |
| 160 | EXPECT_NE(rc, 0); |
| 161 | |
| 162 | // Fix PR, make Name invalid |
| 163 | testData[19] = 4; |
| 164 | testData[25] = 5; |
| 165 | rc = slp::parser::internal::parseSrvTypeRqst(testData, req); |
| 166 | EXPECT_NE(rc, 0); |
| 167 | |
| 168 | // Fix Name, make Scope invalid |
| 169 | testData[25] = 8; |
| 170 | testData[35] = 10; |
| 171 | rc = slp::parser::internal::parseSrvTypeRqst(testData, req); |
| 172 | EXPECT_NE(rc, 0); |
| 173 | } |