/// <summary> /// Saves <see cref="Catalog"/> to an XML file, adds the default stylesheet and sign it it with <see cref="SecretKey"/> (if specified). /// </summary> /// <remarks>Writing and signing the catalog file are performed as an atomic operation (i.e. if signing fails an existing file remains unchanged).</remarks> /// <param name="path">The file to save in.</param> /// <param name="passphrase">The passphrase to use to unlock the secret key; can be <see langword="null"/> if <see cref="SecretKey"/> is <see langword="null"/>.</param> /// <exception cref="IOException">A problem occurs while writing the file.</exception> /// <exception cref="UnauthorizedAccessException">Write access to the file is not permitted.</exception> /// <exception cref="WrongPassphraseException">Passphrase was incorrect.</exception> public void Save([NotNull] string path, [CanBeNull] string passphrase = null) { #region Sanity checks if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } #endregion if (SecretKey == null) { Catalog.SaveXml(path); return; } var openPgp = OpenPgpFactory.CreateDefault(); using (var stream = new MemoryStream()) { Catalog.SaveXml(stream, stylesheet: @"catalog.xsl"); stream.Position = 0; FeedUtils.SignFeed(stream, SecretKey, passphrase, openPgp); stream.WriteTo(path); } string directory = Path.GetDirectoryName(path); if (directory != null) { FeedUtils.DeployPublicKey(directory, SecretKey, openPgp); FeedUtils.DeployStylesheet(directory, @"catalog"); } }
public void TestDeployPublicKey() { using (var tempDir = new TemporaryDirectory("0install-unit-tests")) { var secretKey = new OpenPgpSecretKey("fingerprint", "key", "*****@*****.**", new DateTime(2000, 1, 1), OpenPgpAlgorithm.Rsa, 128); const string publicKey = "public"; var openPgpMock = MockRepository.Create <IOpenPgp>(); openPgpMock.Setup(x => x.GetPublicKey(secretKey.Fingerprint)).Returns(publicKey); FeedUtils.DeployPublicKey(tempDir.Path, secretKey, openPgpMock.Object); Assert.AreEqual(publicKey, File.ReadAllText(tempDir + Path.DirectorySeparatorChar + secretKey.KeyID + ".gpg"), "Public key should be written to parallel file in directory"); } }