private static void RunTests(TestingRow testingRow, string testDataDir)
        {
            Console.WriteLine(testingRow);

            IKsiSignature signature;

            try
            {
                signature = new KsiSignatureFactory(new EmptyVerificationPolicy()).Create(File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, testDataDir + testingRow.FileName)));
            }
            catch (KsiException)
            {
                if (testingRow.ActionName != "parsing")
                {
                    throw;
                }
                return;
            }

            IVerificationContext verificationContext = GetVerificationContext(testingRow, signature, testDataDir, testingRow.ActionName == "userPublication");

            switch (testingRow.ActionName)
            {
            case "userPublication":
                Verify(testingRow, new PublicationBasedVerificationPolicy(), verificationContext);
                break;

            case "publicationsFile":
                Verify(testingRow, new PublicationBasedVerificationPolicy(), verificationContext);
                break;

            case "key":
                Verify(testingRow,
                       new KeyBasedVerificationPolicy(),
                       verificationContext);
                break;

            case "internal":
                Verify(testingRow, new InternalVerificationPolicy(), verificationContext);
                break;

            case "calendar":
                Verify(testingRow, new CalendarBasedVerificationPolicy(), verificationContext);
                break;

            case "parsing":
                Assert.Fail("Parsing exception expected but nothing thrown.");
                break;

            case "not-implemented":
                break;

            default:
                throw new Exception("Unknown testing action: " + testingRow.ActionName);
            }
        }
        private static void Verify(TestingRow data, VerificationPolicy policy, IVerificationContext context)
        {
            VerificationResult result = policy.Verify(context);

            if (!data.VerificationResultMatch(result.VerificationError))
            {
                Assert.Fail("Unexpected verification result: " + (result.VerificationError == null ? "OK" : result.VerificationError.Code) + "; Expected result: " +
                            data.ErrorCode);
            }
        }
        private static IVerificationContext GetVerificationContext(TestingRow testingRow, IKsiSignature signature, string testDataDir, bool setUserPublication = false)
        {
            IPublicationsFile publicationsFile = null;
            IKsiService       service;

            if (!setUserPublication)
            {
                publicationsFile = GetPublicationsFile(string.IsNullOrEmpty(testingRow.PublicationsFilePath) ? null : testDataDir + testingRow.PublicationsFilePath,
                                                       string.IsNullOrEmpty(testingRow.CertFilePath) ? null : testDataDir + testingRow.CertFilePath);
            }

            if (string.IsNullOrEmpty(testingRow.ResourceFile))
            {
                service = IntegrationTests.GetHttpKsiService();
            }
            else
            {
                TestKsiServiceProtocol protocol = new TestKsiServiceProtocol
                {
                    RequestResult = File.ReadAllBytes(Path.Combine(TestSetup.LocalPath, testDataDir + testingRow.ResourceFile))
                };
                service =
                    new TestKsiService(
                        protocol,
                        new ServiceCredentials(Properties.Settings.Default.HttpSigningServiceUser, Properties.Settings.Default.HttpSigningServicePass,
                                               TestUtil.GetHashAlgorithm(Properties.Settings.Default.HttpSigningServiceHmacAlgorithm)),
                        protocol,
                        new ServiceCredentials(Properties.Settings.Default.HttpExtendingServiceUser, Properties.Settings.Default.HttpExtendingServicePass,
                                               TestUtil.GetHashAlgorithm(Properties.Settings.Default.HttpExtendingServiceHmacAlgorithm)),
                        protocol,
                        new PublicationsFileFactory(
                            new PkiTrustStoreProvider(new X509Store(StoreName.Root),
                                                      CryptoTestFactory.CreateCertificateSubjectRdnSelector("[email protected]"))), 1, PduVersion.v2);
            }

            return(new VerificationContext(signature)
            {
                DocumentHash = testingRow.InputHash,
                UserPublication = setUserPublication ? testingRow.PublicationData : null,
                IsExtendingAllowed = testingRow.IsExtendingAllowed,
                KsiService = service,
                PublicationsFile = publicationsFile,
                DocumentHashLevel = testingRow.InputHashLevel
            });
        }
 public void TestPolicies(TestingRow row)
 {
     RunTests(row, PolicyVerificationDir);
 }
 public void TestInternalPolicies(TestingRow row)
 {
     RunTests(row, InternalPolicyVerificationDir);
 }
 public void TestValidSignatures(TestingRow row)
 {
     RunTests(row, ValidSignatureDir);
 }