コード例 #1
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);
        }
コード例 #2
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);
        }
コード例 #3
0
        private void Finding(Object obj)
        {
            var Q = (Queue <Entry>)obj;

            foreach (var file in Q)
            {
                try
                {
                    using (StreamReader SR = file.FileGetStreamReader())
                    {
                        string line;
                        MyFile targetFile = new MyFile(target);
                        while ((line = GetLineWithLock(SR)) != null)
                        {
                            int i = -1;
                            foreach (var reg in RegExpsArray)
                            {
                                i++;
                                var matches = reg.Matches(line);
                                foreach (Match match in matches)
                                {
                                    if (match.Success)
                                    {
                                        lock (syncRoot)
                                        {
                                            targetFile.FileAppendAllText(i + " " + match.Value + Environment.NewLine);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    processedFiles++;
                }
            }
        }
コード例 #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 void ZipArchiveAddFiles()
        {
            // arrange
            MyZipArchive zipArchive = Factory.CreateZipArchive(Path.Combine
                                                                   (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".zip"));
            MyFolder folder = Factory.CreateFolder(Path.Combine
                                                       (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.GetFileNameWithoutExtension(Path.GetRandomFileName())));
            string           content = "S);PFE_NWaf9fAk";
            MyFile           file1   = Factory.CreateFile(folder.FullPath + "\\document.doc"); file1.FileAppendAllText(content);
            MyFile           file2   = Factory.CreateFile(folder.FullPath + "\\documentx.docx"); file2.FileAppendAllText(content);
            MyFile           file3   = Factory.CreateFile(folder.FullPath + "\\document.html"); file3.FileAppendAllText(content);
            MyFile           file4   = Factory.CreateFile(folder.FullPath + "\\document.dot"); file4.FileAppendAllText(content);
            MyFile           file5   = Factory.CreateFile(folder.FullPath + "\\page.doc"); file5.FileAppendAllText(content);
            MyFile           file6   = Factory.CreateFile(folder.FullPath + "\\image.jpg"); file6.FileAppendAllText(content);
            HashSet <string> paths   = new HashSet <string>();

            paths.Add(file1.Name); paths.Add(file2.Name); paths.Add(file3.Name);
            paths.Add(file4.Name); paths.Add(file5.Name); paths.Add(file6.Name);

            // act
            folder.CopyToDirectory(zipArchive);
            var dirs = zipArchive.DirectoryGetFolders;

            if (dirs.Count != 1)
            {
                Assert.Fail("There is " + dirs.Count + " folders. Expected 1.");
            }
            var files = dirs[0].DirectoryGetFiles;

            if (files.Count != 6)
            {
                Assert.Fail("There is " + files.Count + " files in " + dirs[0].FullPath + ". Expected 6.");
            }
            foreach (var file in files)
            {
                if (!paths.Contains(file.Name))
                {
                    Assert.Fail(dirs[0].FullPath + "does not contain " + file.Name + ".");
                }
                else
                {
                    paths.Remove(file.Name);
                }
            }
            // удаляем используемые entry
            folder.Delete();
            zipArchive.Delete();
            // assert
            Assert.IsTrue(paths.Count == 0);
        }