-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.cpp
More file actions
80 lines (68 loc) · 2.97 KB
/
Copy pathtests.cpp
File metadata and controls
80 lines (68 loc) · 2.97 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <gtest/gtest.h>
#include "gmock/gmock.h"
#include "isis.hpp"
#include "utils.hpp"
using namespace std;
template <typename T>
::testing::AssertionResult ArraysMatch(T* expected, T* actual, int size) {
for (int i = 0; i < size; i++) {
if (expected[i] != actual[i]) {
return ::testing::AssertionFailure()
<< "array[" << i << "] (" << actual[i]
<< ") != expected[" << i << "] (" << expected[i]
<< ")";
}
}
return ::testing::AssertionSuccess();
}
TEST(utils, fletcher_chksum) {
uint16_t expected = 0xf4e4;
unsigned char PDU[] = {
0x83, 0x1b, 0x01, 0x00, 0x14, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04,
0x25, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x04, 0x02, 0x00, 0x00, 0x03, 0x01, 0x04, 0x03, 0x49, 0x00, 0x01,
0x0e, 0x02, 0x05, 0xd4, 0x81, 0x02, 0xcc, 0x8e, 0x86, 0x04, 0x65,
0x65, 0x65, 0x65, 0x84, 0x04, 0x65, 0x65, 0x65, 0x65, 0x89, 0x04,
0x76, 0x6d, 0x78, 0x31, 0xf2, 0x05, 0x65, 0x65, 0x65, 0x65, 0x00,
0x80, 0x30, 0x00, 0x80, 0x80, 0x80, 0x65, 0x65, 0x65, 0x65, 0xff,
0xff, 0xff, 0xff, 0x0a, 0x80, 0x80, 0x80, 0x0a, 0x01, 0x00, 0x00,
0xff, 0xff, 0xff, 0xfe, 0x0a, 0x80, 0x80, 0x80, 0x0a, 0x03, 0x00,
0x00, 0xff, 0xff, 0xff, 0xfe, 0x0a, 0x80, 0x80, 0x80, 0x0a, 0x02,
0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x87, 0x24, 0x00, 0x00, 0x00,
0x00, 0x20, 0x65, 0x65, 0x65, 0x65, 0x00, 0x00, 0x00, 0x0a, 0x1f,
0x0a, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x1f, 0x0a, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x1f, 0x0a, 0x02, 0x00, 0x00,
0x02, 0x0c, 0x00, 0x0a, 0x80, 0x80, 0x80, 0x00, 0x02, 0x00, 0x02,
0x00, 0x02, 0x00, 0x16, 0x21, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02,
0x00, 0x00, 0x00, 0x0a, 0x16, 0x06, 0x04, 0x0a, 0x01, 0x00, 0x01,
0x08, 0x04, 0x0a, 0x01, 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, 0x01,
0x4d, 0x00, 0x00, 0x00, 0x07};
ASSERT_EQ(expected,
htons(fletcher_checksum(PDU + 12, sizeof(PDU) - 12, 12)));
}
TEST(structs, eth_header) {
unsigned char expected_hdr[] = {0x09, 0x00, 0x2b, 0x00, 0x00, 0x05,
0x00, 0x0c, 0x29, 0x6f, 0x14, 0xbf,
0x00, 0x00, 0xfe, 0xfe, 0x03};
eth_header hdr;
ASSERT_EQ(0, hdr.length());
EXPECT_TRUE(ArraysMatch<unsigned char>(expected_hdr, hdr.dmac(), sizeof(expected_hdr)));
}
TEST(funcs, ip_to_bytes) {
std::string ip_str = "10.3.0.0";
unsigned char expected[] = { 0xa, 0x3, 0x00, 0x00 };
unsigned char ip_array[4]{};
std::string ip_delimiter = ".";
size_t ip_pos{};
for ( int i=0; i<4; i++) {
ip_pos = ip_str.find(ip_delimiter);
ip_array[i] = static_cast<unsigned char>(std::stoi(ip_str.substr(0, ip_pos), 0, 10));
ip_str.erase(0, ip_pos + ip_delimiter.length());
}
EXPECT_TRUE(ArraysMatch<unsigned char>(expected,ip_array,sizeof(expected)));
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}