Esempio n. 1
0
        public void BasicSyncLocalToOneDrive()
        {
            if (!GlobalTestSettings.RunNetworkTests)
            {
                Assert.Inconclusive(GlobalTestSettings.NetworkTestsDisabledMessage);
            }

            string testRootPath = Path.Combine(this.TestContext.TestLogsDir, this.TestContext.TestName);

            Directory.CreateDirectory(testRootPath);

            string syncSourcePath = Path.Combine(testRootPath, "Source");

            Directory.CreateDirectory(syncSourcePath);

            // Create temp files/folders
            List <string> syncFileList = new List <string>
            {
                TestHelper.CreateDirectory(syncSourcePath, "dir1"),
                TestHelper.CreateFile(syncSourcePath, "dir1\\file1.txt"),
                TestHelper.CreateFile(syncSourcePath, "dir1\\file2.txt"),
                TestHelper.CreateFile(syncSourcePath, "dir1\\file3.txt"),
                TestHelper.CreateDirectory(syncSourcePath, "dir2"),
                TestHelper.CreateFile(syncSourcePath, "dir2\\file1.txt"),
                TestHelper.CreateFile(syncSourcePath, "dir2\\file2.txt"),
                TestHelper.CreateFile(syncSourcePath, "dir2\\file3.txt")
            };

            TokenResponse currentToken = GetCurrentToken();

            Guid remoteTestFolderName = Guid.NewGuid();
            Item remoteTestFolder     = CreateOneDriveTestDirectory(currentToken, remoteTestFolderName.ToString("D")).Result;

            SyncRelationship newRelationship = SetupRelationship(testRootPath, syncSourcePath, remoteTestFolder);

            AnalyzeJob analyzeJob = new AnalyzeJob(newRelationship);

            analyzeJob.ContinuationJob = new SyncJob(newRelationship, analyzeJob.AnalyzeResult)
            {
                TriggerType = SyncTriggerType.Manual
            };

            analyzeJob.Start();

            SyncJob syncJob = (SyncJob)analyzeJob.WaitForCompletion();

            Assert.IsTrue(syncJob.HasFinished);

            Assert.AreEqual(syncFileList.Count, syncJob.AnalyzeResult.AdapterResults.SelectMany(r => r.Value.EntryResults).Count());
            OneDriveAdapter oneDriveAdapter =
                newRelationship.Adapters.First(a => !a.Configuration.IsOriginator) as OneDriveAdapter;

            OneDriveClient client = new OneDriveClient(GetCurrentToken());

            foreach (string syncFile in syncFileList.Where(f => f.EndsWith(".txt")))
            {
                string localPath = Path.Combine(syncSourcePath, syncFile);

                using (var sha1 = new SHA1Managed())
                {
                    byte[] content       = File.ReadAllBytes(localPath);
                    byte[] localFileHash = sha1.ComputeHash(content);

                    EntryUpdateInfo entryResult;
                    using (var db = newRelationship.GetDatabase())
                    {
                        entryResult = syncJob.AnalyzeResult.AdapterResults.SelectMany(r => r.Value.EntryResults).FirstOrDefault(
                            r => r.Entry.GetRelativePath(db, "\\") == syncFile);
                    }

                    Assert.IsNotNull(entryResult);

                    byte[] databaseHash = entryResult.Entry.OriginalSha1Hash;

                    Assert.AreEqual(
                        TestHelper.HashToHex(localFileHash),
                        TestHelper.HashToHex(databaseHash),
                        "Local file hash does not match database hash.");

                    Pre.Assert(oneDriveAdapter != null, "oneDriveAdapter != null");
                    var adapterEntry =
                        entryResult.Entry.AdapterEntries.First(e => e.AdapterId == oneDriveAdapter.Configuration.Id);
                    string itemId       = adapterEntry.AdapterEntryId;
                    var    item         = client.GetItemByItemIdAsync(itemId);
                    var    oneDriveHash = "0x" + item.Result.File.Hashes.Sha1Hash;

                    Assert.AreEqual(
                        TestHelper.HashToHex(localFileHash),
                        oneDriveHash,
                        "Local file hash does not match OneDrive hash.");
                }
            }
        }