public void BTRIEVE_EXE() { var stdoutStream = new MemoryStream(); var stdout = new TextWriterStream(new StreamWriter(stdoutStream)); CopyModuleToTempPath(ResourceManager.GetTestResourceManager()); ExeRuntime exeRuntime = new ExeRuntime( new MZFile(Path.Combine(_modulePath, _runtimeFiles[0])), _serviceResolver.GetService <IClock>(), _serviceResolver.GetService <ILogger>(), _serviceResolver.GetService <IFileUtility>(), _modulePath, null, new TextReaderStream(Console.In), stdout, stdout); exeRuntime.Load(new string[] { Path.Combine(_modulePath, _runtimeFiles[1]) }); exeRuntime.Run(); stdout.Flush(); stdoutStream.Seek(0, SeekOrigin.Begin); var output = Encoding.ASCII.GetString(stdoutStream.ToArray()); output.Should().Be(GetExpectedOutput(Path.Combine(_modulePath, _runtimeFiles[1]))); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Run the export /// </summary> /// ------------------------------------------------------------------------------------ public void Run() { // Check whether we're about to overwrite an existing file. if (File.Exists(m_fileName)) { string sFmt = DlgResources.ResourceString("kstidAlreadyExists"); string sMsg = String.Format(sFmt, m_fileName); string sCaption = DlgResources.ResourceString("kstidExportXHTML"); if (MessageBox.Show(sMsg, sCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { return; } } try { try { m_writer = new StreamWriter(m_fileName, false, Encoding.UTF8); m_strm = new TextWriterStream(m_writer); m_xhtml = new XhtmlHelper(m_writer, m_cache); } catch (Exception e) { MessageBox.Show(e.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } ExportTE(); } catch (Exception e) { Exception inner = e.InnerException != null ? e.InnerException : e; if (inner is IOException) { MessageBox.Show(inner.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { throw inner; } } finally { if (m_writer != null) { try { m_writer.Close(); } catch { // ignore errors on close } } m_writer = null; } }
private static void RedirectConsoleOutput( TextWriter writer, ScriptRuntime runtime) { var stream = new TextWriterStream(writer); runtime.IO.SetOutput(stream, writer.Encoding); runtime.IO.SetErrorOutput(stream, writer.Encoding); }
public void Write_WriteToStream_OutputRedirectToTextWriter_ThenOutToStringBuilder() { StringBuilder outputString = new StringBuilder(); TextWriter writer = new StringWriter(outputString); Stream textWriter = new TextWriterStream(writer); byte[] hello = Encoding.Default.GetBytes("Hello World"); textWriter.Write(hello, 0, hello.Length); Assert.AreEqual("Hello World", outputString.ToString()); }
/// <summary> /// Write the closing element end tag to the output. /// </summary> /// <param name="sDataType"></param> public void Finish(string sDataType) { if (m_sFormat == "xhtml") { if (m_schCurrent.Length > 0) { m_writer.WriteLine("</div>"); // for letData } m_xhtml.WriteXhtmlEnding(); } else { m_writer.WriteLine("</{0}>", sDataType); } m_writer.Close(); m_strm = null; m_writer = null; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Initialize the object with some useful information, and write the initial /// element start tag to the output. /// </summary> /// <param name="cache">The cache.</param> /// <param name="w">The w.</param> /// <param name="sDataType">Type of the s data.</param> /// <param name="sFormat">The s format.</param> /// <param name="sOutPath">The s out path.</param> /// ------------------------------------------------------------------------------------ public void Initialize(FdoCache cache, TextWriter w, string sDataType, string sFormat, string sOutPath) { m_writer = w; m_strm = new TextWriterStream(w); m_cache = cache; m_sFormat = sFormat.ToLowerInvariant(); m_fUseRFC4646 = m_sFormat == "xhtml" || m_sFormat == "lift"; m_xhtml = new XhtmlHelper(w, cache); if (m_sFormat == "xhtml") { m_xhtml.WriteXhtmlHeading(sOutPath, null, "dicBody"); } else { w.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); w.WriteLine("<{0}>", sDataType); } }
public override void Execute(string[] commandline) { if (commandline.Length < 2) { _console.Out.WriteLine("Error: [local_session_alias] not specified"); return; } else if (commandline.Length < 3) { _console.Out.WriteLine("Error: [command] not specified"); return; } ClientContext ctx = _console.GetValue <ClientContext> ("client_context", null); if (ctx == null) { _console.Out.WriteLine("No active connection was found"); return; } string localAlias = commandline[1]; IDictionary <string, TPMSession> tpmSessions = _console.GetValue <IDictionary <string, TPMSession> > ("tpm_sessions", null); if (tpmSessions == null || tpmSessions.ContainsKey(localAlias) == false) { _console.Out.WriteLine("Error: Specified local alias was not found"); return; } if (tpmSessions[localAlias].Keystore == null) { _console.Out.WriteLine("Error: No keystore was opened"); return; } IDictionary <string, string> arguments = _console.SplitArguments(commandline[2], 0); if (arguments.ContainsKey("name") == false) { _console.Out.WriteLine("Error: no key name was specified"); return; } if (arguments.ContainsKey("data_input") == false) { _console.Out.WriteLine("Error: no data input source specified"); return; } TPMSessionSealCommand.DataInputMode dataInputMode; try { dataInputMode = (TPMSessionSealCommand.DataInputMode)Enum.Parse(typeof(TPMSessionSealCommand.DataInputMode), arguments["data_input"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid data input source"); return; } TPMSessionSealCommand.DataOutputMode dataOutputMode; try { dataOutputMode = (TPMSessionSealCommand.DataOutputMode)Enum.Parse(typeof(TPMSessionSealCommand.DataOutputMode), arguments["data_output"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid data output destination"); return; } TPMSessionSealCommand.DataFormat inputDataFormat = TPMSessionSealCommand.DataFormat.Raw; if (arguments.ContainsKey("input_data_format")) { try { inputDataFormat = (TPMSessionSealCommand.DataFormat)Enum.Parse(typeof(TPMSessionSealCommand.DataFormat), arguments["input_data_format"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid input data format"); return; } } TPMSessionSealCommand.DataFormat outputDataFormat = TPMSessionSealCommand.DataFormat.Raw; if (arguments.ContainsKey("output_data_format")) { try { outputDataFormat = (TPMSessionSealCommand.DataFormat)Enum.Parse(typeof(TPMSessionSealCommand.DataFormat), arguments["output_data_format"], true); } catch (Exception) { _console.Out.WriteLine("Error: Invalid output data format"); return; } } if (dataInputMode == TPMSessionSealCommand.DataInputMode.File && arguments.ContainsKey("file") == false) { _console.Out.WriteLine("Error: data_input=file requires file argument!"); return; } if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.File && arguments.ContainsKey("output_file") == false) { _console.Out.WriteLine("Error: data_output=file requires output_file argument!"); return; } ClientKeyHandle keyHandle = tpmSessions[localAlias].KeyClient.GetKeyHandleByFriendlyName(arguments["name"]); Stream inputStream = null; if (dataInputMode == TPMSessionSealCommand.DataInputMode.Console) { inputStream = new TextReaderStream(_console.In); } else if (dataInputMode == TPMSessionSealCommand.DataInputMode.Embedded) { if (commandline.Length <= 3) { _console.Out.WriteLine("Error: no embedded data"); return; } StringBuilder embeddedData = new StringBuilder(); for (int i = 3; i < commandline.Length; i++) { embeddedData.Append(commandline[i]); if (i + 1 < commandline.Length) { embeddedData.Append(" "); } } inputStream = new TextReaderStream(new StringReader(embeddedData.ToString())); } else if (dataInputMode == TPMSessionSealCommand.DataInputMode.File) { inputStream = new FileStream(arguments["file"], FileMode.Open, FileAccess.Read); } if (inputDataFormat == TPMSessionSealCommand.DataFormat.Hex) { inputStream = new HexFilterStream(inputStream); } Stream outputStream = null; if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.Console) { outputStream = new TextWriterStream(_console.Out); } else if (dataOutputMode == TPMSessionSealCommand.DataOutputMode.File) { outputStream = new FileStream(arguments["output_file"], FileMode.OpenOrCreate, FileAccess.Write); } if (outputDataFormat == TPMSessionSealCommand.DataFormat.Hex) { outputStream = new HexFilterStream(outputStream); } IAsymmetricBlockCipher bindCipher = keyHandle.CreateBindBlockCipher(); bindCipher.Init(false, null); int read; byte[] buffer = new byte[bindCipher.GetInputBlockSize()]; do { read = inputStream.Read(buffer, 0, buffer.Length); if (read > 0) { byte[] encrypted = bindCipher.ProcessBlock(buffer, 0, read); outputStream.Write(encrypted, 0, encrypted.Length); } }while(read > 0); _console.Out.WriteLine(); outputStream.Dispose(); inputStream.Dispose(); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Run the export /// </summary> /// ------------------------------------------------------------------------------------ public void Run() { // Check whether we're about to overwrite an existing file. if (File.Exists(m_fileName)) { string sFmt = DlgResources.ResourceString("kstidAlreadyExists"); string sMsg = String.Format(sFmt, m_fileName); string sCaption = DlgResources.ResourceString("kstidExportXHTML"); if (MessageBox.Show(sMsg, sCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { return; } } try { try { m_writer = new StreamWriter(m_fileName, false, Encoding.UTF8); m_strm = new TextWriterStream(m_writer); m_xhtml = new XhtmlHelper(m_writer, m_cache); } catch (Exception e) { MessageBox.Show(e.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } ExportTE(); } catch (Exception e) { Exception inner = e.InnerException != null ? e.InnerException : e; if (inner is IOException) { MessageBox.Show(inner.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else throw inner; } finally { if (m_writer != null) { try { m_writer.Close(); } catch { // ignore errors on close } } m_writer = null; } }
/// <summary> /// Write the closing element end tag to the output. /// </summary> /// <param name="sDataType"></param> public void Finish(string sDataType) { if (m_sFormat == "xhtml") { if (m_schCurrent.Length > 0) m_writer.WriteLine("</div>"); // for letData m_xhtml.WriteXhtmlEnding(); } else { m_writer.WriteLine("</{0}>", sDataType); } m_writer.Close(); m_strm = null; m_writer = null; }