public void HostedCacheServer_PccrrServer_GetBlocks_BlockRangesEmpty()
        {
            CheckApplicability();

            var contentInformation = contentInformationUtility.CreateContentInformationV1();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Send GetBlocks request with no block range to hosted cache server");

            PccrrClient pccrrClient = new PccrrClient(
                testConfig.HostedCacheServerComputerName,
                testConfig.HostedCacheServerHTTPListenPort);

            PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                contentInformation.GetSegmentId(0),
                CryptoAlgoId_Values.AES_128,
                MsgType_Values.MSG_GETBLKS,
                0,
                0);

            pccrrClient.SendPacket(
                pccrrBlkRequest,
                testConfig.Timeout);
            PccrrBLKResponsePacket pccrrBlkResponse
                = (PccrrBLKResponsePacket)pccrrClient.ExpectPacket();

            BaseTestSite.Assert.AreEqual(
                0,
                pccrrBlkResponse.MsgBLK.Block.Length,
                "Hosted cache server should not return any block since no block is requested");
        }
            protected override void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest)
            {
                uint offset       = 0;
                int  chunkIndex   = -1;
                int  segmentIndex = -1;

                for (int i = 0; i < contentInformationV2.chunks.Length; i++)
                {
                    var chunk = contentInformationV2.chunks[i];
                    for (int j = 0; j < chunk.chunkData.Length; j++)
                    {
                        if (Enumerable.SequenceEqual(contentInformationV2.GetSegmentId(i, j), pccrrGetBlkRequest.MsgGetBLKS.SegmentID))
                        {
                            chunkIndex   = i;
                            segmentIndex = j;
                            break;
                        }
                        else
                        {
                            offset += chunk.chunkData[j].cbSegment;
                        }
                    }
                }
                PccrrBLKResponsePacket pccrrBlocksResponse;

                if (segmentIndex == -1) // Mached segement not found
                {
                    pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                        pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                        new byte[0],
                        cryptoAlgoId,
                        (MsgType_Values)0xFEFE,
                        iv,
                        0,
                        0);
                }
                else
                {
                    var segment = contentInformationV2.chunks[chunkIndex].chunkData[segmentIndex];

                    byte[] block = content.Skip((int)offset).Take((int)segment.cbSegment).ToArray();

                    if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                    {
                        block = PccrrUtitlity.Encrypt(aes, block, contentInformationV2.chunks[chunkIndex].chunkData[segmentIndex].SegmentSecret, iv);
                    }

                    pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                        pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                        block,
                        cryptoAlgoId,
                        (MsgType_Values)0xFEFE,
                        iv,
                        0,
                        0);
                }
                pccrrServer.SendPacket(pccrrBlocksResponse);
            }
            protected override void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest)
            {
                if (!IsFirstMsgGetBlksArrived)
                {
                    int segmentIndex = -1;
                    for (int i = 0; i < contentInformation.cSegments; i++)
                    {
                        if (Enumerable.SequenceEqual(contentInformation.GetSegmentId(i), pccrrGetBlkRequest.MsgGetBLKS.SegmentID))
                        {
                            segmentIndex = i;
                            break;
                        }
                    }

                    PccrrBLKResponsePacket pccrrBlocksResponse;
                    if (segmentIndex == -1) // Mached segement not found
                    {
                        pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                            pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                            new byte[0],
                            cryptoAlgoId,
                            (MsgType_Values)0xFEFE,
                            this.iv,
                            0,
                            0);
                    }
                    else
                    {
                        ulong  blockSize = PccrcConsts.V1BlockSize;
                        byte[] block     = content.Skip((int)(contentInformation.segments[segmentIndex].ullOffsetInContent + pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index * blockSize)).Take((int)blockSize).ToArray();

                        if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                        {
                            block = PccrrUtitlity.Encrypt(aes, block, contentInformation.segments[segmentIndex].SegmentSecret, iv);
                        }

                        pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                            pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                            block,
                            cryptoAlgoId,
                            (MsgType_Values)0xFEFE,
                            this.iv,
                            pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index,
                            pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index == contentInformation.cSegments - 1 ? 0 : pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index + 1);
                    }
                    pccrrServer.SendPacket(pccrrBlocksResponse);

                    IsFirstMsgGetBlksArrived = true;
                }

                else
                {
                    IsSecondMsgGetBlksArrived = true;
                }
            }
            protected override void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest)
            {
                int segmentIndex = -1;

                for (int i = 0; i < contentInformation.cSegments; i++)
                {
                    if (Enumerable.SequenceEqual(contentInformation.GetSegmentId(i), pccrrGetBlkRequest.MsgGetBLKS.SegmentID))
                    {
                        segmentIndex = i;
                        break;
                    }
                }

                PccrrBLKResponsePacket pccrrBlocksResponse;

                if (segmentIndex == -1) // Mached segement not found
                {
                    pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                        pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                        new byte[0],
                        cryptoAlgoId,
                        MsgType_Values.MSG_BLK,
                        iv,
                        0,
                        0);
                }
                else
                {
                    ulong  blockSize = PccrcConsts.V1BlockSize;
                    byte[] block     = content.Skip((int)(contentInformation.segments[segmentIndex].ullOffsetInContent + pccrrGetBlkRequest.MsgGetBLKS.ReqBlockRanges[0].Index * blockSize)).Take((int)blockSize).ToArray();

                    if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                    {
                        block = PccrrUtitlity.Encrypt(aes, block, contentInformation.segments[segmentIndex].SegmentSecret, iv);
                    }

                    pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                        pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                        new byte[0],
                        cryptoAlgoId,
                        MsgType_Values.MSG_BLK,
                        iv,
                        0,
                        0);
                }

                TestTools.StackSdk.BranchCache.Pccrr.MESSAGE_HEADER header = pccrrBlocksResponse.MessageHeader;
                header.ProtVer = new ProtoVersion {
                    MajorVersion = 0xFEFE, MinorVersion = 0xFEFE
                };
                pccrrBlocksResponse.MessageHeader = header;

                pccrrServer.SendPacket(pccrrBlocksResponse);
            }
