forked from vikshanker/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm_listen.cc
68 lines (54 loc) · 2.35 KB
/
fsm_listen.cc
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
#include "tcp_config.hh"
#include "tcp_expectation.hh"
#include "tcp_fsm_test_harness.hh"
#include "tcp_header.hh"
#include "tcp_segment.hh"
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
using State = TCPTestHarness::State;
int main() {
try {
TCPConfig cfg{};
// test #1: START -> LISTEN -> data without ACK or SYN (dropped) -> SYN -> SYN/ACK -> ACK
{
TCPTestHarness test_1(cfg);
// tell the FSM to connect, make sure we get a SYN
test_1.execute(Listen{});
test_1.execute(Tick(1));
test_1.send_byte(WrappingInt32{0}, {}, 0);
test_1.send_fin(WrappingInt32{0}, {});
test_1.execute(Tick(1));
test_1.execute(ExpectState{State::LISTEN});
test_1.execute(ExpectNoSegment{}, "test 1 failed: non-ACK data segment should be ignored");
test_1.send_syn(WrappingInt32{0}, {});
test_1.execute(Tick(1));
TCPSegment seg = test_1.expect_seg(ExpectOneSegment{}.with_syn(true).with_ack(true).with_ackno(1),
"test 1 failed: no SYN/ACK in response to SYN");
test_1.execute(ExpectState{State::SYN_RCVD});
// wrong seqno! should get ACK back but not transition
const WrappingInt32 syn_seqno = seg.header().seqno;
test_1.send_ack(WrappingInt32{0}, syn_seqno + 1);
test_1.execute(Tick(1));
test_1.execute(
ExpectOneSegment{}.with_no_flags().with_ack(true).with_ackno(1).with_seqno(seg.header().seqno + 1),
"test 1 failed: wrong response to old seqno");
test_1.send_ack(WrappingInt32(cfg.recv_capacity + 1), syn_seqno + 1);
test_1.execute(Tick(1));
test_1.execute(
ExpectOneSegment{}.with_no_flags().with_ack(true).with_ackno(1).with_seqno(seg.header().seqno + 1));
test_1.send_ack(WrappingInt32{1}, seg.header().seqno + 1);
test_1.execute(Tick(1));
test_1.execute(ExpectNoSegment{}, "test 1 failed: no need to ACK an ACK");
test_1.execute(ExpectState{State::ESTABLISHED});
}
} catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
return EXIT_SUCCESS;
}