/// <summary> /// Loads a SWF, saves it again and then loads what was saved. The final result is compared with /// a predicted output file to make sure it contains what it's supposed to contain. /// </summary> /// <param name="name">The name of the SWF which is retrieved from the resources folder.</param> /// <param name="reAssembleCode">If true, the ABC bytecode will be disassembled and re-assembled /// again to test the assembler.</param> private void ResaveWithPredictedOutput(string name, bool reAssembleCode) { StringBuilder binDump = new StringBuilder(); using (SWFReader sr = new SWFReader(this.ResourceAsStream(name + @".swf"), new SWFReaderOptions() { StrictTagLength = true }, binDump, this)) { SWF swf = null; try { swf = sr.ReadSWF(new SWFContext(name)); } finally { using (FileStream fs = new FileStream(this.TestDir + name + @".bin.dump-1.txt", FileMode.Create)) { byte[] dumpbindata = new ASCIIEncoding().GetBytes(binDump.ToString()); fs.Write(dumpbindata, 0, dumpbindata.Length); } } Assert.IsNotNull(swf); if (reAssembleCode) { swf.MarkCodeAsTampered(); } /* Save it out, then reload it to make sure it can survive intact */ this.SaveAndVerifyPredictedOutput(swf, name, false); } }
private void TestSWF(string name) { using (SWFReader swfIn = new SWFReader(ResourceAsStream(name), new SWFModeller.IO.SWFReaderOptions(), null, null)) { SWF swf = swfIn.ReadSWF(new SWFContext(name)); ConvertSWF(name, swf); } }
/// <summary> /// App entry point /// </summary> /// <param name="args">Command line arguments</param> public static void Main(string[] args) { try { string config; string explode; string job = ParseArguments(args, out config, out explode); if (job == null && explode == null) { PrintUsage(); Environment.Exit(-1); } if (explode != null) { FileInfo swfFile = new FileInfo(explode); using (FileStream swfIn = new FileStream(explode, FileMode.Open, FileAccess.Read)) { ABCCatcher catcher = new ABCCatcher(); SWFReader reader = new SWFReader(swfIn, new SWFReaderOptions(), null, catcher); reader.ReadSWF(new SWFContext(explode)); catcher.SaveAll(explode, swfFile.DirectoryName); } } Swiffotron swiffotron; if (config == null) { swiffotron = new Swiffotron(null); } else { using (FileStream cfs = new FileStream(config, FileMode.Open, FileAccess.Read)) { swiffotron = new Swiffotron(cfs); } } if (job != null) { using (FileStream jobfs = new FileStream(job, FileMode.Open, FileAccess.Read)) { swiffotron.Process(jobfs); } } } catch (Exception e) { System.Console.WriteLine(e.ToString()); } }
public void CheckCommits(Dictionary <string, byte[]> commits) { foreach (string key in commits.Keys) { byte[] data = commits[key]; if (key.ToLower().EndsWith(".swf")) { string swfDump = null; try { using (MemoryStream ms = new MemoryStream(data)) using (SWFReader reader = new SWFReader(ms, new SWFReaderOptions() { StrictTagLength = true }, null, null)) { swfDump = SwfToString(reader.ReadSWF(new SWFContext(key))); } } finally { lastCommitModelOutput = TestDir + key + ".model.txt"; using (FileStream fs = new FileStream(lastCommitModelOutput, FileMode.Create)) { if (swfDump != null) { byte[] modeldata = new ASCIIEncoding().GetBytes(swfDump.ToString().Replace("\r", "")); fs.Write(modeldata, 0, modeldata.Length); } } } } else { Assert.Fail("For unit tests, a file extension is required on output keys"); } } }
private void PredictedOutputTest(string xmlIn, string swfOut, out Swiffotron swiffotron) { MockStore store; MockCache cache; swiffotron = CreateMockSwiffotron(out store, out cache); StringBuilder writeLog = new StringBuilder(); StringBuilder abcWriteLog = new StringBuilder(); Dictionary <string, byte[]> commits = new Dictionary <string, byte[]>(); swiffotron.Process(ResourceAsStream(xmlIn), commits, writeLog, abcWriteLog, this, this); CheckCommits(commits); using (FileStream fs = new FileStream(TestDir + xmlIn + ".writelog.txt", FileMode.Create)) { byte[] writeLogData = new ASCIIEncoding().GetBytes(writeLog.ToString()); fs.Write(writeLogData, 0, writeLogData.Length); } using (FileStream fs = new FileStream(TestDir + xmlIn + ".abcwritelog.txt", FileMode.Create)) { byte[] writeLogData = new ASCIIEncoding().GetBytes(abcWriteLog.ToString()); fs.Write(writeLogData, 0, writeLogData.Length); } CopyStoreToTestDir(store); Assert.IsTrue(store.Has(swfOut), @"Output was not saved"); /* Check that a valid SWF file was produced by reading it back: */ StringBuilder binDump = new StringBuilder(); SWF swf = null; SWFReader sr = new SWFReader( store.OpenInput(swfOut), new SWFReaderOptions() { StrictTagLength = true }, binDump, this); /* constantFilter is a delegate function that will throw an exception * if it spots something objectionable in the SWF's constants. */ try { swf = sr.ReadSWF(new SWFContext(swfOut)); /* The delegate we gave to the SWF reader for trapping ABC constants will * not have been run yet since the SWF reader doesn't parse the ABC unless * it really needs to. Here's how we force it to, and run the filter * delegate... */ swf.ScriptProc(delegate(DoABC abc) { AbcCode code = abc.Code; /* Transparently parses the ABC */ }); } finally { using (FileStream fs = new FileStream(TestDir + xmlIn + ".bin.dump.txt", FileMode.Create)) { byte[] dumpbindata = new ASCIIEncoding().GetBytes(binDump.ToString()); fs.Write(dumpbindata, 0, dumpbindata.Length); } } string predicted = TestDir + swfOut + ".model.predict.txt"; using (Stream input = ResourceAsStream("predicted." + swfOut + ".txt")) using (FileStream output = new FileStream(predicted, FileMode.Create)) { Assert.IsNotNull(input, "Predicted output is missing! " + swfOut); CopyStream(input, output); } using (StreamWriter acceptScript = new StreamWriter(new FileStream(TestDir + "accept.bat", FileMode.Create))) { acceptScript.WriteLine("copy \"" + lastCommitModelOutput + "\" \"" + new FileInfo("..\\..\\..\\SwiffotronTest\\res\\predicted\\" + swfOut + ".txt").FullName + "\""); } using (StreamWriter viewScript = new StreamWriter(new FileStream(TestDir + "viewdiff.bat", FileMode.Create))) { /* ISSUE 44: This should be a diff tool env var */ viewScript.WriteLine("\"c:\\Program Files (x86)\\WinMerge\\WinMergeU.exe\" \"" + lastCommitModelOutput + "\" \"" + new FileInfo("..\\..\\..\\SwiffotronTest\\res\\predicted\\" + swfOut + ".txt").FullName + "\""); } CompareFiles( predicted, lastCommitModelOutput, "Predicted output failure! These files differ: " + swfOut + ".model.predict.txt" + ", " + swfOut + ".model.txt"); }