/// <summary>
        /// createt the transactions packet
        /// </summary>
        /// <param name="request">the request packet</param>
        /// <param name="smbHeader">the smb header of response packet</param>
        /// <param name="channel">the channel contains the packet bytes</param>
        /// <returns>the response packet</returns>
        private SmbPacket CreateTransactionResponsePacket(SmbPacket request, SmbHeader smbHeader, Channel channel)
        {
            SmbPacket smbPacket = null;

            if (smbHeader.Status == 0 && channel.Peek<byte>(0) == 0 && channel.Peek<ushort>(1) == 0)
            {
                return smbPacket;
            }

            SmbTransactionRequestPacket transactionRequest = request as SmbTransactionRequestPacket;
            if (transactionRequest == null)
            {
                return smbPacket;
            }
            switch (smbClient.Capability.TransactionSubCommand)
            {
                case TransSubCommandExtended.TRANS_EXT_MAILSLOT_WRITE:
                    smbPacket = new SmbTransMailslotWriteResponsePacket();
                    break;

                case TransSubCommandExtended.TRANS_EXT_RAP:
                    smbPacket = new SmbTransRapResponsePacket();
                    break;

                default:
                    break;

            }

            // the packet is find
            if (smbPacket != null)
            {
                return smbPacket;
            }

            // if no setup command. break
            if (transactionRequest.SmbParameters.SetupCount == 0)
            {
                return smbPacket;
            }

            // decode packet using the setup command
            switch ((TransSubCommand)transactionRequest.SmbParameters.Setup[0])
            {
                case TransSubCommand.TRANS_SET_NMPIPE_STATE:
                    smbPacket = new SmbTransSetNmpipeStateResponsePacket();
                    break;

                case TransSubCommand.TRANS_QUERY_NMPIPE_STATE:
                    smbPacket = new SmbTransQueryNmpipeStateResponsePacket();
                    break;

                case TransSubCommand.TRANS_RAW_READ_NMPIPE:
                    smbPacket = new SmbTransRawReadNmpipeResponsePacket();
                    break;

                case TransSubCommand.TRANS_QUERY_NMPIPE_INFO:
                    smbPacket = new SmbTransQueryNmpipeInfoResponsePacket();
                    break;

                case TransSubCommand.TRANS_PEEK_NMPIPE:
                    smbPacket = new SmbTransPeekNmpipeResponsePacket();
                    break;

                case TransSubCommand.TRANS_TRANSACT_NMPIPE:
                    smbPacket = new SmbTransTransactNmpipeResponsePacket();
                    break;

                case TransSubCommand.TRANS_READ_NMPIPE:
                    smbPacket = new SmbTransReadNmpipeResponsePacket();
                    break;

                case TransSubCommand.TRANS_WRITE_NMPIPE:
                    smbPacket = new SmbTransWriteNmpipeResponsePacket();
                    break;

                case TransSubCommand.TRANS_WAIT_NMPIPE:
                    smbPacket = new SmbTransWaitNmpipeResponsePacket();
                    break;

                case TransSubCommand.TRANS_CALL_NMPIPE:
                    smbPacket = new SmbTransCallNmpipeResponsePacket();
                    break;

                default:
                    break;
            }

            return smbPacket;
        }
        public void TraditionalTestCase_IgnoreFields_CopyChunk_05()
        {
            #region Connect to the specified server

            smbClientStack.Connect(serverName, serverPort, ipVersion, bufferSize);

            #endregion

            #region Send the Negotiate request

            // Create a SMB_COM_NEGOTIATE request.
            SmbNegotiateRequestPacket negotiateRequest =
                smbClientStack.CreateNegotiateRequest(
                    StackSmb.SignState.NONE,
                    new string[] {
                DialectNameString.PCNET1,
                DialectNameString.LANMAN10,
                DialectNameString.WFW10,
                DialectNameString.LANMAN12,
                DialectNameString.LANMAN21,
                DialectNameString.NTLANMAN
            });

            // Send the SMB_COM_NEGOTIATE request and expect the response in timeout milliseconds.
            smbClientStack.SendPacket(negotiateRequest);
            StackPacket response = smbClientStack.ExpectPacket(timeout);

            // Check whether server return a response.
            Site.Assert.IsNotNull(
                response,
                "SMB_COM_NEGOTIATE response should not be null.");

            // Check whether server returns a SMB_COM_NEGOTIATE response.
            Site.Assert.IsInstanceOfType(
                response,
                typeof(SmbNegotiateResponsePacket),
                "SMB_COM_NEGOTIATE response should be received.");

            // Check the response validity by verifying the Status field in the SMB Header packet.
            SmbNegotiateResponsePacket negotiateResponse = (SmbNegotiateResponsePacket)response;
            Site.Assert.AreEqual <uint>(
                (uint)SmbStatus.STATUS_SUCCESS,
                negotiateRequest.SmbHeader.Status,
                "SMB_COM_NEGOTIATE response status should be SUCCESS.");

            #endregion

            #region Send the first SMB_COM_SESSION_SETUP_ANDX  Request

            SmbSecurityPackage smbSecurityPackage = (SmbSecurityPackage)Enum.Parse(
                typeof(SmbSecurityPackage),
                Site.Properties["SmbSecurityPackageType"] as string,
                true);

            // Create the first SMB_COM_SESSION_SETUP_ANDX request.
            SmbSessionSetupAndxRequestPacket sessionSetupAndxRequest =
                smbClientStack.CreateFirstSessionSetupRequest(
                    smbSecurityPackage,
                    serverName,
                    domainName,
                    userName,
                    password);

            // Send the first SMB_COM_SESSION_SETUP_ANDX request and expect the response in timeout milliseconds.
            smbClientStack.SendPacket(sessionSetupAndxRequest);
            response = smbClientStack.ExpectPacket(timeout);

            // Check whether server return a response.
            Site.Assert.IsNotNull(
                response,
                "SMB_COM_SESSION_SETUP_ANDX response should not be null.");

            // Check whether server returns a the SMB_COM_SESSION_SETUP_ANDX response.
            Site.Assert.IsInstanceOfType(
                response,
                typeof(SmbSessionSetupAndxResponsePacket),
                "SMB_COM_SESSION_SETUP_ANDX response should be received.");

            // Check the response validity by verifying the Status field in the SMB Header packet.
            // If SMB SecurityPackage type is NTLM, the expected SUCCESS response status is STATUS_MORE_PROCESSING_REQUIRED,
            // else if SMB SecurityPackage type is Kerberos, the expected SUCCESS response status is STATUS_SUCCESS.
            SmbSessionSetupAndxResponsePacket sessionSetupResponse = (SmbSessionSetupAndxResponsePacket)response;
            Site.Assert.IsTrue(
                (int)sessionSetupResponse.SmbHeader.Status == (int)SmbStatus.STATUS_MORE_PROCESSING_REQUIRED ||
                (int)sessionSetupResponse.SmbHeader.Status == (int)SmbStatus.STATUS_SUCCESS,
                "SMB_COM_SESSION_SETUP_ANDX response status should be SUCCESS.");

            #endregion

            #region Send the second SMB_COM_SESSION_SETUP_ANDX  request

            // Create the second SMB_COM_SESSION_SETUP_ANDX request.
            ushort sessionUid = sessionSetupResponse.SmbHeader.Uid;

            if ((int)sessionSetupResponse.SmbHeader.Status == (int)SmbStatus.STATUS_MORE_PROCESSING_REQUIRED)
            {
                SmbSessionSetupAndxRequestPacket secondSessionSetupRequest =
                    smbClientStack.CreateSecondSessionSetupRequest(sessionUid, smbSecurityPackage);

                // Send the second SMB_COM_SESSION_SETUP_ANDX request and expect the response in timeout milliseconds.
                smbClientStack.SendPacket(secondSessionSetupRequest);
                response = smbClientStack.ExpectPacket(timeout);

                // Check whether server return a response.
                Site.Assert.IsNotNull(
                    response,
                    "SMB_COM_SESSION_SETUP_ANDX response should not be null.");

                // Check whether server returns a the SMB_COM_SESSION_SETUP_ANDX response.
                Site.Assert.IsInstanceOfType(
                    response,
                    typeof(SmbSessionSetupAndxResponsePacket),
                    "SMB_COM_SESSION_SETUP_ANDX response should be received.");

                // Check the response validity by verifying the Status field in the SMB Header packet.
                sessionSetupResponse = (SmbSessionSetupAndxResponsePacket)response;
                Site.Assert.AreEqual <uint>(
                    (uint)SmbStatus.STATUS_SUCCESS,
                    sessionSetupResponse.SmbHeader.Status,
                    "SMB_COM_SESSION_SETUP_ANDX response status should be SUCCESS.");
            }

            #endregion

            #region Send the SMB_COM_TREE_CONNECT_ANDX request

            string path = Site.Properties["SutNtfsShare1FullName"];

            SmbTreeConnectAndxRequestPacket treeconnectRequest =
                smbClientStack.CreateTreeConnectRequest(sessionUid, path);

            smbClientStack.SendPacket(treeconnectRequest);
            response = smbClientStack.ExpectPacket(timeout);

            SmbTreeConnectAndxResponsePacket treeConnectResponse = (SmbTreeConnectAndxResponsePacket)response;
            #endregion

            #region Send the NT_CREATE_ANDX request

            ushort treeId   = treeConnectResponse.SmbHeader.Tid;
            string fileName = Site.Properties["SutShareTest1"];
            smbClientStack.Capability.Flag |= SmbHeader_Flags_Values.OPLOCK;

            SmbNtCreateAndxRequestPacket createRequest =
                smbClientStack.CreateCreateRequest(
                    treeId,
                    fileName,
                    StackCifs.NtTransactDesiredAccess.FILE_READ_DATA
                    | StackCifs.NtTransactDesiredAccess.FILE_READ_EA
                    | StackCifs.NtTransactDesiredAccess.FILE_READ_ATTRIBUTES
                    | StackCifs.NtTransactDesiredAccess.FILE_WRITE_DATA,
                    StackCifs.SMB_EXT_FILE_ATTR.NONE,
                    StackCifs.NtTransactShareAccess.FILE_SHARE_READ
                    | StackCifs.NtTransactShareAccess.FILE_SHARE_DELETE,
                    StackCifs.NtTransactCreateDisposition.FILE_OPEN_IF,
                    NtTransactCreateOptions.FILE_OPEN_REPARSE_POINT
                    | NtTransactCreateOptions.FILE_SEQUENTIAL_ONLY
                    | NtTransactCreateOptions.FILE_NON_DIRECTORY_FILE,
                    StackCifs.NtTransactImpersonationLevel.SEC_ANONYMOUS,
                    CreateFlags.NT_CREATE_REQUEST_OPLOCK);

            smbClientStack.SendPacket(createRequest);
            response = smbClientStack.ExpectPacket(timeout);

            #endregion

            #region Send the NT_CREATE_ANDX request

            SmbNtCreateAndxResponsePacket createResponse = (SmbNtCreateAndxResponsePacket)response;
            ushort fileId1   = createResponse.SmbParameters.FID;
            uint   offset    = uint.Parse(Site.Properties["SmbTransportWriteOffset"].ToString());
            byte[] writeData = new byte[17000];

            for (int i = 0; i < writeData.Length; i++)
            {
                writeData[i] = (byte)'a';
            }

            SmbWriteAndxRequestPacket writeRequest = smbClientStack.CreateWriteRequest(fileId1, offset, writeData);

            smbClientStack.SendPacket(writeRequest);
            response = smbClientStack.ExpectPacket(timeout);

            #endregion

            #region Send the WRITE_ANDX request

            SmbWriteAndxResponsePacket writeAndxResponsePacket = response as SmbWriteAndxResponsePacket;
            string fileName2 = Site.Properties["SutShareTest2"];

            SmbNtCreateAndxRequestPacket createSecondRequest =
                smbClientStack.CreateCreateRequest(
                    treeId,
                    fileName2,
                    StackCifs.NtTransactDesiredAccess.FILE_READ_DATA
                    | StackCifs.NtTransactDesiredAccess.FILE_WRITE_DATA
                    | StackCifs.NtTransactDesiredAccess.FILE_APPEND_DATA,
                    StackCifs.SMB_EXT_FILE_ATTR.NONE,
                    StackCifs.NtTransactShareAccess.FILE_SHARE_WRITE
                    | StackCifs.NtTransactShareAccess.FILE_SHARE_DELETE,
                    StackCifs.NtTransactCreateDisposition.FILE_OPEN_IF,
                    NtTransactCreateOptions.FILE_OPEN_REPARSE_POINT
                    | NtTransactCreateOptions.FILE_SEQUENTIAL_ONLY
                    | NtTransactCreateOptions.FILE_NON_DIRECTORY_FILE,
                    StackCifs.NtTransactImpersonationLevel.SEC_ANONYMOUS,
                    CreateFlags.NT_CREATE_REQUEST_OPBATCH);

            smbClientStack.SendPacket(createSecondRequest);
            response = smbClientStack.ExpectPacket(timeout);

            #endregion

            #region Send the NT_CREATE_ANDX

            SmbNtCreateAndxResponsePacket createSecondResponse = (SmbNtCreateAndxResponsePacket)response;
            ushort fileId2 = createSecondResponse.SmbParameters.FID;

            SmbNtTransFsctlSrvRequestResumeKeyRequestPacket nTTransIOCtlRequestResumeKeyRequest =
                smbClientStack.CreateNTTransIOCtlRequestResumeKeyRequest(fileId1, true, 0);

            StackCifs.SmbHeader smbHeader = nTTransIOCtlRequestResumeKeyRequest.SmbHeader;
            smbHeader.Uid = sessionUid;
            smbHeader.Tid = treeId;
            nTTransIOCtlRequestResumeKeyRequest.SmbHeader = smbHeader;

            smbClientStack.SendPacket(nTTransIOCtlRequestResumeKeyRequest);
            response = smbClientStack.ExpectPacket(timeout);

            SmbNtTransFsctlSrvRequestResumeKeyResponsePacket nTTransIOCtlRequestResumeKeyResponse =
                (SmbNtTransFsctlSrvRequestResumeKeyResponsePacket)response;

            #endregion

            #region Send the NT_TRANS_IO_CTL request

            bool   isFsctl            = true;
            byte   isFlags            = (byte)0;
            byte[] copychunkResumeKey = nTTransIOCtlRequestResumeKeyResponse.NtTransData.ResumeKey;

            nTTransIOCtlRequestResumeKeyRequest =
                smbClientStack.CreateNTTransIOCtlRequestResumeKeyRequest(fileId1, true, 0);

            smbHeader     = nTTransIOCtlRequestResumeKeyRequest.SmbHeader;
            smbHeader.Uid = sessionUid;
            smbHeader.Tid = treeId;
            nTTransIOCtlRequestResumeKeyRequest.SmbHeader = smbHeader;

            smbClientStack.SendPacket(nTTransIOCtlRequestResumeKeyRequest);
            response = smbClientStack.ExpectPacket(timeout);

            #endregion

            #region Capture requirement r9074

            nTTransIOCtlRequestResumeKeyResponse =
                (SmbNtTransFsctlSrvRequestResumeKeyResponsePacket)response;
            byte[] copychunkResumeKey2 = nTTransIOCtlRequestResumeKeyResponse.NtTransData.ResumeKey;

            // Compare copychunkResumeKey and copychunkResumeKey2, if same, capture 9074
            //
            // Add the debug information
            //
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-SMB_R9074");

            //
            // Verify MS-SMB requirement: MS-SMB_R9074
            //
            bool isVerifyR9074 = CompareArrayEquals(copychunkResumeKey, copychunkResumeKey2);

            Site.CaptureRequirementIfIsTrue(
                isVerifyR9074,
                9074,
                @"[In Copychunk Resume Key Generation] The generation of Copychunk Resume Keys MUST satisfy the 
                following constraints:The Copychunk Resume Key MUST remain valid for the lifetime of the open file 
                on the server.");

            #endregion

            #region Send the NT_TRANSACT_COPY_CHUNK_List request

            NT_TRANSACT_COPY_CHUNK_List list = new NT_TRANSACT_COPY_CHUNK_List();
            list.Length            = (uint)64;
            list.SourceOffset      = (ulong)0;
            list.DestinationOffset = (ulong)0;

            SmbNtTransFsctlSrvCopyChunkRequestPacket nTTransIOCtlCopyChunkRequest =
                smbClientStack.CreateNTTransIOCtlCopyChunkRequest(fileId2, isFsctl, isFlags, copychunkResumeKey, list);

            smbClientStack.SendPacket(nTTransIOCtlCopyChunkRequest);
            response = smbClientStack.ExpectPacket(timeout);

            #endregion

            #region Send the NT_TRANSACT_COPY_CHUNK_Request_NT_Trans_Data request

            SmbNtTransFsctlSrvCopyChunkResponsePacket nTTransIOCtlCopyChunkResponse1 =
                (SmbNtTransFsctlSrvCopyChunkResponsePacket)response;

            list.Length            = (uint)64;
            list.SourceOffset      = (ulong)0;
            list.DestinationOffset = (ulong)0;
            list.Reserved          = 0xFFFF;

            nTTransIOCtlCopyChunkRequest =
                smbClientStack.CreateNTTransIOCtlCopyChunkRequest(fileId2, isFsctl, isFlags, copychunkResumeKey, list);
            NT_TRANSACT_COPY_CHUNK_Request_NT_Trans_Data TransData = nTTransIOCtlCopyChunkRequest.NtTransData;

            TransData.Unused = 0xFFFF;
            nTTransIOCtlCopyChunkRequest.NtTransData = TransData;

            smbClientStack.SendPacket(nTTransIOCtlCopyChunkRequest);
            response = smbClientStack.ExpectPacket(timeout);

            #endregion

            #region Capture requirements R109390

            SmbNtTransFsctlSrvCopyChunkResponsePacket nTTransIOCtlCopyChunkResponse2 =
                (SmbNtTransFsctlSrvCopyChunkResponsePacket)response;

            // Compare 2 copy chunckResponse
            //
            // Add the debug information
            //
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-SMB_R109390");

            //
            // Verify MS-SMB requirement: MS-SMB_R109390
            //
            bool isVerifyR109390 = VerifyResponse(nTTransIOCtlCopyChunkResponse1, nTTransIOCtlCopyChunkResponse2);

            Site.CaptureRequirementIfIsTrue(
                isVerifyR109390,
                109390,
                @"[In  Client Request Extensions]Reserved (4 bytes):
                reply is the same whether zero or non-zero is used this field.");

            //
            // Add the debug information
            //
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-SMB_R109396");

            //
            // Verify MS-SMB requirement: MS-SMB_R109396
            //
            bool isVerifyR109396 = VerifyResponse(nTTransIOCtlCopyChunkResponse1, nTTransIOCtlCopyChunkResponse2);

            Site.CaptureRequirementIfIsTrue(
                isVerifyR109396,
                109396,
                @"[In SRV_COPYCHUNK]Reserved (4 bytes):
                reply is the same whether zero or non-zero is used in this field.");

            #endregion

            #region Send the SMB_NT_TRANS_FSCTL_SRV request request

            nTTransIOCtlRequestResumeKeyRequest =
                smbClientStack.CreateNTTransIOCtlRequestResumeKeyRequest(fileId2, true, 0);

            smbHeader     = nTTransIOCtlRequestResumeKeyRequest.SmbHeader;
            smbHeader.Uid = sessionUid;
            smbHeader.Tid = treeId;
            nTTransIOCtlRequestResumeKeyRequest.SmbHeader = smbHeader;

            smbClientStack.SendPacket(nTTransIOCtlRequestResumeKeyRequest);
            response = smbClientStack.ExpectPacket(timeout);

            #endregion

            #region Capture reuqirements R9073

            nTTransIOCtlRequestResumeKeyResponse =
                (SmbNtTransFsctlSrvRequestResumeKeyResponsePacket)response;

            byte[] copychunkResumeKey3 = nTTransIOCtlRequestResumeKeyResponse.NtTransData.ResumeKey;

            //
            // Add the comment information for debugging
            //
            Site.Log.Add(
                LogEntryKind.Comment,
                @"Verify MS-SMB_R9073");

            //
            // Verify MS-SMB requirement: MS-SMB_R9073
            //
            bool isVerifyR9073 = CompareArrayEquals(copychunkResumeKey, copychunkResumeKey3);

            Site.CaptureRequirementIfIsFalse(
                isVerifyR9073,
                9073,
                @"[In Copychunk Resume Key Generation]
                The generation of Copychunk Resume Keys MUST satisfy the following constraints:
                The Copychunk Resume Key MUST be unique on the SMB server for a given open file on a server.");

            #endregion

            #region Disconnect the tree, session and connection.

            // TreeDisconnect
            SmbTreeDisconnectRequestPacket treeDisconnectRequest =
                smbClientStack.CreateTreeDisconnectRequest(treeId);

            smbClientStack.SendPacket(treeDisconnectRequest);
            response = smbClientStack.ExpectPacket(timeout);

            // Check whether server return a response.
            Site.Assert.IsNotNull(
                response,
                "SMB_COM_TREE_DISCONNECT response should not be null.");

            Site.Assert.IsInstanceOfType(
                response,
                typeof(SmbTreeDisconnectResponsePacket),
                "SMB_COM_TREE_DISCONNECT response should be received.");

            // Check the response validity by verifying the Status field in the SMB Header packet.
            SmbTreeDisconnectResponsePacket treeDisconnectResponse = (SmbTreeDisconnectResponsePacket)response;
            Site.Assert.AreEqual <uint>(
                (uint)SmbStatus.STATUS_SUCCESS,
                treeDisconnectResponse.SmbHeader.Status,
                "SMB_COM_TREE_DISCONNECT response status should be SUCCESS.");

            // LogOff
            SmbLogoffAndxRequestPacket logoffRequest = smbClientStack.CreateLogoffRequest(sessionUid);

            smbClientStack.SendPacket(logoffRequest);
            response = smbClientStack.ExpectPacket(timeout);

            // Check whether server return a response.
            Site.Assert.IsNotNull(
                response,
                "SMB_COM_LOGOFF_ANDX response should not be null.");

            Site.Assert.IsInstanceOfType(
                response,
                typeof(SmbLogoffAndxResponsePacket),
                "SMB_COM_LOGOFF_ANDX response should be received.");

            // Check the response validity by verifying the Status field in the SMB Header packet.
            SmbLogoffAndxResponsePacket logoffResponse = (SmbLogoffAndxResponsePacket)response;
            Site.Assert.AreEqual <uint>(
                (uint)SmbStatus.STATUS_SUCCESS,
                logoffResponse.SmbHeader.Status,
                "SMB_COM_LOGOFF_ANDX response status should be SUCCESS.");

            // Disconnect
            smbClientStack.Disconnect();

            Site.Assert.IsFalse(
                smbClientStack.IsDataAvailable,
                "SmbClient should not receive any packet after Disconnect method is called.");

            #endregion
        }
        /// <summary>
        /// create the nt transaction packet
        /// </summary>
        /// <param name="request">the request packet</param>
        /// <param name="smbHeader">the smb header of response packet</param>
        /// <param name="channel">the channel contains the packet bytes</param>
        /// <returns>the response packet</returns>
        private SmbPacket CreateNtTransactionResponsePacket(SmbPacket request, SmbHeader smbHeader, Channel channel)
        {
            SmbPacket smbPacket = null;

            if (smbHeader.Status == 0 && channel.Peek<byte>(0) == 0 && channel.Peek<ushort>(1) == 0)
            {
                return smbPacket;
            }

            SmbNtTransactRequestPacket ntTransactRequest = request as SmbNtTransactRequestPacket;
            if (ntTransactRequest == null)
            {
                return smbPacket;
            }

            // find regular packet
            switch ((uint)ntTransactRequest.SmbParameters.Function)
            {
                case (uint)NtTransSubCommand.NT_TRANSACT_RENAME:
                    smbPacket = new SmbNtTransRenameResponsePacket();
                    break;

                case (uint)NtTransSubCommand.NT_TRANSACT_CREATE:
                    smbPacket = new SmbNtTransactCreateResponsePacket();
                    break;

                case (uint)NtTransSubCommand.NT_TRANSACT_IOCTL:

                    NT_TRANSACT_IOCTL_SETUP setup =
                       CifsMessageUtils.ToStuct<NT_TRANSACT_IOCTL_SETUP>(
                       CifsMessageUtils.ToBytesArray<ushort>(ntTransactRequest.SmbParameters.Setup));

                    switch ((NtTransFunctionCode)setup.FunctionCode)
                    {
                        case NtTransFunctionCode.FSCTL_SRV_ENUMERATE_SNAPSHOTS:
                            smbPacket = new SmbNtTransFsctlSrvEnumerateSnapshotsResponsePacket();
                            break;

                        case NtTransFunctionCode.FSCTL_SRV_REQUEST_RESUME_KEY:
                            smbPacket = new SmbNtTransFsctlSrvRequestResumeKeyResponsePacket();
                            break;

                        case NtTransFunctionCode.FSCTL_SRV_COPYCHUNK:
                            smbPacket = new SmbNtTransFsctlSrvCopyChunkResponsePacket();
                            break;

                        default:
                            smbPacket = new SmbNtTransactIoctlResponsePacket();
                            break;
                    }

                    break;

                case (uint)SmbNtTransSubCommand.NT_TRANSACT_QUERY_QUOTA:
                    smbPacket = new SmbNtTransQueryQuotaResponsePacket();
                    break;

                case (uint)SmbNtTransSubCommand.NT_TRANSACT_SET_QUOTA:
                    smbPacket = new SmbNtTransSetQuotaResponsePacket();
                    break;

                default:
                    break;
            }

            return smbPacket;
        }
        private SmbPacket CreateTransaction2ResponsePacket(SmbPacket request, SmbHeader smbHeader, Channel channel)
        {
            SmbPacket smbPacket = null;

            if (smbHeader.Status == 0 && channel.Peek<byte>(0) == 0 && channel.Peek<ushort>(1) == 0)
            {
                return smbPacket;
            }

            SmbTransaction2RequestPacket transaction2Request = request as SmbTransaction2RequestPacket;
            if (transaction2Request == null)
            {
                return smbPacket;
            }

            // if no setup command. break
            if (transaction2Request.SmbParameters.SetupCount == 0)
            {
                return smbPacket;
            }

            // decode packet using the setup command
            switch ((Trans2SubCommand)transaction2Request.SmbParameters.Setup[0])
            {
                case Trans2SubCommand.TRANS2_QUERY_FILE_INFORMATION:
                    SmbTrans2QueryFileInformationRequestPacket queryFileRequest =
                        transaction2Request as SmbTrans2QueryFileInformationRequestPacket;
                    if (queryFileRequest != null)
                    {
                        smbPacket = new SmbTrans2QueryFileInformationResponsePacket(
                            queryFileRequest.Trans2Parameters.InformationLevel);
                    }
                    break;

                case Trans2SubCommand.TRANS2_QUERY_PATH_INFORMATION:
                    SmbTrans2QueryPathInformationRequestPacket queryPathRequest =
                       transaction2Request as SmbTrans2QueryPathInformationRequestPacket;
                    if (queryPathRequest != null)
                    {
                        smbPacket = new SmbTrans2QueryPathInformationResponsePacket(
                            queryPathRequest.Trans2Parameters.InformationLevel);
                    }
                    break;

                case Trans2SubCommand.TRANS2_SET_FILE_INFORMATION:
                    smbPacket = new SmbTrans2SetFileInformationResponsePacket();
                    break;

                case Trans2SubCommand.TRANS2_SET_PATH_INFORMATION:
                    smbPacket = new SmbTrans2SetPathInformationResponsePacket();
                    break;

                case Trans2SubCommand.TRANS2_QUERY_FS_INFORMATION:
                    SmbTrans2QueryFsInformationRequestPacket queryFsRequest =
                        transaction2Request as SmbTrans2QueryFsInformationRequestPacket;
                    if (queryFsRequest != null)
                    {
                        smbPacket = new SmbTrans2QueryFsInformationResponsePacket(
                            queryFsRequest.Trans2Parameters.InformationLevel);
                    }
                    break;

                case Trans2SubCommand.TRANS2_SET_FS_INFORMATION:
                    smbPacket = new SmbTrans2SetFsInformationResponsePacket();
                    break;

                case Trans2SubCommand.TRANS2_FIND_FIRST2:
                    SmbTrans2FindFirst2RequestPacket first2Request =
                       transaction2Request as SmbTrans2FindFirst2RequestPacket;
                    if (first2Request != null)
                    {
                        smbPacket = new SmbTrans2FindFirst2ResponsePacket(first2Request.Trans2Parameters.InformationLevel,
                            (first2Request.Trans2Parameters.Flags & Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS)
                            == Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS);
                    }
                    break;

                case Trans2SubCommand.TRANS2_FIND_NEXT2:
                    SmbTrans2FindNext2RequestPacket next2Request =
                       transaction2Request as SmbTrans2FindNext2RequestPacket;
                    if (next2Request != null)
                    {
                        smbPacket = new SmbTrans2FindNext2ResponsePacket(next2Request.Trans2Parameters.InformationLevel,
                            (next2Request.Trans2Parameters.Flags & Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS)
                            == Trans2FindFlags.SMB_FIND_RETURN_RESUME_KEYS);
                    }
                    break;

                case Trans2SubCommand.TRANS2_GET_DFS_REFERRAL:
                    smbPacket = new SmbTrans2GetDfsReferralResponsePacket();
                    break;

                default:
                    break;
            }

            return smbPacket;
        }
        protected override SmbPacket CreateSmbResponsePacket(
            SmbPacket request,
            SmbHeader smbHeader,
            Channel channel)
        {
            SmbPacket smbPacket = null;

            // error packet
            SmbStatus packetStatus = (SmbStatus)smbHeader.Status;

            // error packet
            if (packetStatus != SmbStatus.STATUS_SUCCESS &&
                    packetStatus != SmbStatus.STATUS_MORE_PROCESSING_REQUIRED &&
                    packetStatus != SmbStatus.STATUS_BUFFER_OVERFLOW)
            {
                smbPacket = new SmbErrorResponsePacket();
                smbPacket.SmbHeader = smbHeader;

                return smbPacket;
            }

            // success packet
            switch (smbHeader.Command)
            {
                case SmbCommand.SMB_COM_NEGOTIATE:
                    if (smbClient.Capability.IsSupportsExtendedSecurity)
                    {
                        smbPacket = new SmbNegotiateResponsePacket();
                    }
                    else
                    {
                        smbPacket = new SmbNegotiateImplicitNtlmResponsePacket();
                    }
                    break;

                case SmbCommand.SMB_COM_SESSION_SETUP_ANDX:
                    if (smbClient.Capability.IsSupportsExtendedSecurity)
                    {
                        smbPacket = new SmbSessionSetupAndxResponsePacket();
                    }
                    else
                    {
                        smbPacket = new SmbSessionSetupImplicitNtlmAndxResponsePacket();
                    }
                    break;

                case SmbCommand.SMB_COM_TREE_CONNECT_ANDX:
                    smbPacket = new SmbTreeConnectAndxResponsePacket();
                    break;

                case SmbCommand.SMB_COM_TREE_DISCONNECT:
                    smbPacket = new SmbTreeDisconnectResponsePacket();
                    break;

                case SmbCommand.SMB_COM_LOGOFF_ANDX:
                    smbPacket = new SmbLogoffAndxResponsePacket();
                    break;

                case SmbCommand.SMB_COM_NT_CREATE_ANDX:
                    smbPacket = new SmbNtCreateAndxResponsePacket();
                    break;

                case SmbCommand.SMB_COM_CLOSE:
                    smbPacket = new SmbCloseResponsePacket();
                    break;

                case SmbCommand.SMB_COM_OPEN_ANDX:
                    smbPacket = new SmbOpenAndxResponsePacket();
                    break;

                case SmbCommand.SMB_COM_WRITE_ANDX:
                    smbPacket = new SmbWriteAndxResponsePacket();
                    break;

                case SmbCommand.SMB_COM_READ_ANDX:
                    smbPacket = new SmbReadAndxResponsePacket();
                    break;

                case SmbCommand.SMB_COM_TRANSACTION:
                    smbPacket = this.CreateTransactionResponsePacket(request, smbHeader, channel);

                    break;

                case SmbCommand.SMB_COM_TRANSACTION2:
                    smbPacket = this.CreateTransaction2ResponsePacket(request, smbHeader, channel);

                    break;

                case SmbCommand.SMB_COM_NT_TRANSACT:
                    smbPacket = this.CreateNtTransactionResponsePacket(request, smbHeader, channel);

                    break;

                default:
                    break;

            }
            if (smbPacket != null)
            {
                smbPacket.SmbHeader = smbHeader;
                return smbPacket;
            }

            return base.CreateSmbResponsePacket(request, smbHeader, channel);
        }
        /// <summary>
        /// find the target packet.
        /// </summary>
        /// <param name="smbHeader">the header of smb packet</param>
        /// <param name="channel">the channel to access bytes</param>
        /// <returns>the target packet</returns>
        private static SmbPacket FindTheTargetPacket(SmbHeader smbHeader, Channel channel)
        {
            SmbPacket smbPacket = null;

            switch (smbHeader.Command)
            {
                case SmbCommand.SMB_COM_NEGOTIATE:
                        smbPacket = new SmbNegotiateRequestPacket();
                    break;

                case SmbCommand.SMB_COM_SESSION_SETUP_ANDX:
                    SmbHeader_Flags2_Values flags2 = (SmbHeader_Flags2_Values)smbHeader.Flags2;
                    if ((flags2 & SmbHeader_Flags2_Values.SMB_FLAGS2_EXTENDED_SECURITY)
                        == SmbHeader_Flags2_Values.SMB_FLAGS2_EXTENDED_SECURITY)
                    {
                        smbPacket = new Smb.SmbSessionSetupAndxRequestPacket();
                    }
                    else
                    {
                        smbPacket = new Cifs.SmbSessionSetupAndxRequestPacket();
                    }
                    break;

                case SmbCommand.SMB_COM_TREE_CONNECT_ANDX:
                    smbPacket = new SmbTreeConnectAndxRequestPacket();
                    break;

                case SmbCommand.SMB_COM_NT_CREATE_ANDX:
                    smbPacket = new SmbNtCreateAndxRequestPacket();
                    break;

                case SmbCommand.SMB_COM_OPEN_ANDX:
                    smbPacket = new SmbOpenAndxRequestPacket();
                    break;

                case SmbCommand.SMB_COM_WRITE_ANDX:
                    smbPacket = new SmbWriteAndxRequestPacket();
                    break;

                case SmbCommand.SMB_COM_READ_ANDX:
                    smbPacket = new SmbReadAndxRequestPacket();
                    break;

                case SmbCommand.SMB_COM_CLOSE:
                    smbPacket = new SmbCloseRequestPacket();
                    break;

                case SmbCommand.SMB_COM_TREE_DISCONNECT:
                    smbPacket = new SmbTreeDisconnectRequestPacket();
                    break;

                case SmbCommand.SMB_COM_LOGOFF_ANDX:
                    smbPacket = new SmbLogoffAndxRequestPacket();
                    break;

                case SmbCommand.SMB_COM_TRANSACTION:
                    SMB_COM_TRANSACTION_Request_SMB_Parameters transaction =
                        channel.Read<SMB_COM_TRANSACTION_Request_SMB_Parameters>();
                    if (transaction.SetupCount == 0)
                    {
                        smbPacket = new SmbTransRapRequestPacket();
                    }
                    else
                    {
                        smbPacket = FindTheTransactionPacket(
                            transaction.SetupCount, (TransSubCommand)transaction.Setup[0]);
                    }
                    break;

                case SmbCommand.SMB_COM_TRANSACTION2:
                    SMB_COM_TRANSACTION2_Request_SMB_Parameters transaction2 =
                        channel.Read<SMB_COM_TRANSACTION2_Request_SMB_Parameters>();
                    smbPacket = FindTheTrans2Packet((Trans2SubCommand)transaction2.Subcommand);
                    break;

                case SmbCommand.SMB_COM_NT_TRANSACT:
                    SMB_COM_NT_TRANSACT_Request_SMB_Parameters ntTransactoin =
                        channel.Read<SMB_COM_NT_TRANSACT_Request_SMB_Parameters>();
                    smbPacket = FindTheNtTransPacket(ntTransactoin.Function, CifsMessageUtils.ToBytesArray<ushort>(ntTransactoin.Setup));
                    break;

                default:
                    break;
            }

            return smbPacket;
        }
 /// <summary>
 /// to new a Smb response packet in type of the Command in SmbHeader.
 /// </summary>
 /// <param name="request">the request of the response.</param>
 /// <param name="smbHeader">the SMB header of the packet.</param>
 /// <param name="channel">the channel started with SmbParameters.</param>
 /// <returns>
 /// the new response packet. 
 /// the null means that the utility don't know how to create the response.
 /// </returns>
 protected virtual SmbPacket CreateSmbResponsePacket(
     SmbPacket request,
     SmbHeader smbHeader,
     Channel channel)
 {
     return CifsMessageUtils.CreateSmbResponsePacket(request, smbHeader, channel);
 }
        public SmbLockingAndxRequestPacket CreateLockingAndxRequest(
            CifsServerPerOpenFile open,
            NewOplockLevelValue newOplockLevel)
        {
            SmbLockingAndxRequestPacket request = new SmbLockingAndxRequestPacket();
            SmbHeader smbHeader = new SmbHeader();
            smbHeader.Protocol = CifsMessageUtils.SMB_PROTOCOL_IDENTIFIER;
            smbHeader.Command = SmbCommand.SMB_COM_LOCKING_ANDX;
            smbHeader.Uid = (ushort)open.Session.SessionId;
            smbHeader.Tid = (ushort)open.TreeConnect.TreeConnectId;
            smbHeader.Mid = CifsMessageUtils.INVALID_MID;

            request.SmbHeader = smbHeader;

            SMB_COM_LOCKING_ANDX_Request_SMB_Parameters smbParameters = request.SmbParameters;
            smbParameters.FID = (ushort)open.FileId;
            smbParameters.NewOplockLevel = newOplockLevel;
            smbParameters.TypeOfLock = LockingAndxTypeOfLock.OPLOCK_RELEASE;
            smbParameters.WordCount = (byte)(TypeMarshal.GetBlockMemorySize(smbParameters) / 2);
            request.SmbParameters = smbParameters;

            request.AndxPacket = null;

            return request;
        }
        public void TraditionalTestCase_IgnoreFields_Negotiate_03_Case()
        {
            #region Connect

            smbClientStack.Connect(serverName, serverPort, ipVersion, bufferSize);

            #endregion

            #region Send the SMB_COM_NEGOTIATE request

            // Create the SMB_COM_NEGOTIATE request.
            SmbNegotiateRequestPacket negotiateRequest =
                smbClientStack.CreateNegotiateRequest(StackSmb.SignState.NONE,
                                                      new string[] {
                DialectNameString.PCNET1,
                DialectNameString.LANMAN10,
                DialectNameString.WFW10,
                DialectNameString.LANMAN12,
                DialectNameString.LANMAN21,
                DialectNameString.NTLANMAN
            });

            #region Set the request parameters

            StackCifs.SmbHeader negotiateHeader = negotiateRequest.SmbHeader;

            // Set an invalid flag value of 0xFC4C.
            ushort unUsedAndSecSigReq0 = 0xFC4C;
            ushort flag2 = (ushort)negotiateHeader.Flags2;
            negotiateHeader.Flags2 = (StackCifs.SmbFlags2)(ushort)(flag2 & unUsedAndSecSigReq0);

            negotiateRequest.SmbHeader = negotiateHeader;

            #endregion

            // Send the SMB_COM_NEGOTIATE request and expect a response in timeout milliseconds.
            smbClientStack.SendPacket(negotiateRequest);
            StackPacket response = smbClientStack.ExpectPacket(timeout);

            // Check whether server returns a response.
            Site.Assert.IsNotNull(
                response,
                "SMB_COM_NEGOTIATE response should not be null.");

            // Check whether server returns a SMB_COM_NEGOTIATE response.
            Site.Assert.IsInstanceOfType(
                response,
                typeof(SmbNegotiateResponsePacket),
                "SMB_COM_NEGOTIATE response should be received.");

            // Check the response validity by verifying the Status field in the SMB Header packet.
            SmbNegotiateResponsePacket negotiateResponse = (SmbNegotiateResponsePacket)response;
            Site.Assert.AreEqual <uint>(
                (uint)SmbStatus.STATUS_SUCCESS,
                negotiateResponse.SmbHeader.Status,
                "SMB_COM_NEGOTIATE response status should be SUCCESS.");

            #endregion

            #region Disconnect

            smbClientStack.Disconnect();

            #endregion

            #region Connect

            smbClientStack.Connect(serverName, serverPort, ipVersion, bufferSize);

            #endregion

            #region Send the SMB_COM_NEGOTIATE request

            // Create a SMB_COM_NEGOTIATE request.
            SmbNegotiateRequestPacket negotiateRequest2 =
                smbClientStack.CreateNegotiateRequest(
                    StackSmb.SignState.NONE,
                    new string[] {
                DialectNameString.PCNET1,
                DialectNameString.LANMAN10,
                DialectNameString.WFW10,
                DialectNameString.LANMAN12,
                DialectNameString.LANMAN21,
                DialectNameString.NTLANMAN
            });

            #region Set the request parameters

            StackCifs.SmbHeader negotiateHeader2 = negotiateRequest2.SmbHeader;

            // Set an invalid flag value of 0x3B3.
            ushort unUsedAndSecSigReq1 = 0x03B3;
            flag2 = (ushort)negotiateHeader2.Flags2;
            negotiateHeader2.Flags2 = (StackCifs.SmbFlags2)(ushort)(flag2 | unUsedAndSecSigReq1);

            negotiateRequest2.SmbHeader = negotiateHeader2;

            #endregion

            // Send the SMB_COM_NEGOTIATE request and expect a response in the timeout milliseconds.
            smbClientStack.SendPacket(negotiateRequest2);
            StackPacket response2 = smbClientStack.ExpectPacket(timeout);

            // Check whether server returns a response.
            Site.Assert.IsNotNull(
                response2,
                "SMB_COM_NEGOTIATE response should not be null.");

            // Check whether server returns a SMB_COM_NEGOTIATE response.
            Site.Assert.IsInstanceOfType(
                response2,
                typeof(SmbNegotiateResponsePacket),
                "SMB_COM_NEGOTIATE response should be received.");

            // Check the response validity by verifying the Status field in the SMB Header packet.
            SmbNegotiateResponsePacket negotiateResponse2 = (SmbNegotiateResponsePacket)response2;
            Site.Assert.AreEqual <uint>(
                (uint)SmbStatus.STATUS_SUCCESS,
                negotiateResponse2.SmbHeader.Status,
                "SMB_COM_NEGOTIATE response status should be SUCCESS.");

            #endregion

            #region Capture requirements r5277, r5298, 108

            //
            // Add the debug information
            //
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-SMB_R5277");

            //
            // Verify MS-SMB requirement: MS-SMB_R5277
            //
            bool isVerifyR5277 = VerifyNegotiateResponse(negotiateResponse, negotiateResponse2);

            Site.CaptureRequirementIfIsTrue(
                isVerifyR5277,
                5277,
                @"[In SMB Header Extensions]Flags2 (2 bytes): 
                Reply is the same whether 0 or non-zero is used for Field.");

            //
            // Add the debug information
            //
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-SMB_R5298");

            //
            // Verify MS-SMB requirement: MS-SMB_R5298
            //
            bool isVerifyR5298 = VerifyNegotiateResponse(negotiateResponse, negotiateResponse2);

            Site.CaptureRequirementIfIsTrue(
                isVerifyR5298,
                5298,
                @"[In SMB Header Extensions]Whatever the values of this flag
                [Flags2: SMB_FLAGS2_SMB_SECURITY_SIGNATURE_REQUIRED 0x0010] on other requests[except the first
                SMB_COM_SESSION_SETUP_ANDX request], servers' reply are the same.");

            //
            // Add the debug information
            //
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-SMB_R108");

            //
            // Verify MS-SMB requirement: MS-SMB_R108
            //
            bool isVerifyR108 = VerifyNegotiateResponse(negotiateResponse, negotiateResponse2);

            Site.CaptureRequirementIfIsTrue(
                isVerifyR108,
                108,
                @"<19> Section 2.2.3.1: Windows-based  Windows servers set the bits[Unused bit fields]
                in the Flags2 field with the same value(s) that were sent by the client in the request.");
            #endregion

            #region Disconnect

            // 7.Disconnect.
            smbClientStack.Disconnect();

            #endregion
        }