Microsoft.Protocols.TestSuites.MS_OXORULE.S03_ProcessOutOfOfficeRule.MSOXORULE_S03_TC02_OOFBehaviorsExecuteSubRuleWithST_ONLY_WHEN_OOFForST_EXIT_LEVEL C# (CSharp) Method

MSOXORULE_S03_TC02_OOFBehaviorsExecuteSubRuleWithST_ONLY_WHEN_OOFForST_EXIT_LEVEL() private method

        public void MSOXORULE_S03_TC02_OOFBehaviorsExecuteSubRuleWithST_ONLY_WHEN_OOFForST_EXIT_LEVEL()
        {
            this.CheckMAPIHTTPTransportSupported();

            #region Prepare value for ruleProperties variable.
            RuleProperties ruleProperties = AdapterHelper.GenerateRuleProperties(this.Site, Constants.RuleNameTag);
            string setOOFMailAddress = this.User1Name + "@" + this.Domain;
            string userPassword = this.User1Password;
            #endregion

            #region Set TestUser1 to OOF state.
            bool isSetOOFSuccess = this.SUTSetOOFAdapter.SetUserOOFSettings(setOOFMailAddress, userPassword, true);
            Site.Assert.IsTrue(isSetOOFSuccess, "Turn Out of Office on for {0} should succeed.", this.User1Name);
            Thread.Sleep(this.WaitForSetOOFComplete);
            #endregion

            #region TestUser1 adds an OP_TAG rule with PidTagRuleState set to ST_ENABLED | ST_EXIT_LEVEL.
            TagActionData tagActionData = new TagActionData();
            PropertyTag tagActionDataPropertyTag = new PropertyTag
            {
                PropertyId = (ushort)PropertyId.PidTagImportance,
                PropertyType = (ushort)PropertyType.PtypInteger32
            };
            tagActionData.PropertyTag = tagActionDataPropertyTag;
            tagActionData.PropertyValue = BitConverter.GetBytes(2);

            RuleData ruleOpTag = AdapterHelper.GenerateValidRuleData(ActionType.OP_TAG, TestRuleDataType.ForAdd, 1, RuleState.ST_ENABLED | RuleState.ST_EXIT_LEVEL, tagActionData, ruleProperties, null);
            RopModifyRulesResponse ropModifyRulesResponse = this.OxoruleAdapter.RopModifyRules(this.InboxFolderHandle, ModifyRuleFlag.Modify_ReplaceAll, new RuleData[] { ruleOpTag });
            Site.Assert.AreEqual<uint>(0, ropModifyRulesResponse.ReturnValue, "Adding OP_TAG rule should succeed");
            #endregion

            #region TestUser1 adds OP_MARK_AS_READ rule with PidTagRuleState set to ST_ENABLED | ST_ONLY_WHEN_OOF.
            ruleProperties.Name = Common.GenerateResourceName(this.Site, Constants.RuleNameMarkAsRead);

            RuleData ruleForMarkRead = AdapterHelper.GenerateValidRuleData(ActionType.OP_MARK_AS_READ, TestRuleDataType.ForAdd, 2, RuleState.ST_ENABLED | RuleState.ST_ONLY_WHEN_OOF, new DeleteMarkReadActionData(), ruleProperties, null);
            ropModifyRulesResponse = this.OxoruleAdapter.RopModifyRules(this.InboxFolderHandle, ModifyRuleFlag.Modify_OnExisting, new RuleData[] { ruleForMarkRead });
            Site.Assert.AreEqual<uint>(0, ropModifyRulesResponse.ReturnValue, "Adding Mark as read rule should succeed.");
            #endregion

            #region TestUser2 delivers a message to TestUser1 to trigger these rules.
            // Sleep enough time to wait for the rule to take effect.
            Thread.Sleep(this.WaitForTheRuleToTakeEffect);
            string mailSubject = Common.GenerateResourceName(this.Site, ruleProperties.ConditionSubjectName, 1);
            this.SUTAdapter.SendMailToRecipient(this.User2Name, this.User2Password, this.User1Name, mailSubject);

            // Sleep enough time to wait for the rule to be executed on the delivered message.
            Thread.Sleep(this.WaitForTheRuleToTakeEffect);
            #endregion

            #region Testuser1 verifies whether the specific property value is set on the received mail.
            PropertyTag[] propertyTagList = new PropertyTag[3];
            propertyTagList[0].PropertyId = (ushort)PropertyId.PidTagImportance;
            propertyTagList[0].PropertyType = (ushort)PropertyType.PtypInteger32;
            propertyTagList[1].PropertyId = (ushort)PropertyId.PidTagSubject;
            propertyTagList[1].PropertyType = (ushort)PropertyType.PtypString;
            propertyTagList[2].PropertyId = (ushort)PropertyId.PidTagMessageFlags;
            propertyTagList[2].PropertyType = (ushort)PropertyType.PtypInteger32;

            uint contentsTableHandle = 0;
            int expectedMessageIndex = 0;
            RopQueryRowsResponse getMailMessageContent = this.GetExpectedMessage(this.InboxFolderHandle, ref contentsTableHandle, propertyTagList, ref expectedMessageIndex, mailSubject);

            // If the PidTagImportance is the value which is set on OP_TAG rule, it means the rule tacks action and the rule sets the property specified in the rule's action buffer structure.
            Site.Assert.AreEqual<int>(2, BitConverter.ToInt32(getMailMessageContent.RowData.PropertyRows[expectedMessageIndex].PropertyValues[0].Value, 0), "The value of PidTagImportance field should be the specific value set by the client.");
            #endregion

            #region Testuser1 verifies whether the received mail is marked as read.
            int messageFlags = 0;
            bool isSubjectContainsRuleConditionSubject = false;
            if (getMailMessageContent.RowCount != 0)
            {
                isSubjectContainsRuleConditionSubject = true;
                messageFlags = BitConverter.ToInt32(getMailMessageContent.RowData.PropertyRows[expectedMessageIndex].PropertyValues[2].Value, 0);
            }

            #region Capture Code
            // Add the debug information.
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-OXORULE_R807, the value of markAsReadFlag is {0}", messageFlags);

            // Verify MS-OXORULE requirement: MS-OXORULE_R807.
            // messageFlags indicates whether the mail is marked as read. 
            // 0x00000001 is the bit which indicates the message has been marked as read. So if this bit is set in messageFlags,
            // it means the message is marked as read, which means the rule has been executed.
            bool isVerifyR807 = isSubjectContainsRuleConditionSubject && (messageFlags & 0x00000001) == 0x00000001;

            Site.CaptureRequirementIfIsTrue(
                isVerifyR807,
                807,
                @"[In Interaction Between ST_ONLY_WHEN_OOF and ST_EXIT_LEVEL Flags] Subsequent rules (2) that have the ST_ONLY_WHEN_OOF flag set MUST be evaluated.");

            // When is not mark as read, it means rule evaluation will not terminate.
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-OXORULE_R858");

            // Verify MS-OXORULE requirement: MS-OXORULE_R858
            // If the message is marked as read, it means the rule evaluation is not terminate.
            Site.CaptureRequirementIfAreEqual<int>(
                1,
                messageFlags & 1,
                858,
                "[In PidTagRuleState Property] EL (ST_EXIT_LEVEL, Bitmask 0x00000010): rule (2) evaluation will not terminate after executing this rule (2) if for evaluation of Out of Office rules.");

            #endregion
            #endregion

            #region Set TestUser1 back to normal state (not in OOF state).
            isSetOOFSuccess = this.SUTSetOOFAdapter.SetUserOOFSettings(setOOFMailAddress, userPassword, false);
            Site.Assert.IsTrue(isSetOOFSuccess, "Turn Out of Office off for {0} should succeed.", this.User1Name);
            Thread.Sleep(this.WaitForSetOOFComplete);
            #endregion

            #region TestUser1 adds an OP_Forward rule with rule sequence set to 2.
            ruleProperties.Name = Common.GenerateResourceName(this.Site, Constants.RuleNameForward);
            ForwardDelegateActionData forwardActionData = new ForwardDelegateActionData
            {
                RecipientCount = (ushort)0x01
            };
            RecipientBlock recipientBlock = new RecipientBlock
            {
                Reserved = 0x01,
                NoOfProperties = (ushort)0x04u
            };

            #region Prepare the recipient Block of the rule to forward the message to TestUser2.
            TaggedPropertyValue[] recipientProperties = AdapterHelper.GenerateRecipientPropertiesBlock(this.User2Name, this.User2ESSDN);
            recipientBlock.PropertiesData = recipientProperties;

            #endregion

            forwardActionData.RecipientsData = new RecipientBlock[1] { recipientBlock };
            RuleData ruleForward = AdapterHelper.GenerateValidRuleData(ActionType.OP_FORWARD, TestRuleDataType.ForAdd, 2, RuleState.ST_ENABLED, forwardActionData, ruleProperties, null);
            ropModifyRulesResponse = this.OxoruleAdapter.RopModifyRules(this.InboxFolderHandle, ModifyRuleFlag.Modify_OnExisting, new RuleData[] { ruleForward });
            Site.Assert.AreEqual<uint>(0, ropModifyRulesResponse.ReturnValue, "Adding Forward rule should succeed.");
            #endregion

            #region TestUser2 delivers a message to TestUser1 to trigger these rules.
            mailSubject = Common.GenerateResourceName(this.Site, ruleProperties.ConditionSubjectName, 2);
            this.SUTAdapter.SendMailToRecipient(this.User2Name, this.User2Password, this.User1Name, mailSubject);

            // Sleep enough time to wait for the rule to be executed on the delivered message.
            Thread.Sleep(this.WaitForTheRuleToTakeEffect);

            #endregion

            #region Testuser1 verifies whether the specific property value is set on the received mail.
            PropertyTag[] propertyTagListInNormal = new PropertyTag[3];
            propertyTagListInNormal[0].PropertyId = (ushort)PropertyId.PidTagImportance;
            propertyTagListInNormal[0].PropertyType = (ushort)PropertyType.PtypInteger32;
            propertyTagListInNormal[1].PropertyId = (ushort)PropertyId.PidTagSubject;
            propertyTagListInNormal[1].PropertyType = (ushort)PropertyType.PtypString;
            propertyTagListInNormal[2].PropertyId = (ushort)PropertyId.PidTagMessageFlags;
            propertyTagListInNormal[2].PropertyType = (ushort)PropertyType.PtypInteger32;

            uint contentTableHandlerNormal = 0;
            expectedMessageIndex = 0;
            RopQueryRowsResponse getMailMessageContentNormal = this.GetExpectedMessage(this.InboxFolderHandle, ref contentTableHandlerNormal, propertyTagListInNormal, ref expectedMessageIndex, mailSubject);

            // If the PidTagImportance is the value which is set on OP_TAG rule, it means the rule tacks action and the rule sets the property specified in the rule's action buffer structure.
            Site.Assert.AreEqual<int>(2, BitConverter.ToInt32(getMailMessageContentNormal.RowData.PropertyRows[expectedMessageIndex].PropertyValues[0].Value, 0), "The value of PidTagImportance field should be the specific value set by the client.");
            #endregion

            #region Testuser1 verifies whether the received mail is marked as read.
            bool isSubjectContainsRuleConditionSubjectNormal = false;
            int messageFlagsNormal = 0;
            string mailSubjectNormal = AdapterHelper.PropertyValueConvertToString(getMailMessageContentNormal.RowData.PropertyRows[expectedMessageIndex].PropertyValues[1].Value);
            if (mailSubjectNormal.Contains(mailSubject))
            {
                isSubjectContainsRuleConditionSubjectNormal = true;
                messageFlagsNormal = BitConverter.ToInt32(getMailMessageContentNormal.RowData.PropertyRows[expectedMessageIndex].PropertyValues[2].Value, 0);
            }

            // Verify MS-OXORULE requirement: MS-OXORULE_R67.
            // If the message is marked as read means the execution of the OOF rule was done by the OOF user. 
            bool isExcuted = (messageFlags & 0x00000001) == 0x00000001;

            // If the message is not marked as read, it means the execution of the OOF rule was not done by the normal user.  
            bool isNotExcute = isSubjectContainsRuleConditionSubjectNormal && (messageFlagsNormal & 0x00000001) != 0x00000001;

            // Add the debug information.
            Site.Log.Add(LogEntryKind.Debug, "Verify MS-OXORULE_R67: the value of markAsReadFlag for OOF user is {0}, and for normal user is {0}", messageFlags, messageFlagsNormal);

            // Indicate whether the OOF rule is executed by the OOF user.
            bool isVerify67 = isExcuted && isNotExcute == true;

            Site.CaptureRequirementIfIsTrue(
                isVerify67,
                67,
                "[In PidTagRuleState] OF (ST_ONLY_WHEN_OOF, Bitmask 0x00000004): The rule (2) is executed only when a user sets the Out of Office (OOF) state on the mailbox, as specified in [MS-OXWOOF] section 2.2.5.2.");
            #endregion

            #region TestUser2 Verifies whether the OP_Forward rule is executed.
            // Let TestUser2 logon to the server.
            this.LogonMailbox(TestUser.TestUser2);

            bool doesUnexpectedMessageExist = this.CheckUnexpectedMessageExist(this.InboxFolderHandle, ref contentTableHandlerNormal, propertyTagListInNormal, mailSubject);

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

            // Verify MS-OXORULE requirement: MS-OXORULE_R857.
            // For Out of Office rules, R858 has been verified in the above step, so if the OP_Forward rule can not be executed, this requirement can be captured.
            Site.CaptureRequirementIfIsFalse(
                doesUnexpectedMessageExist,
                857,
                "[In PidTagRuleState Property] EL (ST_EXIT_LEVEL, Bitmask 0x00000010): rule (2) evaluation will terminate after executing this rule (2) except for evaluation of Out of Office rules.");
            #endregion
        }