diff --git a/src/test/java/TestCases.java b/src/test/java/TestCases.java new file mode 100644 index 0000000..b182d13 --- /dev/null +++ b/src/test/java/TestCases.java @@ -0,0 +1,400 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Map.Entry; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import de.mas.wiiu.jnus.NUSTitle; +import de.mas.wiiu.jnus.NUSTitleLoaderLocal; +import de.mas.wiiu.jnus.entities.content.Content; +import de.mas.wiiu.jnus.interfaces.NUSDataProcessor; +import de.mas.wiiu.jnus.utils.HashUtil; +import de.mas.wiiu.jnus.utils.Utils; + +public class TestCases { + @Rule public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void nonHashedFileRead() throws IOException { + String resourceName = "out"; + String resourceName2 = "tmp"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource(resourceName).getFile()); + File file2 = new File(classLoader.getResource(resourceName2).getFile()); + String absolutePath = file.getAbsolutePath(); + NUSTitle title = null; + try { + title = NUSTitleLoaderLocal.loadNUSTitle(absolutePath, Utils.StringToByteArray("00000000000000000000000000000000")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + NUSDataProcessor dp = title.getDataProcessor(); + Content c = title.getTMD().getContentByID(2); + + File dec = new File(file2.getAbsolutePath() + File.separator + String.format("%08X.dec", c.getID())); + + RandomAccessFile fr = new RandomAccessFile(dec, "r"); + + int blockSize = 0x1000000; + + try { + int curOffset = 0; + long totalSize = 0; + while (true) { + byte[] contentDataLib = dp.readPlainDecryptedContent(c, curOffset, blockSize, false); + + if (contentDataLib.length == 0) { + break; + } + byte[] hashLib = HashUtil.hashSHA1(contentDataLib); + + fr.seek(curOffset); + byte[] buffer = new byte[contentDataLib.length]; + fr.read(buffer); + + byte[] hashFile = HashUtil.hashSHA1(buffer); + // System.out.println(Utils.ByteArrayToString(hashFile) + " " + Utils.ByteArrayToString(hashLib)); + Assert.assertArrayEquals(hashFile, hashLib); + curOffset += blockSize; + totalSize += contentDataLib.length; + //System.out.println(totalSize + " " + totalSize * 1.0f / dec.length()); + } + assertEquals(dec.length(), totalSize); + + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + fr.close(); + } + } + + @Test + public void nonHashedFileReadStream() throws IOException { + + String resourceName = "out"; + String resourceName2 = "tmp"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource(resourceName).getFile()); + File file2 = new File(classLoader.getResource(resourceName2).getFile()); + String absolutePath = file.getAbsolutePath(); + NUSTitle title = null; + try { + title = NUSTitleLoaderLocal.loadNUSTitle(absolutePath, Utils.StringToByteArray("00000000000000000000000000000000")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + NUSDataProcessor dp = title.getDataProcessor(); + Content c = title.getTMD().getContentByID(2); + + File dec = new File(file2.getAbsolutePath() + File.separator + String.format("%08X.dec", c.getID())); + //System.out.println(dec.getAbsolutePath()); + InputStream fileIn = new BufferedInputStream(new FileInputStream(dec)); + + int blockSize = 0x1000000; + byte[] buffer = new byte[blockSize]; + try { + int curOffset = 0; + long totalSize = 0; + + InputStream libIn = dp.readPlainDecryptedContentAsStream(c, false); + while (true) { + + int res1 = 0; + while (res1 < blockSize) { + int res = libIn.read(buffer, res1, blockSize - res1); + if (res < 0) { + break; + } + res1 += res; + } + byte[] hashLib = HashUtil.hashSHA1(Arrays.copyOfRange(buffer, 0, res1)); + try (FileOutputStream fos = new FileOutputStream("test.1")) { + // fos.write(buffer); + // fos.close(); There is no more need for this line since you had created the instance of "fos" inside the try. And this will automatically + // close the OutputStream + } + int res2 = 0; + while (res2 < blockSize) { + int res = fileIn.read(buffer, res2, blockSize - res2); + if (res < 0) { + break; + } + res2 += res; + } + + try (FileOutputStream fos = new FileOutputStream("test.2")) { + // fos.write(buffer); + // fos.close(); There is no more need for this line since you had created the instance of "fos" inside the try. And this will automatically + // close the OutputStream + } + //System.out.println(res1); + byte[] hashFile = HashUtil.hashSHA1(Arrays.copyOfRange(buffer, 0, res2)); + //System.out.println(res2 + " " + res1); + assertEquals(res2, res1); + if (res1 == 0) { + break; + } + //System.out.println(Utils.ByteArrayToString(hashFile) + " " + Utils.ByteArrayToString(hashLib)); + Assert.assertArrayEquals(hashFile, hashLib); + curOffset += blockSize; + totalSize += res1; + //System.out.println(totalSize + " " + totalSize * 1.0f / dec.length()); + } + assertEquals(dec.length(), totalSize); + libIn.close(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + fileIn.close(); + + } + } + + @Test + public void HashedFileRead() throws IOException { + String resourceName = "out"; + String resourceName2 = "tmp"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource(resourceName).getFile()); + File file2 = new File(classLoader.getResource(resourceName2).getFile()); + String absolutePath = file.getAbsolutePath(); + NUSTitle title = null; + try { + title = NUSTitleLoaderLocal.loadNUSTitle(absolutePath, Utils.StringToByteArray("00000000000000000000000000000000")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + NUSDataProcessor dp = title.getDataProcessor(); + Content c = title.getTMD().getContentByID(3); + + File dec = new File(file2.getAbsolutePath() + File.separator + String.format("%08X.dec", c.getID())); + RandomAccessFile fr = new RandomAccessFile(dec, "r"); + + int blockSize = 0x10337; + + try { + long curOffset = 0; + long totalSize = 0; + while (true) { + byte[] contentDataLib = dp.readPlainDecryptedContent(c, curOffset, blockSize, false); + + try (FileOutputStream fos = new FileOutputStream("test.1")) { + fos.write(contentDataLib); + } + + if (contentDataLib.length == 0) { + break; + } + + byte[] hashLib = HashUtil.hashSHA1(contentDataLib); + + fr.seek(curOffset); + byte[] buffer = new byte[blockSize]; + + int res = fr.read(buffer); + if (res < 0) { + assertTrue(false); + } + buffer = Arrays.copyOf(buffer, res); + try (FileOutputStream fos = new FileOutputStream("test.2")) { + fos.write(buffer); + } + + Assert.assertEquals(buffer.length, contentDataLib.length); + + byte[] hashFile = HashUtil.hashSHA1(buffer); + // System.out.println(Utils.ByteArrayToString(hashFile) + " " + Utils.ByteArrayToString(hashLib)); + Assert.assertArrayEquals(hashFile, hashLib); + curOffset += blockSize; + totalSize += contentDataLib.length; + + //System.out.println(totalSize + " " + (int) ((totalSize * 1.0f / dec.length()) * 100) + "%"); + } + assertEquals(dec.length(), totalSize); + + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + fr.close(); + } + } + + @Test + public void HashedFileReadDec() throws IOException { + String resourceName = "out"; + String resourceName1 = "out_dec"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource(resourceName).getFile()); + File file1 = new File(classLoader.getResource(resourceName1).getFile()); + NUSTitle title = null; + try { + title = NUSTitleLoaderLocal.loadNUSTitle(file.getAbsolutePath(), Utils.StringToByteArray("00000000000000000000000000000000")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + + NUSDataProcessor dp = title.getDataProcessor(); + for(Entry e : title.getTMD().getAllContents().entrySet()) { + Content c = e.getValue(); + File dec = new File(file1.getAbsolutePath() + File.separator + String.format("%08X.app", c.getID())); + + RandomAccessFile fr = new RandomAccessFile(dec, "r"); + + int blockSize = 0x10337; + + try { + long curOffset = 0; + long totalSize = 0; + while (true) { + byte[] contentDataLib = dp.readDecryptedContent(c, curOffset, blockSize); + if (contentDataLib.length == 0) { + break; + } + + try (FileOutputStream fos = new FileOutputStream("test.1")) { + fos.write(contentDataLib); + } + + byte[] hashLib = HashUtil.hashSHA1(contentDataLib); + + fr.seek(curOffset); + byte[] buffer = new byte[blockSize]; + + int res = fr.read(buffer); + if (res < 0) { + assertTrue(false); + } + buffer = Arrays.copyOf(buffer, res); + try (FileOutputStream fos = new FileOutputStream("test.2")) { + // fos.write(buffer); + } + Assert.assertEquals(res, contentDataLib.length); + + Assert.assertEquals(buffer.length, contentDataLib.length); + + byte[] hashFile = HashUtil.hashSHA1(buffer); + Assert.assertArrayEquals(Utils.ByteArrayToString(hashFile) + " != " + Utils.ByteArrayToString(hashLib), hashLib,hashFile); + + curOffset += blockSize; + totalSize += contentDataLib.length; + //System.out.println(totalSize + " " + (int) ((totalSize * 1.0f / dec.length()) * 100) + "%"); + } + assertEquals(dec.length(), totalSize); + + } catch (NoSuchAlgorithmException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } finally { + fr.close(); + } + } + + } + + @Test + public void HashedFileReadStream() throws IOException { + + String resourceName = "out"; + String resourceName2 = "tmp"; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource(resourceName).getFile()); + File file2 = new File(classLoader.getResource(resourceName2).getFile()); + String absolutePath = file.getAbsolutePath(); + NUSTitle title = null; + try { + title = NUSTitleLoaderLocal.loadNUSTitle(absolutePath, Utils.StringToByteArray("00000000000000000000000000000000")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + NUSDataProcessor dp = title.getDataProcessor(); + Content c = title.getTMD().getContentByID(3); + + File dec = new File(file2.getAbsolutePath() + File.separator + String.format("%08X.dec", c.getID())); + InputStream fileIn = new BufferedInputStream(new FileInputStream(dec)); + + int blockSize = 0x10337; + byte[] buffer = new byte[blockSize]; + try { + int curOffset = 0; + long totalSize = 0; + + InputStream libIn = dp.readPlainDecryptedContentAsStream(c, false); + while (true) { + + int res1 = 0; + while (res1 < blockSize) { + int res = libIn.read(buffer, res1, blockSize - res1); + if (res < 0) { + break; + } + res1 += res; + } + byte[] hashLib = HashUtil.hashSHA1(Arrays.copyOfRange(buffer, 0, res1)); + try (FileOutputStream fos = new FileOutputStream("test.1")) { + fos.write(buffer); + // fos.close(); There is no more need for this line since you had created the instance of "fos" inside the try. And this will automatically + // close the OutputStream + } + int res2 = 0; + while (res2 < blockSize) { + int res = fileIn.read(buffer, res2, blockSize - res2); + if (res < 0) { + break; + } + res2 += res; + } + + try (FileOutputStream fos = new FileOutputStream("test.2")) { + fos.write(buffer); + // fos.close(); There is no more need for this line since you had created the instance of "fos" inside the try. And this will automatically + // close the OutputStream + } + //System.out.println(res1); + byte[] hashFile = HashUtil.hashSHA1(Arrays.copyOfRange(buffer, 0, res2)); + assertEquals(res1, res2); + if (res1 == 0) { + break; + } + //System.out.println(Utils.ByteArrayToString(hashFile) + " " + Utils.ByteArrayToString(hashLib)); + Assert.assertArrayEquals(hashFile, hashLib); + curOffset += blockSize; + totalSize += res1; + //System.out.println(totalSize + " " + totalSize * 1.0f / dec.length()); + } + assertEquals(dec.length(), totalSize); + libIn.close(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + fileIn.close(); + + } + } +} diff --git a/src/test/resources/data/code/1gKpkDwzaCG0.rpx b/src/test/resources/data/code/1gKpkDwzaCG0.rpx new file mode 100644 index 0000000..98645b1 Binary files /dev/null and b/src/test/resources/data/code/1gKpkDwzaCG0.rpx differ diff --git a/src/test/resources/data/content/6s3SQLZDn2SQ b/src/test/resources/data/content/6s3SQLZDn2SQ new file mode 100644 index 0000000..b137dd4 Binary files /dev/null and b/src/test/resources/data/content/6s3SQLZDn2SQ differ diff --git a/src/test/resources/data/content/BxZbREwzgCq5 b/src/test/resources/data/content/BxZbREwzgCq5 new file mode 100644 index 0000000..cdf5ff7 Binary files /dev/null and b/src/test/resources/data/content/BxZbREwzgCq5 differ diff --git a/src/test/resources/data/meta/dummy.txt b/src/test/resources/data/meta/dummy.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/out/00000000.app b/src/test/resources/out/00000000.app new file mode 100644 index 0000000..c11ebbc Binary files /dev/null and b/src/test/resources/out/00000000.app differ diff --git a/src/test/resources/out/00000001.app b/src/test/resources/out/00000001.app new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/out/00000001.h3 b/src/test/resources/out/00000001.h3 new file mode 100644 index 0000000..76be313 --- /dev/null +++ b/src/test/resources/out/00000001.h3 @@ -0,0 +1 @@ + ´ÿ¹²²°)¿ ›Äú+¾c«…n \ No newline at end of file diff --git a/src/test/resources/out/00000002.app b/src/test/resources/out/00000002.app new file mode 100644 index 0000000..05d0412 Binary files /dev/null and b/src/test/resources/out/00000002.app differ diff --git a/src/test/resources/out/00000003.app b/src/test/resources/out/00000003.app new file mode 100644 index 0000000..46b7543 Binary files /dev/null and b/src/test/resources/out/00000003.app differ diff --git a/src/test/resources/out/00000003.h3 b/src/test/resources/out/00000003.h3 new file mode 100644 index 0000000..24cfbe7 --- /dev/null +++ b/src/test/resources/out/00000003.h3 @@ -0,0 +1 @@ +úßW(Ä©!li˜1°VðÑŽ \ No newline at end of file diff --git a/src/test/resources/out/title.cert b/src/test/resources/out/title.cert new file mode 100644 index 0000000..76fc338 Binary files /dev/null and b/src/test/resources/out/title.cert differ diff --git a/src/test/resources/out/title.tik b/src/test/resources/out/title.tik new file mode 100644 index 0000000..37fdf83 Binary files /dev/null and b/src/test/resources/out/title.tik differ diff --git a/src/test/resources/out/title.tmd b/src/test/resources/out/title.tmd new file mode 100644 index 0000000..432becd Binary files /dev/null and b/src/test/resources/out/title.tmd differ diff --git a/src/test/resources/out_dec/00000000.app b/src/test/resources/out_dec/00000000.app new file mode 100644 index 0000000..3f634d3 Binary files /dev/null and b/src/test/resources/out_dec/00000000.app differ diff --git a/src/test/resources/out_dec/00000000.app.bak b/src/test/resources/out_dec/00000000.app.bak new file mode 100644 index 0000000..52af55c Binary files /dev/null and b/src/test/resources/out_dec/00000000.app.bak differ diff --git a/src/test/resources/out_dec/00000001.app b/src/test/resources/out_dec/00000001.app new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/out_dec/00000001.h3 b/src/test/resources/out_dec/00000001.h3 new file mode 100644 index 0000000..76be313 --- /dev/null +++ b/src/test/resources/out_dec/00000001.h3 @@ -0,0 +1 @@ + ´ÿ¹²²°)¿ ›Äú+¾c«…n \ No newline at end of file diff --git a/src/test/resources/out_dec/00000002.app b/src/test/resources/out_dec/00000002.app new file mode 100644 index 0000000..98645b1 Binary files /dev/null and b/src/test/resources/out_dec/00000002.app differ diff --git a/src/test/resources/out_dec/00000003.app b/src/test/resources/out_dec/00000003.app new file mode 100644 index 0000000..e866ed8 Binary files /dev/null and b/src/test/resources/out_dec/00000003.app differ diff --git a/src/test/resources/out_dec/00000003.h3 b/src/test/resources/out_dec/00000003.h3 new file mode 100644 index 0000000..24cfbe7 --- /dev/null +++ b/src/test/resources/out_dec/00000003.h3 @@ -0,0 +1 @@ +úßW(Ä©!li˜1°VðÑŽ \ No newline at end of file diff --git a/src/test/resources/out_dec/title.cert b/src/test/resources/out_dec/title.cert new file mode 100644 index 0000000..76fc338 Binary files /dev/null and b/src/test/resources/out_dec/title.cert differ diff --git a/src/test/resources/out_dec/title.tik b/src/test/resources/out_dec/title.tik new file mode 100644 index 0000000..a00745d Binary files /dev/null and b/src/test/resources/out_dec/title.tik differ diff --git a/src/test/resources/out_dec/title.tmd b/src/test/resources/out_dec/title.tmd new file mode 100644 index 0000000..9f74059 Binary files /dev/null and b/src/test/resources/out_dec/title.tmd differ diff --git a/src/test/resources/tmp/00000001.dec b/src/test/resources/tmp/00000001.dec new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/tmp/00000002.dec b/src/test/resources/tmp/00000002.dec new file mode 100644 index 0000000..98645b1 Binary files /dev/null and b/src/test/resources/tmp/00000002.dec differ diff --git a/src/test/resources/tmp/00000003.dec b/src/test/resources/tmp/00000003.dec new file mode 100644 index 0000000..cc896b5 Binary files /dev/null and b/src/test/resources/tmp/00000003.dec differ