예제 #1
0
        public void FileDeleteTest()
        {
            IEntry entry = FileFactory.CreateEntry(@"C:\ForFileManager\ToDeleteByFile1.txt");

            MyFile.Delete(entry.FullName);
            Assert.IsFalse(MyFile.Exists(@"C:\ForFileManager\ToDeleteByFile1.txt"));
        }
예제 #2
0
        public void CryptDecryptFileWithTrueKey()
        {
            // arrange
            MyFile file = Factory.CreateFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetRandomFileName()));

            file.FileAppendAllText("secretInfo123123");
            List <Entry> entries = new List <Entry>();

            entries.Add(file);
            string key = "key123";

            // act
            FileSystemContainer container = new FileSystemContainer(entries, false, true);

            container.Accept(new DESCryptoVisitor(true, key));
            MyFile cryptFile;

            if (Factory.TryGetFile(file.FullPath + "_crypted", out cryptFile))
            {
                entries.Clear();
                entries.Add(cryptFile);
                container = new FileSystemContainer(entries, false, true);
                container.Accept(new DESCryptoVisitor(false, key));
            }
            else
            {
                Assert.Fail("File " + file.FullPath + " is crypted, but " + file.FullPath + "_crypted " + "could not be found!");
            }
            string decryptResult = file.FileReadAllLines()[0];

            file.Delete();

            // assert
            Assert.AreEqual("secretInfo123123", decryptResult);
        }
예제 #3
0
        public void TestMD5()
        {
            // arrange
            MyFile file    = Factory.CreateFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetRandomFileName()));
            string content = "S);PFE_NWaf9fAk";

            file.FileAppendAllText(content);
            string expectedMD5 = "5cd46939a10ed4faf1d44c99fb2e4be2";

            // act
            string actualMD5 = file.FileMD5;

            file.Delete();

            // assert
            Assert.AreEqual(expectedMD5, actualMD5);
        }
예제 #4
0
        public void AddFileToZipArchiveAndUnzip()
        {
            // arrange
            MyZipArchive zipArchive = Factory.CreateZipArchive(Path.Combine
                                                                   (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".zip"));
            MyFile file = Factory.CreateFile(Path.Combine
                                                 (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetRandomFileName()));
            MyFolder tempFolder = Factory.GetSpecialFolder(Environment.SpecialFolder.ApplicationData);

            // act
            file.FileAppendAllText("content1");
            file.MoveToDirectory(zipArchive); //перемещаем файл в архив
            if (file.Exists)
            {
                Assert.Fail("File " + file.FullPath + "is moved, but Exists is true!");
            }
            var files = zipArchive.DirectoryGetFiles;

            if (files.Count != 1)
            {
                Assert.Fail("Archive contains " + files.Count + " files, expected 1!");
            }
            files[0].CopyToDirectory(tempFolder); //копируем файл из архива на прежнее место
            if (!file.Exists)
            {
                Assert.Fail("File " + file.FullPath + "is unzipped, but Exists is false!");
            }
            string fileContent = file.FileReadAllLines()[0];

            // удаляем все файлы
            zipArchive.Delete();
            file.Delete();

            // assert
            Assert.AreEqual("content1", fileContent);
        }
예제 #5
0
 public override void Visit(MyFile file)
 {
     if (crypt)
     {
         using (FileStream fin = file.FileOpen(FileMode.Open, FileAccess.Read, FileShare.None))
         {
             MyFile cryptedFile = Factory.CreateFile(file.FullPath + "_crypted");
             using (FileStream fout = cryptedFile.FileOpen(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
             {
                 fout.SetLength(0);
                 //Create variables to help with read and write.
                 byte[] bin    = new byte[100]; //This is intermediate storage for the encryption.
                 long   rdlen  = 0;             //This is the total number of bytes written.
                 long   totlen = fin.Length;    //This is the total length of the input file.
                 int    len;                    //This is the number of bytes to be written at a time.
                 DES    des = new DESCryptoServiceProvider();
                 using (CryptoStream encStream = new CryptoStream(fout, desProvider.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write))
                 {
                     //Read from the input file, then encrypt and write to the output file.
                     while (rdlen < totlen)
                     {
                         len = fin.Read(bin, 0, 100);
                         encStream.Write(bin, 0, len);
                         rdlen = rdlen + len;
                     }
                 }
             }
         }
         file.Delete();
     }
     else
     {
         bool success = true;
         using (FileStream fin = file.FileOpen(FileMode.Open, FileAccess.Read, FileShare.None))
         {
             MyFile decryptedFile;
             string pathToCryptedFile = file.FullPath;
             if (pathToCryptedFile.EndsWith("_crypted"))
             {
                 decryptedFile = Factory.CreateFile(file.FullPath.Remove(pathToCryptedFile.Length - "_crypted".Length));
             }
             else
             {
                 decryptedFile = Factory.CreateFile(pathToCryptedFile + "_decrypted");
             }
             using (FileStream fout = decryptedFile.FileOpen(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
             {
                 fout.SetLength(0);
                 //Create variables to help with read and write.
                 byte[] bin    = new byte[100]; //This is intermediate storage for the encryption.
                 long   rdlen  = 0;             //This is the total number of bytes written.
                 long   totlen = fin.Length;    //This is the total length of the input file.
                 int    len;                    //This is the number of bytes to be written at a time.
                 DES    des = new DESCryptoServiceProvider();
                 try
                 {
                     using (CryptoStream encStream = new CryptoStream(fout, desProvider.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write))
                     {
                         //Read from the input file, then decrypt and write to the output file.
                         while (rdlen < totlen)
                         {
                             len = fin.Read(bin, 0, 100);
                             encStream.Write(bin, 0, len);
                             rdlen = rdlen + len;
                         }
                     }
                 }
                 catch (CryptographicException exc)
                 {
                     System.Windows.Forms.MessageBox.Show(exc.Message + "Неверный ключ.");
                     success = false;
                 }
                 catch (IOException ioexc)
                 {
                     System.Windows.Forms.MessageBox.Show(ioexc.Message + "Ошибка доступа.");
                 }
                 catch (Exception exc)
                 {
                     System.Windows.Forms.MessageBox.Show(exc.Message + "Неопознанная ошибка.");
                 }
             }
             if (!success)
             {
                 decryptedFile.Delete();
             }
         }
         if (success)
         {
             file.Delete();
         }
     }
 }
예제 #6
0
 public void Delete(MyFile file)
 {
     file.Delete();
 }