public void EDCompressionTest() { byte[] filebytes = new byte[1024]; RandomNumberGenerator random = new RNGCryptoServiceProvider(); random.GetBytes(filebytes); ED t = new ED(filebytes, "application/bin"); t.Compression = EncapsulatedDataCompression.GZ; t.Data = t.Compress(EncapsulatedDataCompression.GZ); t.Data = t.UnCompress(); Assert.AreEqual(filebytes.Length, t.Data.Length); for (int i = 0; i < filebytes.Length; i++) { Assert.AreEqual(filebytes[i], t.Data[i]); } }
public void EDCompressionTest() { byte[] filebytes = new byte[1024]; RandomNumberGenerator random = new RNGCryptoServiceProvider(); random.GetBytes(filebytes); ED t = new ED(filebytes, "application/bin"); t.Compression = EncapsulatedDataCompression.GZ; t.Data = t.Compress(EncapsulatedDataCompression.GZ); t.Data = t.UnCompress(); Assert.AreEqual(filebytes.Length, t.Data.Length); for (int i = 0; i < filebytes.Length; i++) Assert.AreEqual(filebytes[i], t.Data[i]); }
public override object Parse(System.Xml.XmlReader s, DatatypeFormatterParseResult result) { // Parse base (ANY) from the stream string pathName = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name; // Parse ED ED retVal = base.Parse <ED>(s, result); // Now parse our data out... Attributes if (s.GetAttribute("representation") != null) { retVal.Representation = (EncapsulatedDataRepresentation)Util.FromWireFormat(s.GetAttribute("representation"), typeof(EncapsulatedDataRepresentation)); } if (s.GetAttribute("mediaType") != null) { retVal.MediaType = s.GetAttribute("mediaType"); } if (s.GetAttribute("language") != null) { retVal.Language = s.GetAttribute("language"); } if (s.GetAttribute("compression") != null) { retVal.Compression = (EncapsulatedDataCompression?)Util.FromWireFormat(s.GetAttribute("compression"), typeof(EncapsulatedDataCompression)); } if (s.GetAttribute("integrityCheckAlgorithm") != null) { switch (s.GetAttribute("integrityCheckAlgorithm")) { case "SHA-1": retVal.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; break; case "SHA-256": retVal.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA256; break; } } if (s.GetAttribute("integrityCheck") != null) { retVal.IntegrityCheck = Convert.FromBase64String(s.GetAttribute("integrityCheck")); } // Elements and inner data #region Elements StringBuilder innerData = new StringBuilder(); if (!s.IsEmptyElement) { // Exit markers int sDepth = s.Depth; string sName = s.Name; s.Read(); // Read until exit condition is fulfilled while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName)) { string oldName = s.Name; // Name try { if (s.LocalName == "thumbnail") // Format using ED { var hostResult = this.Host.Parse(s, typeof(ED)); result.AddResultDetail(hostResult.Details); retVal.Thumbnail = (ED)hostResult.Structure; // Parse ED } else if (s.LocalName == "reference") // Format using TEL { var hostResult = this.Host.Parse(s, typeof(TEL)); result.AddResultDetail(hostResult.Details); retVal.Reference = (TEL)hostResult.Structure; } else if (s.NodeType == System.Xml.XmlNodeType.Text || s.NodeType == System.Xml.XmlNodeType.CDATA) { innerData.Append(s.Value); } else if (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName) && (s.NodeType == System.Xml.XmlNodeType.Element || s.NodeType == System.Xml.XmlNodeType.EndElement)) { retVal.Representation = EncapsulatedDataRepresentation.XML; innerData.Append(s.ReadOuterXml()); } } catch (MessageValidationException e) { result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, s.ToString(), e)); } finally { if (s.Name == oldName) { s.Read(); } } } } #endregion Encoding textEncoding = System.Text.Encoding.UTF8; // Parse the innerData string into something meaningful if (innerData.Length > 0) { if (retVal.Representation == EncapsulatedDataRepresentation.B64) { retVal.Data = Convert.FromBase64String(innerData.ToString()); } else { retVal.Data = textEncoding.GetBytes(innerData.ToString()); } } // Finally, the hash, this will validate the data #if !WINDOWS_PHONE if (!retVal.ValidateIntegrityCheck()) { if (retVal.Compression == null || !retVal.UnCompress().ValidateIntegrityCheck()) { result.AddResultDetail(new ResultDetail(ResultDetailType.Warning, string.Format("Encapsulated data with content starting with '{0}' failed integrity check!", retVal.ToString().PadRight(10, ' ').Substring(0, 10)), s.ToString(), null)); } } #endif // Validate base.Validate(retVal, pathName, result); return(retVal); }