Exemple #5
0
        /// <summary>
        /// Verify the received message from client.
        /// </summary>
        /// <param name="remoteAddr">The remote address.</param>
        /// <param name="pccrrPacket">The pccrrPacket.</param>
        private void RetrievalTransport_Receive(IPEndPoint remoteAddr, PccrrPacket pccrrPacket)
        {
            PccrrGETBLKSRequestPacket pccrrGETBLKSRequestPacket = pccrrPacket as PccrrGETBLKSRequestPacket;

            if (pccrrGETBLKSRequestPacket != null)
            {
                MSG_GETBLKS    msgGETBLKS    = pccrrGETBLKSRequestPacket.MsgGetBLKS;
                MESSAGE_HEADER messageHEADER = pccrrGETBLKSRequestPacket.MessageHeader;
                this.sid = msgGETBLKS.SegmentID;
                REQUEST_MESSAGE requestMESSAGE = new REQUEST_MESSAGE();
                requestMESSAGE.MESSAGEBODY   = msgGETBLKS;
                requestMESSAGE.MESSAGEHEADER = messageHEADER;
                this.uiPayload = Marshal.SizeOf(messageHEADER) + Marshal.SizeOf(msgGETBLKS) + 24;

                PccrrBothRoleCaptureCode.CaptureBlockRangeRequirements(msgGETBLKS.ReqBlockRanges[0]);
                PccrrBothRoleCaptureCode.CaptureMessageHeaderRequirements(messageHEADER, this.uiPayload);
                this.VerifyGetBlocks(msgGETBLKS);
                this.VerifyRequestMessage(requestMESSAGE);
                PccrrBothRoleCaptureCode.CaptureSegmentIdRequirements(msgGETBLKS.SegmentID);
                PccrrBothRoleCaptureCode.CaptureHttpRequirements();
                PccrrBothRoleCaptureCode.CaptureMessageRequirements();

                this.ReceiveMsgGetBlk(msgGETBLKS.ReqBlockRanges[0].Index);
            }
            else
            {
                PccrrGETBLKLISTRequestPacket pccrrGETBLKLISTRequestPacket = pccrrPacket as PccrrGETBLKLISTRequestPacket;

                if (pccrrGETBLKLISTRequestPacket != null)
                {
                    MSG_GETBLKLIST msgGETBLKLIST = pccrrGETBLKLISTRequestPacket.MsgGetBLKLIST;
                    this.sid = msgGETBLKLIST.SegmentID;

                    this.VerifyGetBlkList(msgGETBLKLIST);
                    PccrrBothRoleCaptureCode.CaptureCommonDataTypesRequirements(msgGETBLKLIST);
                    _BLOCKRANGE[] blockRanges = ClientHelper.ConvertFromStackBLOCKRANGEArray(msgGETBLKLIST.NeededBlockRanges);

                    this.ReceiveMsgGetBlkList(blockRanges);
                }
                else
                {
                    this.ReceiveMsgNegoReq();
                }
            }
        }
        /// <summary>
        /// The handle of MessageArrivedEventArgs happened.
        /// </summary>
        /// <param name="remoteAddr">Represents the remote endpoint address</param>
        /// <param name="pccrrPacket">The received pccrrPacket, used to judge whether
        /// it is a PccrrGETBLKLISTRequestPacket</param>
        private void RetrievalTransport_Receive(IPEndPoint remoteAddr, PccrrPacket pccrrPacket)
        {
            if (pccrrPacket.PacketType == MsgType_Values.MSG_GETBLKLIST)
            {
                PccrrGETBLKLISTRequestPacket package = (PccrrGETBLKLISTRequestPacket)pccrrPacket;

                int    port      = package.Uri.Port;
                byte[] segmentID = package.MsgGetBLKLIST.SegmentID;
                this.ReceivePccrrRequestHandler(this, new ReceivedPccrrRequestEventArg(port, segmentID));
            }
            else if (pccrrPacket.PacketType == MsgType_Values.MSG_GETBLKS)
            {
                PccrrGETBLKSRequestPacket package = (PccrrGETBLKSRequestPacket)pccrrPacket;

                int    port      = package.Uri.Port;
                byte[] segmentID = package.MsgGetBLKS.SegmentID;
                this.ReceivePccrrRequestHandler(this, new ReceivedPccrrRequestEventArg(port, segmentID));
            }
        }
        public void HostedCacheServer_PccrrServer_GetBlocks_BlockRangeCountNotMatch()
        {
            CheckApplicability();

            var contentInformation = contentInformationUtility.CreateContentInformationV1();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Send malformed GetBlocks request to hosted cache server");

            PccrrClient pccrrClient = new PccrrClient(
                testConfig.HostedCacheServerComputerName,
                testConfig.HostedCacheServerHTTPListenPort);

            PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                contentInformation.GetSegmentId(0),
                CryptoAlgoId_Values.AES_128,
                MsgType_Values.MSG_GETBLKS,
                0,
                1);
            var block = pccrrBlkRequest.MsgGetBLKS;

            block.ReqBlockRangeCount   = 10;
            pccrrBlkRequest.MsgGetBLKS = block;
            pccrrClient.SendPacket(
                pccrrBlkRequest,
                testConfig.Timeout);

            bool passed = false;

            try
            {
                pccrrClient.ExpectPacket();
            }
            catch
            {
                passed = true;
            }

            BaseTestSite.Assert.IsTrue(passed, "Hosted cache server should drop GetBlocks packets with invalid block count");
        }
        public void HostedCacheServer_PccrrClient_MessageHeader_CryptoAlgoIdV2(CryptoAlgoId_Values algoId)
        {
            CheckApplicability();

            EventQueue eventQueue = new EventQueue(BaseTestSite);

            eventQueue.Timeout = testConfig.Timeout;

            byte[] content = TestUtility.GenerateRandomArray(ContentInformationUtility.DefaultBlockSize);
            Content_Information_Data_Structure_V2 contentInformationV2 = contentInformationUtility.CreateContentInformationV2();

            ProtoVersion protoVersion = new ProtoVersion {
                MajorVersion = 2, MinorVersion = ushort.MaxValue
            };

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Start PCCRR server to be ready to serve content to hosted cache server");

            using (PccrrTestServerV2 pccrrTestServerV2 = new PccrrTestServerV2())
            {
                pccrrTestServerV2.Start(
                    testConfig.ClientContentRetrievalListenPort,
                    algoId,
                    contentInformationV2,
                    content,
                    eventQueue);

                PCHCClient pchcClient = new PCHCClient(
                    TransferProtocol.HTTP,
                    testConfig.HostedCacheServerComputerName,
                    testConfig.HostedCacheServerHTTPListenPort,
                    PchcConsts.HttpUrl,
                    testConfig.DomainName,
                    testConfig.UserName,
                    testConfig.UserPassword);

                var batchedOfferMessage = pchcClient.CreateBatchedOfferMessage(
                    testConfig.ClientContentRetrievalListenPort,
                    contentInformationV2);
                pchcClient.SendBatchedOfferMessage(batchedOfferMessage);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Offer content segment 0 of chunk 0 to hosted cache server");

                int segmentCount = 0;
                TestUtility.DoUntilSucceed(delegate()
                {
                    eventQueue.Expect <MessageArrivedEventArgs>(typeof(PccrrServer).GetEvent("MessageArrived"), delegate(System.Net.IPEndPoint sender, PccrrPacket pccrrPacket)
                    {
                        var pccrrGetBlksRequest = pccrrPacket as PccrrGETBLKSRequestPacket;

                        if (pccrrGetBlksRequest != null)
                        {
                            segmentCount++;
                        }
                    });
                    return(segmentCount == 1);
                }, TimeSpan.MaxValue, TimeSpan.Zero);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Wait until cache is available on hosted cache server");

                TestUtility.DoUntilSucceed(() => sutControlAdapter.IsLocalCacheExisted(testConfig.HostedCacheServerComputerFQDNOrNetBiosName), testConfig.Timeout, testConfig.RetryInterval);
            }

            PccrrClient pccrrClient = new PccrrClient(testConfig.HostedCacheServerComputerName, testConfig.HostedCacheServerHTTPListenPort);
            Aes         aes         = PccrrUtitlity.CreateAes(algoId);

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Retrieve segment 0 of chunk 0 from hosted cache server",
                0);
            PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                contentInformationV2.GetSegmentId(0, 0),
                algoId,
                MsgType_Values.MSG_GETBLKS,
                (uint)0,
                1);

            pccrrClient.SendPacket(
                pccrrBlkRequest,
                testConfig.Timeout);
            PccrrBLKResponsePacket pccrrBlkResponse
                = (PccrrBLKResponsePacket)pccrrClient.ExpectPacket();

            byte[] data = pccrrBlkResponse.MsgBLK.Block;

            if (algoId != CryptoAlgoId_Values.NoEncryption)
            {
                data = PccrrUtitlity.Decrypt(aes, data, contentInformationV2.chunks[0].chunkData[0].SegmentSecret, pccrrBlkResponse.MsgBLK.IVBlock);
            }

            BaseTestSite.Assert.IsTrue(
                Enumerable.SequenceEqual(content.Take((int)contentInformationV2.chunks[0].chunkData[0].cbSegment), data),
                "The retrieved cached data should be the same as server data.");
        }
