From 9a323a135503b9f37f9252a75864fc1db42613df Mon Sep 17 00:00:00 2001 From: "honfika@gmail.com" Date: Fri, 4 Dec 2015 08:24:09 +0100 Subject: [PATCH] swf xml test --- .../flash/tags/base/StaticTextTag.java | 2 +- .../jpexs/decompiler/flash/ExportTest.java | 3 +- .../flash/SwfXmlExportImportTest.java | 156 ++++++++++++++++++ 3 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/SwfXmlExportImportTest.java diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/StaticTextTag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/StaticTextTag.java index cdbded97d..4e61062af 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/StaticTextTag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/StaticTextTag.java @@ -107,7 +107,7 @@ public abstract class StaticTextTag extends TextTag { for (TEXTRECORD tr : textRecords) { for (GLYPHENTRY ge : tr.glyphEntries) { glyphBits = SWFOutputStream.enlargeBitCountU(glyphBits, ge.glyphIndex); - advanceBits = SWFOutputStream.enlargeBitCountS(advanceBits, ge.glyphAdvance); + advanceBits = SWFOutputStream.enlargeBitCountU(advanceBits, ge.glyphAdvance); } } diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ExportTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ExportTest.java index 469786427..7d1352d95 100644 --- a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ExportTest.java +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/ExportTest.java @@ -19,6 +19,7 @@ package com.jpexs.decompiler.flash; import com.jpexs.decompiler.flash.configuration.Configuration; import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; import com.jpexs.decompiler.flash.exporters.settings.ScriptExportSettings; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.util.logging.Handler; @@ -90,7 +91,7 @@ public class ExportTest extends FileTestBase { public void testDecompile(String filePath, ScriptExportMode exportMode) { try { File f = new File(filePath); - SWF swf = new SWF(new FileInputStream(filePath), false); + SWF swf = new SWF(new BufferedInputStream(new FileInputStream(filePath)), false); String folderName = exportMode == ScriptExportMode.AS ? "output" : "outputp"; File fdir = new File(TESTDATADIR + File.separator + folderName + File.separator + f.getName()); fdir.mkdirs(); diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/SwfXmlExportImportTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/SwfXmlExportImportTest.java new file mode 100644 index 000000000..49b286ef7 --- /dev/null +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/SwfXmlExportImportTest.java @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2010-2015 JPEXS, All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +package com.jpexs.decompiler.flash; + +import com.jpexs.decompiler.flash.abc.NotSameException; +import com.jpexs.decompiler.flash.configuration.Configuration; +import com.jpexs.decompiler.flash.exporters.swf.SwfXmlExporter; +import com.jpexs.decompiler.flash.importers.SwfXmlImporter; +import com.jpexs.decompiler.flash.tags.DefineSpriteTag; +import com.jpexs.decompiler.flash.tags.Tag; +import com.jpexs.decompiler.flash.tags.base.FontTag; +import com.jpexs.helpers.Helper; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import static org.testng.Assert.fail; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * + * @author JPEXS + */ +public class SwfXmlExportImportTest extends FileTestBase { + + @BeforeClass + public void init() { + Configuration.autoDeobfuscate.set(false); + Configuration.debugCopy.set(false); + } + + public static final String TESTDATADIR = "testdata/decompile"; + + public static Handler loggerHandler; + + @BeforeClass + public void addLogger() { + Logger logger = Logger.getLogger(""); + loggerHandler = new Handler() { + @Override + public void publish(LogRecord record) { + if (record.getLevel() == Level.SEVERE) { + fail("Error during decompilation", record.getThrown()); + } + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } + }; + + logger.addHandler(loggerHandler); + } + + @AfterClass + public void removeLogger() { + if (loggerHandler != null) { + Logger logger = Logger.getLogger(""); + logger.removeHandler(loggerHandler); + } + } + + @Test(dataProvider = "provideFiles") + public void testExportImportXml(String filePath) { + try { + File f = new File(filePath); + SWF swf = new SWF(new BufferedInputStream(new FileInputStream(filePath)), false); + String folderName = "xml"; + File fdir = new File(TESTDATADIR + File.separator + folderName + File.separator + f.getName()); + fdir.mkdirs(); + + File outFile = new File(fdir + File.separator + Helper.makeFileName("swf.xml")); + new SwfXmlExporter().exportXml(swf, outFile); + String xml = Helper.readTextFile(outFile.getPath()); + + SWF swf2 = new SWF(); + new SwfXmlImporter().importSwf(swf2, xml); + + if (swf.tags.size() != swf2.tags.size()) { + throw new NotSameException(0); + } + + for (int i = 0; i < swf.tags.size(); i++) { + Tag oldTag = swf.tags.get(i); + Tag newTag = swf2.tags.get(i); + if (oldTag.getClass() != newTag.getClass()) { + throw new NotSameException(0); + } + + if (oldTag instanceof DefineSpriteTag) { + DefineSpriteTag oldSprite = (DefineSpriteTag) oldTag; + DefineSpriteTag newSprite = (DefineSpriteTag) newTag; + if (oldSprite.subTags.size() != newSprite.subTags.size()) { + throw new NotSameException(0); + } + + for (int k = 0; k < oldSprite.subTags.size(); k++) { + compareTags(oldSprite.subTags.get(k), newSprite.subTags.get(k)); + } + } else if (!(oldTag instanceof FontTag)) { + compareTags(oldTag, newTag); + } + } + } catch (Exception ex) { + fail("Exception during SWF xml export/import: " + filePath + " " + ex.getMessage()); + } + } + + private void compareTags(Tag tag1, Tag tag2) { + if (tag1.getClass() != tag2.getClass()) { + throw new NotSameException(0); + } + + byte[] data1 = tag1.getData(); + byte[] data2 = tag2.getData(); + + int length = Math.min(data1.length, data2.length); + for (int j = 0; j < length; j++) { + if (data1[j] != data2[j]) { + throw new NotSameException(j); + } + } + + if (data1.length != data2.length) { + throw new NotSameException(length); + } + } + + @Override + public String[] getTestDataDirs() { + return new String[]{TESTDATADIR, FREE_ACTIONSCRIPT_AS2, FREE_ACTIONSCRIPT_AS3}; + } +}