-
Notifications
You must be signed in to change notification settings - Fork 1
/
option_test.cpp
122 lines (107 loc) · 4.59 KB
/
option_test.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifdef NDEBUG
# undef NDEBUG
#endif
#include "tftpd.hpp"
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
int main()
{
using namespace std::string_literals;
try {
std::vector<char> ackbuf;
std::string path;
FILE *fp{nullptr};
const char corrupt[]{"invalid data block"};
int err = tftpd::tftp(std::vector<char>(corrupt, corrupt + sizeof(corrupt)), fp, path, ackbuf);
assert(err);
assert(tftpd::g_segsize == SEGSIZE);
assert(path.empty());
assert(ackbuf.empty());
assert(fp == nullptr);
std::string test1 = {"\0\2testfile.dat\0octet\0"s
"blksize\0"s
"1047\0"s
"blksize2\0"s
"1234\0"s
"rollover\0"s
"1\0"s
"timeout\0"s
"1\0"s
"utimeout\0"s
"33333\0"s
"tsize\0"s
"12345678910\0"s};
std::vector<char> msg(test1.begin(), test1.end());
// TODO(CK): why? msg.resize(PKTSIZE);
err = tftpd::tftp(msg, fp, path, ackbuf);
std::cout << path << " segsize:" << tftpd::g_segsize << " tsize:" << tftpd::g_tsize
<< " timeout: " << tftpd::g_timeout << std::endl;
assert(!err);
assert(!ackbuf.empty());
assert(tftpd::g_segsize == 1047);
assert(tftpd::g_tsize == 12345678910);
assert(tftpd::g_timeout == 33); // NOTE: ms
std::string test2 = {"\0\2testfile.dat\0octet\0"s
"timeout\0"s
"2\0"s
"blksize2\0"s
"65464\0"s};
err = tftpd::tftp(std::vector<char>(test2.begin(), test2.end()), fp, path, ackbuf);
std::cout << path << " segsize:" << tftpd::g_segsize << " tsize:" << tftpd::g_tsize
<< " timeout: " << tftpd::g_timeout << std::endl;
assert(!err);
assert(tftpd::g_segsize == (1 << 15));
assert(tftpd::g_tsize == 0);
assert(tftpd::g_timeout == 2000); // NOTE: ms
std::string test3 = {"\0\2testfile.dat\0octet\0"s
"blksize2\0"s
"1234\0"s
"utimeout\0"s
"10000\0"s}; // us!
err = tftpd::tftp(std::vector<char>(test3.begin(), test3.end()), fp, path, ackbuf);
std::cout << path << " segsize:" << tftpd::g_segsize << " tsize:" << tftpd::g_tsize
<< " timeout: " << tftpd::g_timeout << std::endl;
assert(!err);
assert(tftpd::g_segsize == 1024);
assert(tftpd::g_timeout == 10); // NOTE: ms
std::string test4 = {"\0\2minimal.dat\0octet\0"s
"blksize\0"s
"777777\0"s
"blksize\0"s
"0\0"s
"timeout\0"s
"333\0"s
"timeout\0"s
"0\0"s
"tsize\0"s
"NoNumber\0"s
"utimeout\0"s
"999\0"s}; // us!
err = tftpd::tftp(std::vector<char>(test4.begin(), test4.end()), fp, path, ackbuf);
std::cout << path << " segsize:" << tftpd::g_segsize << " tsize:" << tftpd::g_tsize
<< " timeout: " << tftpd::g_timeout << std::endl;
assert(!err);
assert(!ackbuf.empty());
assert(tftpd::g_segsize == MAXSEGSIZE);
assert(tftpd::g_timeout == 1000); // NOTE: ms
const char unknown[] = {"\0\1unknown_mode.dat\0netascii\0"};
err = tftpd::tftp(std::vector<char>(unknown, unknown + sizeof(unknown)), fp, path, ackbuf);
assert(err);
assert(tftpd::g_segsize == SEGSIZE);
assert(!path.empty());
assert(ackbuf.empty());
const char missing[] = {"\0\1missing_mode.dat\0"};
err = tftpd::tftp(std::vector<char>(missing, missing + sizeof(missing)), fp, path, ackbuf);
assert(ackbuf.empty());
assert(err);
// err = tftpd::tftp(std::vector<char>(missing, missing + sizeof(missing) - 3), fp, path, ackbuf);
// assert(ackbuf.empty());
// assert(err);
} catch (std::exception &e) {
std::cerr << "Exception: " << e.what() << "\n\n";
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}