public void NtTransactCreateRequest(
            int messageId,
            int sessionId,
            int treeId,
            [Domain("ImpersonationLevel")] int impersonationLevel,
            [Domain("FileDomain")] string fileName,
            ShareType shareType,
            bool isSigned)
        {
            #region Create Packet

            SmbNtCreateAndxRequestPacket smbPacket = new SmbNtCreateAndxRequestPacket();

            ushort uid = (ushort)this.uId[(uint)sessionId];
            ushort tid = (ushort)this.tId[(uint)treeId];
            string shareName = string.Empty;
            if (shareType == ShareType.NamedPipe)
            {
                if (SmbAdapter.isMessageModePipe)
                {
                    shareName = Site.Properties["SutShareMessagePipe"];
                }
                else
                {
                    shareName = Site.Properties["SutShareStreamPipe"];
                }
            }
            else
            {
                if (fileName == Site.Properties["SutShareTest1"])
                {
                    shareName = Site.Properties["SutShareTest1"];
                }
                else if (fileName == Site.Properties["SutShareTest2"])
                {
                    shareName = Site.Properties["SutShareTest2"];
                }
                else if (fileName == Site.Properties["SutShareExistFile"])
                {
                    shareName = Site.Properties["SutShareExistFile"];
                }
                else if (fileName.Contains("."))
                {
                    shareName = fileName;
                }
                else
                {
                    shareName = fileName;
                }
            }

            this.openedFileName = shareName;
            uint nTDesiredAccess = (uint)NamespaceCifs.NtTransactDesiredAccess.GENERIC_ALL;

            NamespaceCifs.SMB_EXT_FILE_ATTR extFileAttributes = NamespaceCifs.SMB_EXT_FILE_ATTR.NONE;

            NtTransactCreateOptions createOption = NtTransactCreateOptions.FILE_NON_DIRECTORY_FILE;

            smbPacket = this.smbClientStack.CreateCreateRequest(
                tid,
                shareName,
                (NamespaceCifs.NtTransactDesiredAccess)nTDesiredAccess,
                extFileAttributes,
                NamespaceCifs.NtTransactShareAccess.NONE,
                NamespaceCifs.NtTransactCreateDisposition.FILE_OPEN_IF,
                createOption,
                (NamespaceCifs.NtTransactImpersonationLevel)impersonationLevel,
                CreateFlags.NT_CREATE_REQUEST_OPLOCK);

            if (isSigned)
            {
                NamespaceCifs.CifsClientPerConnection connection =
                    this.smbClientStack.Context.GetConnection(ConnectionId);

                NamespaceCifs.CifsClientPerSession session = this.smbClientStack.Context.GetSession(ConnectionId, uid);

                smbPacket.Sign(connection.ClientNextSendSequenceNumber, session.SessionKey);
            }

            #endregion

            #region Send and Receive ExpectPacket

            this.smbClientStack.SendPacket(smbPacket);
            StackPacket response = this.smbClientStack.ExpectPacket(this.timeout);

            NamespaceCifs.SmbPacket smbPacketResponse = (NamespaceCifs.SmbPacket)response;

            this.QueryUidTable(smbPacketResponse);
            this.QueryTidTable(smbPacketResponse);

            VerifyTransport(smbPacketResponse);
            VerifyCommonMessageSyntax(smbPacketResponse);

            if (response.GetType() == typeof(SmbErrorResponsePacket))
            {
                SmbErrorResponsePacket smbErrorResponsePacket = response as SmbErrorResponsePacket;
                NamespaceCifs.SmbHeader smbErrorHeader = smbErrorResponsePacket.SmbHeader;
                this.ErrorResponse(smbErrorHeader.Mid + this.addMidMark, (MessageStatus)smbErrorHeader.Status);
            }
            else
            {
                SmbNtCreateAndxResponsePacket smbNtCreateAndXPacket = response as SmbNtCreateAndxResponsePacket;
                NamespaceCifs.SmbHeader ntCreateAndXResponseHeader = smbNtCreateAndXPacket.SmbHeader;

                SMB_COM_NT_CREATE_ANDX_Response_SMB_Parameters ntCreateAndXResponsePayload
                    = smbNtCreateAndXPacket.SmbParameters;

                this.smbFileId = ntCreateAndXResponsePayload.FID;

                if (!this.fId.ContainsValue(ntCreateAndXResponsePayload.FID))
                {
                    int count = this.fId.Count;
                    this.fId.Add((uint)(count), ntCreateAndXResponsePayload.FID);
                }
                else
                {
                    foreach (uint key in this.fId.Keys)
                    {
                        if (this.fId[key] == ntCreateAndXResponsePayload.FID)
                        {
                            break;
                        }
                    }
                }

                uint creatAction = ntCreateAndXResponsePayload.CreationAction;
                Microsoft.Modeling.Set<CreateAction> actionSet = new Microsoft.Modeling.Set<CreateAction>();
                if (creatAction == CreateActionSuperseded)
                {
                    actionSet = actionSet.Add(CreateAction.FileSuperseded);
                }

                if ((creatAction & CreateActionExists) == CreateActionExists)
                {
                    actionSet = actionSet.Add(CreateAction.FileExists);
                }

                if ((creatAction & CreateActionOpened) == CreateActionOpened)
                {
                    actionSet = actionSet.Add(CreateAction.FileOpened);
                    if (!actionSet.Contains(CreateAction.FileExists))
                    {
                        actionSet = actionSet.Add(CreateAction.FileExists);
                    }
                }

                if ((creatAction & CreateActionNotExists) == CreateActionNotExists)
                {
                    actionSet = actionSet.Add(CreateAction.FileDoesNotExist);
                }

                if ((creatAction & CreateActionCreated) == CreateActionCreated)
                {
                    actionSet = actionSet.Add(CreateAction.FileCreated);
                    if (!actionSet.Contains(CreateAction.FileDoesNotExist))
                    {
                        actionSet = actionSet.Add(CreateAction.FileDoesNotExist);
                    }
                }

                if ((creatAction & CreateActionOverwritten) == CreateActionOverwritten)
                {
                    actionSet = actionSet.Add(CreateAction.FileOverwritten);
                }

                bool isIDZero = false;
                bool isVolumnGuidZero = false;
                if (!this.checkWindowsImplementation)
                {
                    Site.Log.Add(
                        LogEntryKind.Comment,
                        @"isFileIdZero: {0};
                        isVolumnGUIDZero: {1}",
                        isIDZero,
                        isVolumnGuidZero);
                    isIDZero = true;
                    isVolumnGuidZero = true;
                }
                //Workaround temp code (Invalid impersonation level)
                if ((string.Equals(Site.Properties["SutPlatformOS"], "Win2K8") && impersonationLevel == 4)
                    && !bool.Parse(Site.Properties["IsTDI33006Fixed"]))
                {
                    this.ErrorResponse(ntCreateAndXResponseHeader.Mid + this.addMidMark, MessageStatus.BadImpersonationLevel);
                }
                else
                {
                    this.NtTransactCreateResponse(
                        ntCreateAndXResponseHeader.Mid + this.addMidMark,
                        QueryUidTable(smbPacketResponse),
                        QueryTidTable(smbPacketResponse),
                        isSigned,
                        (MessageStatus)ntCreateAndXResponseHeader.Status);
                }
            }

            #endregion
        }
        public void CreateRequest(
            int messageId,
            int sessionId,
            int treeId,
            int desiredAccess,
            CreateDisposition createDisposition,
            [Domain("ImpersonationLevel")] int impersonationLevel,
            string fileName,
            ShareType shareType,
            bool isSigned,
            bool isOpenByFileId,
            bool isDirectoryFile,
            bool isMaximumAllowedSet)
        {
            #region Create Packet

            SmbNtCreateAndxRequestPacket smbPacket = new SmbNtCreateAndxRequestPacket();
            ushort uid = (ushort)this.uId[(uint)sessionId];
            ushort tid = (ushort)this.tId[(uint)treeId];
            string shareName = string.Empty;
            CreateFlags createFlags;
            bool isCreateDirectory = false;

            if (fileName != Site.Properties["SutShareExistFile"].ToString())
            {
                if (createDisposition == CreateDisposition.FileOpenIf)
                {
                    this.createActionInternal = this.createActionInternal.Add(CreateAction.FileCreated);
                    this.createActionInternal = this.createActionInternal.Add(CreateAction.FileDoesNotExist);
                }
            }

            if (shareType == ShareType.NamedPipe)
            {
                if (SmbAdapter.isMessageModePipe)
                {
                    shareName = Site.Properties["SutShareMessagePipe"];
                }
                else
                {
                    shareName = Site.Properties["SutShareStreamPipe"];
                }

                isCreateDirectory = false;
            }
            else
            {
                if (fileName == Site.Properties["SutShareTest1"])
                {
                    shareName = Site.Properties["SutShareTest1"];
                    isCreateDirectory = false;
                }
                else if (fileName == Site.Properties["SutShareTest2"])
                {
                    shareName = Site.Properties["SutShareTest2"];
                    isCreateDirectory = false;
                }
                else if (fileName == Site.Properties["SutShareExistFile"])
                {
                    shareName = Site.Properties["SutShareExistFile"];
                    isCreateDirectory = false;
                }
                else if (fileName.Contains("."))
                {
                    shareName = fileName;
                    isCreateDirectory = false;
                }
                else
                {
                    shareName = fileName;
                    isCreateDirectory = true;
                }
            }

            this.openedFileName = shareName;

            if (isCreateDirectory)
            {
                createFlags = CreateFlags.NT_CREATE_OPEN_TARGET_DIR | CreateFlags.NT_CREATE_REQUEST_EXTENDED_RESPONSE;
            }
            else
            {
                createFlags = CreateFlags.NT_CREATE_REQUEST_EXTENDED_RESPONSE;
            }

            NamespaceCifs.NtTransactShareAccess shareAccess =
                (NamespaceCifs.NtTransactShareAccess)ushort.Parse(Site.Properties["SmbTransportShareAccess"]);

            NamespaceCifs.SMB_EXT_FILE_ATTR extFileAttributes = NamespaceCifs.SMB_EXT_FILE_ATTR.NONE;

            NamespaceCifs.NtTransactCreateDisposition ntCreateDisposition =
                (NamespaceCifs.NtTransactCreateDisposition)createDisposition;

            NtTransactCreateOptions createOption = NtTransactCreateOptions.FILE_DIRECTORY_FILE;
            if (isOpenByFileId && isDirectoryFile)
            {
                createOption =
                    NtTransactCreateOptions.FILE_OPEN_BY_FILE_ID | NtTransactCreateOptions.FILE_DIRECTORY_FILE;
            }

            if (isOpenByFileId && !isDirectoryFile)
            {
                createOption =
                    NtTransactCreateOptions.FILE_NON_DIRECTORY_FILE | NtTransactCreateOptions.FILE_OPEN_BY_FILE_ID;
            }

            if (!isOpenByFileId && isDirectoryFile)
            {
                createOption = NtTransactCreateOptions.FILE_DIRECTORY_FILE;
            }

            if (!isOpenByFileId && !isDirectoryFile)
            {
                createOption = NtTransactCreateOptions.FILE_NON_DIRECTORY_FILE;
            }

            NamespaceCifs.NtTransactDesiredAccess nTDesiredAccess =
                (NamespaceCifs.NtTransactDesiredAccess)desiredAccess;

            if (nTDesiredAccess == (
                NamespaceCifs.NtTransactDesiredAccess.FILE_READ_DATA
                | NamespaceCifs.NtTransactDesiredAccess.FILE_WRITE_DATA))
            {
                nTDesiredAccess = NamespaceCifs.NtTransactDesiredAccess.GENERIC_ALL;
            }

            if (isCreateDirectory)
            {
                nTDesiredAccess = Microsoft.Protocols.TestTools.StackSdk.FileAccessService.Cifs.NtTransactDesiredAccess.FILE_READ_DATA |
                    Microsoft.Protocols.TestTools.StackSdk.FileAccessService.Cifs.NtTransactDesiredAccess.FILE_READ_ATTRIBUTES |
                    Microsoft.Protocols.TestTools.StackSdk.FileAccessService.Cifs.NtTransactDesiredAccess.SYNCHRONIZE;
                shareAccess = NamespaceCifs.NtTransactShareAccess.FILE_SHARE_READ | NamespaceCifs.NtTransactShareAccess.FILE_SHARE_WRITE;
            }

            smbPacket = this.smbClientStack.CreateCreateRequest(
                tid,
                shareName,
                nTDesiredAccess,
                extFileAttributes,
                shareAccess,
                ntCreateDisposition,
                createOption,
                (NamespaceCifs.NtTransactImpersonationLevel)impersonationLevel,
                createFlags);

            if (isSigned)
            {
                NamespaceCifs.CifsClientPerConnection connection =
                    this.smbClientStack.Context.GetConnection(ConnectionId);

                NamespaceCifs.CifsClientPerSession session =
                    this.smbClientStack.Context.GetSession(ConnectionId, uid);

                smbPacket.Sign(connection.ClientNextSendSequenceNumber, session.SessionKey);
            }

            #endregion

            #region Send and Receive ExpectPacket

            this.smbClientStack.SendPacket(smbPacket);
            StackPacket response = this.smbClientStack.ExpectPacket(this.timeout);

            NamespaceCifs.SmbPacket smbPacketResponse = (NamespaceCifs.SmbPacket)response;

            this.QueryUidTable(smbPacketResponse);
            this.QueryTidTable(smbPacketResponse);

            VerifyTransport(smbPacketResponse);
            VerifyCommonMessageSyntax(smbPacketResponse);

            if (response.GetType() == typeof(SmbErrorResponsePacket))
            {
                SmbErrorResponsePacket smbErrorResponsePacket = response as SmbErrorResponsePacket;
                NamespaceCifs.SmbHeader smbErrorHeader = smbErrorResponsePacket.SmbHeader;
                this.ErrorResponse(smbErrorHeader.Mid + this.addMidMark, (MessageStatus)smbErrorHeader.Status);
            }
            else
            {
                SmbNtCreateAndxResponsePacket smbNtCreateAndXPacket = response as SmbNtCreateAndxResponsePacket;
                NamespaceCifs.SmbHeader ntCreateAndXResponseHeader = smbNtCreateAndXPacket.SmbHeader;

                SMB_COM_NT_CREATE_ANDX_Response_SMB_Parameters ntCreateAndXResponsePayload =
                    smbNtCreateAndXPacket.SmbParameters;

                this.smbFileId = ntCreateAndXResponsePayload.FID;

                int fidValue = (int)uint.MinValue;
                if (!this.fId.ContainsValue(ntCreateAndXResponsePayload.FID))
                {
                    int count = this.fId.Count;
                    this.fId.Add((uint)(count), ntCreateAndXResponsePayload.FID);
                    fidValue = count;
                }
                else
                {
                    foreach (uint key in this.fId.Keys)
                    {
                        if (this.fId[key] == ntCreateAndXResponsePayload.FID)
                        {
                            fidValue = (int)key;
                            break;
                        }
                    }
                }

                uint creatAction = ntCreateAndXResponsePayload.CreationAction;
                Microsoft.Modeling.Set<CreateAction> actionSet = new Microsoft.Modeling.Set<CreateAction>();
                if (creatAction == CreateActionSuperseded)
                {
                    actionSet = actionSet.Add(CreateAction.FileSuperseded);
                }

                if ((creatAction & CreateActionExists) == CreateActionExists)
                {
                    actionSet = actionSet.Add(CreateAction.FileExists);
                }

                if ((creatAction & CreateActionOpened) == CreateActionOpened)
                {
                    actionSet = actionSet.Add(CreateAction.FileOpened);
                    if (!actionSet.Contains(CreateAction.FileExists))
                    {
                        actionSet = actionSet.Add(CreateAction.FileExists);
                    }
                }

                if ((creatAction & CreateActionNotExists) == CreateActionNotExists)
                {
                    actionSet = actionSet.Add(CreateAction.FileDoesNotExist);
                }

                if ((creatAction & CreateActionCreated) == CreateActionCreated)
                {
                    actionSet = actionSet.Add(CreateAction.FileCreated);
                    if (!actionSet.Contains(CreateAction.FileDoesNotExist))
                    {
                        actionSet = actionSet.Add(CreateAction.FileDoesNotExist);
                    }
                }

                if ((creatAction & CreateActionOverwritten) == CreateActionOverwritten)
                {
                    actionSet = actionSet.Add(CreateAction.FileOverwritten);
                }

                bool isIdZero = false;
                bool isNoSubStreams = false;
                if (ntCreateAndXResponsePayload.ResourceType == NamespaceCifs.FileTypeValue.FileTypeDisk)
                {
                    if (SmbAdapter.FsType == FileSystemType.Ntfs.ToString())
                    {
                        isNoSubStreams = false;
                    }
                    else
                    {
                        isNoSubStreams = true;
                    }
                }

                if (SmbAdapter.ServiceShareType == ShareType.NamedPipe)
                {
                    isNoSubStreams = true;
                }

                bool isVolumnGuidZero = false;
                if (!this.checkWindowsImplementation)
                {
                    Site.Log.Add(
                        LogEntryKind.Comment,
                        @"isFileIdZero: {0};
                        isVolumnGUIDZero: {1}",
                        isIdZero,
                        isVolumnGuidZero);
                    isIdZero = true;
                    isVolumnGuidZero = true;
                }

                bool isNotionSupportedForCreate = Boolean.Parse(Site.Properties["IsNotionSupportedForCreate"]);
                bool isSerWantCliLeverageNewCap = Boolean.Parse(Site.Properties["IsSerWantCliLeverageNewCap"]);

                VerifyMessageSyntaxSmbComNtCreateAndXResponse(
                    smbNtCreateAndXPacket,
                    isSerWantCliLeverageNewCap);

                VerifyMessageSyntaxSmbComNtCreateAndXResponseForNotNotionSupported(
                    smbNtCreateAndXPacket,
                    isNotionSupportedForCreate);

                VerifyMessageSyntaxFileIdGeneration(smbNtCreateAndXPacket.SmbParameters.FileId);
                VerifyMessageSyntaxVolumeGUIDGeneration(smbNtCreateAndXPacket.SmbParameters.VolumeGUID);

                if (0x00000003 >= (uint)impersonationLevel)
                {
                    VerifyMessageSyntaxNtTransactCreateRequest(impersonationLevel);
                }

                VerifyMessageSyntaxAccessMasks(smbNtCreateAndXPacket, isDirectoryFile);

                uint computedMaximalAccessRights = (uint)Int32.Parse(Site.Properties["ComputedMaximalAccessRights"]);
                uint computedGuestMaximalAccessRights =
                    (uint)Int32.Parse(Site.Properties["ComputedGuestMaximalAccessRights"]);

                VerifyReceiveSmbComNtCreateAndXRequest(
                    (uint)smbPacket.SmbHeader.Flags,
                    smbNtCreateAndXPacket,
                    computedMaximalAccessRights,
                    computedGuestMaximalAccessRights,
                    impersonationLevel == (int)NamespaceCifs.NtTransactImpersonationLevel.SEC_ANONYMOUS ? true : false);

                //Workaround temp code (Invalid impersonation level)
                if ((string.Equals(Site.Properties["SutPlatformOS"], "Win2K8") && impersonationLevel == 4) &&
                    !bool.Parse(Site.Properties["IsTDI33006Fixed"]))
                {
                    this.ErrorResponse(ntCreateAndXResponseHeader.Mid + this.addMidMark, MessageStatus.BadImpersonationLevel);
                }
                else
                {
                    if (this.createActionInternal.Count == uint.MinValue)
                    {
                        this.CreateResponse(
                            ntCreateAndXResponseHeader.Mid + this.addMidMark,
                            this.QueryUidTable(smbPacketResponse),
                            this.QueryTidTable(smbPacketResponse),
                            fidValue,
                            smbPacketResponse.IsSignRequired,
                            actionSet,
                            isIdZero,
                            isVolumnGuidZero,
                            (ntCreateAndXResponsePayload.Directory == uint.MinValue),
                            true,
                            isNoSubStreams,
                            (MessageStatus)ntCreateAndXResponseHeader.Status);
                    }
                    else
                    {
                        this.CreateResponse(
                            ntCreateAndXResponseHeader.Mid + this.addMidMark,
                            this.QueryUidTable(smbPacketResponse),
                            this.QueryTidTable(smbPacketResponse),
                            fidValue,
                            (smbPacketResponse).IsSignRequired,
                            this.createActionInternal,
                            isIdZero,
                            isVolumnGuidZero,
                            (ntCreateAndXResponsePayload.Directory == 0),
                            true,
                            isNoSubStreams,
                            (MessageStatus)ntCreateAndXResponseHeader.Status);
                    }
                }
                this.createActionInternal = new Microsoft.Modeling.Set<CreateAction>();
            }

            #endregion
        }
        /// <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;
        }