Exemple #9
0
        public void HostedCacheServer_BVT_CacheOfferingRetrieval_V1()
        {
            CheckApplicability();

            EventQueue eventQueue = new EventQueue(BaseTestSite);

            eventQueue.Timeout = testConfig.Timeout;

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Trigger hash generation on content server");

            byte[] content = contentInformationUtility.RetrieveContentData();

            Content_Information_Data_Structure contentInformation =
                PccrcUtility.ParseContentInformation(contentInformationUtility.RetrieveContentInformation(BranchCacheVersion.V1));

            CryptoAlgoId_Values cryptoAlgoId = CryptoAlgoId_Values.AES_128;

            PccrrClient pccrrClient = new PccrrClient(testConfig.HostedCacheServerComputerName, testConfig.HostedCacheServerHTTPListenPort);

            for (int i = 0; i < contentInformation.cSegments; ++i)
            {
                var pccrrBlkListRequest = pccrrClient.CreateMsgGetBlkListRequest(
                    contentInformation.GetSegmentId(i),
                    new BLOCK_RANGE[] { new BLOCK_RANGE {
                                            Index = 0, Count = contentInformation.segments[i].BlockCount
                                        } },
                    cryptoAlgoId,
                    MsgType_Values.MSG_GETBLKLIST);
                pccrrClient.SendPacket(
                    pccrrBlkListRequest,
                    testConfig.Timeout);
                var pccrrBlkListResponse
                    = (PccrrBLKLISTResponsePacket)pccrrClient.ExpectPacket();

                BaseTestSite.Assert.AreEqual <uint>(
                    0,
                    pccrrBlkListResponse.MsgBLKLIST.BlockRangeCount,
                    "The server MUST set the BlockRangeCount field to zero if it doesn't have the requested blocks data.");
            }

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Retrieve content information from content server");

            using (PccrrTestServerV1 pccrrTestServer = new PccrrTestServerV1())
            {
                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Start PCCRR server to be ready to serve content to hosted cache server");

                pccrrTestServer.Start(
                    testConfig.ClientContentRetrievalListenPort,
                    cryptoAlgoId,
                    contentInformation,
                    content,
                    eventQueue);

                PCHCClient pchcClient = new PCHCClient(
                    TransferProtocol.HTTPS,
                    testConfig.HostedCacheServerComputerName,
                    testConfig.HostedCacheServerHTTPSListenPort,
                    PchcConsts.HttpsUrl,
                    testConfig.DomainName,
                    testConfig.UserName,
                    testConfig.UserPassword);

                for (int i = 0; i < contentInformation.cSegments; i++)
                {
                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Offer content segment {0} to hosted cache server",
                        i);

                    INITIAL_OFFER_MESSAGE initialOfferMessage = pchcClient.CreateInitialOfferMessage(
                        testConfig.ClientContentRetrievalListenPort,
                        contentInformation.GetSegmentId(i));
                    Microsoft.Protocols.TestTools.StackSdk.BranchCache.Pchc.RESPONSE_MESSAGE responseMessage
                        = pchcClient.SendInitialOfferMessage(initialOfferMessage);

                    TestClassBase.BaseTestSite.Assert.AreEqual <RESPONSE_CODE>(
                        RESPONSE_CODE.INTERESTED,
                        responseMessage.ResponseCode,
                        @"The hosted cache MUST specify a response code of 1 
                        if its list of block hashes associated with the segment is incomplete.");

                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Supply segment info to hosted cache server");

                    SEGMENT_INFO_MESSAGE segmentInfoMessage = pchcClient.CreateSegmentInfoMessage(
                        testConfig.ClientContentRetrievalListenPort,
                        contentInformation,
                        i);
                    responseMessage = pchcClient.SendSegmentInfoMessage(segmentInfoMessage);

                    TestClassBase.BaseTestSite.Assert.AreEqual <RESPONSE_CODE>(
                        RESPONSE_CODE.OK,
                        responseMessage.ResponseCode,
                        @"The hosted cache MUST send a response code of 0 when SEGMENT_INFO_MESSAGE request received");

                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Make sure all blocks in segment {0} are retrieved by hosted cache server",
                        i);

                    int blockCount = 0;
                    TestUtility.DoUntilSucceed(delegate()
                    {
                        eventQueue.Expect <MessageArrivedEventArgs>(typeof(PccrrServer).GetEvent("MessageArrived"), delegate(System.Net.IPEndPoint sender, PccrrPacket pccrrPacket)
                        {
                            var pccrrGetBlksRequest = pccrrPacket as PccrrGETBLKSRequestPacket;

                            if (pccrrGetBlksRequest != null)
                            {
                                blockCount++;
                            }
                        });
                        return(blockCount == contentInformation.segments[i].BlockCount);
                    }, TimeSpan.MaxValue, TimeSpan.Zero);
                }

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Wait until cache is available on hosted cache server");

                TestUtility.DoUntilSucceed(() => sutControlAdapter.IsLocalCacheExisted(testConfig.HostedCacheServerComputerFQDNOrNetBiosName), testConfig.Timeout, testConfig.RetryInterval);
            }

            List <byte> retrievedContent = new List <byte>();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Negotiate PCCRR version");

            var pccrrNegotiateRequest = pccrrClient.CreateMsgNegoRequest(
                new ProtoVersion {
                MajorVersion = 1, MinorVersion = 0
            },
                new ProtoVersion {
                MajorVersion = 1, MinorVersion = ushort.MaxValue
            },
                cryptoAlgoId,
                MsgType_Values.MSG_NEGO_REQ);

            pccrrClient.SendPacket(
                pccrrNegotiateRequest,
                testConfig.Timeout);
            var pccrrNegotiateResponse
                = (PccrrNegoResponsePacket)pccrrClient.ExpectPacket();

            if (testConfig.SupportBranchCacheV1)
            {
                BaseTestSite.Assert.IsTrue(
                    pccrrNegotiateResponse.MsgNegoResp.MinSupporteProtocolVersion.MajorVersion <= 1 &&
                    pccrrNegotiateResponse.MsgNegoResp.MaxSupporteProtocolVersion.MajorVersion >= 1,
                    "SupportedProtocolVersion doesn't match configuration");
            }

            if (testConfig.SupportBranchCacheV2)
            {
                BaseTestSite.Assert.IsTrue(
                    pccrrNegotiateResponse.MsgNegoResp.MinSupporteProtocolVersion.MajorVersion <= 2 &&
                    pccrrNegotiateResponse.MsgNegoResp.MaxSupporteProtocolVersion.MajorVersion >= 2,
                    "SupportedProtocolVersion doesn't match configuration");
            }

            Aes aes = PccrrUtitlity.CreateAes(cryptoAlgoId);

            for (int i = 0; i < contentInformation.cSegments; i++)
            {
                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Retrieve block list for segment {0}",
                    i);

                var pccrrBlkListRequest = pccrrClient.CreateMsgGetBlkListRequest(
                    contentInformation.GetSegmentId(i),
                    new BLOCK_RANGE[] { new BLOCK_RANGE {
                                            Index = 0, Count = contentInformation.segments[i].BlockCount
                                        } },
                    cryptoAlgoId,
                    MsgType_Values.MSG_GETBLKLIST);
                pccrrClient.SendPacket(
                    pccrrBlkListRequest,
                    testConfig.Timeout);
                var pccrrBlkListResponse
                    = (PccrrBLKLISTResponsePacket)pccrrClient.ExpectPacket();

                BaseTestSite.Assert.AreNotEqual <uint>(
                    0,
                    pccrrBlkListResponse.MsgBLKLIST.BlockRangeCount,
                    "The server MUST set the BlockRangeCount field to a value greater than zero if it has the requested blocks data.");

                for (int j = 0; j < contentInformation.segments[i].BlockCount; j++)
                {
                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Retrieve block {0} for segment {1}",
                        j,
                        i);

                    PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                        contentInformation.GetSegmentId(i),
                        cryptoAlgoId,
                        MsgType_Values.MSG_GETBLKS,
                        (uint)j,
                        1);
                    pccrrClient.SendPacket(
                        pccrrBlkRequest,
                        testConfig.Timeout);
                    PccrrBLKResponsePacket pccrrBlkResponse
                        = (PccrrBLKResponsePacket)pccrrClient.ExpectPacket();

                    BaseTestSite.Assert.AreNotEqual <uint>(
                        0,
                        pccrrBlkResponse.MsgBLK.SizeOfBlock,
                        "The server MUST set the SizeOfBlock field to a value greater than zero if it has the requested blocks data.");

                    byte[] block = pccrrBlkResponse.MsgBLK.Block;

                    if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                    {
                        block = PccrrUtitlity.Decrypt(aes, block, contentInformation.segments[i].SegmentSecret, pccrrBlkResponse.MsgBLK.IVBlock);
                    }

                    retrievedContent.AddRange(block);
                }
            }

            BaseTestSite.Assert.IsTrue(
                Enumerable.SequenceEqual(content, retrievedContent),
                "The retrieved cached data should be the same as server data.");
        }
