public Restore(XElement xml, List <CloudBackupService> cloud_backup_services, DecryptStream decrypt_stream, KeyManager key_manager, ReportEvent_Callback report_event = null) { this.decrypt_stream = decrypt_stream; this.key_manager = key_manager; this.report_event = report_event; // <destination_name> backup_destination_name = xml.Element("destination_name").Value; if (backup_destination_name.Equals("disk") == false) { // search for the destination in cloud_backup_services foreach (var cloud_backup in cloud_backup_services) { if (backup_destination_name.Equals(cloud_backup.Name)) { this.cloud_backup = cloud_backup; break; } } if (cloud_backup == null) { throw new Exception("XML file error. While processing <RestoreManager> " + "there is a <restore> with destination_name \"" + backup_destination_name + "\" that refers to an unknown cloud backup service."); } } // <destination_path>, <file_prefixes> backup_destination_base = xml.Element("destination_path").Value; file_prefixes = xml.Element("file_prefixes").Value.Trim().Split(' '); }
/// <summary> /// Tests EncryptStream and DecryptStream using cloud storage. A single /// file is uploaded in encrypted form and then downloaded. /// </summary> public void test_Encrypt_and_Decrypt_streams_on_cloud() { Console.WriteLine("About to upload a file to the cloud and download it back, " + "using EncryptStream and DecryptStream. Press any key to continue, or ESC to skip.\n"); if (hit_esc_key()) { return; } var backup_services = create_cloud_backup_services_from_xml(cloud_backup_services_xml); // Choose one of the "backup_services" for use in testing. CloudBackupService backup_service = backup_services[2]; string bucket_name = "xxxxxxxx"; string original_file = @"E:\temp\temp\temp.bin"; string base_path = @"E:\temp\temp"; string relative_path = original_file.Substring(base_path.Length + 1); string decrypted_file = @"E:\temp\temp2\temp.bin"; var encrypt_stream = new EncryptStream(); var decrypt_stream = new DecryptStream(); Random random = new Random(); byte[] buffer = new byte[1024]; // create random file using (var fs = new FileStream(original_file, FileMode.Create, FileAccess.Write)) { for (int j = 0; j < 640; j++) { random.NextBytes(buffer); fs.Write(buffer, 0, buffer.Length); } } // encrypted upload Console.WriteLine("Encrypting and uploading file to the cloud."); encrypt_stream.reset(original_file, relative_path, key); backup_service.upload(encrypt_stream, bucket_name, "temp.bin"); Console.WriteLine("The file has been encrypted and uploaded to the cloud."); Thread.Sleep(2000); // download and decrypt decrypt_stream.reset(key, full_path_request_handler); // be sure the download can be done in pieces backup_service.download(bucket_name, "temp.bin", decrypt_stream, 0, 1024); Console.WriteLine("The first 1kB header says the pre-encrypt file size is " + file_pre_encrypt_size + " bytes."); backup_service.download(bucket_name, "temp.bin", decrypt_stream, 1024, file_pre_encrypt_size); decrypt_stream.Flush(); backup_service.delete(bucket_name, "temp.bin"); Debug.Assert(compare_files(original_file, decrypted_file)); Console.WriteLine("EncryptStream and DecryptStream has been tested using cloud storage.\n"); }
/// <summary> /// Tests the CloudBackupService classes. The content of a storage account /// is listed. A file is uploaded, downloaded, and finally deleted. /// </summary> public void test_CloudBackupService() { Console.WriteLine("About to start a test using CloudBackupService." + " Press any key to continue, or ESC to skip.\n"); if (hit_esc_key()) { return; } var backup_services = create_cloud_backup_services_from_xml(cloud_backup_services_xml); // Choose one of the "backup_services" for use in testing. CloudBackupService backup_service = backup_services[1]; string bucket_name = "xxxxxxxx"; // Choose bucket name Console.WriteLine("Testing using " + backup_service.Name + "\\" + bucket_name); Console.WriteLine("Listing objects:"); foreach (var obj in backup_service.list_objects(bucket_name, null)) { Console.WriteLine(new String(' ', 4) + obj); } Console.WriteLine("Manually confirm that this is correct."); pause(); // generate files for testing string longer_file_path = @"E:\temp\temp2\temp.bin"; string shorter_file_path = @"E:\temp\temp2\temp_shorter.bin"; string download_file_path = @"E:\temp\temp2\temp_downloaded.bin"; string key_name = Path.GetFileName(longer_file_path); // generate shorter file var random = new Random(); byte[] buffer = new byte[1 * 1024]; random.NextBytes(buffer); using (FileStream fs_short = new FileStream(shorter_file_path, FileMode.Create, FileAccess.Write)) { fs_short.Write(buffer, 0, buffer.Length); } // generate longer file using (FileStream fs_long = new FileStream(longer_file_path, FileMode.Create, FileAccess.Write)) { fs_long.Write(buffer, 0, buffer.Length); for (int i = 0; i < 10; i++) { random.NextBytes(buffer); fs_long.Write(buffer, 0, buffer.Length); } } // upload file Console.WriteLine("Uploading file."); using (FileStream fs_long = new FileStream(longer_file_path, FileMode.Open, FileAccess.Read)) { backup_service.upload(fs_long, bucket_name, key_name); } // download partial and compare file Console.WriteLine("Downloading the start of the file and checking."); using (FileStream fs_downloaded = new FileStream(download_file_path, FileMode.Create, FileAccess.Write)) { backup_service.download(bucket_name, key_name, fs_downloaded, 0, 1 * 1024); } Debug.Assert(compare_files(shorter_file_path, download_file_path)); // download full file and compare file Console.WriteLine("Downloading the full file and checking."); using (FileStream fs_downloaded = new FileStream(download_file_path, FileMode.Create, FileAccess.Write)) { backup_service.download(bucket_name, key_name, fs_downloaded); } Debug.Assert(compare_files(longer_file_path, download_file_path)); // Test to see if downloading too many bytes cause errors? using (FileStream fs_downloaded = new FileStream(download_file_path, FileMode.Create, FileAccess.Write)) { backup_service.download(bucket_name, key_name, fs_downloaded, 0, 1024 * 1024); } Debug.Assert(compare_files(longer_file_path, download_file_path)); // delete file Console.WriteLine("Deleting the file and checking."); backup_service.delete(bucket_name, key_name); bool found = false; foreach (var obj in backup_service.list_objects(bucket_name, null)) { if (obj.Equals(key_name)) { found = true; break; } } Debug.Assert(found == false); Console.WriteLine("CloudBackupService test completed."); }
/// <summary> /// Comparison function for sorting list of "CloudBackupService". /// </summary> int compare_two_cloud_backups(CloudBackupService c1, CloudBackupService c2) { return(String.Compare(c1.Name, c2.Name)); }