Exemplo n.º 1
0
        public void RevertWithFileCopy()
        {
            this.TestInitialize();
            string[] lines = { "First line", "Second line", "Third 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);

            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.COPY);
            Assert.IsTrue(instructionList[0].dst == @"ciao\tre.txt");
        }
        public void DetectEqualVersionTest()
        {
            doTransaction();
            server.commit();

            FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName);
            FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate();

            SerializedVersion serV = new SerializedVersion(v.serialize());

            Assert.IsFalse(server.newTransaction(serV));
        }
        public void GetFileFailTest()
        {
            doTransaction();
            server.commit();

            FBVersion vers = new FBVersion();
            FBFile file = (FBFile)(new FBFileBuilder(@"asd\due.txt")).generate();
            file.hash = "asdsdaasd";
            vers.addElement(file);
            SerializedVersion serV = new SerializedVersion(vers.serialize());
            var uploadData = server.getFile(serV);
            Assert.IsNull(uploadData);
        }
        public void GetFileTest()
        {
            doTransaction();
            server.commit();

            FBVersion vers = new FBVersion();
            FBFile file = (FBFile) (new FBFileBuilder(@"asd\due.txt")).generate();
            vers.addElement(file);
            SerializedVersion serV = new SerializedVersion(vers.serialize());
            var uploadData = server.getFile(serV);

            UsefullMethods.ReceiveFile(uploadData.ip, uploadData.port, uploadData.token, @"asd\test.txt");
            String content = File.ReadAllText(@"asd\test.txt");
            Assert.IsTrue(content.Contains("Third lines"));
        }
        public void PersistenceTest()
        {
            TransactionCommitTest();
            server = null;

            server = new Server.Server();
            AuthenticationData ad = server.authStep1("test1");
            token = server.authStep2(ad.token, "test1", AuthenticationPrimitives.hashPassword("test1", ad.salt, ad.token));

            FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName);
            FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate();

            SerializedVersion serV = new SerializedVersion();
            serV.encodedVersion = v.serialize();

            Assert.IsTrue(server.newTransaction(token, serV));
        }
        public void RollbackTest()
        {
            FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName);
            FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate();

            SerializedVersion serV = new SerializedVersion();
            serV.encodedVersion = v.serialize();

            Assert.IsTrue(server.newTransaction(token, serV));

            byte[][] bfiles = server.getFilesToUpload(token);
            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 = { token + "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();

            file = (FBFile)new FBFileBuilder(@"asd\due.txt").generate();
            string[] lines1 = { token + "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();

            Assert.IsTrue(server.rollback(token));

            Assert.IsTrue(server.newTransaction(token, serV));

            bfiles = server.getFilesToUpload(token);
            foreach (byte[] bf in bfiles)
            {
                FBFile f = FBFile.deserialize(bf);
                Assert.IsTrue(v.fileList.Contains(f));
            }
        }
Exemplo n.º 7
0
 public Boolean newTransaction(SerializedVersion newVersion)
 {
     return true;
 }
Exemplo n.º 8
0
        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());
        }
Exemplo n.º 9
0
        private void sync()
        {
            cv = (FBVersion)vb.generate();
            String dirPath = conf.targetPath.get();
            if (dirPath == null || !Directory.Exists(dirPath))
            {
                throw new DirectoryNotFoundException("Directory in configuration is not valid");
            }
            SerializedVersion serV = new SerializedVersion();
            serV.encodedVersion = cv.serialize();
            threadCallback.Invoke(TypeThread.SYNC, StatusCode.WORKING, "Start syncing");

            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;
                try
                {
                    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();

                    }
                }
                catch
                {
                    server.rollback();
                }

                this.status = "Idle";

            }else
            {
                threadCallback.Invoke(TypeThread.SYNC, StatusCode.IDLE, "Nothing to be done");
            }
        }
Exemplo n.º 10
0
 public void resetToVersion(FBVersion v)
 {
     SerializedVersion serV = new SerializedVersion();
     serV.encodedVersion = v.serialize();
     UploadData ud = server.resetToCraftedVersion(serV);
     resetVersion(ud, v);
 }
Exemplo n.º 11
0
 public string getFile(FBFile f)
 {
     SerializedVersion serV = new SerializedVersion();
     FBVersion tmpVer = new FBVersion();
     tmpVer.addElement(f);
     serV.encodedVersion = tmpVer.serialize();
     var uploadData = server.getFile(serV);
     string tmpPath = Path.GetTempPath()+f.Name;
     if (File.Exists(tmpPath))
     {
         File.Delete(tmpPath);
     }
     UsefullMethods.ReceiveFile(uploadData.ip, uploadData.port, uploadData.token, tmpPath);
     return tmpPath;
 }
