Ejemplo n.º 1
0
 /// <summary>
 /// Returns the alternative file names, which looks like "a1000.bin".
 /// If the restore is on a disk, then the full path is returned. If
 /// the restore is in the cloud, only the file name is returned.
 /// </summary>
 string[] get_alt_file_names()
 {
     if (cloud_backup == null)
     {
         return(Directory.GetFiles(backup_destination_base));
     }
     else
     {
         return(cloud_backup.list_objects(backup_destination_base, null).ToArray());
     }
 }
Ejemplo n.º 2
0
        /// <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.");
        }