def send_image_file(middlebox_module, testing_part_1): """ Verifies that sending an image (as opposed to text) file works correctly. """ middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) # Send a file from client 1 to client 2. filename = "sample.jpg" client1.send_file(filename, client2_address) # Make sure that the files have the same contents. with open(filename, "r") as input_file: input_data = input_file.read() output_file_name = "{}-{}".format("client2", filename) with open(output_file_name, "r") as output_file: result_data = output_file.read() # Removing the output file just created os.remove(output_file_name) if input_data != result_data: raise Exception("send_image_file failed, because the file received" + "did not match the file sent.")
def send_multiple_files(middlebox_module, testing_part_1): total_count = 0 sent_files = 4 middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize clients 1 and 2, which are connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) client2_address = "1.2.3.5" client2 = client.EndHost("client2", client2_address, middlebox1) # Initialize clients 3 and 4, which are connected to middlebox 2. client3_address = "5.6.7.8" client3 = client.EndHost("client3", client3_address, middlebox2) client4_address = "5.6.7.9" client4 = client.EndHost("client4", client4_address, middlebox2) filename = "sample.txt" with open(filename, "rb") as input_file: input_data = input_file.read() # Send the sample file from client 1 to both clients 3 and 4. client1.send_file(filename, client3_address) client1.send_file(filename, client4_address) # Make sure that the files have the same contents. for receiver in ["client3", "client4"]: output_file_name = "{}-{}".format(receiver, filename) with open(output_file_name, "rb") as output_file: result_data = output_file.read() # Remove the output file just created. os.remove(output_file_name) if input_data == result_data: total_count += 1 # Send a file from client 2 to clients 3 and 4. client2.send_file(filename, client3_address) client2.send_file(filename, client4_address) # Make sure that the files have the same contents. for receiver in ["client3", "client4"]: output_file_name = "{}-{}".format(receiver, filename) with open(output_file_name, "rb") as output_file: result_data = output_file.read() # Removing the output file just created os.remove(output_file_name) if input_data == result_data: total_count += 1 if total_count != sent_files: raise Exception( "send_mutiple_files failed, because the all files" + "received did not match the file sent. Files received correctly:" + " {} and files sent are: {}\n".format(total_count, sent_files))
def data_reduction_same_files_small(middlebox_module, testing_part_1): """ Tests that the WAN optimizer reduces data sent over the WAN. This test has the same functionality as data_reduction_same_files, but operates on smaller files. This test sends the same file twice, and then checks that the reduction ratio: (bytes sent from client - bytes sent over wan) / bytes sent from client is as expected. The reduction ratios in the test are hardcoded based on a reference solution. """ if testing_part_1: expected_value = 0.49 else: expected_value = 0.49 middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) # Send a file twice from client 1 to client 2. filename = "sample_short.txt" output_file_name = "{}-{}".format("client2", filename) client1.send_file(filename, client2_address) # Removing the file just created os.remove(output_file_name) client1.send_file(filename, client2_address) # Removing the file just created os.remove(output_file_name) # Compute the data reduction ratio with open(filename, "rb") as input_file: input_data = input_file.read() extra_data_length = len(filename) + len(client.FILENAME_DELIMITER) bytes_in_sent_file = len(input_data) + extra_data_length bytes_sent = wide_area_network.get_total_bytes_sent() # print bytes_sent, bytes_in_sent_file, bytes_in_received_file reduction = (float(bytes_in_sent_file * 2 - bytes_sent) / float(bytes_in_sent_file * 2)) if (reduction < expected_value): raise Exception("data_reduction_same_files failed," + " because reduction ratio should be greater than " + " {}, was {}.".format(expected_value, reduction))
def send_just_over_one_block(middlebox_module, testing_part_1): """ Sends a file that contains a bit over on block. Verifies that when you send the last packet of this file you send out the packetized block and then send out the remaining bytes in the buffer. To demonstrate, we must make sure that if you sent out the 8000 bytes below, you do not send a fin on the packet with bytes 7500 to 8000 but you do send one for the byte with the last 500 bytes of the file. |____|____|____|____|____|__| |__| 1500 3000 4500 6000 7500 8000 500 """ middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) # Send a file from client 1 to client 2. if testing_part_1: filename = "8500a.txt" else: filename = "just_over_block_pt_2.txt" client1.send_file(filename, client2_address) # Make sure that the files have the same contents. with open(filename, "rb") as input_file: input_data = input_file.read() output_file_name = "{}-{}".format("client2", filename) with open(output_file_name, "rb") as output_file: result_data = output_file.read() # Remove the output file just created. os.remove(output_file_name) if input_data != result_data: raise Exception( ("The file received did not match the file sent. File sent (size {}):\n{}\nFile " + "received (size {}):\n{}\n").format(len(input_data), input_data, len(result_data), result_data))
def send_fin_overload_buffer(middlebox_module, testing_part_1): """ Sends a single large file and verifies that it's received correctly. This test only verifies that the correct data is received, and does not check the optimizer's data compression. """ middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) # Create a file to send from client 1 to client 2. filename = "fin-overload-input.txt" f = open(filename, "w") f.write("a" * 8500) f.close() # Send file from client 1 to client 2. client1.send_file(filename, client2_address) client1.send_file(filename, client2_address) # Make sure that the files have the same contents. with open(filename, "r") as input_file: input_data = input_file.read() os.remove(filename) output_file_name = "{}-{}".format("client2", filename) with open(output_file_name, "r") as output_file: result_data = output_file.read() # Remove the output file just created. os.remove(output_file_name) if input_data != result_data: raise Exception( "The file received did not match the file sent. File received had: " + "{}\n and file sent had: {}\n".format(result_data, input_data))
def cache_is_not_flow_specific(middlebox_module, testing_part_1): """ Checks that a given block appears in the cache at most once. First, client 1 sends a file to client 2 and client 3. Then both client 2 and client 3 send the file back to client 1. If your cache is flow-specific, you will end up with duplicate blocks. """ middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Iniitialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client A, connected to middlebox 2. client2_address = "5.5.5.5" client2 = client.EndHost("client2", client2_address, middlebox2) # Initialize client B, connected to middlebox 2. client3_address = "6.6.6.6" client3 = client.EndHost("client3", client3_address, middlebox2) filename = "8000B.txt" client1.send_file(filename, client2_address) client1.send_file(filename, client3_address) if len(set(middlebox1.buffers.values())) > len( middlebox1.buffers.values()): raise Exception("SRC middlebox has duplicate cache entries: %s" % middlebox1.cache) client2.send_file(filename, client1_address) client3.send_file(filename, client1_address) if len(set(middlebox2.buffers.values())) > len( middlebox2.buffers.values()): raise Exception("DST middlebox has duplicate cache entries: %s" % middlebox2.cache)
def send_empty_file(middlebox_module, testing_part_1): middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) # Send a file from client 1 to client 2. filename = "send_small_files_1.txt" client1.send_file(filename, client2_address) # Make sure that the files have the same contents. with open(filename, "rb") as input_file: input_data = input_file.read() output_file_name = "{}-{}".format("client2", filename) with open(output_file_name, "rb") as output_file: result_data = output_file.read() # Remove the output file just created. os.remove(output_file_name) if input_data != result_data: raise Exception(( "The file received did not match the file sent. File sent (size {}):\n{}\nFile " + "received (size {}):\n{}\n").format(len(input_data), input_data, len(result_data), result_data)) # Make sure no extra bytes are sent. expected_bytes = compute_bytes_in_file(filename) sent_bytes = wide_area_network.get_total_bytes_sent() if expected_bytes != sent_bytes: raise Exception( "An incorrect number of bytes were sent over the WAN. Expected {} bytes, but got {} bytes." .format(expected_bytes, sent_bytes))
def hash_partial_blocks(middlebox_module, testing_part_1): expected_value_1 = 0.995 #For both parts. expected_value_2 = 0.9985 #For both parts. middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) # Define names of files. filenames = ["hash_partial_sample_1.txt", "hash_partial_sample_2.txt"] # Send both files back and forth. for file_id in range(len(filenames)): filename = filenames[file_id] for count in range(2): client1.send_file(filename, client2_address) output_file_name = "{}-{}".format("client2", filename) if not check_file_integrity(filename, output_file_name): return os.remove(output_file_name) if count == 0: # This is a hack! Don't do this! wide_area_network._Wan__total_bytes_sent = 0 reduction_rate = compute_reduction(filename, wide_area_network) expected_value = expected_value_1 if file_id == 0 else expected_value_2 if (reduction_rate < expected_value): raise Exception( "Reduction ratio should be greater than " + " {}, was {}.".format(expected_value, reduction_rate))
def send_50_random_files(middlebox_module, testing_part_1): """ Generates 50 random files that have between 6000-32000 bytes of text. For each file, between 1 and 6 times (random), chooses a random pair of hosts on opposite sides of the WAN, and sends the random file between them. Makes sure that all files are sent properly. """ random.seed("168isthebesttho") NUM_FILES = 50 total_count = 0 send_count = 0 middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) middleboxes = [middlebox1, middlebox2] # Base names for clients on either side of the WAN client_address_bases = ["1.2.3.", "9.8.7."] # Initialize and connect all clients: mb1_clients = [] mb1_client_addr = [] mb2_clients = [] mb2_client_addr = [] for addr_base, middlebox in zip(client_address_bases, middleboxes): for i in range(0, 8): client_address = addr_base + str(i) if middlebox == middlebox1: client_i = client.EndHost(client_address, client_address, middlebox1) mb1_clients.append(client_i) mb1_client_addr.append(client_address) else: client_i = client.EndHost(client_address, client_address, middlebox2) mb2_clients.append(client_i) mb2_client_addr.append(client_address) filename = "random.txt" for i in range(NUM_FILES): generate_random_file(filename) with open(filename, "rb") as input_file: input_data = input_file.read() input_file.close() for num_send in range(0, random.randint(1, 6)): clientA_index = random.randint(0, 7) clientB_index = random.randint(0, 7) client_pair = [ mb1_clients[clientA_index], mb2_clients[clientB_index] ] client_addr_pair = [ mb1_client_addr[clientA_index], mb2_client_addr[clientB_index] ] # zipped_pair = list(zip(client_pair, client_addr_pair)) zipped_pair = [(client_pair[i], client_addr_pair[i]) for i in range(2)] random.shuffle(zipped_pair) sender = zipped_pair[0][0] senderAddr = zipped_pair[0][1] receiver = zipped_pair[1][0] receiverAddr = zipped_pair[1][1] sender.send_file(filename, receiverAddr) # Make sure that the files have the same contents. output_file_name = "{}-{}".format(receiverAddr, filename) with open(output_file_name, "rb") as output_file: result_data = output_file.read() output_file.close() # Remove the output file just created. os.remove(output_file_name) send_count += 1 if input_data == result_data: total_count += 1 if total_count != send_count: raise Exception( "send_mutiple_files failed, because the all files" + "received did not match the file sent. Files received correctly:" + " {} and files sent are: {}\n".format(total_count, NUM_FILES)) ### IMPLEMENTATION-SPECIFIC ### # You should change the variable names here to match the class variables # that cache the hash blocks' keys! if middlebox1.buffers != middlebox2.buffers: raise Exception( "The WAN Optimizers don't have the same state at the end!")
def data_reduction_suffixed_files_2(middlebox_module, testing_part_1): """ Tests that the WAN optimizer reduces data sent over the WAN. This test sends a file and then sends the same file with extra data appended at the end. Both tests have a filename of the same length, so that all of the data sent will be the same, until the suffix at the end. For both types of WAN optimizer, this should result in the first blocks being the same when the file is sent again (so there should be significant compression). The test checks that the reduction ratio: (bytes sent from client - bytes sent over wan) / bytes sent from client is as expected. The reduction ratios in the test are hardcoded based on a reference solution. """ if testing_part_1: expected_value = 0 else: expected_value = 0.49 # if testing_part_1: # expected_value = 0.40 # else: # expected_value = 0.63 middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) client3_address = "5.6.7.9" client3 = client.EndHost("client3", client3_address, middlebox2) # filename = ["good_text_with_limiter.txt", "good_text_with_limiter.txt"] filename = ["suffix_sample3_var_1.txt", "suffix_sample3_var_1.txt"] # Send a file from client 1 to client 2. client1.send_file(filename[0], client2_address) output_file_name = "{}-{}".format("client2", filename[0]) # Removing the output file just created os.remove(output_file_name) # Send a file prefixed with some data in the beginning # of the same file client2.send_file(filename[1], client1_address) output_file_name = "{}-{}".format("client1", filename[1]) # Removing the output file just created os.remove(output_file_name) bytes_in_sent_files = 0 for f in filename: with open(f, "rb") as input_file: input_data = input_file.read() extra_data_length = len(f) + len(client.FILENAME_DELIMITER) bytes_in_sent_files += len(input_data) + extra_data_length bytes_sent = wide_area_network.get_total_bytes_sent() # print bytes_sent, bytes_in_sent_files reduction = (float(bytes_in_sent_files - bytes_sent) / float(bytes_in_sent_files)) print(reduction) if (reduction < expected_value): raise Exception("data_reduction_suffixed_files failed," + " because reduciton ratio should be less than " + " {}, was {}.".format(expected_value, reduction))
def one_sender_multiple_sends(middlebox_module, testing_part_1): total_count = 0 sent_files = 2 middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize clients 1 which is connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize clients 2 which is connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) filename = "sample.txt" with open(filename, "rb") as input_file: input_data = input_file.read() # Send the sample file from client 1 to both clients 3 and 4. client1.send_file(filename, client2_address) # Make sure that the files have the same contents. receiver = "client2" output_file_name = "{}-{}".format(receiver, filename) with open(output_file_name, "rb") as output_file: result_data = output_file.read() # Remove the output file just created. os.remove(output_file_name) if input_data == result_data: total_count += 1 # Send same file to client 2 again. client1.send_file(filename, client2_address) # Make sure that the files have the same contents. receiver = "client2" output_file_name = "{}-{}".format(receiver, filename) with open(output_file_name, "rb") as output_file: result_data = output_file.read() # Removing the output file just created os.remove(output_file_name) if input_data == result_data: total_count += 1 if total_count != sent_files: raise Exception( "send_mutiple_files failed, because the all files" + "received did not match the file sent. Files received correctly:" + " {} and files sent are: {}\n".format(total_count, sent_files)) # Compute the data reduction ratio expected_value = 0.49 with open(filename, "rb") as input_file: input_data = input_file.read() extra_data_length = len(filename) + len(client.FILENAME_DELIMITER) bytes_in_sent_file = len(input_data) + extra_data_length bytes_sent = wide_area_network.get_total_bytes_sent() reduction = (float(bytes_in_sent_file * 2 - bytes_sent) / float(bytes_in_sent_file * 2)) if (reduction < expected_value): raise Exception("data_reduction_same_files failed," + " because reduction ratio should be greater than " + " {}, was {}.".format(expected_value, reduction))
def cross_sending(middlebox_module, testing_part_1): """ Tests that a large file without a delimeter will be sent correctly Only works for a a """ filename = "Diamond_top.txt" block1 = "a" * 8000 block2 = "b" * 8000 block3 = "c" * 8000 expected_value = .88 if testing_part_1: delimeter = "" combo1 = [block1[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block2, block3] combo2 = [block1[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block3, block2] combo3 = [block2[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block1, block3] combo4 = [block2[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block3, block1] combo5 = [block3[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block1, block2] combo6 = [block3[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block2, block1] else: delimeter = " straight chin suggestive of resolution pushed t" combo1 = [delimeter, block1, block2, block3] combo2 = [delimeter, block1, block3, block2] combo3 = [delimeter, block2, block1, block3] combo4 = [delimeter, block2, block3, block1] combo5 = [delimeter, block3, block1, block2] combo6 = [delimeter, block3, block2, block1] combos = [combo1, combo2, combo3, combo4, combo5, combo6] filename = "Diamond_top.txt" files = [] for combo in combos: files.append(delimeter.join(combo)) middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize clients connected to middlebox 1. client1_address = "1.2.3.4" client2_address = "1.3.4.2" client3_address = "1.4.2.3" client1 = client.EndHost("client1", client1_address, middlebox1) client2 = client.EndHost("client2", client2_address, middlebox1) client3 = client.EndHost("client3", client3_address, middlebox1) # Initialize clients connected to middlebox 2. client4_address = "5.6.7.8" client5_address = "5.8.7.6" client6_address = "5.7.6.8" client4 = client.EndHost("client4", client4_address, middlebox2) client5 = client.EndHost("client5", client5_address, middlebox2) client6 = client.EndHost("client6", client6_address, middlebox2) bytes_in_sent_files = 0 for data in files: f = open(filename, 'w') f.write(data) f.close() past2 = wide_area_network.get_total_bytes_sent() client1.send_file(filename, client4_address) client5.send_file(filename, client2_address) client3.send_file(filename, client6_address) output_file_name = "{}-{}".format("client2", filename) # Removing the output file just created os.remove(output_file_name) output_file_name = "{}-{}".format("client4", filename) # Removing the output file just created os.remove(output_file_name) output_file_name = "{}-{}".format("client6", filename) # Removing the output file just created os.remove(output_file_name) past = bytes_in_sent_files bytes_in_sent_files += (len(data) + len(filename) + len(client.FILENAME_DELIMITER)) * 3 bytes_sent = wide_area_network.get_total_bytes_sent() reduction = (float(bytes_in_sent_files - bytes_sent) / float(bytes_in_sent_files)) if (reduction < expected_value): raise Exception("data_reduction_random_edit_file failed," + " because reduciton ratio should be greater than " + " {}, was {}.".format(expected_value, reduction))
def data_reduction_with_jumbled_files(middlebox_module, testing_part_1): """ Tests whether sending files with the same blocks in different orders results in proper data compression. """ filename = "Diamond_top.txt" block1 = "a" * 8000 block2 = "b" * 8000 block3 = "c" * 8000 expected_value = .65 if testing_part_1: delimeter = "" combo1 = [block1[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block2, block3] combo2 = [block1[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block3, block2] combo3 = [block2[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block1, block3] combo4 = [block2[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block3, block1] combo5 = [block3[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block1, block2] combo6 = [block3[:8000 - len(filename) - len(client.FILENAME_DELIMITER)], block2, block1] else: delimeter = " straight chin suggestive of resolution pushed t" combo1 = [delimeter, block1, block2, block3] combo2 = [delimeter, block1, block3, block2] combo3 = [delimeter, block2, block1, block3] combo4 = [delimeter, block2, block3, block1] combo5 = [delimeter, block3, block1, block2] combo6 = [delimeter, block3, block2, block1] combos = [combo1, combo2, combo3, combo4, combo5, combo6] filename = "Diamond_top.txt" files = [] for combo in combos: files.append(delimeter.join(combo)) middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) bytes_in_sent_files = 0 for data in files: f = open(filename, 'w') f.write(data) f.close() past2 = wide_area_network.get_total_bytes_sent() client1.send_file(filename, client2_address) output_file_name = "{}-{}".format("client2", filename) # Removing the output file just created os.remove(output_file_name) past = bytes_in_sent_files bytes_in_sent_files += len(data) + len(filename) + len(client.FILENAME_DELIMITER) bytes_sent = wide_area_network.get_total_bytes_sent() reduction = (float(bytes_in_sent_files - bytes_sent) / float(bytes_in_sent_files)) if (reduction < expected_value): raise Exception("data_reduction_random_edit_file failed," + " because reduciton ratio should be greater than " + " {}, was {}.".format(expected_value, reduction))
def data_reduction_prefixed_files(middlebox_module, testing_part_1): """ Tests that the WAN optimizer reduces data sent over the WAN. This test sends a file and then sends the same file with extra data at the beginning. Because the data is offset, this will result in no blocks being the same for the part 1 middlebox. However, the part 2 middlebox should be able to handle this, and still significantly reduce data sent over the WAN. The test checks that the reduction ratio: (bytes sent from client - bytes sent over wan) / bytes sent from client is as expected. The reduction ratios in the test are hardcoded based on a reference solution. """ if testing_part_1: expected_value = 0.0 else: expected_value = 0.45 middlebox1 = middlebox_module.WanOptimizer() middlebox2 = middlebox_module.WanOptimizer() wide_area_network = wan.Wan(middlebox1, middlebox2) # Initialize client connected to middlebox 1. client1_address = "1.2.3.4" client1 = client.EndHost("client1", client1_address, middlebox1) # Initialize client connected to middlebox 2. client2_address = "5.6.7.8" client2 = client.EndHost("client2", client2_address, middlebox2) filename = ["sample.txt", "prefix_sample.txt"] # Send a file from client 1 to client 2. client1.send_file(filename[0], client2_address) output_file_name = "{}-{}".format("client2", filename[0]) # Removing the output file just created os.remove(output_file_name) # Send a file prefixed with some data in the beginning # of the same file client1.send_file(filename[1], client2_address) output_file_name = "{}-{}".format("client2", filename[1]) # Removing the output file just created os.remove(output_file_name) bytes_in_sent_files = 0 for f in filename: with open(f, "r") as input_file: input_data = input_file.read() extra_data_length = len(f) + len(client.FILENAME_DELIMITER) bytes_in_sent_files += len(input_data) + extra_data_length bytes_sent = wide_area_network.get_total_bytes_sent() # print bytes_sent, bytes_in_sent_file, bytes_in_received_file reduction = (float(bytes_in_sent_files - bytes_sent) / float(bytes_in_sent_files)) if (reduction < expected_value): raise Exception("data_reduction_prefixed_files failed," + " because reduction ratio should be greater than " + " {}, was {}.".format(expected_value, reduction))