Exemplo n.º 12
0
 public UploadData getFile(SerializedVersion serV)
 {
     return(null);
 }
Exemplo n.º 13
0
 public UploadData getFile(SerializedVersion serV)
 {
     return null;
 }
Exemplo n.º 14
0
 public UploadData resetToCraftedVersion(SerializedVersion serV)
 {
     return null;
 }
Exemplo n.º 15
0
        public SerializedVersion[] getOldVersions()
        {
            LinkedList<FBVersion> versions = this.OldVersions();
            SerializedVersion[] svers = new SerializedVersion[versions.Count - 1];
            int i = 0;
            foreach (FBVersion ver in versions)
            {
                if (ver.root.Name.Contains(firstDirectory)
                    && ver.fileList.Count == 0)
                    continue;

                svers[i++] = new SerializedVersion(ver.serialize());
            }

            return svers;
        }
Exemplo n.º 16
0
        public UploadData getFile(SerializedVersion serV)
        {
            FBVersion ver = FBVersion.deserialize(serV.encodedVersion);
            PhysicFile found = null;

            if (ver.fileList.Count != 1)
                return null;

            foreach(FBFile file in ver.fileList)
            {
                found = findPhysicFile(file);
                if (found == null)
                    return null;
            }

            FileStream fStream = new FileStream(found.getRealFileInfo().FullName,
                FileMode.Open, FileAccess.Read);
            String token = Server.GetUniqueKey(20);
            var secDown = new SecureDownloader(this, token, null, null, fStream);

            return new UploadData(UsefullMethods.GetLocalIPAddress(), secDown.port,
                token);
        }
Exemplo n.º 17
0
        public SerializedVersion getCurrentVersion()
        {
            this.checkAuthentication();

            this.checkTransactionIsNotEnabled();

            try
            {
                FBVersion version = currentVersion();
                SerializedVersion serVersion = new SerializedVersion(version.serialize());

                return serVersion;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new FaultException<ServiceErrorMessage>(new ServiceErrorMessage(ServiceErrorMessage.ROOTDIRECTORYNOTFOUND));
            }
        }
Exemplo n.º 18
0
 public UploadData resetToCraftedVersion(SerializedVersion serV)
 {
     return(null);
 }
        private void doTransaction()
        {
            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);

            UploadData credential = server.uploadFile();
            UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream);

            //Assert.AreEqual(server.uploadFile(fstream), file.hash);
            //fstream.Close();

            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);

            credential = server.uploadFile();
            UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream);
        }
Exemplo n.º 20
0
        public bool newTransaction(SerializedVersion newVersion)
        {
            this.checkAuthentication();

            if (this.transactionEnabled)
                throw new FaultException<ServiceErrorMessage>(new ServiceErrorMessage(ServiceErrorMessage.TRANSACTIONALREADYENABLED));

            FBVersion vers = FBVersion.deserialize(newVersion.encodedVersion);
            FBVersion current = FBVersion.deserialize(this.getCurrentVersion().encodedVersion);
            if (vers.Equals(current))
            {
                return false;
            }

            this.transactionEnabled = true;
            this.inSyncVersion = vers;

            String newDirPath = this.user.rootDirectory.FullName;
            newDirPath += "\\" + this.inSyncVersion.timestamp.ToString(directoryFormat, CultureInfo.InvariantCulture);
            this.transactDir = Directory.CreateDirectory(newDirPath);
            if (this.transactDir == null)
                throw new FaultException<ServiceErrorMessage>(new ServiceErrorMessage(ServiceErrorMessage.CREATEVERSIONDIRECTORYFAILED));

            necessaryFiles = new ThreadSafeList<FBFile>(FBVersion.getNecessaryFilesToUpgrade(this.inSyncVersion, this.realFiles.filesAlreadyRepresented()));

            return true;
        }
Exemplo n.º 21
0
 public Boolean newTransaction(SerializedVersion newVersion)
 {
     return(true);
 }
Exemplo n.º 22
0
 public UploadData resetToCraftedVersion(SerializedVersion serV)
 {
     FBVersion newVersion = FBVersion.deserialize(serV.encodedVersion);
     FBVersion current = this.currentVersion();
     return this.resetInternals(newVersion, current);
 }
Exemplo n.º 23
0
 public Boolean newTransaction(string token, SerializedVersion newVersion)
 {
     return true;
 }