diff --git a/test/Makefile b/test/Makefile index 20cdf25..7f24427 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,14 +2,6 @@ PYTHON = python3 CMD = $(PYTHON) -model_tb: - cd ../ && PYTHONPATH=./ $(CMD) test/model/mac.py - cd ../ && PYTHONPATH=./ $(CMD) test/model/arp.py - cd ../ && PYTHONPATH=./ $(CMD) test/model/ip.py - cd ../ && PYTHONPATH=./ $(CMD) test/model/udp.py - cd ../ && PYTHONPATH=./ $(CMD) test/model/icmp.py - cd ../ && PYTHONPATH=./ $(CMD) test/model/etherbone.py - examples: cd ../examples && $(PYTHON) make.py -t base -s BaseSoC -p kc705 -Ob run False build-bitstream cd ../examples && $(PYTHON) make.py -t base -s BaseSoCDevel -p kc705 -Ob run False build-bitstream @@ -20,4 +12,4 @@ examples: cd ../examples && $(PYTHON) make.py -t tty -s TTYSoC -p kc705 -Ob run False build-bitstream cd ../examples && $(PYTHON) make.py -t tty -s TTYSoCDevel -p kc705 -Ob run False build-bitstream -all: model_tb examples \ No newline at end of file +all: examples \ No newline at end of file diff --git a/test/model/arp.py b/test/model/arp.py index 5e378f9..8290ebe 100644 --- a/test/model/arp.py +++ b/test/model/arp.py @@ -125,37 +125,3 @@ class ARP(Module): request.sender_ip = self.ip_address request.target_mac = 0xffffffffffff request.target_ip = ip_address - -if __name__ == "__main__": - from test.model.dumps import * - from test.model.mac import * - errors = 0 - # ARP request - packet = MACPacket(arp_request) - packet.decode_remove_header() - packet = ARPPacket(packet) - # check decoding - packet.decode() - # print(packet) - errors += verify_packet(packet, arp_request_infos) - # check encoding - packet.encode() - packet.decode() - # print(packet) - errors += verify_packet(packet, arp_request_infos) - - # ARP Reply - packet = MACPacket(arp_reply) - packet.decode_remove_header() - packet = ARPPacket(packet) - # check decoding - packet.decode() - # print(packet) - errors += verify_packet(packet, arp_reply_infos) - # check encoding - packet.encode() - packet.decode() - # print(packet) - errors += verify_packet(packet, arp_reply_infos) - - print("arp errors " + str(errors)) diff --git a/test/model/etherbone.py b/test/model/etherbone.py index 68231b6..a2ace9e 100644 --- a/test/model/etherbone.py +++ b/test/model/etherbone.py @@ -2,7 +2,6 @@ # License: BSD import math -import copy from litex.soc.interconnect.stream_sim import * from litex.tools.remote.etherbone import * @@ -56,31 +55,3 @@ class Etherbone(Module): def process(self, packet): pass - -if __name__ == "__main__": - # Writes/Reads - writes = EtherboneWrites(base_addr=0x1000, datas=[i for i in range(16)]) - reads = EtherboneReads(base_ret_addr=0x2000, addrs=[i for i in range(16)]) - - # Record - record = EtherboneRecord() - record.writes = writes - record.reads = reads - record.wcount = len(writes.get_datas()) - record.rcount = len(reads.get_addrs()) - - # Packet - packet = EtherbonePacket() - packet.records = [deepcopy(record) for i in range(8)] - # print(packet) - packet.encode() - # print(packet) - - # Send packet over UDP to check against Wireshark dissector - import socket - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock.sendto(bytes(packet), ("192.168.1.1", 20000)) - - packet = EtherbonePacket(packet) - packet.decode() - print(packet) diff --git a/test/model/icmp.py b/test/model/icmp.py index 2b28c34..7bb621c 100644 --- a/test/model/icmp.py +++ b/test/model/icmp.py @@ -86,26 +86,3 @@ class ICMP(Module): def process(self, packet): pass - -if __name__ == "__main__": - from test.model.dumps import * - from test.model.mac import * - from test.model.ip import * - errors = 0 - # ICMP packet - packet = MACPacket(ping_request) - packet.decode_remove_header() - # print(packet) - packet = IPPacket(packet) - packet.decode() - # print(packet) - packet = ICMPPacket(packet) - packet.decode() - # print(packet) - errors += verify_packet(packet, ping_request_infos) - packet.encode() - packet.decode() - # print(packet) - errors += verify_packet(packet, ping_request_infos) - - print("icmp errors " + str(errors)) diff --git a/test/model/ip.py b/test/model/ip.py index a911ba1..198a8fa 100644 --- a/test/model/ip.py +++ b/test/model/ip.py @@ -133,27 +133,3 @@ class IP(Module): elif packet.protocol == icmp_protocol: if self.icmp_callback is not None: self.icmp_callback(packet) - -if __name__ == "__main__": - from test.model.dumps import * - from test.model.mac import * - errors = 0 - # UDP packet - packet = MACPacket(udp) - packet.decode_remove_header() - # print(packet) - packet = IPPacket(packet) - # check decoding - errors += not packet.check_checksum() - packet.decode() - # print(packet) - errors += verify_packet(packet, {}) - # check encoding - packet.encode() - packet.insert_checksum() - errors += not packet.check_checksum() - packet.decode() - # print(packet) - errors += verify_packet(packet, {}) - - print("ip errors " + str(errors)) diff --git a/test/model/mac.py b/test/model/mac.py index 8df1680..99de822 100644 --- a/test/model/mac.py +++ b/test/model/mac.py @@ -136,26 +136,3 @@ class MAC(Module): self.arp_callback(packet) else: raise ValueError # XXX handle this properly - -if __name__ == "__main__": - from test.model.dumps import * - errors = 0 - packet = MACPacket(arp_request) - packet.decode_remove_header() - # print(packet) - errors += verify_packet(packet, arp_request_infos) - packet.encode_header() - packet.decode_remove_header() - # print(packet) - errors += verify_packet(packet, arp_request_infos) - - # print(packet) - packet = MACPacket(arp_reply) - packet.decode_remove_header() - errors += verify_packet(packet, arp_reply_infos) - packet.encode_header() - packet.decode_remove_header() - # print(packet) - errors += verify_packet(packet, arp_reply_infos) - - print("mac errors " + str(errors)) diff --git a/test/model/udp.py b/test/model/udp.py index 48da7b7..23f8c4e 100644 --- a/test/model/udp.py +++ b/test/model/udp.py @@ -97,30 +97,3 @@ class UDP(Module): if packet.dst_port == 20000: if self.etherbone_callback is not None: self.etherbone_callback(packet) - -if __name__ == "__main__": - from test.model.dumps import * - from test.model.mac import * - from test.model.ip import * - errors = 0 - # UDP packet - packet = MACPacket(udp) - packet.decode_remove_header() - # print(packet) - packet = IPPacket(packet) - packet.decode() - # print(packet) - packet = UDPPacket(packet) - packet.decode() - # print(packet) - if packet.length != (len(packet)+udp_header.length): - errors += 1 - errors += verify_packet(packet, udp_infos) - packet.encode() - packet.decode() - # print(packet) - if packet.length != (len(packet)+udp_header.length): - errors += 1 - errors += verify_packet(packet, udp_infos) - - print("udp errors " + str(errors)) diff --git a/test/test_model.py b/test/test_model.py new file mode 100644 index 0000000..39c672c --- /dev/null +++ b/test/test_model.py @@ -0,0 +1,168 @@ +# This file is Copyright (c) 2015-2019 Florent Kermarrec +# License: BSD + +import unittest + +from liteeth.common import * + +from test.model.dumps import * +from test.model.mac import MACPacket +from test.model.arp import ARPPacket +from test.model.ip import IPPacket +from test.model.icmp import ICMPPacket +from test.model.udp import UDPPacket +from litex.tools.remote.etherbone import * +from test.model.etherbone import Etherbone + + +class TestModel(unittest.TestCase): + def test_mac(self): + errors = 0 + packet = MACPacket(arp_request) + packet.decode_remove_header() + # print(packet) + errors += verify_packet(packet, arp_request_infos) + packet.encode_header() + packet.decode_remove_header() + # print(packet) + errors += verify_packet(packet, arp_request_infos) + + # print(packet) + packet = MACPacket(arp_reply) + packet.decode_remove_header() + errors += verify_packet(packet, arp_reply_infos) + packet.encode_header() + packet.decode_remove_header() + # print(packet) + errors += verify_packet(packet, arp_reply_infos) + + self.assertEqual(errors, 0) + + def test_arp(self): + errors = 0 + # ARP request + packet = MACPacket(arp_request) + packet.decode_remove_header() + packet = ARPPacket(packet) + # check decoding + packet.decode() + # print(packet) + errors += verify_packet(packet, arp_request_infos) + # check encoding + packet.encode() + packet.decode() + # print(packet) + errors += verify_packet(packet, arp_request_infos) + + # ARP Reply + packet = MACPacket(arp_reply) + packet.decode_remove_header() + packet = ARPPacket(packet) + # check decoding + packet.decode() + # print(packet) + errors += verify_packet(packet, arp_reply_infos) + # check encoding + packet.encode() + packet.decode() + # print(packet) + errors += verify_packet(packet, arp_reply_infos) + + self.assertEqual(errors, 0) + + def test_ip(self): + errors = 0 + # UDP packet + packet = MACPacket(udp) + packet.decode_remove_header() + # print(packet) + packet = IPPacket(packet) + # check decoding + errors += not packet.check_checksum() + packet.decode() + # print(packet) + errors += verify_packet(packet, {}) + # check encoding + packet.encode() + packet.insert_checksum() + errors += not packet.check_checksum() + packet.decode() + # print(packet) + errors += verify_packet(packet, {}) + + self.assertEqual(errors, 0) + + def test_icmp(self): + errors = 0 + # ICMP packet + packet = MACPacket(ping_request) + packet.decode_remove_header() + # print(packet) + packet = IPPacket(packet) + packet.decode() + # print(packet) + packet = ICMPPacket(packet) + packet.decode() + # print(packet) + errors += verify_packet(packet, ping_request_infos) + packet.encode() + packet.decode() + # print(packet) + errors += verify_packet(packet, ping_request_infos) + + self.assertEqual(errors, 0) + + def test_udp(self): + errors = 0 + # UDP packet + packet = MACPacket(udp) + packet.decode_remove_header() + # print(packet) + packet = IPPacket(packet) + packet.decode() + # print(packet) + packet = UDPPacket(packet) + packet.decode() + # print(packet) + if packet.length != (len(packet)+udp_header.length): + errors += 1 + errors += verify_packet(packet, udp_infos) + packet.encode() + packet.decode() + # print(packet) + if packet.length != (len(packet)+udp_header.length): + errors += 1 + errors += verify_packet(packet, udp_infos) + + self.assertEqual(errors, 0) + + def test_etherbone(self): + # Writes/Reads + writes = EtherboneWrites(base_addr=0x1000, datas=[i for i in range(16)]) + reads = EtherboneReads(base_ret_addr=0x2000, addrs=[i for i in range(16)]) + + # Record + record = EtherboneRecord() + record.writes = writes + record.reads = reads + record.wcount = len(writes.get_datas()) + record.rcount = len(reads.get_addrs()) + + # Packet + packet = EtherbonePacket() + from copy import deepcopy + packet.records = [deepcopy(record) for i in range(8)] + # print(packet) + packet.encode() + # print(packet) + + # Send packet over UDP to check against Wireshark dissector + #import socket + #sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + #sock.sendto(bytes(packet), ("192.168.1.1", 20000)) + + packet = EtherbonePacket(packet) + packet.decode() + print(packet) + + self.assertEqual(0, 0) # FIXME