class cases_BasicPacketEncodingLayer(object): @abc.abstractmethod def get_encoder(self): """returns the encoder to be used""" def setUp(self): self.encoder1 = self.get_encoder() self.encoder2 = self.get_encoder() self.packetEncodingLayer1 = BasicPacketEncodingLayer( encoder=self.encoder1) self.packetEncodingLayer2 = BasicPacketEncodingLayer( encoder=self.encoder2) self.udp4interface1 = UDP4Interface(0) synced_data_struct_factory1 = PiCNSyncDataStructFactory() synced_data_struct_factory1.register("faceidtable", FaceIDDict) synced_data_struct_factory1.create_manager() self.faceidtable1 = synced_data_struct_factory1.manager.faceidtable() self.linkLayer1 = BasicLinkLayer([self.udp4interface1], self.faceidtable1) self.udp4interface2 = UDP4Interface(0) synced_data_struct_factory2 = PiCNSyncDataStructFactory() synced_data_struct_factory2.register("faceidtable", FaceIDDict) synced_data_struct_factory2.create_manager() self.faceidtable2 = synced_data_struct_factory2.manager.faceidtable() self.linkLayer2 = BasicLinkLayer([self.udp4interface2], self.faceidtable2) self.port1 = self.linkLayer1.interfaces[0].get_port() self.port2 = self.linkLayer2.interfaces[0].get_port() self.q1_fromLower = Queue() self.q1_fromHigher = Queue() self.q1_toLower = Queue() self.q1_toHigher = Queue() self.q2_fromLower = Queue() self.q2_fromHigher = Queue() self.q2_toLower = Queue() self.q2_toHigher = Queue() self.packetEncodingLayer1.queue_from_lower = self.q1_fromLower self.packetEncodingLayer1.queue_from_higher = self.q1_fromHigher self.packetEncodingLayer1.queue_to_lower = self.q1_toLower self.packetEncodingLayer1.queue_to_higher = self.q1_toHigher self.packetEncodingLayer2.queue_from_lower = self.q2_fromLower self.packetEncodingLayer2.queue_from_higher = self.q2_fromHigher self.packetEncodingLayer2.queue_to_lower = self.q2_toLower self.packetEncodingLayer2.queue_to_higher = self.q2_toHigher self.linkLayer1.queue_from_higher = self.q1_toLower #from higher in Linklayer is to Lower from Encoding Layer self.linkLayer1.queue_to_higher = self.q1_fromLower #to higher in Linklayer is from lower from Encoding Layer self.linkLayer2.queue_from_higher = self.q2_toLower # from higher in Linklayer is to Lower from Encoding Layer self.linkLayer2.queue_to_higher = self.q2_fromLower # to higher in Linklayer is from lower from Encoding Layer def tearDown(self): self.packetEncodingLayer1.stop_process() self.packetEncodingLayer2.stop_process() self.linkLayer1.stop_process() self.linkLayer2.stop_process() def test_BasicPacketEncodingLayer_bidirectional(self): """Test the BasicPacketEncodingLayer bidirectional""" self.packetEncodingLayer1.start_process() #test interest i = [2, Interest("/test/data")] self.q1_fromHigher.put(i) try: ei = self.q1_toLower.get(timeout=2.0) except: self.fail() self.q1_fromLower.put(ei) di = self.q1_toHigher.get() self.assertEqual(i, di) #test content c = [2, Content("/test/data", "HelloWorld")] self.q1_fromHigher.put(c) try: ec = self.q1_toLower.get(timeout=2.0) except: self.fail() self.q1_fromLower.put(ec) try: dc = self.q1_toHigher.get(timeout=2.0) except: self.fail() self.assertEqual(c, dc) def test_BasicPacketEncodingLayer_interest_transfer_udp4(self): """Test the BasicPacketEncodingLayer and the UDP4LinkLayer to verify interest transport""" self.linkLayer1.start_process() self.linkLayer2.start_process() self.packetEncodingLayer1.start_process() self.packetEncodingLayer2.start_process() fid = self.linkLayer1.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.port2), 0)) i = Interest("/test/data") #PUT interest in node 1 queues self.packetEncodingLayer1.queue_from_higher.put([fid, i]) #GET interest from node 2 queues try: data = self.packetEncodingLayer2.queue_to_higher.get(timeout=2.0) except: self.fail() #Check Packet ri = data[1] self.assertEqual(ri, i) def test_BasicPacketEncodingLayer_content_transfer_udp4(self): """Test the BasicPacketEncodingLayer and the UDP4LinkLayer to verify content transport""" self.linkLayer1.start_process() self.linkLayer2.start_process() self.packetEncodingLayer1.start_process() self.packetEncodingLayer2.start_process() fid = self.linkLayer1.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.port2), 0)) c = Content("/test/data", "HelloWorld") # PUT interest in node 1 queues self.packetEncodingLayer1.queue_from_higher.put([fid, c]) # GET interest from node 2 queues try: data = self.packetEncodingLayer2.queue_to_higher.get(timeout=2.0) except: self.fail() # Check Packet rc = data[1] self.assertEqual(rc, c)
class test_Mgmt(unittest.TestCase): def setUp(self): synced_data_struct_factory = PiCNSyncDataStructFactory() synced_data_struct_factory.register("cs", ContentStoreMemoryExact) synced_data_struct_factory.register( "fib", ForwardingInformationBaseMemoryPrefix) synced_data_struct_factory.register("pit", PendingInterstTableMemoryExact) synced_data_struct_factory.register("faceidtable", FaceIDDict) synced_data_struct_factory.create_manager() cs = synced_data_struct_factory.manager.cs() fib = synced_data_struct_factory.manager.fib() pit = synced_data_struct_factory.manager.pit() faceidtable = synced_data_struct_factory.manager.faceidtable() interface = UDP4Interface(0) self.linklayer = BasicLinkLayer([interface], faceidtable) self.linklayerport = self.linklayer.interfaces[0].get_port() self.q1 = multiprocessing.Queue() self.linklayer.queue_from_higher = self.q1 self.mgmt = Mgmt(cs, fib, pit, self.linklayer, self.linklayerport) self.testMgmtSock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.testMgmtSock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.testMgmtSock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.mgmt_client = MgmtClient(self.linklayerport) def tearDown(self): self.linklayer.stop_process() self.mgmt.stop_process() self.testMgmtSock1.close() self.testMgmtSock2.close() self.testMgmtSock3.close() def test_mgmt_new_face(self): """Test the mgmt interace to create a new face""" self.linklayer.start_process() self.mgmt.start_process() self.testMgmtSock1.connect(("127.0.0.1", self.linklayerport)) self.testMgmtSock1.send( "GET /linklayer/newface/127.0.0.1:9000:0 HTTP/1.1\r\n\r\n".encode( )) data = self.testMgmtSock1.recv(1024) self.testMgmtSock1.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newface OK:0\r\n" ) self.assertEqual(self.linklayer.faceidtable.get_num_entries(), 1) self.assertEqual( self.linklayer.faceidtable.get_face_id( AddressInfo(("127.0.0.1", 9000), 0)), 0) self.assertEqual(self.linklayer.faceidtable.get_address_info(0), AddressInfo(("127.0.0.1", 9000), 0)) def test_mgmt_multiple_new_face(self): """Test the mgmt interace to create multiple new faces with deduplication""" self.linklayer.start_process() self.mgmt.start_process() self.testMgmtSock1.connect(("127.0.0.1", self.linklayerport)) self.testMgmtSock1.send( "GET /linklayer/newface/127.0.0.1:9000:0 HTTP/1.1\r\n\r\n".encode( )) data = self.testMgmtSock1.recv(1024) self.testMgmtSock1.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newface OK:0\r\n" ) self.testMgmtSock2.connect(("127.0.0.1", self.linklayerport)) self.testMgmtSock2.send( "GET /linklayer/newface/127.0.0.1:8000:0 HTTP/1.1\r\n\r\n".encode( )) data = self.testMgmtSock2.recv(1024) self.testMgmtSock2.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newface OK:1\r\n" ) self.testMgmtSock3.connect(("127.0.0.1", self.linklayerport)) self.testMgmtSock3.send( "GET /linklayer/newface/127.0.0.1:9000:0 HTTP/1.1\r\n\r\n".encode( )) data = self.testMgmtSock3.recv(1024) self.testMgmtSock3.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newface OK:0\r\n" ) self.assertEqual(self.linklayer.faceidtable.get_num_entries(), 2) self.assertEqual( self.linklayer.faceidtable.get_face_id( AddressInfo(("127.0.0.1", 9000), 0)), 0) self.assertEqual( self.linklayer.faceidtable.get_face_id( AddressInfo(("127.0.0.1", 8000), 0)), 1) def test_mgmt_add_forwaring_rule(self): """Test adding Forwarding rules""" self.linklayer.start_process() self.mgmt.start_process() self.testMgmtSock1.connect(("127.0.0.1", self.linklayerport)) self.testMgmtSock1.send( "GET /icnlayer/newforwardingrule/%2Ftest%2Fdata:2 HTTP/1.1\r\n\r\n" .encode()) data = self.testMgmtSock1.recv(1024) self.testMgmtSock1.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newforwardingrule OK:2\r\n" ) time.sleep(1) self.testMgmtSock2.connect(("127.0.0.1", self.linklayerport)) self.testMgmtSock2.send( "GET /icnlayer/newforwardingrule/%2Fdata%2Ftest:3 HTTP/1.1\r\n\r\n" .encode()) data = self.testMgmtSock2.recv(1024) self.testMgmtSock2.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newforwardingrule OK:3\r\n" ) self.assertEqual( self.mgmt.fib.find_fib_entry(Name("/test/data")).faceid, [2]) self.assertEqual( self.mgmt.fib.find_fib_entry(Name("/data/test")).faceid, [3]) def test_mgmt_add_content(self): """Test adding content""" self.linklayer.start_process() self.mgmt.start_process() self.testMgmtSock1.connect(("127.0.0.1", self.linklayerport)) self.testMgmtSock1.send( "GET /icnlayer/newcontent/%2Ftest%2Fdata:HelloWorld HTTP/1.1\r\n\r\n" .encode()) data = self.testMgmtSock1.recv(1024) self.testMgmtSock1.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n" ) time.sleep(1) self.testMgmtSock2.connect(("127.0.0.1", self.linklayerport)) self.testMgmtSock2.send( "GET /icnlayer/newcontent/%2Fdata%2Ftest:GoodBye HTTP/1.1\r\n\r\n". encode()) data = self.testMgmtSock2.recv(1024) self.testMgmtSock2.close() self.assertEqual( data.decode(), "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n" ) self.assertEqual( self.mgmt.cs.find_content_object( Name("/test/data")).content.content, "HelloWorld") self.assertEqual( self.mgmt.cs.find_content_object( Name("/data/test")).content.content, "GoodBye") def test_add_face_mgmt_client(self): """Test adding a face using the mgmtclient""" self.linklayer.start_process() self.mgmt.start_process() data = self.mgmt_client.add_face("127.0.0.1", 9000, 0) self.assertEqual( data, "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newface OK:0\r\n" ) self.assertEqual(self.linklayer.faceidtable.get_num_entries(), 1) self.assertEqual(self.linklayer.faceidtable.get_address_info(0), AddressInfo(("127.0.0.1", 9000), 0)) self.assertEqual( self.linklayer.faceidtable.get_face_id( AddressInfo(("127.0.0.1", 9000), 0)), 0) def test_add_forwarding_rule_mgmt_client(self): """Test adding forwarding rule using MgmtClient""" self.linklayer.start_process() self.mgmt.start_process() data = self.mgmt_client.add_forwarding_rule(Name("/test/data"), [2]) self.assertEqual( "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newforwardingrule OK:2\r\n", data) time.sleep(1) data = self.mgmt_client.add_forwarding_rule(Name("/data/test"), [3]) self.assertEqual( data, "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newforwardingrule OK:3\r\n" ) self.assertEqual( self.mgmt.fib.find_fib_entry(Name("/test/data")).faceid, [2]) self.assertEqual( self.mgmt.fib.find_fib_entry(Name("/data/test")).faceid, [3]) def test_mgmt_add_content_mgmt_client(self): """Test adding content using MgmtClient""" self.linklayer.start_process() self.mgmt.start_process() data = self.mgmt_client.add_new_content(Name("/test/data"), "HelloWorld") self.assertEqual( data, "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n" ) time.sleep(1) data = self.mgmt_client.add_new_content(Name("/data/test"), "GoodBye") self.assertEqual( data, "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n newcontent OK\r\n" ) self.assertEqual( self.mgmt.cs.find_content_object( Name("/test/data")).content.content, "HelloWorld") self.assertEqual( self.mgmt.cs.find_content_object( Name("/data/test")).content.content, "GoodBye") def test_mgmt_shutdown_mgmt_client(self): """Test adding content""" self.linklayer.start_process() self.mgmt.start_process() data = self.mgmt_client.shutdown() self.assertEqual( data, "HTTP/1.1 200 OK \r\n Content-Type: text/html \r\n\r\n shutdown\r\n" )
class test_BasicLinkLayer(unittest.TestCase): """Test the Basic Link Layer""" def setUp(self): self.udp4interface1 = UDP4Interface(0) synced_data_struct_factory1 = PiCNSyncDataStructFactory() synced_data_struct_factory1.register("faceidtable", FaceIDDict) synced_data_struct_factory1.create_manager() self.faceidtable1 = synced_data_struct_factory1.manager.faceidtable() self.linklayer1 = BasicLinkLayer([self.udp4interface1], self.faceidtable1) self.linklayer1.queue_to_higher = multiprocessing.Queue() self.linklayer1.queue_from_higher = multiprocessing.Queue() self.udp4interface2 = UDP4Interface(0) synced_data_struct_factory2 = PiCNSyncDataStructFactory() synced_data_struct_factory2.register("faceidtable", FaceIDDict) synced_data_struct_factory2.create_manager() self.faceidtable2 = synced_data_struct_factory2.manager.faceidtable() self.linklayer2 = BasicLinkLayer([self.udp4interface2], self.faceidtable2) self.linklayer2.queue_to_higher = multiprocessing.Queue() self.linklayer2.queue_from_higher = multiprocessing.Queue() self.udp4interface3 = UDP4Interface(0) synced_data_struct_factory3 = PiCNSyncDataStructFactory() synced_data_struct_factory3.register("faceidtable", FaceIDDict) synced_data_struct_factory3.create_manager() self.faceidtable3 = synced_data_struct_factory3.manager.faceidtable() self.linklayer3 = BasicLinkLayer([self.udp4interface3], self.faceidtable3) self.linklayer3.queue_to_higher = multiprocessing.Queue() self.linklayer3.queue_from_higher = multiprocessing.Queue() self.testSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.testSock.bind(("0.0.0.0", 0)) self.test_port = self.testSock.getsockname()[1] def tearDown(self): self.testSock.close() self.udp4interface1.close() self.udp4interface2.close() self.linklayer1.stop_process() self.linklayer2.stop_process() def test_receiving_a_packet(self): """Test if a packet is received correctly""" self.linklayer1.start_process() self.testSock.sendto("HelloWorld".encode(), ("127.0.0.1", self.udp4interface1.get_port())) data = self.linklayer1.queue_to_higher.get(timeout=2.0) faceid = data[0] content = data[1].decode() self.assertEqual("HelloWorld", content) self.assertEqual(self.linklayer1.faceidtable.get_num_entries(), 1) self.assertEqual( self.linklayer1.faceidtable.get_address_info(0).address[1], self.test_port) self.assertEqual( self.linklayer1.faceidtable.get_address_info(0).interface_id, 0) def test_sending_a_packet(self): """Test if a packet is sent correctly""" self.linklayer1.start_process() fid = self.linklayer1.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.test_port), self.linklayer1.interfaces.index(self.udp4interface1))) self.linklayer1.queue_from_higher.put([fid, "HelloWorld".encode()]) data, addr = self.testSock.recvfrom(8192) self.assertEqual(data.decode(), "HelloWorld") def test_sending_and_receiving_a_packet(self): """Test sending/receiving in a single case""" self.linklayer1.start_process() self.linklayer2.start_process() fid = self.linklayer1.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.udp4interface2.get_port()), self.linklayer1.interfaces.index(self.udp4interface1))) self.linklayer1.queue_from_higher.put([fid, "HelloWorld".encode()]) data = self.linklayer2.queue_to_higher.get(timeout=2.0) faceid = data[0] packet = data[1] self.assertEqual(faceid, 0) self.assertEqual(packet.decode(), "HelloWorld") def test_sending_and_receiving_packets(self): """Test sending/receiving many packets in a single case""" self.linklayer1.start_process() self.linklayer2.start_process() fid1 = self.linklayer1.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.udp4interface2.get_port()), self.linklayer1.interfaces.index(self.udp4interface1))) self.linklayer1.start_process() self.linklayer2.start_process() fid2 = self.linklayer2.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.udp4interface1.get_port()), self.linklayer2.interfaces.index(self.udp4interface2))) for i in range(1, int(1e3)): str1 = "HelloWorld" + str(i) str2 = "GoodBye" + str(i) self.linklayer1.queue_from_higher.put([fid1, str1.encode()]) self.linklayer2.queue_from_higher.put([fid2, str2.encode()]) d2 = self.linklayer2.queue_to_higher.get(timeout=5.0) d1 = self.linklayer1.queue_to_higher.get(timeout=5.0) packet2 = d1[1].decode() packet1 = d2[1].decode() self.assertEqual(packet1, str1) self.assertEqual(packet2, str2) for i in range(1, int(1e3)): str1 = "HelloWorld" + str(i) str2 = "GoodBye" + str(i) self.linklayer1.queue_from_higher.put([fid1, str1.encode()]) self.linklayer2.queue_from_higher.put([fid2, str2.encode()]) d1 = self.linklayer1.queue_to_higher.get(timeout=5.0) d2 = self.linklayer2.queue_to_higher.get(timeout=5.0) packet2 = d1[1].decode() packet1 = d2[1].decode() self.assertEqual(packet1, str1) self.assertEqual(packet2, str2) def test_sending_and_receiving_pacets_three_nodes(self): """Testing sending/receiving packets with three nodes""" self.linklayer1.start_process() self.linklayer2.start_process() self.linklayer3.start_process() fid1_2 = self.linklayer1.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.udp4interface2.get_port()), self.linklayer1.interfaces.index(self.udp4interface1))) fid1_3 = self.linklayer1.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.udp4interface3.get_port()), self.linklayer1.interfaces.index(self.udp4interface1))) fid2_1 = self.linklayer2.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.udp4interface1.get_port()), self.linklayer2.interfaces.index(self.udp4interface2))) fid3_1 = self.linklayer3.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.udp4interface1.get_port()), self.linklayer3.interfaces.index(self.udp4interface3))) fid3_2 = self.linklayer3.faceidtable.get_or_create_faceid( AddressInfo(("127.0.0.1", self.udp4interface2.get_port()), self.linklayer3.interfaces.index(self.udp4interface3))) for i in range(1, 100): str1 = "Node1" + str(i) str2 = "Node2" + str(i) str3 = "Node3" + str(i) # Node 1 ---> Node 2 self.linklayer1.queue_from_higher.put([fid1_2, str1.encode()]) try: data1_2 = self.linklayer2.queue_to_higher.get(timeout=2.0) except: self.fail() # Node 1 ---> Node 3 self.linklayer1.queue_from_higher.put([fid1_3, str1.encode()]) try: data1_3 = self.linklayer3.queue_to_higher.get(timeout=2.0) except: self.fail() # Node 2 ---> Node 1 self.linklayer2.queue_from_higher.put([fid2_1, str2.encode()]) try: data2_1 = self.linklayer1.queue_to_higher.get(timeout=2.0) except: self.fail() # Node 3 ---> Node 1 self.linklayer3.queue_from_higher.put([fid3_1, str3.encode()]) try: data3_1 = self.linklayer1.queue_to_higher.get(timeout=2.0) except: self.fail() # Node 3 ---> Node 2 self.linklayer3.queue_from_higher.put([fid3_2, str3.encode()]) try: data3_2 = self.linklayer2.queue_to_higher.get(timeout=2.0) except: self.fail() self.assertEqual(data1_2[1].decode(), str1) self.assertEqual(data1_3[1].decode(), str1) self.assertEqual(data2_1[1].decode(), str2) self.assertEqual(data3_1[1].decode(), str3) self.assertEqual(data3_2[1].decode(), str3)