// Use TestInitialize to run code before running every test in the class
 protected override void TestInitialize()
 {
     base.TestInitialize();
     fsrvpStatus = FsrvpStatus.None;
     shadowCopySetId = Guid.Empty;
     shadowCopyList.Clear();
 }
        /// <summary>
        /// Test the operation of shadow copy set.
        /// </summary>
        /// <param name="context">The context to be used for the shadow copy operations.</param>
        /// <param name="shareUncPaths">The full path list of the shares in UNC format.</param>
        /// <param name="statusToAbort">Indicates which status the server is in, the creation process will be aborted and exit.
        /// FsrvpStatus.None indicates that AbortShadowCopySet will not be called and all operations will be executed.</param>
        /// <param name="sharePathsType">Indicates the type of share paths.</param>
        private void TestShadowCopySet(ulong context, List<string> shareUncPaths, FsrvpStatus statusToAbort,
            FsrvpSharePathsType sharePathsType)
        {
            int ret;
            string FsrvpServerName;

            #region Query FSRVP Server Name
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "Query FSRVP Server Name.");
            FsrvpServerName = GetFsrvpServerName(shareUncPaths[0]);
            #endregion

            #region Connect to FSRVP server
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "Connect to FSRVP server.");
            fsrvpClientForCreation = new FsrvpClient();
            DoUntilSucceed(() => ConnectServer(ref fsrvpClientForCreation, FsrvpServerName), TestConfig.LongerTimeout,
                "Retry ConnectServer until succeed within timeout span");
            #endregion

            #region GetSupportedVersion
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "Get supported FSRVP versions.");
            BaseTestSite.Log.Add(LogEntryKind.Debug, "Start to call GetSupportedVersion(out MinVersion, out MaxVersion)");
            uint MinVersion;
            uint MaxVersion;
            ret = fsrvpClientForCreation.GetSupportedVersion(out MinVersion, out MaxVersion);
            BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
            BaseTestSite.Assert.AreEqual<uint>((uint)FsrvpVersionValues.FSRVP_RPC_VERSION_1,
                MinVersion,
                "Expect that the minimum version of the protocol supported by the server is 0x{0:x8}. The server actually returns 0x{1:x8}.",
                (uint)FsrvpVersionValues.FSRVP_RPC_VERSION_1,
                MinVersion);
            BaseTestSite.Assert.AreEqual<uint>((uint)FsrvpVersionValues.FSRVP_RPC_VERSION_1,
                MaxVersion,
                "Expect that the maximum version of the protocol supported by the server is 0x{0:x8}. The server actually returns 0x{1:x8}.",
                (uint)FsrvpVersionValues.FSRVP_RPC_VERSION_1,
                MaxVersion);
            #endregion

            #region SetContext
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "SetContext.");
            BaseTestSite.Log.Add(LogEntryKind.Debug, "Start to call SetContext(0x{0:x8}).", context);
            ret = fsrvpClientForCreation.SetContext(context);
            BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
            #endregion

            #region StartShadowCopySet
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "StartShadowCopySet.");
            Guid clientShadowCopySetId = Guid.NewGuid();
            BaseTestSite.Log.Add(LogEntryKind.Debug,
                "Start to call StartShadowCopySet({0}, out pShadowCopySetId)",
                clientShadowCopySetId);
            ret = fsrvpClientForCreation.StartShadowCopySet(clientShadowCopySetId, out shadowCopySetId);
            BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
            BaseTestSite.Assert.AreNotEqual<Guid>(Guid.Empty, shadowCopySetId,
                "The server is expected to return a valid shadowCopySetId. But the shadowCopySetId which the server returns is empty.");
            fsrvpStatus = FsrvpStatus.Started;
            #endregion

            if (statusToAbort == FsrvpStatus.Started)
            {
                BaseTestSite.Log.Add(LogEntryKind.TestStep, "Abort StartShadowCopySet request.");
                BaseTestSite.Log.Add(LogEntryKind.Debug,
                    "Start to call AbortShadowCopySet({0})",
                    shadowCopySetId);
                ret = fsrvpClientForCreation.AbortShadowCopySet(shadowCopySetId);
                BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                    "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
                return;
            }

            #region AddToShadowCopySet
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "AddToShadowCopySet.");
            if (sharePathsType == FsrvpSharePathsType.OnClusterAndNonOwnerNode ||
                sharePathsType == FsrvpSharePathsType.OnDifferentNode)
            {
                // Negative test cases
                BaseTestSite.Assume.IsTrue(shareUncPaths.Count == 2, "shareUncPaths should contains two paths.");

                #region Valid ShareName
                FsrvpClientShadowCopy shadowCopy = new FsrvpClientShadowCopy();
                shadowCopy.shareName = shareUncPaths[0];
                shadowCopy.clientShadowCopyId = Guid.NewGuid();
                BaseTestSite.Log.Add(LogEntryKind.Debug,
                    "Start to call AddToShadowCopySet({0},{1},{2},out pShadowCopyId)",
                    shadowCopy.clientShadowCopyId, shadowCopySetId, shadowCopy.shareName);
                ret = fsrvpClientForCreation.AddToShadowCopySet(shadowCopy.clientShadowCopyId,
                    shadowCopySetId,
                    shadowCopy.shareName,
                    out shadowCopy.serverShadowCopyId);

                BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                    "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
                BaseTestSite.Assert.AreNotEqual<Guid>(Guid.Empty, shadowCopy.serverShadowCopyId,
                    "The server is expected to send a valid shadowCopyId.  But the shadowCopyId which the server returns is empty.");
                shadowCopyList.Add(shadowCopy);
                fsrvpStatus = FsrvpStatus.Added;
                #endregion

                #region Invalid ShareName
                shadowCopy = new FsrvpClientShadowCopy();
                shadowCopy.shareName = shareUncPaths[1];
                shadowCopy.clientShadowCopyId = Guid.NewGuid();
                BaseTestSite.Log.Add(LogEntryKind.Debug,
                    "Start to call AddToShadowCopySet({0},{1},{2},out pShadowCopyId)",
                    shadowCopy.clientShadowCopyId, shadowCopySetId, shadowCopy.shareName);
                ret = fsrvpClientForCreation.AddToShadowCopySet(shadowCopy.clientShadowCopyId,
                    shadowCopySetId,
                    shadowCopy.shareName,
                    out shadowCopy.serverShadowCopyId);

                BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_E_INVALIDARG, (FsrvpErrorCode)ret,
                    "The server is expected to return 0x{0:x8}. The server actually returns 0x{1:x8}.",
                    (uint)FsrvpErrorCode.FSRVP_E_INVALIDARG, ret);
                BaseTestSite.Assert.AreEqual<Guid>(Guid.Empty, shadowCopy.serverShadowCopyId,
                    "The server is expected to send an empty shadowCopyId.  The actual shadowCopyId which the server returns is {0}.",
                    shadowCopy.serverShadowCopyId.ToString());
                return;
                #endregion
            }
            else
            {
                foreach (string shareUncPath in shareUncPaths)
                {
                    FsrvpClientShadowCopy shadowCopy = new FsrvpClientShadowCopy();
                    shadowCopy.shareName = shareUncPath;
                    shadowCopy.clientShadowCopyId = Guid.NewGuid();
                    BaseTestSite.Log.Add(LogEntryKind.Debug,
                        "Start to call AddToShadowCopySet({0},{1},{2},out pShadowCopyId)",
                        shadowCopy.clientShadowCopyId, shadowCopySetId, shareUncPath);
                    ret = fsrvpClientForCreation.AddToShadowCopySet(shadowCopy.clientShadowCopyId,
                        shadowCopySetId,
                        shareUncPath,
                        out shadowCopy.serverShadowCopyId);

                    BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                        "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
                    BaseTestSite.Assert.AreNotEqual<Guid>(Guid.Empty, shadowCopy.serverShadowCopyId,
                        "The server is expected to send a valid shadowCopyId.  But the shadowCopyId which the server returns is empty.");
                    shadowCopyList.Add(shadowCopy);

                    fsrvpStatus = FsrvpStatus.Added;
                }
            }
            #endregion

            if (statusToAbort == FsrvpStatus.Added)
            {
                BaseTestSite.Log.Add(LogEntryKind.TestStep, "Abort AddToShadowCopySet request.");
                BaseTestSite.Log.Add(LogEntryKind.Debug,
                    "Start to call AbortShadowCopySet({0})",
                    shadowCopySetId);
                ret = fsrvpClientForCreation.AbortShadowCopySet(shadowCopySetId);
                BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                    "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
                return;
            }

            #region PrepareShadowCopySet
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "PrepareShadowCopySet.");
            BaseTestSite.Log.Add(LogEntryKind.Debug,
                "Start to call PrepareShadowCopySet({0}, {1})",
                shadowCopySetId, FsrvpUtility.FSRVPPrepareTimeoutInSeconds * 1000);
            ret = fsrvpClientForCreation.PrepareShadowCopySet(shadowCopySetId,
                (uint)FsrvpUtility.FSRVPPrepareTimeoutInSeconds * 1000);
            BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
            fsrvpStatus = FsrvpStatus.CreateInProgress;
            #endregion

            #region CommitShadowCopySet
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "CommitShadowCopySet.");
            BaseTestSite.Log.Add(LogEntryKind.Debug,
                "Start to call CommitShadowCopySet({0},{1})",
                shadowCopySetId,
                FsrvpUtility.FSRVPCommitTimeoutInSeconds * 1000);
            ret = fsrvpClientForCreation.CommitShadowCopySet(shadowCopySetId,
                (uint)FsrvpUtility.FSRVPCommitTimeoutInSeconds * 1000);
            BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
            fsrvpStatus = FsrvpStatus.Committed;
            #endregion

            #region ExposeShadowCopySet
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "ExposeShadowCopySet.");
            BaseTestSite.Log.Add(LogEntryKind.Debug,
                "Start to call ExposeShadowCopySet({0}, {1})",
                shadowCopySetId,
                FsrvpUtility.FSRVPExposeTimeoutInSeconds * 1000);
            ret = fsrvpClientForCreation.ExposeShadowCopySet(shadowCopySetId,
                (uint)FsrvpUtility.FSRVPExposeTimeoutInSeconds * 1000);
            BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
            fsrvpStatus = FsrvpStatus.Exposed;
            #endregion

            #region GetShareMapping
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "GetShareMapping.");
            for (int i = 0; i < shadowCopyList.Count; i++)
            {
                FsrvpClientShadowCopy shadowCopy = shadowCopyList[i];
                FSSAGENT_SHARE_MAPPING mapping;
                BaseTestSite.Log.Add(LogEntryKind.Debug,
                    "Start to call GetShareMapping({0}, {1}, {2}, {3}, out ShareMapping)",
                    shadowCopy.serverShadowCopyId,
                    shadowCopySetId,
                    shadowCopy.shareName,
                    FsrvpLevel.FSRVP_LEVEL_1);
                ret = fsrvpClientForCreation.GetShareMapping(shadowCopy.serverShadowCopyId,
                    shadowCopySetId,
                    shadowCopy.shareName,
                    (uint)FsrvpLevel.FSRVP_LEVEL_1,
                    out mapping);
                BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                    "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
                BaseTestSite.Assert.IsFalse(mapping.ShareMapping1IsNull,
                    "Expect ShareMapping.ShareMapping1 the server returns is not null.");
                string exposedName = GetExposedShareName(shadowCopy.shareName, shadowCopy.serverShadowCopyId);
                BaseTestSite.Assert.IsTrue(
                    mapping.ShareMapping1.ShadowCopyShareName.Equals(exposedName, StringComparison.InvariantCultureIgnoreCase),
                    "Expect the exposed sharename returns by server is valid.");
                BaseTestSite.Assert.IsTrue(
                    mapping.ShareMapping1.CreationTimestamp > 0,
                    "Expect the CreationTimestamp returns by server is valid.");

                shadowCopy.exposedName = mapping.ShareMapping1.ShadowCopyShareName;
                shadowCopy.CreationTimestamp = mapping.ShareMapping1.CreationTimestamp;

                shadowCopyList[i] = shadowCopy;
            }
            #endregion

            #region Create a file in the exposed share and expect the failure.
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "Create a file in the exposed share and expect the failure.");
            foreach (FsrvpClientShadowCopy shadowCopy in shadowCopyList)
            {
                string exposedSharePath = @"\\" + FsrvpServerName + @"\" + shadowCopy.exposedName;
                BaseTestSite.Log.Add(LogEntryKind.Debug, "Create a file {0} in the share: {1}.",
                    CurrentTestCaseName,
                    exposedSharePath);
                bool result = sutProtocolController.CreateFile(exposedSharePath, CurrentTestCaseName, string.Empty);
                if ((context & (ulong)FsrvpShadowCopyAttributes.FSRVP_ATTR_AUTO_RECOVERY) != 0)
                {
                    // Test writable snapshot
                    BaseTestSite.Assert.IsTrue(result, "Expect that creating the file in the share succeeds.");
                }
                else
                {
                    // Test readonly snapshot
                    BaseTestSite.Assert.IsFalse(result, "Expect that creating the file in the share fails.");
                }
            }
            #endregion

            #region RecoveryCompleteShadowCopySet
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "RecoveryCompleteShadowCopySet.");
            BaseTestSite.Log.Add(LogEntryKind.Debug,
                "Start to call RecoveryCompleteShadowCopySet({0})",
                shadowCopySetId);
            ret = fsrvpClientForCreation.RecoveryCompleteShadowCopySet(shadowCopySetId);
            BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
            fsrvpStatus = FsrvpStatus.Recovered;
            #endregion

            if (statusToAbort == FsrvpStatus.Recovered)
            {
                BaseTestSite.Log.Add(LogEntryKind.TestStep, "Abort RecoveryCompleteShadowCopySet request.");
                BaseTestSite.Log.Add(LogEntryKind.Debug,
                    "Start to call AbortShadowCopySet({0})",
                    shadowCopySetId);
                ret = fsrvpClientForCreation.AbortShadowCopySet(shadowCopySetId);
                BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_E_BAD_STATE, (FsrvpErrorCode)ret,
                    "The server is expected to return 0x{0:x8}. The server actually returns 0x{1:x8}.",
                    (uint)FsrvpErrorCode.FSRVP_E_BAD_STATE, ret);
            }

            #region IsPathShadowCopied
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "Check if the share paths are shadow copied.");
            foreach (string shareName in shareUncPaths)
            {
                BaseTestSite.Log.Add(LogEntryKind.Debug,
                    "Start to call IsPathShadowCopied({0}, out ShadowCopyPresent, out ShadowCopyCompatibility)",
                    shareName);
                bool ShadowCopyPresent = false;
                long ShadowCopyCompatibility = 0;
                ret = fsrvpClientForCreation.IsPathShadowCopied(shareName, out ShadowCopyPresent, out ShadowCopyCompatibility);
                BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                    "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
                BaseTestSite.Assert.IsTrue(ShadowCopyPresent,
                    "Expect that ShadowCopyPresent returned by the server is true.");
                BaseTestSite.Assert.IsTrue((ShadowCopyCompatibility == 0)
                    || (ShadowCopyCompatibility == (long)FsrvpShadowCopyCompatibilityValues.FSRVP_DISABLE_CONTENTINDEX)
                    || (ShadowCopyCompatibility == (long)FsrvpShadowCopyCompatibilityValues.FSRVP_DISABLE_DEFRAG)
                    || (ShadowCopyCompatibility == (long)(FsrvpShadowCopyCompatibilityValues.FSRVP_DISABLE_CONTENTINDEX | FsrvpShadowCopyCompatibilityValues.FSRVP_DISABLE_DEFRAG)),
                    "Expect that ShadowCopyCompatibility returned by the server is valid. The server actually returns 0x{0:x8}.",
                    ShadowCopyCompatibility);
            }
            #endregion

            #region DeleteShareMapping
            BaseTestSite.Log.Add(LogEntryKind.TestStep, "DeleteShareMapping.");
            foreach (FsrvpClientShadowCopy shadowCopy in shadowCopyList)
            {
                BaseTestSite.Log.Add(LogEntryKind.Debug,
                    "Start to call DeleteShareMapping({0}, {1}, {2})",
                    shadowCopySetId,
                    shadowCopy.serverShadowCopyId,
                    shadowCopy.shareName);
                ret = fsrvpClientForCreation.DeleteShareMapping(shadowCopySetId,
                    shadowCopy.serverShadowCopyId,
                    shadowCopy.shareName);
                BaseTestSite.Assert.AreEqual<FsrvpErrorCode>(FsrvpErrorCode.FSRVP_SUCCESS, (FsrvpErrorCode)ret,
                    "The server is expected to return 0x00000000. The server actually returns 0x{0:x8}.", ret);
            }
            shadowCopyList.Clear();
            #endregion
        }
        // Use TestCleanup to run code after every test in a class have run
        protected override void TestCleanup()
        {
            if (fsrvpClientForCreation != null)
            {
                try
                {
                    if (fsrvpStatus == FsrvpStatus.Started || fsrvpStatus == FsrvpStatus.Added)
                    {
                        fsrvpClientForCreation.AbortShadowCopySet(shadowCopySetId);
                    }

                    if (fsrvpStatus == FsrvpStatus.CreateInProgress)
                    {
                        fsrvpClientForCreation.CommitShadowCopySet(shadowCopySetId,
                            (uint)FsrvpUtility.FSRVPCommitTimeoutInSeconds * 1000);
                        fsrvpStatus = FsrvpStatus.Committed;
                    }

                    if (fsrvpStatus == FsrvpStatus.Committed)
                    {
                        fsrvpClientForCreation.ExposeShadowCopySet(shadowCopySetId,
                            (uint)FsrvpUtility.FSRVPExposeTimeoutInSeconds * 1000);
                        fsrvpStatus = FsrvpStatus.Exposed;

                        #region GetShareMapping
                        for (int i = 0; i < shadowCopyList.Count; i++)
                        {
                            FsrvpClientShadowCopy shadowCopy = shadowCopyList[i];
                            FSSAGENT_SHARE_MAPPING mapping;
                            int ret = fsrvpClientForCreation.GetShareMapping(shadowCopy.serverShadowCopyId,
                                shadowCopySetId,
                                shadowCopy.shareName,
                                (uint)FsrvpLevel.FSRVP_LEVEL_1,
                                out mapping);
                            if ((FsrvpErrorCode)ret == FsrvpErrorCode.FSRVP_SUCCESS)
                            {
                                shadowCopy.exposedName = mapping.ShareMapping1.ShadowCopyShareName;
                                shadowCopy.CreationTimestamp = mapping.ShareMapping1.CreationTimestamp;

                                shadowCopyList[i] = shadowCopy;
                            }
                        }
                        #endregion
                    }

                    if (fsrvpStatus == FsrvpStatus.Exposed)
                    {
                        fsrvpClientForCreation.RecoveryCompleteShadowCopySet(shadowCopySetId);
                            fsrvpStatus = FsrvpStatus.Recovered;

                        foreach (FsrvpClientShadowCopy shadowCopy in shadowCopyList)
                        {
                            fsrvpClientForCreation.DeleteShareMapping(shadowCopySetId,
                                shadowCopy.serverShadowCopyId,
                                shadowCopy.shareName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    BaseTestSite.Log.Add(LogEntryKind.Warning, "TestCleanup at status {0} got an unexpected Exception: {1}", (uint)fsrvpStatus, ex.Message);
                }

                DisconnectServer(ref fsrvpClientForCreation);
            }
            shadowCopyList.Clear();
            fsrvpStatus = FsrvpStatus.None;

            base.TestCleanup();
        }