示例#1
0
        protected void DoMux(PayloadConfiguration payloadConfig, List <PayloadItem> items, List <FileInfo> files, bool outputPayload = false)
        {
            var ms = new MemoryStream();

            for (var index = 0; index < items.Count; index++)
            {
                var index1 = index;
                items[index].SetStreamBinding(() => new FileStream(files[index1].FullName, FileMode.Open));
                items[index].ExternalLength = items[index].StreamBinding.Length;
            }

            var itemPreKeys = new Dictionary <Guid, byte[]> ();

            var mux = PayloadMuxFactory.CreatePayloadMultiplexer(payloadConfig.SchemeName.ToEnum <PayloadLayoutScheme>(), true, ms,
                                                                 items, itemPreKeys, payloadConfig);

            Assert.DoesNotThrow(mux.Execute);

            Debug.Print("\n##### END OF MUXING OPERATION #####\n");

            foreach (var item in items)
            {
                item.StreamBinding.Close();
            }

            // Write out muxed payload - optional
            if (outputPayload)
            {
                if (!DestinationDirectory.Exists)
                {
                    DestinationDirectory.Create();
                }
                var path = DestinationDirectory.FullName + Path.DirectorySeparatorChar +
                           payloadConfig.SchemeName.ToLower() + IOTestBase.RawPayloadExtension;
                using (var fs = new FileStream(path, FileMode.Create)) {
                    ms.WriteTo(fs);
                }
            }

            // DEMUX

            var demuxPath = DestinationDirectory.FullName + Path.DirectorySeparatorChar + DemuxDir;

            if (!Directory.Exists(demuxPath))
            {
                Directory.CreateDirectory(demuxPath);
            }
            foreach (var payloadItem in items)
            {
                PayloadItem item = payloadItem;
                payloadItem.SetStreamBinding(() => new FileStream(demuxPath + Path.DirectorySeparatorChar + item.Path, FileMode.Create));
            }

            ms.Seek(0, SeekOrigin.Begin);
            mux = PayloadMuxFactory.CreatePayloadMultiplexer(payloadConfig.SchemeName.ToEnum <PayloadLayoutScheme>(), false, ms,
                                                             items, itemPreKeys, payloadConfig);

            Assert.DoesNotThrow(mux.Execute);

            Debug.Print("\n##### END OF DEMUXING OPERATION #####\n");

            foreach (var item in items)
            {
                item.StreamBinding.Close();
            }

            if (mux is FrameshiftPayloadMux)
            {
                Assert.Pass("Overhead: {0} bytes", ((FrameshiftPayloadMux)mux).Overhead);
            }
            else
            {
                Assert.Pass();
            }
        }
示例#2
0
        /// <summary>
        ///     Read the payload.
        /// </summary>
        /// <remarks>
        ///     All payload items to be read must have have valid stream bindings
        ///     (<see cref="PayloadItem.StreamBinding" />) prior to calling this.
        /// </remarks>
        /// <param name="payloadKeys">Potential keys for payload items (optional).</param>
        /// <exception cref="KeyConfirmationException">Key confirmation for payload items failed.</exception>
        /// <exception cref="ConfigurationInvalidException">Payload layout scheme malformed/missing.</exception>
        /// <exception cref="InvalidDataException">Package data structure malformed.</exception>
        /// <exception cref="EndOfStreamException">Package is truncated or otherwise shorter than expected.</exception>
        private void ReadPayload(IEnumerable <SymmetricKey> payloadKeys = null)
        {
            if (_readingPayloadStreamOffset != 0 && _readingStream.Position != _readingPayloadStreamOffset)
            {
                _readingStream.Seek(_readingPayloadStreamOffset, SeekOrigin.Begin);
            }
            Debug.Print(DebugUtility.CreateReportString("PackageReader", "Read", "Payload offset (absolute)",
                                                        _readingStream.Position));

            // Check that all payload items have decryption keys - if they do not, confirm them from potentials
            try {
                ConfirmItemPreKeys(payloadKeys);
            } catch (Exception e) {
                throw new KeyConfirmationException("Error in key confirmation of payload items.", e);
            }

            // Read the payload
            PayloadMux mux;

            try {
                var payloadScheme = _manifest.PayloadConfiguration.SchemeName.ToEnum <PayloadLayoutScheme>();
                mux = PayloadMuxFactory.CreatePayloadMultiplexer(payloadScheme, false, _readingStream,
                                                                 _manifest.PayloadItems,
                                                                 _itemPreKeys, _manifest.PayloadConfiguration);
            } catch (EnumerationParsingException e) {
                throw new ConfigurationInvalidException(
                          "Payload layout scheme specified is unsupported/unknown or missing.", e);
            } catch (Exception e) {
                throw new Exception("Error in creation of payload demultiplexer.", e);
            }

            // Demux the payload
            try {
                mux.Execute();
            } catch (Exception e) {
                // Catch different kinds of exception in future
                throw new Exception("Error in demultiplexing payload.", e);
            }

            // Read the trailer
            byte[] referenceTrailerTag = Athena.Packaging.GetPackageTrailerTag();
            var    trailerTag          = new byte[referenceTrailerTag.Length];

            Debug.Print(DebugUtility.CreateReportString("PackageReader", "ReadPayload", "Trailer offset (absolute)",
                                                        _readingStream.Position));
            int trailerBytesRead = _readingStream.Read(trailerTag, 0, trailerTag.Length);

            if (trailerTag.SequenceEqualShortCircuiting(referenceTrailerTag) == false)
            {
                const string pragmatist =
                    "It would appear, however, that the package has unpacked successfully despite this.";
                if (trailerBytesRead != referenceTrailerTag.Length)
                {
                    throw new EndOfStreamException("Insufficient data to read package trailer tag. " + pragmatist);
                }
                throw new InvalidDataException("Package is malformed. Trailer tag is either absent or malformed."
                                               + pragmatist);
            }

            Debug.Print(DebugUtility.CreateReportString("PackageReader", "ReadPayload",
                                                        "[* PACKAGE END *] Offset (absolute)", _readingStream.Position));
        }