private void WriteArchiveAndReadBack()
        {
            // write the contents of the files to the record
            using (var fs = new FileStream(archivePath, FileMode.Create))
            {
                var archive = new SAFArchive(fs, SAFMode.Write, sourcePath, true);
                archive.WriteDirectoryToArchive();
            }

            // read them back.
            using (var fs = new FileStream(archivePath, FileMode.Open))
            {
                var archive = new SAFArchive(fs, SAFMode.Read, extractPath);
                archive.ReadAllEntriesToDisk();
            }
        }
        public void VerifyChecksumOperation()
        {
            // create the archive normally
            using (var fs = new FileStream(archivePath, FileMode.Create))
            {
                var archive = new SAFArchive(fs, SAFMode.Write, sourcePath, true);
                archive.WriteDirectoryToArchive();
            }

            // tamper with the archive.
            using (var fs = new FileStream(archivePath, FileMode.Open))
            {
                var archiveToTamperWith = new SAFArchive(fs, SAFMode.Read, extractPath);

                // find a file entry
                while (true)
                {
                    var header = archiveToTamperWith.ReadNextHeader();
                    if (header.Type == SAFEntryType.File)
                    {
                        break;                                   // we don't need to skip - directorys have no length.
                    }
                }

                // we're now on a file. Invert the second byte.
                var  loc           = fs.Position;
                byte byteToInvert  = (byte)fs.ReadByte();
                byte invertedValue = (byte)(0xFF ^ byteToInvert);

                // reset our position
                fs.Position = loc;
                fs.WriteByte(invertedValue);

                // flush
                fs.Flush();
            }

            // try to extract, this should throw an IO Error.
            using (var fs = new FileStream(archivePath, FileMode.Open))
            {
                var corruptedArchive = new SAFArchive(fs, SAFMode.Read, extractPath);
                Assert.Throws <IOException>(() => corruptedArchive.ReadAllEntriesToDisk());
            }
        }
        public void TestRelativePaths(string relativePath, bool useLeadingDotSlash, char dirSeparator)
        {
            // we build all paths under the designated relative path, for easy cleanup.
            var targetPath = RelativeDir + dirSeparator + relativePath;

            // confirm the path isn't rooted
            if (Path.IsPathRooted(targetPath))
            {
                throw new ArgumentException("Path under test should be relative.");
            }

            // if we use the leading dotslash, prepend .\ (or ./) to path (as might be fed in if paths were generated via hiting tab
            // on the command line
            if (useLeadingDotSlash)
            {
                targetPath = "." + dirSeparator + targetPath;
            }

            // build the directory structure (we can use full paths here)
            Directory.CreateDirectory(targetPath);

            // create the file target
            File.WriteAllText(targetPath + dirSeparator + "testFile.bin", "I EXIST");

            // do saf operations
            using (var fs = new FileStream(archivePath, FileMode.Create))
            {
                var archive = new SAFArchive(fs, SAFMode.Write, targetPath);
                archive.WriteDirectoryToArchive();
            }

            using (var fs = new FileStream(archivePath, FileMode.Open))
            {
                var archive = new SAFArchive(fs, SAFMode.Read, extractPath);
                archive.ReadAllEntriesToDisk();
            }

            // test that our file exists in the expected path (we fed in the target path to the archive root, so it won't show here)
            var extractedPath = Path.Combine(extractPath, "testFile.bin");

            Assert.True(File.Exists(extractedPath), "Extract path test failed.");
        }
        public void CheckFileHeader()
        {
            // write the contents of the files to the record
            using (var fs = new FileStream(archivePath, FileMode.Create))
            {
                var archive = new SAFArchive(fs, SAFMode.Write, sourcePath, true);
                archive.WriteDirectoryToArchive();

                // confirm the archive header was set correctly
                Assert.Equal(archive.SAFArchiveHeader, SAFArchive.SAF_ARCHIVE_HEADER_STRING);
            }

            // read them back.
            using (var fs = new FileStream(archivePath, FileMode.Open))
            {
                var archive = new SAFArchive(fs, SAFMode.Read, extractPath);
                archive.ReadAllEntriesToDisk();

                // confirm the header was read correctly
                Assert.Equal(archive.SAFArchiveHeader, SAFArchive.SAF_ARCHIVE_HEADER_STRING);
            }
        }