public void RollbackTest() { FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); Assert.IsTrue(server.newTransaction(serV)); byte[][] bfiles = server.getFilesToUpload(); foreach (byte[] bf in bfiles) { FBFile f = FBFile.deserialize(bf); Assert.IsTrue(v.fileList.Contains(f)); } FBFile file = (FBFile) new FBFileBuilder(@"asd\uno.txt").generate(); string[] lines = { "First line", "Second line", "Third line" }; System.IO.File.WriteAllLines(@"asd\uno.txt", lines); FileStream fstream = new FileStream(@"asd\uno.txt", FileMode.Open, FileAccess.Read); //Assert.AreEqual(server.uploadFile(fstream), file.hash); //fstream.Close(); UploadData credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); file = (FBFile) new FBFileBuilder(@"asd\due.txt").generate(); string[] lines1 = { "First line", "Second line", "Third lines" }; System.IO.File.WriteAllLines(@"asd\due.txt", lines1); fstream = new FileStream(@"asd\due.txt", FileMode.Open, FileAccess.Read); //Assert.AreEqual(server.uploadFile(fstream), file.hash); //fstream.Close(); credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); Assert.IsTrue(server.rollback()); Assert.IsTrue(server.newTransaction(serV)); bfiles = server.getFilesToUpload(); foreach (byte[] bf in bfiles) { FBFile f = FBFile.deserialize(bf); Assert.IsTrue(v.fileList.Contains(f)); } }
private void TestInitialize() { server = new Server.Server(); AuthenticationData ad = server.authStep1("test1"); token = server.authStep2(ad.token, "test1", AuthenticationPrimitives.hashPassword("test1", ad.salt, ad.token)); CleanUp(); server = new Server.Server(); ad = server.authStep1("test1"); token = server.authStep2(ad.token, "test1", AuthenticationPrimitives.hashPassword("test1", ad.salt, ad.token)); string[] lines = { "First line", "Second line", "Third line" }; string[] lines1 = { "First line", "Second line", "Third lines" }; System.IO.Directory.CreateDirectory("asd"); System.IO.Directory.CreateDirectory(@"asd\ciao"); rinfo = new DirectoryInfo("asd"); System.IO.File.WriteAllLines(@"asd\uno.txt", lines); System.IO.File.WriteAllLines(@"asd\due.txt", lines1); System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); server.newTransaction(serV); byte[][] bfiles = server.getFilesToUpload(); FBFile file = (FBFile) new FBFileBuilder(@"asd\uno.txt").generate(); FileStream fstream = new FileStream(@"asd\uno.txt", FileMode.Open, FileAccess.Read); UploadData credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); fstream.Close(); file = (FBFile) new FBFileBuilder(@"asd\due.txt").generate(); fstream = new FileStream(@"asd\due.txt", FileMode.Open, FileAccess.Read); credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); Assert.IsTrue(server.commit()); }
public void RevertWithNewFile() { this.TestInitialize(); string[] lines = { "First line", "Second line", "Third line", "Fourth line" }; System.IO.File.WriteAllLines(@"asd\ciao\tre.txt", lines); FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); server.newTransaction(serV); FileStream fstream = new FileStream(@"asd\ciao\tre.txt", FileMode.Open, FileAccess.Read); server.getFilesToUpload(); UploadData credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); server.commit(); System.IO.File.Delete(@"asd\ciao\tre.txt"); vb = new FBVersionBuilder(rinfo.FullName); v = (FolderBackup.Shared.FBVersion)vb.generate(); serV = new SerializedVersion(v.serialize()); server.newTransaction(serV); server.commit(); UploadData ud = server.resetToPreviousVersion(1); UsefullMethods.ReceiveFile(ud.ip, ud.port, ud.token, @"asd\asd.zip"); String ignore; List <Instruction> instructionList = UsefullMethods.ExtractInstructions(@"asd\asd.zip", out ignore); Assert.IsTrue(instructionList.Count == 1); Assert.IsTrue(instructionList[0].cmd == InstructionType.NEW); Assert.IsTrue(instructionList[0].dst == @"ciao\tre.txt"); }
private void sync() { String dirPath = conf.targetPath.get(); if (dirPath == null || !Directory.Exists(dirPath)) { throw new DirectoryNotFoundException("Directory in configuration is not valid"); } try { cv = (FBVersion)vb.generate(); }catch (Exception e) { MessageBox.Show(e.Message + "\nThe application will be closed", "Unexpected error"); Environment.Exit(0); } SerializedVersion serV = new SerializedVersion(); serV.encodedVersion = cv.serialize(); threadCallback.Invoke(TypeThread.SYNC, StatusCode.WORKING, "Start syncing"); FileStream fs = null; try { if (server.newTransaction(serV)) { this.status = "Syncing"; byte[][] bfiles = server.getFilesToUpload(); List <FBFile> fileToSync = new List <FBFile>(); foreach (byte[] bf in bfiles) { fileToSync.Add(FBFile.deserialize(bf)); } int i = 0; foreach (FBFile f in fileToSync) { if (!this.stopSync) { threadCallback.Invoke(TypeThread.SYNC, StatusCode.WORKING, "Syncing file " + i + " of " + fileToSync.Count); i++; FBFileClient cf = FBFileClient.generate(f); UploadData cedential = server.uploadFile(); UsefullMethods.SendFile(cedential.ip, cedential.port, cedential.token, new FileStream(cf.FullName, FileMode.Open)); } else { break; } } if (!this.stopSync) { server.commit(); threadCallback.Invoke(TypeThread.SYNC, StatusCode.SUCCESS, "Sync completed"); } else { threadCallback.Invoke(TypeThread.SYNC, StatusCode.ABORTED, "Syncing Stopped"); server.rollback(); } this.status = "Idle"; } else { threadCallback.Invoke(TypeThread.SYNC, StatusCode.IDLE, "Nothing to be done"); } } catch (System.ServiceModel.CommunicationException e) { MessageBox.Show("There is a problem with connection, please retry to login!", "Error in connection"); threadCallback.Invoke(TypeThread.SYNC, StatusCode.ABORTED, "Connection error"); } catch { server.rollback(); } }