Skip to content

Commit

Permalink
Test the ECN observer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
irl committed Oct 18, 2016
1 parent 60075f0 commit 22cb382
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pathspider/observer/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
TCP_SYN = 0x02

TCP_SEC = ( TCP_SYN | TCP_ECE | TCP_CWR )
TCP_SAEW = (TCP_SYN | TCP_ACK | TCP_ECE | TCP_CWR)
TCP_SAEC = (TCP_SYN | TCP_ACK | TCP_ECE | TCP_CWR)
TCP_SAE = (TCP_SYN | TCP_ACK | TCP_ECE)

def tcp_setup(rec, ip):
Expand Down
12 changes: 6 additions & 6 deletions pathspider/plugins/ecn.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pathspider.observer.tcp import tcp_handshake
from pathspider.observer.tcp import tcp_complete
from pathspider.observer.tcp import TCP_SAE
from pathspider.observer.tcp import TCP_SAEW
from pathspider.observer.tcp import TCP_SAEC

Connection = collections.namedtuple("Connection", ["client", "port", "state", "tstart"])
SpiderRecord = collections.namedtuple("SpiderRecord", ["ip", "rport", "port",
Expand All @@ -41,16 +41,16 @@ def ecn_setup(rec, ip):
return True

def ecn_code(rec, ip, rev):
EZ = 0x01
EO = 0x02
EZ = 0x02
EO = 0x01
CE = 0x03

if (ip.traffic_class & EZ == EZ):
if (ip.traffic_class & CE == EZ):
if rev:
rec['rev_ez'] = True
else:
rec['fwd_ez'] = True
if (ip.traffic_class & EO == EO):
if (ip.traffic_class & CE == EO):
if rev:
rec['rev_eo'] = True
else:
Expand Down Expand Up @@ -190,7 +190,7 @@ def combine_flows(self, flow):
cond_conn = 'ecn.connectivity.offline'
conditions.append(cond_conn)

if flows[1]['rev_syn_flags'] & TCP_SAEW == TCP_SAE:
if flows[1]['rev_syn_flags'] & TCP_SAEC == TCP_SAE:
negotiated = True
conditions.append('ecn.negotiated')
else:
Expand Down
93 changes: 93 additions & 0 deletions tests/test_observer_ecn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

import logging
import queue
import threading

import nose

from pathspider.base import SHUTDOWN_SENTINEL
from pathspider.observer import Observer

from pathspider.observer import basic_flow
from pathspider.observer.tcp import TCP_SEC
from pathspider.observer.tcp import TCP_SAE
from pathspider.plugins.ecn import ECN

def test_observer_ecn():
try:
import plt # libtrace may not be available
except:
raise nose.SkipTest

lturi = "pcap:tests/testdata/tcp_ecn.pcap"

logging.getLogger().setLevel(logging.INFO)

spider = ECN(1, lturi, None)
o = spider.create_observer()
q = queue.Queue()
t = threading.Thread(target=o.run_flow_enqueuer,
args=(q,),
daemon=True)
t.start()

flows = []
while True:
f = q.get()
if f == SHUTDOWN_SENTINEL:
break
flows.append(f)

assert len(flows) == 1

flow = flows[0]
assert flow['sp'] == 46557
assert flow['dp'] == 80
assert flow['fwd_syn_flags'] == TCP_SEC
assert flow['rev_syn_flags'] == TCP_SAE
assert flow['tcp_connected'] == True
assert flow['fwd_fin'] == True
assert flow['rev_fin'] == True
assert flow['fwd_rst'] == False
assert flow['rev_rst'] == False
assert flow['fwd_ez'] == True
assert flow['rev_ez'] == True
assert flow['fwd_eo'] == False
assert flow['rev_eo'] == False
assert flow['fwd_ce'] == False
assert flow['rev_ce'] == True

def test_observer_ecn_partial_flow():
try:
import plt # libtrace may not be available
except:
raise nose.SkipTest

lturi = "pcap:tests/testdata/tcp_http.pcap"

logging.getLogger().setLevel(logging.INFO)

spider = ECN(1, lturi, None)
o = spider.create_observer()
q = queue.Queue()
t = threading.Thread(target=o.run_flow_enqueuer,
args=(q,),
daemon=True)
t.start()

flows = []
while True:
f = q.get()
if f == SHUTDOWN_SENTINEL:
break
flows.append(f)

assert len(flows) == 3

for flow in flows:
assert flow['fwd_ez'] == False
assert flow['rev_ez'] == False
assert flow['fwd_eo'] == False
assert flow['rev_eo'] == False
assert flow['fwd_ce'] == False
assert flow['rev_ce'] == False
Binary file added tests/testdata/tcp_ecn.pcap
Binary file not shown.

0 comments on commit 22cb382

Please sign in to comment.