Exemple #10
0
        public void HostedCacheServer_BVT_CacheOfferingRetrieval_V2()
        {
            CheckApplicability();

            EventQueue eventQueue = new EventQueue(BaseTestSite);

            eventQueue.Timeout = testConfig.Timeout;

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Retrieve the original content data from content server");
            byte[] content = contentInformationUtility.RetrieveContentData();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Retrieve the content information from content server");
            Content_Information_Data_Structure_V2 contentInformation =
                PccrcUtility.ParseContentInformationV2(contentInformationUtility.RetrieveContentInformation(BranchCacheVersion.V2));

            CryptoAlgoId_Values cryptoAlgoId = CryptoAlgoId_Values.AES_128;

            PccrrClient pccrrClient = new PccrrClient(testConfig.HostedCacheServerComputerName, testConfig.HostedCacheServerHTTPListenPort);

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Send PCCRR GetSegList request to HostedCacheServer to make sure that this file does not exist in HostedCacheServer.");
            for (int i = 0; i < contentInformation.chunks.Length; ++i)
            {
                var chunk = contentInformation.chunks[i];
                for (int j = 0; j < chunk.chunkData.Length; j++)
                {
                    var pccrrGetSegListRequest = pccrrClient.CreateMsgGetSegListRequest(
                        cryptoAlgoId,
                        Guid.NewGuid(),
                        new byte[][] { contentInformation.GetSegmentId(i, j) });
                    pccrrClient.SendPacket(
                        pccrrGetSegListRequest,
                        testConfig.Timeout);
                    var pccrrGetSegListResponse
                        = (PccrrSegListResponsePacket)pccrrClient.ExpectPacket();

                    BaseTestSite.Assert.AreEqual <uint>(
                        0,
                        pccrrGetSegListResponse.MsgSegList.SegmentRangeCount,
                        "The server MUST set the SegmentRangeCount field to zero if it doesn't have the requested segments data.");
                }
            }

            using (PccrrTestServerV2 pccrrTestServer = new PccrrTestServerV2())
            {
                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Start PCCRR server to be ready to serve content to hosted cache server");

                pccrrTestServer.Start(
                    testConfig.ClientContentRetrievalListenPort,
                    cryptoAlgoId,
                    contentInformation,
                    content,
                    eventQueue);

                PCHCClient pchcClient = new PCHCClient(
                    TransferProtocol.HTTP,
                    testConfig.HostedCacheServerComputerName,
                    testConfig.ContentServerHTTPListenPort,
                    PchcConsts.HttpUrl,
                    testConfig.DomainName,
                    testConfig.UserName,
                    testConfig.UserPassword);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Offer all content segments to hosted cache server");

                var batchedOfferMessage = pchcClient.CreateBatchedOfferMessage(
                    testConfig.ClientContentRetrievalListenPort,
                    contentInformation);
                var responseMessage = pchcClient.SendBatchedOfferMessage(batchedOfferMessage);

                TestClassBase.BaseTestSite.Assert.AreEqual <RESPONSE_CODE>(
                    RESPONSE_CODE.OK,
                    responseMessage.ResponseCode,
                    @"The hosted cache MUST send a response code of 0 when BATCHED_OFFER_MESSAGE request received");

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Make sure all segments are retrieved by hosted cache server");

                int totalBlockCount = contentInformation.GetBlockCount();
                int blockCount      = 0;
                TestUtility.DoUntilSucceed(delegate()
                {
                    eventQueue.Expect <MessageArrivedEventArgs>(typeof(PccrrServer).GetEvent("MessageArrived"), delegate(System.Net.IPEndPoint sender, PccrrPacket pccrrPacket)
                    {
                        var pccrrGetBlksRequest = pccrrPacket as PccrrGETBLKSRequestPacket;

                        if (pccrrGetBlksRequest != null)
                        {
                            blockCount++;
                        }
                    });
                    return(blockCount == totalBlockCount);
                }, TimeSpan.MaxValue, TimeSpan.Zero);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Wait until cache is available on hosted cache server");

                TestUtility.DoUntilSucceed(() => sutControlAdapter.IsLocalCacheExisted(testConfig.HostedCacheServerComputerFQDNOrNetBiosName), testConfig.Timeout, testConfig.RetryInterval);
            }

            List <byte> retrievedContent = new List <byte>();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Start PCCRR client to retrieve all the content from hosted cache server");
            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Negotiate PCCRR version");
            var pccrrNegotiateRequest = pccrrClient.CreateMsgNegoRequest(
                ///[MS-PCCRR]Section 2.2.3: ProtVer: Both the Major and Minor version number can express the version range of 0x0000 to 0xFFFF. Currently, the protocol
                ///version number MUST be set to {major = 1 (0x0001), minor = 0 (0x0000)}.
                ///Active TDI#69240: Windows server 2012 Standard RTM sets the major version to 2
                new ProtoVersion {
                MajorVersion = 1, MinorVersion = 0
            },                                                           //MinSupportedProtocolVersion
                new ProtoVersion {
                MajorVersion = 1, MinorVersion = 0
            },                                                           //MaxSupportedProtocolVersion
                cryptoAlgoId,
                MsgType_Values.MSG_NEGO_REQ);

            pccrrClient.SendPacket(
                pccrrNegotiateRequest,
                testConfig.Timeout);
            var pccrrNegotiateResponse
                = (PccrrNegoResponsePacket)pccrrClient.ExpectPacket();

            if (testConfig.SupportBranchCacheV1)
            {
                BaseTestSite.Assert.IsTrue(
                    pccrrNegotiateResponse.MsgNegoResp.MinSupporteProtocolVersion.MajorVersion <= 1 &&
                    pccrrNegotiateResponse.MsgNegoResp.MaxSupporteProtocolVersion.MajorVersion >= 1,
                    "SupportedProtocolVersion doesn't match configuration");
            }

            Aes aes = PccrrUtitlity.CreateAes(cryptoAlgoId);

            for (int i = 0; i < contentInformation.chunks.Length; i++)
            {
                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Retrieve all segments in chunk {0}",
                    i);

                var chunk = contentInformation.chunks[i];
                for (int j = 0; j < chunk.chunkData.Length; j++)
                {
                    // Retrieve segment list
                    var pccrrGetSegListRequest = pccrrClient.CreateMsgGetSegListRequest(
                        cryptoAlgoId,
                        Guid.NewGuid(),
                        new byte[][] { contentInformation.GetSegmentId(i, j) });
                    pccrrClient.SendPacket(
                        pccrrGetSegListRequest,
                        testConfig.Timeout);
                    var pccrrGetSegListResponse
                        = (PccrrSegListResponsePacket)pccrrClient.ExpectPacket();

                    BaseTestSite.Assert.AreNotEqual <uint>(
                        0,
                        pccrrGetSegListResponse.MsgSegList.SegmentRangeCount,
                        "The server MUST set the SegmentRangeCount field to a value greater than zero if it has the requested segments data.");

                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Retrieve segment {0} in chunk {1}",
                        j,
                        i);

                    PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                        contentInformation.GetSegmentId(i, j),
                        cryptoAlgoId,
                        MsgType_Values.MSG_GETBLKS,
                        0,
                        1);
                    pccrrClient.SendPacket(
                        pccrrBlkRequest,
                        testConfig.Timeout);
                    PccrrBLKResponsePacket pccrrBlkResponse
                        = (PccrrBLKResponsePacket)pccrrClient.ExpectPacket();

                    BaseTestSite.Assert.AreNotEqual <uint>(
                        0,
                        pccrrBlkResponse.MsgBLK.SizeOfBlock,
                        "The server MUST set the SizeOfBlock field to a value greater than zero if it has the requested blocks data.");

                    byte[] block = pccrrBlkResponse.MsgBLK.Block;

                    if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                    {
                        block = PccrrUtitlity.Decrypt(aes, block, contentInformation.chunks[i].chunkData[j].SegmentSecret, pccrrBlkResponse.MsgBLK.IVBlock);
                    }

                    retrievedContent.AddRange(block);
                }
            }

            BaseTestSite.Assert.IsTrue(
                Enumerable.SequenceEqual(content, retrievedContent),
                "The retrieved cached data should be the same as server data.");
        }
 protected abstract void HandlePccrrGETBLKSRequestPacket(PccrrGETBLKSRequestPacket pccrrGetBlkRequest);
