public void NecessaryFilesToUpgradeTest()
        {
            string[] lines1 = { "First line", "Second line", "Third line" };
            string[] lines2 = { "First line", "Second line", "NO" };
            System.IO.Directory.CreateDirectory("asd");
            System.IO.Directory.CreateDirectory(@"asd\ciao");
            DirectoryInfo rinfo = new DirectoryInfo("asd");

            System.IO.File.WriteAllLines(@"asd\uno.txt", lines1);
            System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines1);
            System.IO.File.WriteAllLines(@"asd\ciao\tre.txt", lines1);
            System.IO.File.WriteAllLines(@"asd\ciao\quattro.txt", lines1);

            FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName);

            FolderBackup.Shared.FBVersion vold = (FolderBackup.Shared.FBVersion)vb.generate();
            System.IO.File.WriteAllLines(@"asd\ciao\cinque.txt", lines2);
            System.IO.File.WriteAllLines(@"asd\sei.txt", lines2);
            System.IO.File.WriteAllLines(@"asd\sette.txt", lines1);
            FolderBackup.Shared.FBVersion vnew = (FolderBackup.Shared.FBVersion)vb.generate();

            FBVersion diff = vnew - vold;

            List <FBFile> fl = FBVersion.getNecessaryFilesToUpgrade(vnew, vold.fileList);

            Assert.AreEqual(fl.Count, 1);
            FBFileBuilder fb        = new FBFileBuilder(@"asd\ciao\cinque.txt");
            FBFile        necessary = (FBFile)fb.generate();

            Assert.AreEqual(fl[0], necessary);

            System.IO.Directory.Delete("asd", true);
        }
示例#2
0
        public void RevertWithFileDeletion()
        {
            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();

            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.DELETE);
            Assert.IsTrue(instructionList[0].src == @"ciao\tre.txt");
        }
示例#3
0
        private void initializeUser()
        {
            if (!File.Exists(this.user.rootDirectory + @"\files.bin")) //primo accesso
            {
                this.realFiles = new PhysicFilesList();
                Stream          FilesStream = File.OpenWrite(this.user.rootDirectory + @"\files.bin");
                BinaryFormatter serializer  = new BinaryFormatter();
                serializer.Serialize(FilesStream, realFiles);
                FilesStream.Close();

                this.user.rootDirectory.CreateSubdirectory(firstDirectory);
                FBVersionBuilder vb = new FBVersionBuilder(user.rootDirectory.FullName + @"\" + firstDirectory);
                FBVersion        v  = (FBVersion)vb.generate();
                FilesStream = File.OpenWrite(this.user.rootDirectory.FullName + @"\" + firstDirectory + @"\version.bin");
                serializer.Serialize(FilesStream, v);
                FilesStream.Close();
            }
            else
            {
                Stream          FilesStream1 = File.OpenRead(this.user.rootDirectory + @"\files.bin");
                BinaryFormatter deserializer = new BinaryFormatter();
                this.realFiles = (PhysicFilesList)deserializer.Deserialize(FilesStream1);
                FilesStream1.Close();
            }
        }
示例#4
0
        public void PersistAndDeserialize()
        {
            string[] lines = { "First line", "Second line", "Third line" };
            System.IO.Directory.CreateDirectory("asd");
            System.IO.Directory.CreateDirectory(@"asd\ciao");
            DirectoryInfo rinfo = new DirectoryInfo("asd");

            System.IO.File.WriteAllLines(@"asd\uno.txt", lines);
            System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines);
            FileInfo finfo = new FileInfo(@"asd\ciao\due.txt");

            FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName);

            FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate();

            Stream          TestFileStream = File.Create("version.bin");
            BinaryFormatter serializer     = new BinaryFormatter();

            serializer.Serialize(TestFileStream, v);
            TestFileStream.Close();

            TestFileStream = File.OpenRead("version.bin");
            BinaryFormatter deserializer = new BinaryFormatter();
            FBVersion       v2           = (FBVersion)deserializer.Deserialize(TestFileStream);

            TestFileStream.Close();

            File.Delete("version.bin");
            System.IO.Directory.Delete("asd", true);

            Assert.AreEqual(v2, v);
        }
        public void TwoVersionsAreEquals()
        {
            string[] lines = { "First line", "Second line", "Third line" };
            System.IO.Directory.CreateDirectory("asd");
            System.IO.Directory.CreateDirectory(@"asd\ciao");
            DirectoryInfo rinfo = new DirectoryInfo("asd");

            System.IO.File.WriteAllLines(@"asd\uno.txt", lines);
            System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines);
            FileInfo finfo = new FileInfo(@"asd\ciao\due.txt");

            FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName);

            FolderBackup.Shared.FBVersion v1 = (FolderBackup.Shared.FBVersion)vb.generate();
            FolderBackup.Shared.FBVersion v2 = (FolderBackup.Shared.FBVersion)vb.generate();

            Assert.AreEqual(v1, v2);
            System.IO.Directory.Delete("asd", true);
        }
