Example #1
0
		/// <summary>
		/// Downloads the specified test file and verifies its contents.
		/// </summary>
		private async Task TestFileDownload(TestData.TestFile testFile)
		{
			var target = Path.GetTempFileName();

			try
			{
				Debug.WriteLine("Temporary local file: " + target);

				using (var feedback = new DebugFeedbackChannel("Test"))
				{
					using (var initializing = feedback.BeginSubOperation("InitializeData"))
						await TestData.Current.BringToInitialState(initializing);

					var client = new MegaClient(TestData.Current.Email1, TestData.Current.Password1);
					var filesystem = await client.GetFilesystemSnapshotAsync(feedback);

					var file = testFile.TryFind(filesystem);

					if (file == null)
						Assert.Fail("Could not find expected file to download: " + testFile.Name);

					await file.DownloadContentsAsync(target, feedback);

					using (var expectedContents = testFile.Open())
					using (var contents = File.OpenRead(target))
						TestHelper.AssertStreamsAreEqual(expectedContents, contents);
				}
			}
			finally
			{
				File.Delete(target);
			}
		}
Example #2
0
		public async Task FilesystemLoad_DoesNotCauseApocalypse()
		{
			using (var feedback = new DebugFeedbackChannel("Test"))
			{
				using (var initializing = feedback.BeginSubOperation("InitializeData"))
					await TestData.Current.BringToInitialState(initializing);

				var client = new MegaClient(TestData.Current.Email1, TestData.Current.Password1);
				await client.EnsureConnectedAsync(feedback);
				await client.GetFilesystemSnapshotAsync(feedback);
			}
		}
Example #3
0
		public async Task SendFileToContact_SeemsToWork()
		{
			using (var feedback = new DebugFeedbackChannel("Test"))
			{
				using (var initializing = feedback.BeginSubOperation("InitializeData"))
					await TestData.Current.BringToInitialState(initializing);

				using (var sending = feedback.BeginSubOperation("Sending"))
				{
					var client = new MegaClient(TestData.Current.Email1, TestData.Current.Password1);

					await client.AddContactAsync(TestData.Current.Email2, sending);
					var contactList = await client.GetContactListSnapshotAsync(sending);
					Assert.AreEqual(1, contactList.Count);
					var account2 = contactList.Single();

					var filesystem = await client.GetFilesystemSnapshotAsync(sending);

					var file = TestData.SmallFile.Find(filesystem);

					await file.SendToContactAsync(account2, sending);
				}

				using (var receiving = feedback.BeginSubOperation("Receiving"))
				{
					var client = new MegaClient(TestData.Current.Email2, TestData.Current.Password2);

					var filesystem = await client.GetFilesystemSnapshotAsync(receiving);

					var file = TestData.SmallFile.Find(filesystem);

					Assert.IsNotNull(file);

					var target = Path.GetTempFileName();

					// Verify contents.
					try
					{
						Debug.WriteLine("Temporary local file: " + target);

						await file.DownloadContentsAsync(target, feedback);

						using (var expectedContents = TestData.SmallFile.Open())
						using (var contents = File.OpenRead(target))
							TestHelper.AssertStreamsAreEqual(expectedContents, contents);
					}
					finally
					{
						File.Delete(target);
					}
				}
			}
		}
Example #4
0
		/// <summary>
		/// Uploads the specified test file and ensures that it looks like it really did get uploaded.
		/// </summary>
		private async Task TestFileUpload(TestData.TestFile testFile)
		{
			using (var feedback = new DebugFeedbackChannel("Test"))
			{
				var client = new MegaClient(TestData.Current.Email1, TestData.Current.Password1);
				var filesystem = await client.GetFilesystemSnapshotAsync(feedback);

				// Just stick it in the root, do not care about what already exists there.
				CloudItem file;
				using (var stream = testFile.Open())
					file = await filesystem.Files.NewFileAsync(testFile.Name, stream, feedback);

				Assert.AreEqual(testFile.Name, file.Name);
				Assert.AreEqual(testFile.Size, file.Size);
			}
		}
Example #5
0
		public async Task SendFolderToContact_SeemsToWork()
		{
			using (var feedback = new DebugFeedbackChannel("Test"))
			{
				using (var initializing = feedback.BeginSubOperation("InitializeData"))
					await TestData.Current.BringToInitialState(initializing);

				using (var sending = feedback.BeginSubOperation("Sending"))
				{
					var client = new MegaClient(TestData.Current.Email1, TestData.Current.Password1);

					await client.AddContactAsync(TestData.Current.Email2, sending);
					var contactList = await client.GetContactListSnapshotAsync(sending);
					Assert.AreEqual(1, contactList.Count);
					var account2 = contactList.Single();

					var filesystem = await client.GetFilesystemSnapshotAsync(sending);

					var folder = filesystem.Files.Children
						.Single(i => i.Type == ItemType.Folder && i.Name == "Folder1");

					await folder.SendToContactAsync(account2, sending);
				}

				using (var receiving = feedback.BeginSubOperation("Receiving"))
				{
					var client = new MegaClient(TestData.Current.Email2, TestData.Current.Password2);

					var filesystem = await client.GetFilesystemSnapshotAsync(receiving);

					var file = TestData.SmallFile.Find(filesystem);

					Assert.IsNotNull(file);
					Assert.IsNotNull(file.Parent);
					Assert.AreEqual("Folder1", file.Parent.Name);
				}
			}
		}