Exemple #12
0
        public void HostedCacheServer_PccrrServer_MessageHeader_CryptoAlgoId(CryptoAlgoId_Values algoId)
        {
            CheckApplicability();

            EventQueue eventQueue = new EventQueue(BaseTestSite);

            eventQueue.Timeout = testConfig.Timeout;

            byte[] content = TestUtility.GenerateRandomArray(ContentInformationUtility.DefaultBlockSize);
            Content_Information_Data_Structure contentInformation = contentInformationUtility.CreateContentInformationV1();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Start PCCRR server to be ready to serve content to hosted cache server");

            using (PccrrTestServerV1 pccrrTestServer = new PccrrTestServerV1())
            {
                pccrrTestServer.Start(
                    testConfig.ClientContentRetrievalListenPort,
                    algoId,
                    new ProtoVersion {
                    MajorVersion = 1, MinorVersion = 0
                },
                    contentInformation,
                    content,
                    eventQueue);

                PCHCClient pchcClient = new PCHCClient(
                    TransferProtocol.HTTPS,
                    testConfig.HostedCacheServerComputerName,
                    testConfig.HostedCacheServerHTTPSListenPort,
                    PchcConsts.HttpsUrl,
                    testConfig.DomainName,
                    testConfig.UserName,
                    testConfig.UserPassword);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Directly Supply segment info to hosted cache server");

                SEGMENT_INFO_MESSAGE segmentInfoMessage = pchcClient.CreateSegmentInfoMessage(
                    testConfig.ClientContentRetrievalListenPort,
                    contentInformation,
                    0);
                pchcClient.SendSegmentInfoMessage(segmentInfoMessage);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Make sure block 0 in segment 0 is retrieved by hosted cache server");

                int blockCount = 0;
                TestUtility.DoUntilSucceed(delegate()
                {
                    eventQueue.Expect <MessageArrivedEventArgs>(typeof(PccrrServer).GetEvent("MessageArrived"), delegate(System.Net.IPEndPoint sender, PccrrPacket pccrrPacket)
                    {
                        var pccrrGetBlksRequest = pccrrPacket as PccrrGETBLKSRequestPacket;

                        if (pccrrGetBlksRequest != null)
                        {
                            blockCount++;
                        }
                    });
                    return(blockCount == 1);
                }, TimeSpan.MaxValue, TimeSpan.Zero);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Wait until cache is available on hosted cache server");

                TestUtility.DoUntilSucceed(() => sutControlAdapter.IsLocalCacheExisted(testConfig.HostedCacheServerComputerFQDNOrNetBiosName), testConfig.Timeout, testConfig.RetryInterval);
            }

            PccrrClient pccrrClient = new PccrrClient(testConfig.HostedCacheServerComputerName, testConfig.HostedCacheServerHTTPListenPort);
            Aes         aes         = PccrrUtitlity.CreateAes(algoId);

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Retrieve block 0 in segment 0 from hosted cache server");
            PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                contentInformation.GetSegmentId(0),
                algoId,
                MsgType_Values.MSG_GETBLKS,
                (uint)0,
                1);

            pccrrClient.SendPacket(
                pccrrBlkRequest,
                testConfig.Timeout);
            PccrrBLKResponsePacket pccrrBlkResponse
                = (PccrrBLKResponsePacket)pccrrClient.ExpectPacket();

            byte[] block = pccrrBlkResponse.MsgBLK.Block;

            if (algoId != CryptoAlgoId_Values.NoEncryption)
            {
                block = PccrrUtitlity.Decrypt(aes, block, contentInformation.segments[0].SegmentSecret, pccrrBlkResponse.MsgBLK.IVBlock);
            }

            BaseTestSite.Assert.IsTrue(
                Enumerable.SequenceEqual(content, block),
                "The retrieved cached data should be the same as server data.");
        }
