def readFile(self, filename, owner): logger.info("Request to Read file %s by owner %s" % (filename, owner)) self.curr_node = getCurrentNode() node = self.findSucc(sha256(owner + ":" + filename).hexdigest()) if node.port == self.node.port and node.ip == self.node.ip: if os.path.isfile(os.getcwd() + "/" + filename): with open(filename, 'r') as f: data = f.readlines() if str(owner) == str(data[2].strip().split('=')[1]): rf_metadata = RFileMetadata() rf_metadata.filename = data[0].strip().split('=')[1] rf_metadata.version = int( data[1].strip().split('=')[1]) rf_metadata.owner = data[2].strip().split('=')[1] rf_metadata.contentHash = data[3].strip().split('=')[1] r_file = RFile() r_file.meta = rf_metadata r_file.content = data[4].strip().split('=')[1] return r_file else: logger.error( "File %s does not exist for the given owner %s" % (filename, owner)) raise system_exception( "File does not exist with the given owner name") else: logger.error("No file exist with the name %s" % filename) raise system_exception("No file exist with the name%s" % filename) else: logger.error("Key is not associated with the node") raise system_exception("Key is not associated with the node")
def incorrectOwnerTest(host, port_list): if len(port_list) < 2: return # Make socket transport = TSocket.TSocket(host, port_list[0]) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() meta_obj = RFileMetadata() meta_obj.filename = "book.txt" meta_obj.version = 0 meta_obj.owner = "Brad" meta_obj.contentHash = \ hashlib.sha256(("%s:%s" % (meta_obj.filename,\ meta_obj.owner)).encode("utf-8")).hexdigest() content_str = "Test String" file_obj = RFile() file_obj.meta = meta_obj file_obj.content = content_str client.writeFile(file_obj) transport.close() # Make socket transport = TSocket.TSocket(host, port_list[1]) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() try: res = client.readFile("book.txt", "Drake") except SystemException: print("Success: Incorrect owner") transport.close()
def testReadAfterWriteError(): # Make socket transport = TSocket.TSocket('alpha.cs.binghamton.edu', 9000) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() meta_obj = RFileMetadata() meta_obj.filename = "book.txt" meta_obj.version = 0 meta_obj.owner = "Brad" meta_obj.contentHash = hashlib.sha256(meta_obj.filename +\ ":" + meta_obj.owner).hexdigest() content_str = "Knowledge Bitch!" file_obj = RFile() file_obj.meta = meta_obj file_obj.content = content_str client.writeFile(file_obj) transport.close() # Make socket transport = TSocket.TSocket('alpha.cs.binghamton.edu', 9001) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() try: res = client.readFile("book.txt", "Brad") print "Read after write error NOT succesful" except SystemException: print "Read after write error successful" transport.close()
def incorrectOwnerTest(): # Make socket transport = TSocket.TSocket('alpha.cs.binghamton.edu', 9000) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() meta_obj = RFileMetadata() meta_obj.filename = "book.txt" meta_obj.version = 0 meta_obj.owner = "Brad" meta_obj.contentHash = hashlib.sha256(meta_obj.filename +\ ":" + meta_obj.owner).hexdigest() content_str = "Test String" file_obj = RFile() file_obj.meta = meta_obj file_obj.content = content_str client.writeFile(file_obj) transport.close() # Make socket transport = TSocket.TSocket('alpha.cs.binghamton.edu', 9000) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() try: res = client.readFile("book.txt", "Drake") except SystemException: print "Success: Incorrect owner" transport.close()
def readFile(self, filename, owner): key = hashlib.sha256(owner + ":" + filename).hexdigest() node = self.findSucc(key) if not node.id == self.NodeID: raise SystemException("Write File Error - Server does not own fil`s id") found = False for my_file in self.files: if filename == my_file.meta.filename and owner == my_file.meta.owner: found = True f = RFile() m = RFileMetadata() m.filename = filename m.version = my_file.meta.version m.owner = owner m.contentHash = my_file.meta.contentHash f.meta = m f.content = my_file.content return f if not found: raise SystemException("Read File Error - File not found")
def writeFile(self, rFile): key = hashlib.sha256(rFile.meta.owner + ":" + rFile.meta.filename).hexdigest() node = self.findSucc(key) if not node.id == self.NodeID: raise SystemException("Write File Error - Server does not own fil`s id") new_file = True for my_file in self.files: if rFile.meta.filename == my_file.meta.filename: my_file.meta.version += 1 my_file.meta.owner = rFile.meta.owner my_file.meta.contentHash = hashlib.sha256(rFile.meta.contentHash).hexdigest() my_file.content = rFile.content new_file = False if new_file: f = RFile() m = RFileMetadata() m.filename = rFile.meta.filename m.version = 0 m.owner = rFile.meta.owner m.contentHash = hashlib.sha256(rFile.meta.contentHash).hexdigest() f.meta = m f.content = rFile.content self.files.append(f)
assert err.message == "Fingertable not exist for the current node", "Test_Find_Pred - message content mismatch" print "Test case passed 16 -- ", err.message try: filestore.getNodeSucc() raise AssertionError( "Test_Get_Node_Succ - No Exception on empty finger table") except SystemException as err: assert err.message == "Fingertable not exist for the current node", "Test_Node_Succ - message content mismatch" print "Test case passed 17 -- ", err.message if __name__ == '__main__': rf_metadata = RFileMetadata() rf_metadata.filename = "file_name" rf_metadata.version = 0 rf_metadata.owner = "owner_name" rf_metadata.contentHash = sha256(rf_metadata.owner + ":" + rf_metadata.filename).hexdigest() rfile = RFile() rfile.meta = rf_metadata rfile.content = "This is my first apache thrift programming experience" try: t = TestChord() t.test_write_file(rfile) t.test_read_file(rf_metadata.filename, rf_metadata.owner) #t.test_negative_cases(make_socket('128.226.180.166', 9095), rfile.meta.contentHash) except Thrift.TException as tx: print('%s' % tx.message)
def testOverwrite(host, port_list): rand_idx = random.randint(0, len(port_list)-1) rand_port = port_list[rand_idx] # Make socket transport = TSocket.TSocket(host, rand_port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() meta_obj = RFileMetadata() meta_obj.filename = "book.txt" meta_obj.version = 0 meta_obj.owner = "Brad" meta_obj.contentHash = \ hashlib.sha256(("%s:%s" % (meta_obj.filename,\ meta_obj.owner)).encode("utf-8")).hexdigest() content_str = "Test String" file_obj = RFile() file_obj.meta = meta_obj file_obj.content = content_str succ = client.findSucc(file_obj.meta.contentHash) transport.close() # Make socket transport = TSocket.TSocket(host, succ.port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() client.writeFile(file_obj) # Make socket transport = TSocket.TSocket(host, succ.port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() file_obj.meta.version = file_obj.meta.version + 1 file_obj.content = "New Test String" client.writeFile(file_obj) try: rand_idx = random.randint(0, len(port_list)-1) rand_port = port_list[rand_idx] # Make socket transport = TSocket.TSocket(host, rand_port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() res = client.readFile("book.txt", "Brad") assert res.content == "New Test String" assert res.meta.version == 1 print("File Overwrite Successful") except SystemException: print("Success: Incorrect owner") transport.close()
def testReadAfterWriteError(host, port_list): rand_idx = random.randint(0, len(port_list)-1) rand_port = port_list[rand_idx] # Make socket transport = TSocket.TSocket(host, rand_port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() meta_obj = RFileMetadata() meta_obj.filename = "book.txt" meta_obj.version = 0 meta_obj.owner = "Brad" meta_obj.contentHash = \ hashlib.sha256(("%s:%s" % (meta_obj.filename,\ meta_obj.owner)).encode("utf-8")).hexdigest() content_str = "Knowledge Bitch!" file_obj = RFile() file_obj.meta = meta_obj file_obj.content = content_str succ = client.findSucc(file_obj.meta.contentHash) transport.close() # Make socket transport = TSocket.TSocket(host, succ.port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() client.writeFile(file_obj) transport.close() other_port = [port for port in port_list if port != succ.port][0] # Make socket transport = TSocket.TSocket(host, other_port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = FileStore.Client(protocol) # Connect! transport.open() try: res = client.readFile("book.txt", "Brad") print("Read after write error NOT succesful") except SystemException: print("Read after write error successful") transport.close()