Microsoft.Protocols.TestSuites.SharedAdapter.FsshttpbResponse.DeserializeResponseFromByteArray C# (CSharp) Méthode

DeserializeResponseFromByteArray() public static méthode

Deserialize response from byte array.
public static DeserializeResponseFromByteArray ( byte byteArray, int startIndex ) : FsshttpbResponse
byteArray byte Server returned message.
startIndex int The index special where start.
Résultat FsshttpbResponse
        public static FsshttpbResponse DeserializeResponseFromByteArray(byte[] byteArray, int startIndex)
        {
            int index = startIndex;

            FsshttpbResponse response = new FsshttpbResponse();

            response.ProtocolVersion = LittleEndianBitConverter.ToUInt16(byteArray, index);
            index += 2;

            response.MinimumVersion = LittleEndianBitConverter.ToUInt16(byteArray, index);
            index += 2;

            response.Signature = LittleEndianBitConverter.ToUInt64(byteArray, index);
            index += 8;

            int length = 0;
            StreamObjectHeaderStart streamObjectHeader;
            if ((length = StreamObjectHeaderStart.TryParse(byteArray, index, out streamObjectHeader)) == 0)
            {
                throw new ResponseParseErrorException(index, "Failed to parse the response header", null);
            }

            if (!(streamObjectHeader is StreamObjectHeaderStart32bit))
            {
                throw new ResponseParseErrorException(index, "Unexpected 16-bit response stream object header, expect 32-bit stream object header for Response", null);
            }

            if (streamObjectHeader.Type != StreamObjectTypeHeaderStart.FsshttpbResponse)
            {
                throw new ResponseParseErrorException(index, "Failed to extract the response header type, unexpected value " + streamObjectHeader.Type, null);
            }

            if (streamObjectHeader.Length != 1)
            {
                throw new ResponseParseErrorException(index, "Response object over-parse error", null);
            }

            index += length;
            response.ResponseStart = streamObjectHeader as StreamObjectHeaderStart32bit;

            response.Status = (byteArray[index] & 0x1) == 0x1 ? true : false;
            response.Reserved = (byte)(byteArray[index] >> 1);
            index += 1;

            try
            {
                if (response.Status)
                {
                    response.ResponseError = StreamObject.GetCurrent<ResponseError>(byteArray, ref index);
                    response.DataElementPackage = null;
                    response.CellSubResponses = null;
                }
                else
                {
                    DataElementPackage package;
                    if (StreamObject.TryGetCurrent<DataElementPackage>(byteArray, ref index, out package))
                    {
                        response.DataElementPackage = package;
                    }

                    response.CellSubResponses = new List<FsshttpbSubResponse>();
                    FsshttpbSubResponse subResponse;
                    while (StreamObject.TryGetCurrent<FsshttpbSubResponse>(byteArray, ref index, out subResponse))
                    {
                        response.CellSubResponses.Add(subResponse);
                    }
                }

                response.ResponseEnd = BasicObject.Parse<StreamObjectHeaderEnd16bit>(byteArray, ref index);
            }
            catch (StreamObjectParseErrorException streamObjectException)
            {
                throw new ResponseParseErrorException(index, streamObjectException);
            }
            catch (DataElementParseErrorException dataElementException)
            {
                throw new ResponseParseErrorException(index, dataElementException);
            }
            catch (KnowledgeParseErrorException knowledgeException)
            {
                throw new ResponseParseErrorException(index, knowledgeException);
            }

            if (index != byteArray.Length)
            {
                throw new ResponseParseErrorException(index, "Failed to pass the whole response, not reach the end of the byte array", null);
            }

            return response;
        }
    }

Usage Example

        /// <summary>
        /// This method is used to validate the sub response according to the current record sub request token and sub request type.
        /// </summary>
        /// <param name="rawResponse">Specify the raw XML string response returned by the protocol server.</param>
        /// <param name="site">An object provides logging, assertions, and SUT adapters for test code onto its execution context.</param>
        public void Validate(string rawResponse, ITestSite site)
        {
            // Extract the sub response whose token equals the SubToken value.
            XmlDocument subResponseDocument = this.ExtractSubResponseNode(rawResponse);

            // De-serialize the sub response to instance
            object subResponse = this.SerializeSubResponse(subResponseDocument, site);

            // Try to parse the MS-FSSHTTPB structure
            if (subResponse is CellSubResponseType)
            {
                // If the sub request type is CellSubRequestType, then indicating that there is one MS-FSSHTTPB response embedded. Try parse this an capture all the related requirements.
                CellSubResponseType cellSubResponse = subResponse as CellSubResponseType;
                if (cellSubResponse.SubResponseData != null && cellSubResponse.SubResponseData.Text.Length == 1)
                {
                    string           subResponseBase64 = cellSubResponse.SubResponseData.Text[0];
                    byte[]           subResponseBinary = Convert.FromBase64String(subResponseBase64);
                    FsshttpbResponse fsshttpbResponse  = FsshttpbResponse.DeserializeResponseFromByteArray(subResponseBinary, 0);

                    if (fsshttpbResponse.DataElementPackage != null && fsshttpbResponse.DataElementPackage.DataElements != null)
                    {
                        // If the response data elements is complete, then try to verify the requirements related in the MS-FSSHTPD
                        foreach (DataElement storageIndex in fsshttpbResponse.DataElementPackage.DataElements.Where(dataElement => dataElement.DataElementType == DataElementType.StorageIndexDataElementData))
                        {
                            // Just build the root node to try to parse the signature related requirements, no need to restore the result.
                            new RootNodeObject.RootNodeObjectBuilder().Build(
                                fsshttpbResponse.DataElementPackage.DataElements,
                                storageIndex.DataElementExtendedGUID);
                        }
                    }

                    if (SharedContext.Current.IsMsFsshttpRequirementsCaptured)
                    {
                        new MsfsshttpbAdapterCapture().VerifyTransport(site);

                        // Capture the response related requirements
                        new MsfsshttpbAdapterCapture().VerifyFsshttpbResponse(fsshttpbResponse, site);
                    }
                }
            }

            // Validating the fragment of the sub response
            // Record the validation errors and warnings.
            ValidationResult result = SchemaValidation.ValidateXml(subResponseDocument.OuterXml);

            if (!SharedContext.Current.IsMsFsshttpRequirementsCaptured)
            {
                if (result != ValidationResult.Success)
                {
                    // Add error log
                    site.Assert.Fail("Schema validation fails, the reason is " + SchemaValidation.GenerateValidationResult());
                }

                // No need to run the capture code, just return.
                return;
            }

            if (result == ValidationResult.Success)
            {
                // Capture the requirement related to the sub response token.
                MsfsshttpAdapterCapture.ValidateSubResponseToken(site);

                // Call corresponding sub response capture code.
                this.InvokeCaptureCode(subResponse, site);
            }
            else
            {
                // Add error log
                site.Assert.Fail("Schema validation fails, the reason is " + SchemaValidation.GenerateValidationResult());
            }
        }