Skip to content

Commit

Permalink
Per #15 Cleaned up serialToTUN()
Browse files Browse the repository at this point in the history
* Clarified port and IP addressing with TUN
* Removed hardcoded values where possible.
* Used mtu value from Monitor
* Passes local pytest and linting
  • Loading branch information
kb1lqc committed Jan 28, 2018
1 parent 375ebe4 commit 574c084
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions tests/test_tun.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,28 @@ def test_serialToTUN():
# Configure TUN IP:PORT and IP Packet source IP:PORT parameters for test
sourceAddress = '10.0.0.2'
sourcePort = 9998
tunAddress = '10.0.0.1'
tunPort = 9999
destPort = 9999

# Start a TUN Monitor class
TUNMonitor = faraday.Monitor(serialPort=serialPort,
name="Faraday",
addr=tunAddress,
mtu=1500)
TUNMonitor = faraday.Monitor(serialPort=serialPort)

# Open a socket for UDP packets and bind it to the TUN address:port
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('10.0.0.1', 9999))
s.bind((TUNMonitor._TUN._tun.addr, destPort))

# Create simple IP packet with message. Send to TUN address:port
message = bytes("Hello, World! {0}".format(time.time()), "utf-8")
etherType = b"\x00\x00\x08\x00"
packet = etherType + (IP(dst=tunAddress,
packet = etherType + (IP(dst=TUNMonitor._TUN._tun.addr,
src=sourceAddress) /
UDP(sport=sourcePort,
dport=tunPort)/message).__bytes__()
dport=destPort)/message).__bytes__()

# Write a simple message over the TUN, no need for checker thread
TUNMonitor._TUN._tun.write(packet)

# Receive data from the socket bound to the TUN address:port
data, address = s.recvfrom(4096)
data, address = s.recvfrom(TUNMonitor._TUN._tun.mtu)

# Check that data written to TUN matches data received from socket
assert data == message
Expand Down

0 comments on commit 574c084

Please sign in to comment.