示例#6
0
        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));
        }
示例#7
0
        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));
            }
        }
示例#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());
        }
示例#9
0
        public void PersistenceTest()
        {
            doTransaction();
            server.commit();
            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(v.serialize());

            Assert.IsFalse(server.newTransaction(serV));
        }
示例#10
0
        public bool commit()
        {
            this.checkAuthentication();
            this.checkTransactionIsEnabled();

            FBVersionBuilder fvb           = new FBVersionBuilder(this.ptransactDir.FullName);
            FBVersion        actualVersion = (FBVersion)fvb.generate();

            actualVersion.timestamp = inSyncVersion.timestamp;

            foreach (KeyValuePair <String, SecureChannel> pair in channels)
            {
                pair.Value.join();
            }

            if (this.necessaryFiles.Count == 0)
            {
                Stream          FileStream = File.Create(this.transactDir.FullName + @"\version.bin");
                BinaryFormatter serializer = new BinaryFormatter();
                serializer.Serialize(FileStream, this.inSyncVersion);
                FileStream.Close();

                if (this.uploadedFiles != null)
                {
                    realFiles.add(this.uploadedFiles);
                }

                FileStream = File.Open(this.user.rootDirectory + @"\files.bin", FileMode.Create, FileAccess.Write, FileShare.None);
                serializer.Serialize(FileStream, realFiles);
                FileStream.Close();

                this.uploadedFiles      = null;
                this.transactDir        = null;
                this.inSyncVersion      = null;
                this.transactionEnabled = false;
                return(true);
            }
            else
            {
                throw new FaultException <ServiceErrorMessage>(new ServiceErrorMessage(ServiceErrorMessage.SYNCNOTTERMINATED));
            }
        }
示例#11
0
        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");
        }
示例#12
0
        public void VersionBuilderGenerate()
        {
            string[] lines = { "First line", "Second line", "Third line" };
            System.IO.Directory.CreateDirectory("asd");
            System.IO.Directory.CreateDirectory(@"asd\ciao");
            DirectoryInfo rinfo = new DirectoryInfo("asd");

            System.IO.File.WriteAllLines(@"asd\uno.txt", lines);
            System.IO.File.WriteAllLines(@"asd\ciao\due.txt", lines);
            FileInfo finfo = new FileInfo(@"asd\ciao\due.txt");

            FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName);

            FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate();

            Assert.AreEqual(v.root.content.Count, (int)2);
            Assert.IsInstanceOfType(v.root.content["ciao"], v.root.GetType());
            Assert.AreEqual(((FolderBackup.Shared.FBDirectory)v.root.content["ciao"]).content.Count, 1);
            Assert.AreEqual(((FolderBackup.Shared.FBFile)((FolderBackup.Shared.FBDirectory)v.root.content["ciao"]).content["due.txt"]).dimension, finfo.Length);
            Assert.AreEqual(((FolderBackup.Shared.FBDirectory)v.root.content["ciao"]).content["due.txt"].isEqualTo(v.root.content["uno.txt"]), true);
            System.IO.Directory.Delete("asd", true);
        }
示例#13
0
        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();
            }
        }