private void ConnectCertificates( HashedCertificate endCertificate, IReadOnlyCollection <HashedCertificate> parentCertificates, IReadOnlyDictionary <string, EndCertificate> thumbprintToEndCertificate, IReadOnlyDictionary <string, ParentCertificate> thumbprintToParentCertificates) { var endCertificateEntity = thumbprintToEndCertificate[endCertificate.Thumbprint]; var parentCertificateKeys = new HashSet <long>(endCertificateEntity .CertificateChainLinks .Select(x => x.ParentCertificateKey)); foreach (var parentCertificate in parentCertificates) { var parentCertificateEntity = thumbprintToParentCertificates[parentCertificate.Thumbprint]; // If either end of the link is new, the link must be new. if (endCertificateEntity.Key == default(long) || parentCertificateEntity.Key == default(long) || !parentCertificateKeys.Contains(parentCertificateEntity.Key)) { var link = new CertificateChainLink { EndCertificate = endCertificateEntity, ParentCertificate = parentCertificateEntity, }; _validationEntitiesContext.CertificateChainLinks.Add(link); endCertificateEntity.CertificateChainLinks.Add(link); parentCertificateEntity.CertificateChainLinks.Add(link); if (parentCertificateEntity.Key != default(long)) { parentCertificateKeys.Add(parentCertificateEntity.Key); } } } }
public async Task DoesNotDuplicateWhenSomeDataAlreadyExist() { // Arrange var signature = await TestResources.LoadPrimarySignatureAsync(TestResources.SignedPackageLeaf1); var existingParentCertificate = new ParentCertificate { Key = 1, Thumbprint = TestResources.RootThumbprint, CertificateChainLinks = new List <CertificateChainLink>(), }; var existingEndCertificate = new EndCertificate { Key = 1, Thumbprint = TestResources.Leaf1Thumbprint, Status = EndCertificateStatus.Good, // Different than the default. Use = EndCertificateUse.CodeSigning, CertificateChainLinks = new List <CertificateChainLink>(), }; var existingLink = new CertificateChainLink { ParentCertificate = existingParentCertificate, ParentCertificateKey = existingParentCertificate.Key, EndCertificate = existingEndCertificate, EndCertificateKey = existingEndCertificate.Key, }; existingParentCertificate.CertificateChainLinks.Add(existingLink); existingEndCertificate.CertificateChainLinks.Add(existingLink); var existingPackageSignature = new PackageSignature { Key = 1, EndCertificate = existingEndCertificate, EndCertificateKey = existingEndCertificate.Key, Status = PackageSignatureStatus.Valid, CreatedAt = new DateTime(2017, 1, 1, 8, 30, 0, DateTimeKind.Utc), PackageKey = _packageKey, Type = PackageSignatureType.Author, TrustedTimestamps = new List <TrustedTimestamp>(), }; _entitiesContext .Setup(x => x.ParentCertificates) .Returns(DbSetMockFactory.Create(existingParentCertificate)); _entitiesContext .Setup(x => x.EndCertificates) .Returns(DbSetMockFactory.Create(existingEndCertificate)); _entitiesContext .Setup(x => x.CertificateChainLinks) .Returns(DbSetMockFactory.Create(existingLink)); _entitiesContext .Setup(x => x.PackageSignatures) .Returns(DbSetMockFactory.Create(existingPackageSignature)); // Act await _target.ExtractAsync(_packageKey, signature, _token); // Assert VerifyExtractedInformation(Leaf1Certificates, Leaf1TimestampValue, PackageSignatureType.Author); Assert.Equal(2, _entitiesContext.Object.EndCertificates.Count()); Assert.Equal(4, _entitiesContext.Object.ParentCertificates.Count()); Assert.Equal(4, _entitiesContext.Object.CertificateChainLinks.Count()); Assert.Equal(1, _entitiesContext.Object.PackageSignatures.Count()); Assert.Equal(1, _entitiesContext.Object.TrustedTimestamps.Count()); Assert.Equal(EndCertificateStatus.Good, existingEndCertificate.Status); Assert.Equal(PackageSignatureStatus.Valid, existingPackageSignature.Status); }