public void Base64Encode() { EngineState s = EngineTests.CreateEngineState(); void Template(string binFileName, string encFileName) { string workDir = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "EncodedFile")); // Be careful! encFile will be converted to LF from CRLF in git tree! string binFile = Path.Combine(workDir, binFileName); string encFile = Path.Combine(workDir, encFileName); string compStr; StringBuilder b = new StringBuilder(); using (StreamReader r = new StreamReader(encFile, Encoding.UTF8)) { string rawLine; while ((rawLine = r.ReadLine()) != null) { b.AppendLine(rawLine); } compStr = b.ToString(); } using (FileStream fs = new FileStream(binFile, FileMode.Open, FileAccess.Read, FileShare.Read)) using (StringWriter sw = new StringWriter()) { SplitBase64.Encode(fs, sw); string result = sw.ToString(); Assert.IsTrue(result.Equals(compStr, StringComparison.Ordinal)); } } Template("BigData.bin", "BigDataEnc4090.txt"); Template("Type3.pdf", "Type3Enc4090.txt"); Template("Type5.bin", "Type5Enc4090.txt"); }
public void Base64Decode() { EngineState s = EngineTests.CreateEngineState(); void Template(string binFileName, string encFileName, string section, bool inMem) { string workDir = StringEscaper.Preprocess(s, Path.Combine("%TestBench%", "EncodedFile")); // Be careful! encFile will be converted to LF from CRLF in git tree! string binFile = Path.Combine(workDir, binFileName); string encFile = Path.Combine(workDir, encFileName); byte[] binDigest; byte[] encDigest; using (FileStream fs = new FileStream(binFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { binDigest = HashHelper.GetHash(HashHelper.HashType.SHA256, fs); } if (inMem) { List <string> lines = new List <string>(); using (StreamReader r = new StreamReader(encFile, Encoding.UTF8)) { string rawLine; while ((rawLine = r.ReadLine()) != null) { string line = rawLine.Trim(); if (0 < line.Length) { lines.Add(line); } } } byte[] decoded = SplitBase64.DecodeInMem(lines); #if DEBUG_MIDDLE_FILE using (FileStream fs = new FileStream(binFile + ".inMem.comp", FileMode.Create, FileAccess.Write)) { fs.Write(decoded, 0, decoded.Length); } #endif encDigest = HashHelper.GetHash(HashHelper.HashType.SHA256, decoded); } else { using (MemoryStream ms = new MemoryStream()) { using (StreamReader tr = new StreamReader(encFile, Encoding.UTF8)) { if (section.Length != 0) { IniReadWriter.FastForwardTextReader(tr, section); } SplitBase64.Decode(tr, ms); } ms.Position = 0; #if DEBUG_MIDDLE_FILE using (FileStream fs = new FileStream(binFile + ".noMem.comp", FileMode.Create, FileAccess.Write)) { ms.CopyTo(fs); } ms.Position = 0; #endif encDigest = HashHelper.GetHash(HashHelper.HashType.SHA256, ms); } } Assert.IsTrue(binDigest.SequenceEqual(encDigest)); } Template("BigData.bin", "BigDataEnc4090.txt", string.Empty, true); Template("BigData.bin", "BigDataEnc4090.txt", string.Empty, false); Template("BigData.bin", "BigDataEnc4090S.txt", "Base64", false); Template("Type3.pdf", "Type3Enc4090.txt", string.Empty, true); Template("Type3.pdf", "Type3Enc4090.txt", string.Empty, false); Template("Type3.pdf", "Type3Enc4090S.txt", "Base64", false); Template("Type3.pdf", "Type3Enc1024.txt", string.Empty, true); Template("Type3.pdf", "Type3Enc1024.txt", string.Empty, false); Template("Type3.pdf", "Type3Enc1024S.txt", "Base64", false); // https://github.com/pebakery/pebakery/issues/90 Template("Type5.bin", "Type5Enc4090.txt", string.Empty, true); Template("Type5.bin", "Type5Enc4090.txt", string.Empty, false); }