public void CalculateOffsetTest()
        {
            using (Stream stream = File.OpenRead(@"Rpm/libplist-2.0.1.151-1.1.x86_64.rpm"))
            {
                var originalPackage = RpmPackageReader.Read(stream);

                using (var payloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                    using (var cpio = new CpioFile(payloadStream, false))
                    {
                        ArchiveBuilder    builder = new ArchiveBuilder(new PlistFileAnalyzer());
                        RpmPackageCreator creator = new RpmPackageCreator(new PlistFileAnalyzer());
                        var entries = builder.FromCpio(cpio);
                        var files   = creator.CreateFiles(entries);

                        // Core routine to populate files and dependencies
                        RpmPackage package  = new RpmPackage();
                        var        metadata = new PublicRpmMetadata(package);
                        metadata.Name    = "libplist";
                        metadata.Version = "2.0.1.151";
                        metadata.Arch    = "x86_64";
                        metadata.Release = "1.1";

                        creator.AddPackageProvides(metadata);
                        creator.AddLdDependencies(metadata);

                        metadata.Files = files;
                        creator.AddRpmDependencies(metadata, null);

                        PlistMetadata.ApplyDefaultMetadata(metadata);

                        metadata.Vendor              = "obs://build.opensuse.org/home:qmfrederik";
                        metadata.Description         = "libplist is a library for manipulating Apple Binary and XML Property Lists";
                        metadata.Url                 = "http://www.libimobiledevice.org/";
                        metadata.Size                = 0x26e6d;
                        metadata.ImmutableRegionSize = -976;

                        creator.CalculateHeaderOffsets(package);

                        foreach (var record in originalPackage.Header.Records)
                        {
                            if (record.Key == IndexTag.RPMTAG_HEADERIMMUTABLE)
                            {
                                continue;
                            }

                            this.AssertTagOffsetEqual(record.Key, originalPackage, package);
                        }

                        this.AssertTagOffsetEqual(IndexTag.RPMTAG_HEADERIMMUTABLE, originalPackage, package);
                    }
            }
        }
        public void CreatePackageMetadata()
        {
            using (Stream stream = File.OpenRead(@"Rpm/libplist-2.0.1.151-1.1.x86_64.rpm"))
            {
                var originalPackage = RpmPackageReader.Read(stream);

                using (var payloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                    using (var cpio = new CpioFile(payloadStream, false))
                    {
                        ArchiveBuilder    builder = new ArchiveBuilder(new PlistFileAnalyzer());
                        RpmPackageCreator creator = new RpmPackageCreator(new PlistFileAnalyzer());
                        var entries = builder.FromCpio(cpio);
                        var files   = creator.CreateFiles(entries);

                        // Core routine to populate files and dependencies
                        RpmPackage package  = new RpmPackage();
                        var        metadata = new PublicRpmMetadata(package);
                        metadata.Name    = "libplist";
                        metadata.Version = "2.0.1.151";
                        metadata.Arch    = "x86_64";
                        metadata.Release = "1.1";

                        creator.AddPackageProvides(metadata);
                        creator.AddLdDependencies(metadata);

                        metadata.Files = files;
                        creator.AddRpmDependencies(metadata, null);

                        PlistMetadata.ApplyDefaultMetadata(metadata);

                        metadata.Size = 0x26e6d;
                        metadata.ImmutableRegionSize = -976;

                        foreach (var record in originalPackage.Header.Records)
                        {
                            this.AssertTagEqual(record.Key, originalPackage, package);
                        }
                    }
            }
        }
        public void CalculateSignatureTest()
        {
            using (Stream stream = File.OpenRead(@"Rpm/libplist-2.0.1.151-1.1.x86_64.rpm"))
            {
                var originalPackage = RpmPackageReader.Read(stream);

                RpmPackageCreator    creator = new RpmPackageCreator(new PlistFileAnalyzer());
                Collection <RpmFile> files;

                using (var payloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                    using (var cpio = new CpioFile(payloadStream, false))
                    {
                        ArchiveBuilder builder = new ArchiveBuilder(new PlistFileAnalyzer());
                        var            entries = builder.FromCpio(cpio);
                        files = creator.CreateFiles(entries);
                    }

                // Core routine to populate files and dependencies
                RpmPackage package  = new RpmPackage();
                var        metadata = new PublicRpmMetadata(package);
                metadata.Name    = "libplist";
                metadata.Version = "2.0.1.151";
                metadata.Arch    = "x86_64";
                metadata.Release = "1.1";

                creator.AddPackageProvides(metadata);
                creator.AddLdDependencies(metadata);

                metadata.Files = files;
                creator.AddRpmDependencies(metadata, null);

                PlistMetadata.ApplyDefaultMetadata(metadata);

                creator.CalculateHeaderOffsets(package);

                // Make sure the header is really correct
                using (Stream originalHeaderStream = new SubStream(
                           originalPackage.Stream,
                           originalPackage.HeaderOffset,
                           originalPackage.PayloadOffset - originalPackage.HeaderOffset,
                           leaveParentOpen: true,
                           readOnly: true))
                    using (Stream headerStream = creator.GetHeaderStream(package))
                    {
                        byte[] originalData = new byte[originalHeaderStream.Length];
                        originalHeaderStream.Read(originalData, 0, originalData.Length);

                        byte[] data = new byte[headerStream.Length];
                        headerStream.Read(data, 0, data.Length);

                        int      delta     = 0;
                        int      dataDelta = 0;
                        IndexTag tag;
                        for (int i = 0; i < data.Length; i++)
                        {
                            if (originalData[i] != data[i])
                            {
                                delta     = i;
                                dataDelta = delta - package.Header.Records.Count * Marshal.SizeOf <IndexHeader>();
                                tag       = package.Header.Records.OrderBy(r => r.Value.Header.Offset).Last(r => r.Value.Header.Offset <= dataDelta).Key;

                                break;
                            }
                        }

                        Assert.Equal(originalData, data);
                    }

                var krgen         = PgpSigner.GenerateKeyRingGenerator("dotnet", "dotnet");
                var secretKeyRing = krgen.GenerateSecretKeyRing();
                var privateKey    = secretKeyRing.GetSecretKey().ExtractPrivateKey("dotnet".ToCharArray());

                using (var payload = RpmPayloadReader.GetCompressedPayloadStream(originalPackage))
                {
                    // Header should be OK now (see previous test), so now get the signature block and the
                    // trailer
                    creator.CalculateSignature(package, privateKey, payload);
                    creator.CalculateSignatureOffsets(package);

                    foreach (var record in originalPackage.Signature.Records)
                    {
                        if (record.Key == SignatureTag.RPMTAG_HEADERSIGNATURES)
                        {
                            continue;
                        }

                        this.AssertTagEqual(record.Key, originalPackage, package);
                    }

                    this.AssertTagEqual(SignatureTag.RPMTAG_HEADERSIGNATURES, originalPackage, package);
                }
            }
        }
        public void SetFilesTest()
        {
            using (Stream stream = File.OpenRead(@"Rpm/libplist-2.0.1.151-1.1.x86_64.rpm"))
            {
                var originalPackage = RpmPackageReader.Read(stream);
                var package         = new RpmPackage();

                using (var payloadStream = RpmPayloadReader.GetDecompressedPayloadStream(originalPackage))
                    using (var cpio = new CpioFile(payloadStream, false))
                    {
                        ArchiveBuilder    builder = new ArchiveBuilder(new PlistFileAnalyzer());
                        RpmPackageCreator creator = new RpmPackageCreator(new PlistFileAnalyzer());
                        var entries = builder.FromCpio(cpio);
                        var files   = creator.CreateFiles(entries);

                        var metadata = new PublicRpmMetadata(package);
                        metadata.Name    = "libplist";
                        metadata.Version = "2.0.1.151";
                        metadata.Arch    = "x86_64";
                        metadata.Release = "1.1";

                        creator.AddPackageProvides(metadata);
                        creator.AddLdDependencies(metadata);

                        metadata.Files = files;
                        creator.AddRpmDependencies(metadata, null);

                        this.AssertTagEqual(IndexTag.RPMTAG_FILESIZES, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEMODES, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILERDEVS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEMTIMES, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEDIGESTS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILELINKTOS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEFLAGS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEUSERNAME, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEGROUPNAME, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEVERIFYFLAGS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEDEVICES, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEINODES, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILELANGS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILECOLORS, originalPackage, package);

                        this.AssertTagEqual(IndexTag.RPMTAG_FILECLASS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_CLASSDICT, originalPackage, package);

                        this.AssertTagEqual(IndexTag.RPMTAG_BASENAMES, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_DIRINDEXES, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_DIRNAMES, originalPackage, package);

                        // The require and provides records contain file dependencies, as well as package dependencies.
                        // That's why there's a call to AddLdDependencies and AddRpmDependencies, to make sure
                        // these dependencies are written out in order.
                        this.AssertTagEqual(IndexTag.RPMTAG_REQUIRENAME, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_REQUIREFLAGS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_REQUIREVERSION, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_PROVIDENAME, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_PROVIDEFLAGS, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_REQUIREVERSION, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEDEPENDSN, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_FILEDEPENDSX, originalPackage, package);
                        this.AssertTagEqual(IndexTag.RPMTAG_DEPENDSDICT, originalPackage, package);
                    }
            }
        }