Exemple #1
0
        public void ResignManifest()
        {
            var lf             = new LoggerFactory();
            var resignerLogger = new ResignerLoggingExtensions(lf.CreateLogger <ResignerLoggingExtensions>());

            var d = DateTime.Now;

            using var testContentStream = ResourcesHook.GetManifestResourceStream("Resources.ResignTestManifest.zip");

            using var m = new MemoryStream();
            testContentStream.CopyTo(m);
            var zipContent = m.ToArray();

            var m1 = new ContentEntity {
                Content = zipContent, PublishingId = "1", ContentTypeName = "Meh", Type = ContentTypes.Manifest, Created = d, Release = d
            };

            var dbc = _contentDbProvider.CreateNew();

            dbc.Content.AddRange(new[] {
                m1,
                new ContentEntity {
                    Content = new byte[0], PublishingId = "2", ContentTypeName = "Meh", Type = ContentTypes.AppConfig, Created = d, Release = d
                },
                new ContentEntity {
                    Content = new byte[0], PublishingId = "3", ContentTypeName = "Meh", Type = ContentTypes.AppConfigV2, Created = d, Release = d
                },
                new ContentEntity {
                    Content = new byte[0], PublishingId = "4", ContentTypeName = "Meh", Type = ContentTypes.ExposureKeySet, Created = d, Release = d
                },
                new ContentEntity {
                    Content = new byte[0], PublishingId = "5", ContentTypeName = "Meh", Type = ContentTypes.ExposureKeySetV2, Created = d, Release = d
                },
            });

            dbc.SaveChanges();

            var resigner = new NlContentResignCommand(_contentDbProvider.CreateNew, TestSignerHelpers.CreateCmsSignerEnhanced(lf), resignerLogger);

            resigner.ExecuteAsync(ContentTypes.Manifest, ContentTypes.ManifestV2, ZippedContentEntryNames.Content).GetAwaiter().GetResult();

            //check the numbers
            Assert.Equal(6, dbc.Content.Count());

            var m2 = dbc.Content.Single(x => x.PublishingId == "1" && x.Type == ContentTypes.ManifestV2);

            Assert.Equal(m1.Created, m2.Created);
            Assert.Equal(m1.Release, m2.Release);

            var ms1 = new MemoryStream(zipContent);

            using var zip1 = new ZipArchive(ms1);

            var ms2 = new MemoryStream(m2.Content);

            using var zip2 = new ZipArchive(ms2);

            Assert.True(Enumerable.SequenceEqual(zip1.ReadEntry(ZippedContentEntryNames.Content), zip2.ReadEntry(ZippedContentEntryNames.Content)));
            Assert.NotEqual(zip1.GetEntry(ZippedContentEntryNames.NlSignature), zip2.GetEntry(ZippedContentEntryNames.NlSignature));
        }
        public void EksBuilderV1WithDummy_NLSigHasDummyText()
        {
            //Arrange
            var keyCount           = 500;
            var lf                 = new LoggerFactory();
            var eksBuilderV1Logger = new EksBuilderV1LoggingExtensions(lf.CreateLogger <EksBuilderV1LoggingExtensions>());
            var dtp                = new StandardUtcDateTimeProvider();
            var dummySigner        = new DummyCmsSigner();

            var sut = new EksBuilderV1(
                new FakeEksHeaderInfoConfig(),
                TestSignerHelpers.CreateEcdsaSigner(lf),
                dummySigner,
                dtp,
                new GeneratedProtobufEksContentFormatter(),
                eksBuilderV1Logger);

            //Act
            var result = sut.BuildAsync(GetRandomKeys(keyCount, 123)).GetAwaiter().GetResult();

            //Assert
            using var zipFileInMemory = new MemoryStream();
            zipFileInMemory.Write(result, 0, result.Length);

            using (var zipFileContent = new ZipArchive(zipFileInMemory, ZipArchiveMode.Read, false))
            {
                var nlSignature = zipFileContent.ReadEntry(ZippedContentEntryNames.NlSignature);
                Assert.NotNull(nlSignature);
                Assert.Equal(nlSignature, dummySigner.DummyContent);
            }
        }
        public void Build(int keyCount, int seed)
        {
            //Arrange
            var lf = new LoggerFactory();
            var eksBuilderV1Logger = new EksBuilderV1LoggingExtensions(lf.CreateLogger <EksBuilderV1LoggingExtensions>());
            var dtp = new StandardUtcDateTimeProvider();

            var sut = new EksBuilderV1(
                new FakeEksHeaderInfoConfig(),
                TestSignerHelpers.CreateEcdsaSigner(lf),
                TestSignerHelpers.CreateCmsSignerEnhanced(lf),
                dtp,
                new GeneratedProtobufEksContentFormatter(),
                eksBuilderV1Logger);

            //Act
            var result = sut.BuildAsync(GetRandomKeys(keyCount, seed)).GetAwaiter().GetResult();

            Trace.WriteLine($"{keyCount} keys = {result.Length} bytes.");

            //Assert
            Assert.True(result.Length > 0);

            using (var fs = new FileStream("EKS.zip", FileMode.Create, FileAccess.Write))
            {
                fs.Write(result, 0, result.Length);
            }
        }
        public void Build(int length)
        {
            var lf      = new LoggerFactory();
            var content = Encoding.UTF8.GetBytes(CreateString(length));
            var sig     = TestSignerHelpers.CreateCmsSignerEnhanced(lf).GetSignature(content);

            Assert.True((sig?.Length ?? 0) != 0);
        }
        private ManifestUpdateCommand CompileManifestUpdateCommand()
        {
            var eksConfigMock       = new Mock <IEksConfig>();
            var lf                  = new LoggerFactory();
            var dateTimeProvider    = new StandardUtcDateTimeProvider();
            var jsonSerialiser      = new StandardJsonSerializer();
            var entityFormatterMock = new Mock <IContentEntityFormatter>();

            Func <IContentEntityFormatter> formatterForV3 = () =>
                                                            new StandardContentEntityFormatter(
                new ZippedSignedContentFormatter(
                    TestSignerHelpers.CreateCmsSignerEnhanced(lf)),
                new Sha256HexPublishingIdService(),
                jsonSerialiser
                );

            var result = new ManifestUpdateCommand(
                new ManifestBuilder(
                    _ContentDbProvider.CreateNew(),
                    eksConfigMock.Object,
                    dateTimeProvider),
                new ManifestBuilderV3(
                    _ContentDbProvider.CreateNew(),
                    eksConfigMock.Object,
                    dateTimeProvider),
                new ManifestBuilderV4(
                    _ContentDbProvider.CreateNew(),
                    eksConfigMock.Object,
                    dateTimeProvider),
                _ContentDbProvider.CreateNew,
                new ManifestUpdateCommandLoggingExtensions(lf.CreateLogger <ManifestUpdateCommandLoggingExtensions>()),
                dateTimeProvider,
                jsonSerialiser,
                entityFormatterMock.Object,
                formatterForV3
                );

            return(result);
        }
