public void CreatePackageTest()
        {
            var krgen         = PgpSigner.GenerateKeyRingGenerator("dotnet", "dotnet");
            var secretKeyRing = krgen.GenerateSecretKeyRing();
            var privateKey    = secretKeyRing.GetSecretKey().ExtractPrivateKey("dotnet".ToCharArray());
            var publicKey     = secretKeyRing.GetPublicKey();

            using (Stream stream = File.OpenRead(@"Rpm\libplist-2.0.1.151-1.1.x86_64.rpm"))
                using (var targetStream = File.Open(@"RpmPackageCreatorTests_CreateTest.rpm", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    var originalPackage         = RpmPackageReader.Read(stream);
                    List <ArchiveEntry> archive = null;

                    using (var decompressedPayloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                        using (CpioFile cpio = new CpioFile(decompressedPayloadStream, leaveOpen: false))
                        {
                            ArchiveBuilder builder = new ArchiveBuilder();
                            archive = builder.FromCpio(cpio);
                        }

                    using (var decompressedPayloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                        using (var payloadStream = new MemoryStream())
                        {
                            decompressedPayloadStream.CopyTo(payloadStream);
                            payloadStream.Position = 0;

                            RpmPackageCreator creator = new RpmPackageCreator(new PlistFileAnalyzer());
                            creator.CreatePackage(
                                archive,
                                payloadStream,
                                "libplist",
                                "2.0.1.151",
                                "x86_64",
                                "1.1",
                                false,
                                null,
                                false,
                                null,
                                null,
                                null,
                                (metadata) => PlistMetadata.ApplyDefaultMetadata(metadata),
                                privateKey,
                                targetStream);
                        }
                }

            using (var targetStream = File.Open(@"RpmPackageCreatorTests_CreateTest.rpm", FileMode.Open, FileAccess.Read, FileShare.None))
            {
                var package = RpmPackageReader.Read(targetStream);

                var metadata  = new RpmMetadata(package);
                var signature = new RpmSignature(package);

                Assert.True(signature.Verify(publicKey));
            }
        }
 public void VerifyTest()
 {
     using (Stream stream = File.OpenRead(@"Rpm/libplist-2.0.1.151-1.1.x86_64.rpm"))
         using (Stream pgpKey = File.OpenRead(@"Rpm/_key.pub"))
         {
             var package   = RpmPackageReader.Read(stream);
             var signature = new RpmSignature(package);
             signature.Verify(pgpKey);
         }
 }
        public void SignaturePropertiesTest()
        {
            using (Stream stream = File.OpenRead(@"Rpm/libplist-2.0.1.151-1.1.x86_64.rpm"))
            {
                var package   = RpmPackageReader.Read(stream);
                var signature = new RpmSignature(package);

                Assert.Equal(package, signature.Package);
                Assert.Equal(unchecked ((long)0xed356a6f4dec2b0a), signature.HeaderAndPayloadPgpSignature.KeyId);
                Assert.Equal(0xfd84, signature.HeaderAndPayloadSize);
                Assert.Equal(0x27430, signature.UncompressedPayloadSize);
                Assert.Equal(unchecked ((long)0xed356a6f4dec2b0a), signature.HeaderPgpSignature.KeyId);
                Assert.Equal(-112, signature.ImmutableRegionSize);
                Assert.Equal(new byte[] { 0xd4, 0x8e, 0x07, 0xc5, 0x68, 0xbe, 0x27, 0xe2, 0x26, 0xcb, 0xb8, 0xaf, 0x0e, 0xf7, 0x32, 0xec }, signature.MD5Hash);
                Assert.Equal(new byte[] { 0xd7, 0x2d, 0x1e, 0x8a, 0x93, 0x26, 0x43, 0x14, 0x72, 0xa1, 0xe3, 0x9e, 0x1b, 0x79, 0x16, 0xad, 0x07, 0xcc, 0xc3, 0x1b }, signature.Sha1Hash);
            }
        }
        public void CreatePackageBinaryTest()
        {
            var pgpSignatureData = File.ReadAllBytes("Rpm/RpmSigTag_Pgp.bin");
            var rsaSignatureData = File.ReadAllBytes("Rpm/RpmSigTag_Rsa.bin");

            var signer = new DummySigner();

            signer.Add("D72D1E8A9326431472A1E39E1B7916AD07CCC31B", rsaSignatureData);
            signer.Add("A0A33779FBBED565A15FA85BAFBD1473E00F1257", pgpSignatureData);
            signer.Add("5E24172F773FEB67FC0BF4831BB4E8A75B4554EF", pgpSignatureData);

            using (Stream stream = File.OpenRead(@"Rpm/libplist-2.0.1.151-1.1.x86_64.rpm"))
                using (var targetStream = File.Open(@"RpmPackageCreatorTests_CreateBinaryTest.rpm", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    var originalPackage         = RpmPackageReader.Read(stream);
                    List <ArchiveEntry> archive = null;

                    using (var decompressedPayloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                        using (CpioFile cpio = new CpioFile(decompressedPayloadStream, leaveOpen: false))
                        {
                            ArchiveBuilder builder = new ArchiveBuilder();
                            archive = builder.FromCpio(cpio);
                        }

                    using (var compressedPayloadStream = RpmPayloadReader.GetCompressedPayloadStream(originalPackage))
                    {
                        RpmPackageCreator creator = new RpmPackageCreator(new PlistFileAnalyzer());
                        creator.CreatePackage(
                            archive,
                            compressedPayloadStream,
                            "libplist",
                            "2.0.1.151",
                            "x86_64",
                            "1.1",
                            false,
                            null,
                            false,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            (metadata) => PlistMetadata.ApplyDefaultMetadata(metadata),
                            signer,
                            targetStream,
                            includeVersionInName: true,
                            payloadIsCompressed: true);
                    }
                }

            using (var originalStream = File.OpenRead(@"Rpm/libplist-2.0.1.151-1.1.x86_64.rpm"))
                using (var targetStream = File.Open(@"RpmPackageCreatorTests_CreateBinaryTest.rpm", FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    var originalPackage = RpmPackageReader.Read(originalStream);
                    var package         = RpmPackageReader.Read(targetStream);

                    RpmDumper.Dump(originalPackage, "RpmPackageCreatorTests_CreateBinaryTest_original.txt");
                    RpmDumper.Dump(package, "RpmPackageCreatorTests_CreateBinaryTest_reconstructed.txt");

                    var metadata  = new RpmMetadata(package);
                    var signature = new RpmSignature(package);

                    foreach (var record in originalPackage.Signature.Records)
                    {
                        this.AssertTagEqual(record.Key, originalPackage, package);
                    }

                    originalStream.Position = 0;
                    targetStream.Position   = 0;

                    int    index          = 0;
                    byte[] originalBuffer = new byte[1024];
                    byte[] targetBuffer   = new byte[1024];

                    while (originalStream.Position < originalStream.Length)
                    {
                        originalStream.Read(originalBuffer, 0, originalBuffer.Length);
                        targetStream.Read(targetBuffer, 0, targetBuffer.Length);

                        Assert.Equal(originalBuffer, targetBuffer);

                        index += originalBuffer.Length;
                    }
                }
        }
        public void CreatePackageTest()
        {
            var          krgen            = PgpSigner.GenerateKeyRingGenerator("dotnet", "dotnet");
            var          secretKeyRing    = krgen.GenerateSecretKeyRing();
            var          privateKey       = secretKeyRing.GetSecretKey().ExtractPrivateKey("dotnet".ToCharArray());
            var          publicKey        = secretKeyRing.GetPublicKey();
            const string preInstScript    = "echo preinst\n";
            const string postInstScript   = "echo postinst\n";
            const string preRemoveScript  = "echo preremove\n";
            const string postRemoveScript = "echo postremove\n";
            const string nameString       = "libplist";
            const string versionString    = "2.0.1.151";
            const string releaseString    = "1.1";
            const string archString       = "x86_64";

            using (Stream stream = File.OpenRead($"Rpm/{nameString}-{versionString}-{releaseString}.{archString}.rpm"))
                using (var targetStream = File.Open(@"RpmPackageCreatorTests_CreateTest.rpm", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    var originalPackage         = RpmPackageReader.Read(stream);
                    List <ArchiveEntry> archive = null;

                    using (var decompressedPayloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                        using (CpioFile cpio = new CpioFile(decompressedPayloadStream, leaveOpen: false))
                        {
                            ArchiveBuilder builder = new ArchiveBuilder();
                            archive = builder.FromCpio(cpio);
                        }

                    using (var decompressedPayloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                        using (var payloadStream = new MemoryStream())
                        {
                            decompressedPayloadStream.CopyTo(payloadStream);
                            payloadStream.Position = 0;

                            RpmPackageCreator creator = new RpmPackageCreator(new PlistFileAnalyzer());
                            creator.CreatePackage(
                                archive,
                                payloadStream,
                                nameString,
                                versionString,
                                archString,
                                releaseString,
                                false,
                                null,
                                false,
                                null,
                                null,
                                preInstScript,
                                postInstScript,
                                preRemoveScript,
                                postRemoveScript,
                                null,
                                (metadata) => PlistMetadata.ApplyDefaultMetadata(metadata),
                                privateKey,
                                targetStream);
                        }
                }

            using (var targetStream = File.Open(@"RpmPackageCreatorTests_CreateTest.rpm", FileMode.Open, FileAccess.Read, FileShare.None))
            {
                var package = RpmPackageReader.Read(targetStream);

                var metadata = new RpmMetadata(package);
                Assert.Equal(metadata.Version, versionString);
                Assert.Equal(metadata.Name, nameString);
                Assert.Equal(metadata.Arch, archString);
                Assert.Equal(metadata.Release, releaseString);
                Assert.StartsWith(preInstScript, metadata.PreIn);
                Assert.StartsWith(postInstScript, metadata.PostIn);
                Assert.StartsWith(preRemoveScript, metadata.PreUn);
                Assert.StartsWith(postRemoveScript, metadata.PostUn);
                var signature = new RpmSignature(package);

                Assert.True(signature.Verify(publicKey));
            }
        }