public void CabBadUnpackStreamContexts() { int txtSize = 40960; CabInfo cabInfo = new CabInfo("test2.cab"); CompressionTestUtil.GenerateRandomFile("cabtest-0.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("cabtest-1.txt", 1, txtSize); cabInfo.PackFiles(null, new string[] { "cabtest-0.txt", "cabtest-1.txt" }, null); using (CabEngine cabEngine = new CabEngine()) { CompressionTestUtil.TestBadUnpackStreamContexts(cabEngine, "test2.cab"); } }
public static string GetKeyedWsp(string wspFile, string company, string servers, DateTime expiry) { Guid transGuid = new Guid(); //hax: config me string baseDir = @"D:\Hosting\5709750\html\sprockettools\d\"; string source = baseDir + @"original\" + transGuid.ToString() + @"\"; string dest = baseDir + @"modified\"; //load the cab and unpack it to the temporary directory used for this transaction CabInfo cab = new CabInfo(baseDir + wspFile); cab.Unpack(source); //get xml file string keyXmlFile = source + @"Layouts\SprocketValidator\key.xml"; XmlDocument xd = new XmlDocument(); xd.Load(keyXmlFile); //update XML file and write back to disk XmlNode n = xd.SelectSingleNode(@"/key"); n.InnerText = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(company + "|" + servers + "|" + DateTime.Today.AddDays(14).ToShortDateString())); using (XmlWriter xw = XmlWriter.Create(keyXmlFile)) { xd.WriteTo(xw); xw.Flush(); } //create a list with files and add them to a cab file string newFile = (dest + DateTime.Now.ToString("yyyyMMdd") + "_" + company + "_" + wspFile).Replace(" ", ""); CabInfo newCab = new CabInfo(newFile); List<string> paths = new List<string>(); //put files into list to be packed foreach (string compFile in Directory.GetFiles(source, "*", SearchOption.AllDirectories)) { paths.Add(compFile.Replace(source, "")); } newCab.PackFiles(source, paths, paths); //delete the transaction directory Directory.Delete(source,true); return newFile; }
public void CabInfoSerialization() { CabInfo cabInfo = new CabInfo("testser.cab"); int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("testser00.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("testser01.txt", 1, txtSize); cabInfo.PackFiles(null, new string[] { "testser00.txt", "testser01.txt" }, null); ArchiveFileInfo cfi = cabInfo.GetFiles()[1]; MemoryStream memStream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); memStream.Seek(0, SeekOrigin.Begin); formatter.Serialize(memStream, cabInfo); memStream.Seek(0, SeekOrigin.Begin); CabInfo cabInfo2 = (CabInfo) formatter.Deserialize(memStream); Assert.AreEqual<string>(cabInfo.FullName, cabInfo2.FullName); memStream.Seek(0, SeekOrigin.Begin); formatter.Serialize(memStream, cfi); memStream.Seek(0, SeekOrigin.Begin); CabFileInfo cfi2 = (CabFileInfo) formatter.Deserialize(memStream); Assert.AreEqual<string>(cfi.FullName, cfi2.FullName); Assert.AreEqual<long>(cfi.Length, cfi2.Length); CabException cabEx = new CabException(); memStream.Seek(0, SeekOrigin.Begin); formatter.Serialize(memStream, cabEx); memStream.Seek(0, SeekOrigin.Begin); formatter.Deserialize(memStream); cabEx = new CabException("Test exception.", null); Assert.AreEqual<string>("Test exception.", cabEx.Message); }
public void CabFileInfoNullParams() { Exception caughtEx; CabInfo cabInfo = new CabInfo("test.cab"); int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("test00.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("test01.txt", 1, txtSize); cabInfo.PackFiles(null, new string[] { "test00.txt", "test01.txt" }, null); CabFileInfo cfi = new CabFileInfo(cabInfo, "test01.txt"); caughtEx = null; try { new CabFileInfo(null, "test00.txt"); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException)); caughtEx = null; try { new CabFileInfo(cabInfo, null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException)); caughtEx = null; try { cfi.CopyTo(null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException)); }
public void CabFileInfoOpenText() { CabInfo cabInfo = new CabInfo("test.cab"); int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("test00.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("test01.txt", 1, txtSize); string expectedText = File.ReadAllText("test01.txt"); cabInfo.PackFiles(null, new string[] { "test00.txt", "test01.txt" }, null); CabFileInfo cfi = new CabFileInfo(cabInfo, "test01.txt"); using (StreamReader cabFileReader = cfi.OpenText()) { string text = cabFileReader.ReadToEnd(); Assert.AreEqual(expectedText, text); // Check the assumption that the cab can't be deleted while a stream is open. Exception caughtEx = null; try { File.Delete(cabInfo.FullName); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(IOException)); } // Ensure all streams are closed after disposing of the StreamReader returned by OpenText. File.Delete(cabInfo.FullName); }
public void CabFileInfoProperties() { CabInfo cabInfo = new CabInfo("test.cab"); int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("test00.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("test01.txt", 1, txtSize); File.SetAttributes("test01.txt", FileAttributes.ReadOnly | FileAttributes.Archive); DateTime testTime = File.GetLastWriteTime("test01.txt"); cabInfo.PackFiles(null, new string[] { "test00.txt", "test01.txt" }, null); File.SetAttributes("test01.txt", FileAttributes.Archive); CabFileInfo cfi = new CabFileInfo(cabInfo, "test01.txt"); Assert.AreEqual(cabInfo.FullName, cfi.CabinetName); Assert.AreEqual<int>(0, ((CabFileInfo) cfi).CabinetFolderNumber); Assert.AreEqual<string>(Path.Combine(cabInfo.FullName, "test01.txt"), cfi.FullName); cfi = new CabFileInfo(cabInfo, "test01.txt"); Assert.IsTrue(cfi.Exists); cfi = new CabFileInfo(cabInfo, "test01.txt"); Assert.AreEqual<long>(txtSize, cfi.Length); cfi = new CabFileInfo(cabInfo, "test00.txt"); Assert.AreEqual<FileAttributes>(FileAttributes.Archive, cfi.Attributes); cfi = new CabFileInfo(cabInfo, "test01.txt"); Assert.AreEqual<FileAttributes>(FileAttributes.ReadOnly | FileAttributes.Archive, cfi.Attributes); cfi = new CabFileInfo(cabInfo, "test01.txt"); Assert.IsTrue((testTime - cfi.LastWriteTime).Duration() < new TimeSpan(0, 0, 5)); Assert.AreEqual<string>(Path.Combine(cabInfo.FullName, "test01.txt"), cfi.ToString()); cfi.CopyTo("testcopy.txt"); Assert.IsTrue(File.Exists("testCopy.txt")); Assert.AreEqual<long>(cfi.Length, new FileInfo("testCopy.txt").Length); Exception caughtEx = null; try { cfi.CopyTo("testcopy.txt", false); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(IOException)); }
public void CabInfoCompressExtract() { int fileCount = 10, fileSize = 1024; string dirA = String.Format("{0}-{1}-A", fileCount, fileSize); if (Directory.Exists(dirA)) Directory.Delete(dirA, true); Directory.CreateDirectory(dirA); Directory.CreateDirectory(Path.Combine(dirA, "sub")); string dirB = String.Format("{0}-{1}-B", fileCount, fileSize); if (Directory.Exists(dirB)) Directory.Delete(dirB, true); Directory.CreateDirectory(dirB); string[] files = new string[fileCount]; for (int iFile = 0; iFile < fileCount; iFile++) { files[iFile] = "€" + iFile + ".txt"; CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[iFile]), iFile, fileSize); } CompressionTestUtil.GenerateRandomFile(Path.Combine(Path.Combine(dirA, "sub"), "€-.txt"), fileCount + 1, fileSize); CabInfo cabInfo = new CabInfo("test.cab"); cabInfo.Pack(dirA); cabInfo.Unpack(dirB); bool directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB); Assert.IsFalse(directoryMatch, "Testing whether cabinet output directory matches input directory."); Directory.Delete(dirB, true); Directory.CreateDirectory(dirB); cabInfo.Pack(dirA, true, CompressionLevel.Normal, null); cabInfo.Unpack(dirB); directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB); Assert.IsTrue(directoryMatch, "Testing whether cabinet output directory matches input directory."); Directory.Delete(dirB, true); Directory.Delete(Path.Combine(dirA, "sub"), true); Directory.CreateDirectory(dirB); cabInfo.Delete(); cabInfo.PackFiles(dirA, files, null); cabInfo.UnpackFiles(files, dirB, null); directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB); Assert.IsTrue(directoryMatch, "Testing whether cabinet output directory matches input directory."); Directory.Delete(dirB, true); Directory.CreateDirectory(dirB); cabInfo.Delete(); IDictionary<string, string> testMap = new Dictionary<string, string>(files.Length); for (int iFile = 0; iFile < fileCount; iFile++) { testMap[files[iFile] + ".key"] = files[iFile]; } cabInfo.PackFileSet(dirA, testMap); cabInfo.UnpackFileSet(testMap, dirB); directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB); Assert.IsTrue(directoryMatch, "Testing whether cabinet output directory matches input directory."); Directory.Delete(dirB, true); Directory.CreateDirectory(dirB); testMap.Remove(files[1] + ".key"); cabInfo.UnpackFileSet(testMap, dirB); directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB); Assert.IsFalse(directoryMatch, "Testing whether cabinet output directory matches input directory."); Directory.Delete(dirB, true); Directory.CreateDirectory(dirB); cabInfo.Delete(); cabInfo.PackFiles(dirA, files, null); cabInfo.UnpackFile("€2.txt", Path.Combine(dirB, "test.txt")); Assert.IsTrue(File.Exists(Path.Combine(dirB, "test.txt"))); Assert.AreEqual<int>(1, Directory.GetFiles(dirB).Length); }
public void CabInfoGetFiles() { IList<CabFileInfo> fileInfo; CabInfo cabInfo = new CabInfo("test.cab"); int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("testinfo0.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("testinfo1.txt", 1, txtSize); cabInfo.PackFiles(null, new string[] { "testinfo0.txt", "testinfo1.txt" }, null); fileInfo = cabInfo.GetFiles(); Assert.IsNotNull(fileInfo); Assert.AreEqual<int>(2, fileInfo.Count); Assert.AreEqual<string>("testinfo0.txt", fileInfo[0].Name); Assert.AreEqual<string>("testinfo1.txt", fileInfo[1].Name); fileInfo = cabInfo.GetFiles("*.txt"); Assert.IsNotNull(fileInfo); Assert.AreEqual<int>(2, fileInfo.Count); Assert.AreEqual<string>("testinfo0.txt", fileInfo[0].Name); Assert.AreEqual<string>("testinfo1.txt", fileInfo[1].Name); fileInfo = cabInfo.GetFiles("testinfo1.txt"); Assert.IsNotNull(fileInfo); Assert.AreEqual<int>(1, fileInfo.Count); Assert.AreEqual<string>("testinfo1.txt", fileInfo[0].Name); }
public void CabInfoProperties() { Exception caughtEx; CabInfo cabInfo = new CabInfo("test.cab"); int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("test00.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("test01.txt", 1, txtSize); cabInfo.PackFiles(null, new string[] { "test00.txt", "test01.txt" }, null); Assert.AreEqual<string>(new FileInfo("test.cab").Directory.FullName, cabInfo.Directory.FullName, "CabInfo.FullName"); Assert.AreEqual<string>(new FileInfo("test.cab").DirectoryName, cabInfo.DirectoryName, "CabInfo.DirectoryName"); Assert.AreEqual<long>(new FileInfo("test.cab").Length, cabInfo.Length, "CabInfo.Length"); Assert.AreEqual<string>("test.cab", cabInfo.Name, "CabInfo.Name"); Assert.AreEqual<string>(new FileInfo("test.cab").FullName, cabInfo.ToString(), "CabInfo.ToString()"); cabInfo.CopyTo("test3.cab"); caughtEx = null; try { cabInfo.CopyTo("test3.cab"); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(IOException), "CabInfo.CopyTo() caught exception: " + caughtEx); cabInfo.CopyTo("test3.cab", true); cabInfo.MoveTo("test4.cab"); Assert.AreEqual<string>("test4.cab", cabInfo.Name); Assert.IsTrue(cabInfo.Exists, "CabInfo.Exists()"); Assert.IsTrue(cabInfo.IsValid(), "CabInfo.IsValid"); cabInfo.Delete(); Assert.IsFalse(cabInfo.Exists, "!CabInfo.Exists()"); }
public void CabExtractorExtract() { int txtSize = 40960; CabInfo cabInfo = new CabInfo("test.cab"); CompressionTestUtil.GenerateRandomFile("test0.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("test1.txt", 1, txtSize); cabInfo.PackFiles(null, new string[] { "test0.txt", "test1.txt" }, null); using (CabEngine cabEngine = new CabEngine()) { using (Stream cabStream = File.OpenRead("test.cab")) { using (Stream exStream = cabEngine.Unpack(cabStream, "test0.txt")) { string str = new StreamReader(exStream).ReadToEnd(); string expected = new StreamReader("test0.txt").ReadToEnd(); Assert.AreEqual<string>(expected, str); } cabStream.Seek(0, SeekOrigin.Begin); using (Stream exStream = cabEngine.Unpack(cabStream, "test1.txt")) { string str = new StreamReader(exStream).ReadToEnd(); string expected = new StreamReader("test1.txt").ReadToEnd(); Assert.AreEqual<string>(expected, str); } } using (Stream txtStream = File.OpenRead("test0.txt")) { Exception caughtEx = null; try { cabEngine.Unpack(txtStream, "test0.txt"); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(CabException)); Assert.AreEqual<int>(2, ((CabException) caughtEx).Error); Assert.AreEqual<int>(0, ((CabException) caughtEx).ErrorCode); Assert.AreEqual<string>("Cabinet file does not have the correct format.", caughtEx.Message); } } }
public void CabExtractorGetFiles() { IList<ArchiveFileInfo> fileInfo; CabInfo cabInfo = new CabInfo("testgetfiles.cab"); int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("testgetfiles0.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("testgetfiles1.txt", 1, txtSize); cabInfo.PackFiles(null, new string[] { "testgetfiles0.txt", "testgetfiles1.txt" }, null); using (CabEngine cabEngine = new CabEngine()) { IList<string> files; using (Stream cabStream = File.OpenRead("testgetfiles.cab")) { files = cabEngine.GetFiles(cabStream); } Assert.IsNotNull(files); Assert.AreEqual<int>(2, files.Count); Assert.AreEqual<string>("testgetfiles0.txt", files[0]); Assert.AreEqual<string>("testgetfiles1.txt", files[1]); using (Stream cabStream = File.OpenRead("testgetfiles.cab")) { files = cabEngine.GetFiles(new ArchiveFileStreamContext("testgetfiles.cab"), null); } Assert.IsNotNull(files); Assert.AreEqual<int>(2, files.Count); Assert.AreEqual<string>("testgetfiles0.txt", files[0]); Assert.AreEqual<string>("testgetfiles1.txt", files[1]); using (Stream cabStream = File.OpenRead("testgetfiles.cab")) { fileInfo = cabEngine.GetFileInfo(cabStream); } Assert.IsNotNull(fileInfo); Assert.AreEqual<int>(2, fileInfo.Count); Assert.AreEqual<string>("testgetfiles0.txt", fileInfo[0].Name); Assert.AreEqual<string>("testgetfiles1.txt", fileInfo[1].Name); using (Stream cabStream = File.OpenRead("testgetfiles.cab")) { fileInfo = cabEngine.GetFileInfo(new ArchiveFileStreamContext("testgetfiles.cab"), null); } Assert.IsNotNull(fileInfo); Assert.AreEqual<int>(2, fileInfo.Count); Assert.AreEqual<string>("testgetfiles0.txt", fileInfo[0].Name); Assert.AreEqual<string>("testgetfiles1.txt", fileInfo[1].Name); } fileInfo = this.RunCabinetPackUnpack(15, 20 * 1024, 1 * 1024, 130 * 1024); Assert.IsNotNull(fileInfo); Assert.AreEqual<int>(15, fileInfo.Count); for (int i = 0; i < fileInfo.Count; i++) { Assert.IsNull(fileInfo[i].Archive); Assert.AreEqual<string>(TEST_FILENAME_PREFIX + i + ".txt", fileInfo[i].Name); Assert.IsTrue(DateTime.Now - fileInfo[i].LastWriteTime < new TimeSpan(0, 1, 0)); } }
public void CabTruncatedArchive() { CabInfo cabInfo = new CabInfo("test-t.cab"); CompressionTestUtil.GenerateRandomFile("cabtest-0.txt", 0, 5); CompressionTestUtil.GenerateRandomFile("cabtest-1.txt", 1, 5); cabInfo.PackFiles(null, new string[] { "cabtest-0.txt", "cabtest-1.txt" }, null); CompressionTestUtil.TestTruncatedArchive(cabInfo, typeof(CabException)); }
public void CabinetTruncateOnCreate() { CabInfo cabInfo = new CabInfo("testtruncate.cab"); int txtSize = 20240; CompressionTestUtil.GenerateRandomFile("testtruncate0.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("testtruncate1.txt", 1, txtSize); cabInfo.PackFiles(null, new string[] { "testtruncate0.txt", "testtruncate1.txt" }, null); long size1 = cabInfo.Length; txtSize /= 5; CompressionTestUtil.GenerateRandomFile("testtruncate2.txt", 2, txtSize); CompressionTestUtil.GenerateRandomFile("testtruncate3.txt", 3, txtSize); cabInfo.PackFiles(null, new string[] { "testtruncate2.txt", "testtruncate3.txt" }, null); // The newly created cab file should be smaller than before. Assert.AreNotEqual<long>(size1, cabInfo.Length, "Checking that cabinet file got truncated when creating a smaller cab in-place."); }