def setUp(self): self.key = _key print "Runing test ", self self.input_queue = Queue.Queue() self.output_queue = Queue.Queue() # Apple is unsigned, only has name name_apple = CCNxNameFactory.from_uri("lci:/apple") self.co_apple = CCNxContentObject(name_apple, None) self.co_apple.generate_wireformat() # Blueberry is signed, so has a KeyId in it name_blueberry = CCNxNameFactory.from_uri("lci:/blue/berry") self.co_blueberry = CCNxContentObject(name_blueberry, None) self.co_blueberry.sign(self.key) self.objects_by_name = {} self.objects_by_hash = {} self.objects_by_name[self.co_apple.name] = self.co_apple self.objects_by_name[self.co_blueberry.name] = self.co_blueberry self.objects_by_hash[self.co_blueberry.hash().tostring()] = self.co_blueberry signer = CCNxSignature(self.key) self.__keyid_array = signer.keyid_array self.thread = LookupThread(self.input_queue, self.output_queue, self.__keyid_array, self.objects_by_name, self.objects_by_hash) self.thread.start()
class TestCCnxzGenServer(unittest.TestCase): def setUp(self): self.key = _key print "Runing test ", self self.input_queue = Queue.Queue() self.output_queue = Queue.Queue() # Apple is unsigned, only has name name_apple = CCNxNameFactory.from_uri("lci:/apple") self.co_apple = CCNxContentObject(name_apple, None) self.co_apple.generate_wireformat() # Blueberry is signed, so has a KeyId in it name_blueberry = CCNxNameFactory.from_uri("lci:/blue/berry") self.co_blueberry = CCNxContentObject(name_blueberry, None) self.co_blueberry.sign(self.key) self.objects_by_name = {} self.objects_by_hash = {} self.objects_by_name[self.co_apple.name] = self.co_apple self.objects_by_name[self.co_blueberry.name] = self.co_blueberry self.objects_by_hash[self.co_blueberry.hash().tostring()] = self.co_blueberry def test_server(self): """ Send an interest to the SocketReaderThread, let it go through the prodessing pipeline, and read the response from SocketWriterThread via our udp client port. :return: """ port = 9696 gen_server = CCNxzGenServer(port=port, key=_key, objects_by_name=self.objects_by_name, objects_by_hash=self.objects_by_hash) gen_server.start() client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: client.settimeout(0.25) interest = CCNxInterest(self.co_apple.name) client.sendto(interest.wire_format, ("127.0.0.1", port)) data = client.recv(4096) data_array = array.array("B", data) self.assertEqual(self.co_apple.wire_format, data_array, "Did not receive expected data") finally: client.close() gen_server.stop() try: gen_server.join() except KeyboardInterrupt: print "Got keyboard interrupt"
def _generate_content_object(self): length = 0 manifest_links_tlv = None manifest_start_chunk_tlv = None manifest_hashes_tlv = None data_links_tlv = None data_start_chunk_tlv = None data_hashes_tlv = None # Manifest hash list section if len(self.__manifest_links) > 0: manifest_start_chunk = self.__manifest_links[0].name.chunk_number manifest_start_chunk_array = CCNxTlv.number_to_array( manifest_start_chunk) manifest_start_chunk_tlv = CCNxTlv(T_START_CHUNK_NUMBER, len(manifest_start_chunk_array), manifest_start_chunk_array) manifest_hashes = CCNxManifest._generate_hash_list( self.__manifest_links) manifest_hashes_tlv = CCNxTlv(T_HASH_LIST, len(manifest_hashes), manifest_hashes) manifest_links_tlv = CCNxTlv( T_MANIFEST_LINKS, 4 + manifest_start_chunk_tlv.length + 4 + manifest_hashes_tlv.length, None) length += 4 + manifest_links_tlv.length # Data links section if len(self.__data_links) > 0: data_start_chunk = self.__data_links[0].name.chunk_number data_start_chunk_array = CCNxTlv.number_to_array(data_start_chunk) data_start_chunk_tlv = CCNxTlv(T_START_CHUNK_NUMBER, len(data_start_chunk_array), data_start_chunk_array) data_hashes = CCNxManifest._generate_hash_list(self.__data_links) data_hashes_tlv = CCNxTlv(T_HASH_LIST, len(data_hashes), data_hashes) data_links_tlv = CCNxTlv( T_DATA_LINKS, 4 + data_start_chunk_tlv.length + 4 + data_hashes_tlv.length, None) length += 4 + data_links_tlv.length ## manifest_tlv = CCNxTlv(T_MANIFEST, length, None) co = CCNxContentObject(self.__name, None, manifest_tlv, manifest_links_tlv, manifest_start_chunk_tlv, manifest_hashes_tlv, data_links_tlv, data_start_chunk_tlv, data_hashes_tlv) return co
def test_not_interest(self): co = CCNxContentObject(CCNxNameFactory.from_uri("lci:/foo/bar"), None) entry = QueueEntry(client=None, data=None, socket=None, message=co) self.input_queue.put(entry) try: self.output_queue.get(block=True, timeout=0.25) empty = False except Queue.Empty: empty = True self.assertTrue(empty, "Should have failed for non-interest")
def test_object_name_expiry_none(self): name = CCNxNameFactory.from_uri("lci:/apple/pie") expiry_time = 6321 co = CCNxContentObject(name, expiry_time) self.assertEqual(name, co.name, "Names do not match") truth = [ 0x01, 0x02, 0x00, 0x26, 0x00, 0x00, 0x00, 0x08, # Content Object 0x00, 0x02, 0x00, 0x1a, # name 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00, 0x05, 0x61, 0x70, 0x70, 0x6c, 0x65, 0x00, 0x01, 0x00, 0x03, 0x70, 0x69, 0x65, # Expiry Time 0x00, 0x06, 0x00, 0x02, 0x18, 0xB1 ] truth_array = array.array("B", truth) wire_format = co.wire_format # print ["0x{:02x}".format(b) for b in wire_format] self.assertEqual(truth_array, wire_format)
def from_wire_format(wire_format): parser = CCNxParser(wire_format) parser.parse() keyid_restr = None if parser.keyid_restriction_tlv is not None: keyid_restr = array.array("B", parser.keyid_restriction_tlv.value) hash_restr = None if parser.hash_restriction_tlv is not None: hash_restr = array.array("B", parser.hash_restriction_tlv.value) linear = parser.linearize_body() if linear[0].type == T_INTEREST: message = CCNxInterest(name=CCNxNameFactory.from_tlv_list( parser.name_tlv.value), keyid_restr=keyid_restr, hash_restr=hash_restr) elif linear[0].type == T_OBJECT: name = CCNxNameFactory.from_tlv_list(parser.name_tlv.value) # convert linear which is a list to varargs with "*" message = CCNxContentObject(name, None, *linear) keyid = None if parser.keyid_tlv is not None: keyid = parser.keyid_tlv.value message.keyid = keyid manifest = None if parser.manifest_tlv is not None: manifest = parser.manifest_tlv.value message.manifest = manifest payload = None if parser.payload_tlv is not None: payload = parser.payload_tlv.value message.payload = payload else: raise ValueError("Unsupported message type: ", linear[0]) return message
def test_container_tlvs_and_none_tlvs(self): name = CCNxNameFactory.from_uri("lci:/apple/pie") expiry_time = 6321 valalg_tlv = CCNxTlv(T_VALALG, 8, None) keyid_tlv = CCNxTlv(T_KEYID, 4, [1, 2, 3, 4]) payload_tlv = CCNxTlv(T_PAYLOAD, 3, [1, 2, 3]) co = CCNxContentObject(name, expiry_time, None, payload_tlv, valalg_tlv, keyid_tlv) self.assertEqual(name, co.name, "Names do not match") truth = [ 0x01, 0x02, 0x00, 0x39, 0x00, 0x00, 0x00, 0x08, # Content Object 0x00, 0x02, 0x00, 0x2d, # Name 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00, 0x05, 0x61, 0x70, 0x70, 0x6c, 0x65, 0x00, 0x01, 0x00, 0x03, 0x70, 0x69, 0x65, # Expiry Time 0x00, 0x06, 0x00, 0x02, 0x18, 0xb1, # Payload TLV 0x00, 0x01, 0x00, 0x03, 0x01, 0x02, 0x03, # ValAlg 0x00, 0x03, 0x00, 0x08, # KeyId 0x00, 0x09, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04 ] truth_array = array.array("B", truth) wire_format = co.wire_format # print ["0x{:02x}".format(b) for b in wire_format] self.assertEqual(truth_array, wire_format)
def test_object_name_expiry_extras(self): name = CCNxNameFactory.from_uri("lci:/apple/pie") expiry_time = 6321 payload_tlv = CCNxTlv(T_PAYLOAD, 3, [1, 2, 3]) co = CCNxContentObject(name, expiry_time, None, payload_tlv) self.assertEqual(name, co.name, "Names do not match") truth = [ 0x01, 0x02, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x08, # Content Object 0x00, 0x02, 0x00, 0x21, # name 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00, 0x05, 0x61, 0x70, 0x70, 0x6c, 0x65, 0x00, 0x01, 0x00, 0x03, 0x70, 0x69, 0x65, # Expiry Time 0x00, 0x06, 0x00, 0x02, 0x18, 0xB1, # Payload TLV 0x00, 0x01, 0x00, 0x03, 0x01, 0x02, 0x03 ] truth_array = array.array("B", truth) wire_format = co.wire_format # print ["0x{:02x}".format(b) for b in wire_format] self.assertEqual(truth_array, wire_format)
class TestCCNxzGenServerLookupThread(unittest.TestCase): def setUp(self): self.key = _key print "Runing test ", self self.input_queue = Queue.Queue() self.output_queue = Queue.Queue() # Apple is unsigned, only has name name_apple = CCNxNameFactory.from_uri("lci:/apple") self.co_apple = CCNxContentObject(name_apple, None) self.co_apple.generate_wireformat() # Blueberry is signed, so has a KeyId in it name_blueberry = CCNxNameFactory.from_uri("lci:/blue/berry") self.co_blueberry = CCNxContentObject(name_blueberry, None) self.co_blueberry.sign(self.key) self.objects_by_name = {} self.objects_by_hash = {} self.objects_by_name[self.co_apple.name] = self.co_apple self.objects_by_name[self.co_blueberry.name] = self.co_blueberry self.objects_by_hash[self.co_blueberry.hash().tostring()] = self.co_blueberry signer = CCNxSignature(self.key) self.__keyid_array = signer.keyid_array self.thread = LookupThread(self.input_queue, self.output_queue, self.__keyid_array, self.objects_by_name, self.objects_by_hash) self.thread.start() def tearDown(self): self.thread.stop() while self.thread.is_alive(): self.thread.join(timeout=0.1) def test_not_interest(self): co = CCNxContentObject(CCNxNameFactory.from_uri("lci:/foo/bar"), None) entry = QueueEntry(client=None, data=None, socket=None, message=co) self.input_queue.put(entry) try: self.output_queue.get(block=True, timeout=0.25) empty = False except Queue.Empty: empty = True self.assertTrue(empty, "Should have failed for non-interest") def test_interest_bad_keyid(self): """Interest with keyid resriction that does not match our key""" name = CCNxNameFactory.from_uri("lci:/blue/berry") keyid = [1] * 32 interest = CCNxInterest(name=name, keyid_restr=keyid) entry = QueueEntry(client=None, data=None, socket=None, message=interest) self.input_queue.put(entry) try: self.output_queue.get(block=True, timeout=0.25) empty = False except Queue.Empty: empty = True self.assertTrue(empty, "Timed out trying to match interest") def test_interest_good_keyid(self): """Interest with keyid resriction that does not match our key""" name = CCNxNameFactory.from_uri("lci:/blue/berry") keyid = self.__keyid_array interest = CCNxInterest(name=name, keyid_restr=keyid) entry = QueueEntry(client=None, data=None, socket=None, message=interest) self.input_queue.put(entry) try: test = self.output_queue.get(block=True, timeout=0.25) empty = False except Queue.Empty: test = None empty = True self.assertTrue(not empty, "Timed out trying to match interest") self.assertTrue(type(test) == tuple, "Should have gotten a tuple") def test_interest_name_match(self): name = CCNxNameFactory.from_uri("lci:/apple") interest = CCNxInterest(name) entry = QueueEntry(client=None, data=None, socket=None, message=interest) self.input_queue.put(entry) try: test = self.output_queue.get(block=True, timeout=.25) empty = False except Queue.Empty: test = None empty = True self.assertTrue(not empty, "Timed out trying to match interest") self.assertTrue(type(test) == tuple, "Should have gotten a tuple") def test_interest_hash_match(self): name = CCNxNameFactory.from_uri("lci:/blue/berry") hash_restriction = self.co_blueberry.hash() interest = CCNxInterest(name=name, keyid_restr=None, hash_restr=hash_restriction) entry = QueueEntry(client=None, data=None, socket=None, message=interest) self.input_queue.put(entry) try: test = self.output_queue.get(block=True, timeout=.25) empty = False except Queue.Empty: test = None empty = True self.assertTrue(not empty, "Timed out trying to match interest") self.assertTrue(type(test) == tuple, "Should have gotten a tuple")