Exemple #13
0
        public void HostedCacheServer_PchcServer_GetBlocks_SizeOfBlockNotMatch()
        {
            CheckApplicability();

            var contentInformation = contentInformationUtility.CreateContentInformationV1();

            using (var pccrrServer = new PccrrServer(testConfig.ClientContentRetrievalListenPort))
            {
                bool blockRetrieved = false;
                pccrrServer.MessageArrived += new MessageArrivedEventArgs(delegate(IPEndPoint sender, PccrrPacket pccrrPacket)
                {
                    var pccrrGetBlkRequest = (PccrrGETBLKSRequestPacket)pccrrPacket;

                    if (pccrrGetBlkRequest != null)
                    {
                        BaseTestSite.Log.Add(
                            LogEntryKind.Debug,
                            "PCCRR GetBlks request received from hosted cache server. Send malformed Blks response");

                        var pccrrBlocksResponse = pccrrServer.CreateMsgBlkResponse(
                            pccrrGetBlkRequest.MsgGetBLKS.SegmentID,
                            TestUtility.GenerateRandomArray(ContentInformationUtility.DefaultBlockSize),
                            CryptoAlgoId_Values.AES_128,
                            MsgType_Values.MSG_BLK,
                            TestUtility.GenerateRandomArray(16),
                            0,
                            0);
                        var blk                    = pccrrBlocksResponse.MsgBLK;
                        blk.SizeOfBlock            = 0;
                        pccrrBlocksResponse.MsgBLK = blk;
                        pccrrServer.SendPacket(pccrrBlocksResponse);

                        blockRetrieved = true;
                    }
                });
                pccrrServer.StartListening();

                PCHCClient pchcClient = new PCHCClient(
                    TransferProtocol.HTTPS,
                    testConfig.HostedCacheServerComputerName,
                    testConfig.HostedCacheServerHTTPSListenPort,
                    PchcConsts.HttpsUrl,
                    testConfig.DomainName,
                    testConfig.UserName,
                    testConfig.UserPassword);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Offer block to hosted cache server");

                SEGMENT_INFO_MESSAGE segmentInfoMessage = pchcClient.CreateSegmentInfoMessage(
                    testConfig.ClientContentRetrievalListenPort,
                    contentInformation,
                    0);
                pchcClient.SendSegmentInfoMessage(segmentInfoMessage);

                TestUtility.DoUntilSucceed(() =>
                {
                    return(blockRetrieved);
                }, testConfig.Timeout, testConfig.RetryInterval);
            }

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Try to retrieve the block from hosted cache server");

            PccrrClient pccrrClient = new PccrrClient(
                testConfig.HostedCacheServerComputerName,
                testConfig.HostedCacheServerHTTPListenPort);

            PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                contentInformation.GetSegmentId(0),
                CryptoAlgoId_Values.AES_128,
                MsgType_Values.MSG_GETBLKS,
                0,
                1);

            pccrrClient.SendPacket(
                pccrrBlkRequest,
                testConfig.Timeout);
            PccrrBLKResponsePacket pccrrBlkResponse
                = (PccrrBLKResponsePacket)pccrrClient.ExpectPacket();

            BaseTestSite.Assert.AreEqual <uint>(
                0,
                pccrrBlkResponse.MsgBLK.SizeOfBlock,
                "The server MUST set the SizeOfBlock field to zero if blocks data is not available.");
        }