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 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); } } }