<commit_before>﻿/*
 * Copyright 2013-2016 Guardtime, Inc.
 *
 * This file is part of the Guardtime client SDK.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES, CONDITIONS, OR OTHER LICENSES OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 * "Guardtime" and "KSI" are trademarks or registered trademarks of
 * Guardtime, Inc., and no license to trademarks is granted; Guardtime
 * reserves and retains all trademark rights.
 */

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Guardtime.KSI.Exceptions;
using Guardtime.KSI.Publication;
using Guardtime.KSI.Signature;
using Guardtime.KSI.Signature.Verification;
using Guardtime.KSI.Signature.Verification.Policy;
using NUnit.Framework;

namespace Guardtime.KSI.Test.Integration
{
    [TestFixture]
    public class ExtendIntegrationTests : IntegrationTests
    {
        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCasesInvalidExtendingPass))]
        public void ExtendInvalidPassTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);

                Exception ex = Assert.Throws<KsiException>(delegate
                {
                    ksi.Extend(ksiSignature);
                });

                Assert.AreEqual("Error occured during extending: The request could not be authenticated.", ex.Message);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCasesInvalidExtendingUrl))]
        public void ExtendInvalidUrlTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);

                Exception ex = Assert.Throws<KsiServiceProtocolException>(delegate
                {
                    ksi.Extend(ksiSignature);
                });

                Assert.That(ex.Message.StartsWith("Request failed"), "Unexpected exception message: " + ex.Message);
                Assert.That(ex.InnerException.Message.StartsWith("The remote name could not be resolved"), "Unexpected inner exception message: " + ex.InnerException.Message);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCasesInvalidSigningUrl))]
        public void ExtendSuccessWithInvalidSigningUrlTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);

                Assert.DoesNotThrow(delegate
                {
                    ksi.Extend(ksiSignature);
                }, "Invalid signing url should not prevent extending.");
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCasesInvalidSigningPass))]
        public void ExtendSuccessWithInvalidSigningPassTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);

                Assert.DoesNotThrow(delegate
                {
                    ksi.Extend(ksiSignature);
                }, "Invalid signing pass should not prevent extending.");
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCases))]
        public void ExtendAndVerifyTest(Ksi ksi)
        {
            PublicationBasedVerificationPolicy rule = new PublicationBasedVerificationPolicy();

            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);
                IKsiSignature extendedSignature = ksi.Extend(ksiSignature);
                PublicationData publicationData = ksi.GetPublicationsFile().GetNearestPublicationRecord(ksiSignature.AggregationTime).PublicationData;

                VerificationContext context = new VerificationContext(extendedSignature)
                {
                    UserPublication = publicationData
                };

                VerificationResult verificationResult = rule.Verify(context);
                Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCases))]
        public void ExtendInvalidSignatureTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Invalid_Aggregation_Chain_Input_Hash), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory() { DisableVerification = true }.Create(stream);

                KsiSignatureInvalidContentException ex = Assert.Throws<KsiSignatureInvalidContentException>(delegate
                {
                    IKsiSignature extendedSignature = ksi.Extend(ksiSignature);
                });

                Assert.That(ex.Message.StartsWith("Signature verification failed"), "Unexpected exception message: " + ex.Message);
                Assert.IsNotNull(ex.Signature);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCases))]
        public void ExtendAndVerifyToUserProvidedPublicationTest(Ksi ksi)
        {
            PublicationBasedVerificationPolicy rule = new PublicationBasedVerificationPolicy();

            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                PublicationData publicationData = new PublicationData("AAAAAA-CW45II-AAKWRK-F7FBNM-KB6FNV-DYYFW7-PJQN6F-JKZWBQ-3OQYZO-HCB7RA-YNYAGA-ODRL2V");

                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);
                IKsiSignature extendedSignature = ksi.Extend(ksiSignature, publicationData);

                VerificationContext context = new VerificationContext(extendedSignature)
                {
                    UserPublication = publicationData
                };

                VerificationResult verificationResult = rule.Verify(context);
                Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCases))]
        public void ExtendAndVerifyToUserProvidedPublicationNotInPublicationsFileTest(Ksi ksi)
        {
            PublicationBasedVerificationPolicy rule = new PublicationBasedVerificationPolicy();<commit_msg>KSINETAPI-80 Improved extending integration tests<commit_after>﻿/*
 * Copyright 2013-2016 Guardtime, Inc.
 *
 * This file is part of the Guardtime client SDK.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES, CONDITIONS, OR OTHER LICENSES OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 * "Guardtime" and "KSI" are trademarks or registered trademarks of
 * Guardtime, Inc., and no license to trademarks is granted; Guardtime
 * reserves and retains all trademark rights.
 */

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Guardtime.KSI.Exceptions;
using Guardtime.KSI.Publication;
using Guardtime.KSI.Signature;
using Guardtime.KSI.Signature.Verification;
using Guardtime.KSI.Signature.Verification.Policy;
using NUnit.Framework;

namespace Guardtime.KSI.Test.Integration
{
    [TestFixture]
    public class ExtendIntegrationTests : IntegrationTests
    {
        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCasesInvalidExtendingPass))]
        public void ExtendInvalidPassTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);

                Exception ex = Assert.Throws<KsiException>(delegate
                {
                    ksi.Extend(ksiSignature);
                });

                Assert.AreEqual("Error occured during extending: The request could not be authenticated.", ex.Message);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCasesInvalidExtendingUrl))]
        public void ExtendInvalidUrlTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);

                Exception ex = Assert.Throws<KsiServiceProtocolException>(delegate
                {
                    ksi.Extend(ksiSignature);
                });

                Assert.That(ex.Message.StartsWith("Request failed"), "Unexpected exception message: " + ex.Message);
                Assert.That(ex.InnerException.Message.StartsWith("The remote name could not be resolved"), "Unexpected inner exception message: " + ex.InnerException.Message);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCasesInvalidSigningUrl))]
        public void ExtendSuccessWithInvalidSigningUrlTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);

                Assert.DoesNotThrow(delegate
                {
                    ksi.Extend(ksiSignature);
                }, "Invalid signing url should not prevent extending.");
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCasesInvalidSigningPass))]
        public void ExtendSuccessWithInvalidSigningPassTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);

                Assert.DoesNotThrow(delegate
                {
                    ksi.Extend(ksiSignature);
                }, "Invalid signing pass should not prevent extending.");
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCases))]
        public void ExtendAndVerifyTest(Ksi ksi)
        {
            PublicationBasedVerificationPolicy rule = new PublicationBasedVerificationPolicy();

            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);
                IKsiSignature extendedSignature = ksi.Extend(ksiSignature);
                PublicationData publicationData = ksi.GetPublicationsFile().GetNearestPublicationRecord(ksiSignature.AggregationTime).PublicationData;

                VerificationContext context = new VerificationContext(extendedSignature)
                {
                    UserPublication = publicationData
                };

                VerificationResult verificationResult = rule.Verify(context);
                Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCases))]
        public void ExtendInvalidSignatureTest(Ksi ksi)
        {
            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Invalid_Aggregation_Chain_Input_Hash), FileMode.Open))
            {
                IKsiSignature ksiSignature = new KsiSignatureFactory() { DisableVerification = true }.Create(stream);

                KsiSignatureInvalidContentException ex = Assert.Throws<KsiSignatureInvalidContentException>(delegate
                {
                    IKsiSignature extendedSignature = ksi.Extend(ksiSignature);
                });

                Assert.That(ex.Message.StartsWith("Signature verification failed"), "Unexpected exception message: " + ex.Message);
                Assert.IsNotNull(ex.Signature);
                Assert.IsTrue(ex.Signature.IsExtended);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCases))]
        public void ExtendAndVerifyToUserProvidedPublicationTest(Ksi ksi)
        {
            PublicationBasedVerificationPolicy rule = new PublicationBasedVerificationPolicy();

            using (FileStream stream = new FileStream(Path.Combine(TestSetup.LocalPath, Properties.Resources.KsiSignatureDo_Ok), FileMode.Open))
            {
                PublicationData publicationData = new PublicationData("AAAAAA-CW45II-AAKWRK-F7FBNM-KB6FNV-DYYFW7-PJQN6F-JKZWBQ-3OQYZO-HCB7RA-YNYAGA-ODRL2V");

                IKsiSignature ksiSignature = new KsiSignatureFactory().Create(stream);
                IKsiSignature extendedSignature = ksi.Extend(ksiSignature, publicationData);

                VerificationContext context = new VerificationContext(extendedSignature)
                {
                    UserPublication = publicationData
                };

                VerificationResult verificationResult = rule.Verify(context);
                Assert.AreEqual(VerificationResultCode.Ok, verificationResult.ResultCode);
            }
        }

        [Test, TestCaseSource(typeof(IntegrationTests), nameof(HttpTestCases))]
        public void ExtendAndVerifyToUserProvidedPublicationNotInPublicationsFileTest(Ksi ksi)
        {
            PublicationBasedVerificationPolicy rule = new PublicationBasedVerificationPolicy();
{'fraction_non_alphanumeric': 0.07862585391487126, 'fraction_numerical': 0.002890173410404624, 'mean_word_length': 2.7297893189612936, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4039440', 'n_tokens_mistral': 3944, 'n_tokens_neox': 3718, 'n_words': 802}