public static void SignerInfo_RemoveUnsignedAttributeFromCounterSigner_Removes()
        {
            SignedCms cms = new SignedCms();

            cms.Decode(SignedDocuments.OneRsaSignerTwoRsaCounterSigners);

            AsnEncodedData attribute1 = CreateTimestampToken(1);
            AsnEncodedData attribute2 = CreateTimestampToken(2);

            SignerInfo counterSigner = cms.SignerInfos[0].CounterSignerInfos[0];

            counterSigner.AddUnsignedAttribute(attribute1);
            counterSigner.AddUnsignedAttribute(attribute2);

            ReReadSignedCms(ref cms);

            SignerInfo signer = cms.SignerInfos[0];

            // attributes got sorted, therefore number changed
            Assert.Equal(2, signer.CounterSignerInfos.Count);
            Assert.Equal(0, signer.CounterSignerInfos[0].UnsignedAttributes.Count);
            Assert.Equal(1, signer.CounterSignerInfos[1].UnsignedAttributes.Count);
            Assert.Equal(2, signer.CounterSignerInfos[1].UnsignedAttributes[0].Values.Count);

            counterSigner = signer.CounterSignerInfos[1];
            counterSigner.RemoveUnsignedAttribute(counterSigner.UnsignedAttributes[0].Values[0]);

            // We didn't modify existing instances
            Assert.Equal(2, signer.CounterSignerInfos.Count);
            Assert.Equal(1, counterSigner.UnsignedAttributes.Count);
            Assert.Equal(2, counterSigner.UnsignedAttributes[0].Values.Count);

            // parent got updated
            Assert.Equal(0, signer.CounterSignerInfos[0].UnsignedAttributes.Count);
            Assert.Equal(1, signer.CounterSignerInfos[1].UnsignedAttributes.Count);

            // We did modify the state
            Assert.Equal(1, cms.SignerInfos[0].CounterSignerInfos[1].UnsignedAttributes.Count);
            Assert.Equal(1, cms.SignerInfos[0].CounterSignerInfos[1].UnsignedAttributes[0].Values.Count);

            ReReadSignedCms(ref cms);

            counterSigner = cms.SignerInfos[0].CounterSignerInfos[1];
            Assert.Equal(1, counterSigner.UnsignedAttributes.Count);
            Assert.Equal(1, counterSigner.UnsignedAttributes[0].Values.Count);
            counterSigner.RemoveUnsignedAttribute(counterSigner.UnsignedAttributes[0].Values[0]);

            // We didn't modify current instance
            Assert.Equal(1, counterSigner.UnsignedAttributes.Count);
            Assert.Equal(1, counterSigner.UnsignedAttributes[0].Values.Count);

            // We did modify the state
            Assert.Equal(0, cms.SignerInfos[0].CounterSignerInfos[1].UnsignedAttributes.Count);
        }
        public static void SignerInfo_AddRemoveUnsignedAttributeWithDetachtedSigner_Throws()
        {
            SignedCms cms = new SignedCms();

            cms.Decode(SignedDocuments.OneRsaSignerTwoRsaCounterSigners);

            // Detatch signer (and its counter signers)
            SignerInfo counterSigner = cms.SignerInfos[0].CounterSignerInfos[0];

            cms.RemoveSignature(0);

            // we shouldn't throw
            Assert.Equal(0, counterSigner.UnsignedAttributes.Count);

            AsnEncodedData attribute = CreateTimestampToken(1);

            Assert.Throws <CryptographicException>(() => counterSigner.AddUnsignedAttribute(attribute));
            Assert.Throws <CryptographicException>(() => counterSigner.RemoveUnsignedAttribute(attribute));
        }