Example #6
0
		public async Task CancelingDownload_SeemsToWork()
		{
			var target = Path.GetTempFileName();

			try
			{
				Debug.WriteLine("Temporary local file: " + target);

				using (var feedback = new DebugFeedbackChannel("Test"))
				{
					using (var initializing = feedback.BeginSubOperation("InitializeData"))
						await TestData.Current.BringToInitialState(initializing);

					var client = new MegaClient(TestData.Current.Email1, TestData.Current.Password1);
					var filesystem = await client.GetFilesystemSnapshotAsync(feedback);

					var file = TestData.BigFile.TryFind(filesystem);

					if (file == null)
						Assert.Fail("Could not find the test file to download.");

					CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));

					await file.DownloadContentsAsync(target, feedback, cts.Token);
				}
			}
			finally
			{
				File.Delete(target);
			}
		}
Example #7
0
		public async Task TransferringFiles_OfVariousSizes_SeemsToWork()
		{
			var interestingSizes = new List<int>
			{
				0,
				1,
				8,
				15,
				16,
				255,
				256,
				1023,
				1024,
			};

			// Now also add all the chunk sizes and chunk +-1 sizes for a reasonable amount of chunks.
			var chunkSizes = Algorithms.MeasureChunks(8 * 1024 * 1024);

			foreach (var chunkSize in chunkSizes)
			{
				interestingSizes.Add(chunkSize - 16);
				interestingSizes.Add(chunkSize - 15);
				interestingSizes.Add(chunkSize - 1);
				interestingSizes.Add(chunkSize);
				interestingSizes.Add(chunkSize + 1);
				interestingSizes.Add(chunkSize + 15);
				interestingSizes.Add(chunkSize + 16);
			}

			using (var feedback = new DebugFeedbackChannel("Test"))
			{
				var client = new MegaClient(TestData.Current.Email1, TestData.Current.Password1);
				var filesystem = await client.GetFilesystemSnapshotAsync(feedback);

				foreach (var size in interestingSizes)
					await TestFileUploadAndDownload(size, filesystem, feedback);
			}
		}
Example #8
0
		/// <summary>
		/// Brings the two test accounts to the initial state.
		/// </summary>
		/// <remarks>
		/// Account 1 contents:
		/// Files
		/// - Folder1
		/// -- Folder2
		/// --- EmptyFile
		/// -- SmallFile
		/// - BigFile
		/// - MediumFile
		/// Contacts
		/// 
		/// Account 2 contents:
		/// Files
		/// Contacts
		/// - Account 1
		/// </remarks>
		public async Task BringToInitialState(IFeedbackChannel feedback)
		{
			var client1 = new MegaClient(Email1, Password1);
			var client2 = new MegaClient(Email2, Password2);

			var filesystem1 = await client1.GetFilesystemSnapshotAsync(feedback);

			// Got folders?
			var folder1 = filesystem1.Files.Children
				.FirstOrDefault(ci => ci.Type == ItemType.Folder && ci.Name == "Folder1");

			CloudItem folder2 = null;

			if (folder1 != null)
				folder2 = folder1.Children
					.FirstOrDefault(ci => ci.Type == ItemType.Folder && ci.Name == "Folder2");

			// Make folders, if needed.
			if (folder1 == null)
				folder1 = await filesystem1.Files.NewFolderAsync("Folder1", feedback);

			if (folder2 == null)
				folder2 = await folder1.NewFolderAsync("Folder2", feedback);

			// Got files?
			var bigFile = BigFile.TryFind(filesystem1);
			var mediumFile = MediumFile.TryFind(filesystem1);
			var smallFile = SmallFile.TryFind(filesystem1);
			var emptyFile = EmptyFile.TryFind(filesystem1);

			// Then upload the new files.
			if (emptyFile == null || emptyFile.Parent != folder2)
				using (var stream = OpenTestDataFile(EmptyFile.Name))
					emptyFile = await folder2.NewFileAsync(EmptyFile.Name, stream, feedback);

			if (smallFile == null || smallFile.Parent != folder1)
				using (var stream = OpenTestDataFile(SmallFile.Name))
					smallFile = await folder1.NewFileAsync(SmallFile.Name, stream, feedback);

			if (mediumFile == null || mediumFile.Parent != filesystem1.Files)
				using (var stream = OpenTestDataFile(MediumFile.Name))
					mediumFile = await filesystem1.Files.NewFileAsync(MediumFile.Name, stream, feedback);

			if (bigFile == null || bigFile.Parent != filesystem1.Files)
				using (var stream = OpenTestDataFile(BigFile.Name))
					bigFile = await filesystem1.Files.NewFileAsync(BigFile.Name, stream, feedback);

			// Delete all items that we do not care about for account 1.
			var goodItems = new[]
			{
				folder1.ID,
				folder2.ID,
				bigFile.ID,
				mediumFile.ID,
				smallFile.ID,
				emptyFile.ID
			};

			await DeleteUnwantedItems(filesystem1.Files, goodItems, feedback);
			await DeleteUnwantedItems(filesystem1.Trash, goodItems, feedback);

			// Delete all files and folders for account 2.
			var filesystem2 = await client2.GetFilesystemSnapshotAsync(feedback);

			foreach (var item in filesystem2.AllItems.Where(ci => ci.Type == ItemType.File || ci.Type == ItemType.Folder))
				await item.DeleteAsync(feedback);

			// Delete all contacts.
			foreach (var contact in await client1.GetContactListSnapshotAsync(feedback))
				await contact.RemoveAsync(feedback);

			foreach (var contact in await client2.GetContactListSnapshotAsync(feedback))
				await contact.RemoveAsync(feedback);

			// Add account 1 as contact to account 2.
			await client2.AddContactAsync(Email1, feedback);
		}