Exemple #6
0
        public void Re_sign_all_existing_earlier_content()
        {
            var lf             = new LoggerFactory();
            var resignerLogger = new ResignerLoggingExtensions(lf.CreateLogger <ResignerLoggingExtensions>());

            //Add some db rows to Content
            var dbc = _contentDbProvider.CreateNew();

            var d            = DateTime.Now;
            var laterDate    = d.AddDays(1);
            var publishingId = "1";

            using var testContentStream = ResourcesHook.GetManifestResourceStream("Resources.ResignAppConfig.zip");
            using var m = new MemoryStream();
            testContentStream.CopyTo(m);
            var zipContent = m.ToArray();

            //Adding identical content items
            var sourceAppConfigContent1 = new ContentEntity {
                Content = zipContent, PublishingId = publishingId, ContentTypeName = ".", Type = ContentTypes.AppConfig, Created = d.AddMilliseconds(1), Release = laterDate
            };
            var sourceAppConfigContent2 = new ContentEntity {
                Content = zipContent, PublishingId = publishingId, ContentTypeName = ".", Type = ContentTypes.AppConfig, Created = d.AddMilliseconds(2), Release = laterDate
            };
            var sourceAppConfigContent3 = new ContentEntity {
                Content = zipContent, PublishingId = publishingId, ContentTypeName = ".", Type = ContentTypes.AppConfig, Created = d.AddMilliseconds(3), Release = laterDate
            };

            dbc.Content.AddRange(
                sourceAppConfigContent1,
                sourceAppConfigContent2,
                sourceAppConfigContent3
                );

            dbc.SaveChanges();

            Assert.Equal(3, dbc.Content.Count());

            var resigner = new NlContentResignCommand(_contentDbProvider.CreateNew, TestSignerHelpers.CreateCmsSignerEnhanced(lf), resignerLogger);

            resigner.ExecuteAsync(ContentTypes.AppConfig, ContentTypes.AppConfigV2, ZippedContentEntryNames.Content).GetAwaiter().GetResult();

            //check the numbers
            Assert.Equal(6, dbc.Content.Count());

            var resignedAppConfigContent = dbc.Content.Where(x => x.PublishingId == publishingId && x.Type == ContentTypes.AppConfigV2);

            var originalContentStream = new MemoryStream(zipContent);

            using var originalZipArchive = new ZipArchive(originalContentStream);
            foreach (var i in resignedAppConfigContent)
            {
                //Created is bumped by a few ms so can't be compared here
                Assert.Equal(sourceAppConfigContent1.Release, i.Release);

                var s = new MemoryStream(i.Content);
                using var z = new ZipArchive(s);

                Assert.True(Enumerable.SequenceEqual(originalZipArchive.ReadEntry(ZippedContentEntryNames.Content), z.ReadEntry(ZippedContentEntryNames.Content)));
                Assert.NotEqual(originalZipArchive.GetEntry(ZippedContentEntryNames.NlSignature), z.GetEntry(ZippedContentEntryNames.NlSignature));
            }

            //Repeating should have no effect
            resigner.ExecuteAsync(ContentTypes.AppConfig, ContentTypes.AppConfigV2, ZippedContentEntryNames.Content).GetAwaiter().GetResult();
            Assert.Equal(6, dbc.Content.Count());
        }