/// <summary> /// Helper method to load a disc FILE into a MemoryStream (as unicode string) /// </summary> /// <param name="filePath">The path to the FILE containing the data</param> /// <returns>MemoryStream containing the data in the FILE</returns> public static MemoryStream LoadFileToStream(string filePath) { var ms = StreamHelper1.LoadFileToStream(filePath); var strm = StreamHelper1.EncodeStream(ms, Encoding.Unicode); return(StreamHelper1.LoadMemoryStream(strm)); }
/// <summary> /// IValidationStep.ExecuteValidation() implementation /// </summary> /// <param name='data'>The stream cintaining the data to be validated.</param> /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param> public override Stream Execute(Stream data, Context context) { MemoryStream dataToValidateAgainst = null; try { try { if (ReadAsString) { dataToValidateAgainst = StreamHelper.LoadMemoryStream(Common.StreamHelper.LoadFileToString(_comparisonDataPath)); } else { dataToValidateAgainst = !ReadAsUnicode?StreamHelper.LoadFileToStream(_comparisonDataPath) : Common.StreamHelper.LoadFileToStream(_comparisonDataPath); } } catch (Exception e) { context.LogError("BinaryValidationStep failed, exception caugh trying to open comparison file: {0}", _comparisonDataPath); context.LogException(e); throw; } try { data.Seek(0, SeekOrigin.Begin); dataToValidateAgainst.Seek(0, SeekOrigin.Begin); if (_compareAsUtf8) { // Compare the streams, make sure we are comparing like for like StreamHelper.CompareStreams(StreamHelper.EncodeStream(data, System.Text.Encoding.UTF8), StreamHelper.EncodeStream(dataToValidateAgainst, System.Text.Encoding.UTF8)); } else { StreamHelper.CompareStreams(data, dataToValidateAgainst); } } catch (Exception e) { context.LogError("Binary validation failed while comparing the two data streams with the following exception: {0}", e.ToString()); // Dump out streams for validation... data.Seek(0, SeekOrigin.Begin); dataToValidateAgainst.Seek(0, SeekOrigin.Begin); context.LogData("Stream 1:", data); context.LogData("Stream 2:", dataToValidateAgainst); throw; } } finally { if (null != dataToValidateAgainst) { dataToValidateAgainst.Close(); } } return(data); }