public void AddMissingExceptions() { using (var stream = typeof(ExtractorTest).GetEmbedded("testArchive.zip")) using (var microServer = new MicroServer("archive.zip", stream)) { var implementation = new Implementation {ManifestDigest = new ManifestDigest(sha1New: "invalid"), RetrievalMethods = {new Archive {Href = microServer.FileUri}}}; Assert.Throws<DigestMismatchException>(() => implementation.AddMissing(new SilentTaskHandler())); } }
public void DownloadAndApplyArchive() { using (var stream = typeof(ExtractorTest).GetEmbedded("testArchive.zip")) using (var microServer = new MicroServer("archive.zip", stream)) { var archive = new Archive {Href = microServer.FileUri}; archive.DownloadAndApply(new SilentTaskHandler()).Dispose(); Assert.AreEqual(Archive.MimeTypeZip, archive.MimeType); Assert.AreEqual(stream.Length, archive.Size); } }
public void AddMissingArchive() { using (var stream = typeof(ExtractorTest).GetEmbedded("testArchive.zip")) using (var microServer = new MicroServer("archive.zip", stream)) { var implementation = new Implementation {RetrievalMethods = {new Archive {Href = microServer.FileUri}}}; implementation.AddMissing(new SilentTaskHandler()); Assert.AreEqual(ArchiveSha256Digest, implementation.ManifestDigest.Sha256New); var archive = (Archive)implementation.RetrievalMethods[0]; Assert.AreEqual(Archive.MimeTypeZip, archive.MimeType); Assert.AreEqual(stream.Length, archive.Size); } }
/// <summary> /// Tests the sync logic with custom <see cref="AppList"/>s. /// </summary> /// <param name="resetMode">The <see cref="SyncResetMode"/> to pass to <see cref="SyncIntegrationManager.Sync"/>.</param> /// <param name="appListLocal">The current local <see cref="AppList"/>.</param> /// <param name="appListLast">The state of the <see cref="AppList"/> after the last successful sync.</param> /// <param name="appListServer">The current server-side <see cref="AppList"/>.</param> private void TestSync(SyncResetMode resetMode, AppList appListLocal, AppList appListLast, AppList appListServer) { appListLocal.SaveXml(_appListPath); if (appListLast != null) appListLast.SaveXml(_appListPath + SyncIntegrationManager.AppListLastSyncSuffix); using (var stream = File.Create(_appListPath + ".zip")) appListServer.SaveXmlZip(stream); using (var appListServerFile = File.OpenRead(_appListPath + ".zip")) using (var syncServer = new MicroServer("app-list", appListServerFile)) { using (var integrationManager = new SyncIntegrationManager(_appListPath, new SyncServer {Uri = syncServer.ServerUri, Username = "******", Password = "******"}, interfaceId => new Feed(), new SilentTaskHandler())) integrationManager.Sync(resetMode); appListServer = AppList.LoadXmlZip(syncServer.FileContent); } appListLocal = XmlStorage.LoadXml<AppList>(_appListPath); appListLast = XmlStorage.LoadXml<AppList>(_appListPath + SyncIntegrationManager.AppListLastSyncSuffix); Assert.AreEqual(appListLocal, appListServer, "Server and local data should be equal after sync"); Assert.AreEqual(appListLocal, appListLast, "Last sync snapshot and local data should be equal after sync"); }
public void Download() { var feed = FeedTest.CreateTestFeed(); var feedStream = new MemoryStream(); using (var server = new MicroServer("feed.xml", feedStream)) { feed.Uri = new FeedUri(server.FileUri); feed.SaveXml(feedStream); var data = feedStream.ToArray(); feedStream.Position = 0; Target.IsStale(feed.Uri).Should().BeTrue(because: "Non-cached feeds should be reported as stale"); // No previous feed FeedCacheMock.Setup(x => x.Contains(feed.Uri)).Returns(false); FeedCacheMock.Setup(x => x.GetSignatures(feed.Uri)).Throws <KeyNotFoundException>(); FeedCacheMock.Setup(x => x.Add(feed.Uri, data)); FeedCacheMock.Setup(x => x.GetFeed(feed.Uri)).Returns(feed); TrustManagerMock.Setup(x => x.CheckTrust(data, feed.Uri, It.IsAny <string>())).Returns(OpenPgpUtilsTest.TestSignature); Target[feed.Uri].Should().Be(feed); } }
public void SkipBroken() { StoreMock.Setup(x => x.Flush()); using var serverArchive = new MicroServer("archive.zip", ZipArchiveStream); using var serverSingleFile = new MicroServer("regular", TestFile.DefaultContents.ToStream()); TestDownload( new TestRoot { new TestFile("regular") { LastWrite = UnixEpoch } }, // broken: wrong size new Archive { Href = serverArchive.FileUri, MimeType = Archive.MimeTypeZip, Size = 0 }, // broken: unknown archive format new Archive { Href = serverArchive.FileUri, MimeType = "test/format", Size = ZipArchiveStream.Length }, // works new Recipe { Steps = { new SingleFile { Href = serverSingleFile.FileUri, Size = TestFile.DefaultContents.Length, Destination = "regular" } } }); }
public void Download() { var feed = FeedTest.CreateTestFeed(); var feedStream = new MemoryStream(); using (var server = new MicroServer("feed.xml", feedStream)) { feed.Uri = new FeedUri(server.FileUri); feed.SaveXml(feedStream); var data = feedStream.ToArray(); feedStream.Position = 0; Assert.IsTrue(Target.IsStale(feed.Uri), "Non-cached feeds should be reported as stale"); // No previous feed _feedCacheMock.Setup(x => x.Contains(feed.Uri)).Returns(false); _feedCacheMock.Setup(x => x.GetSignatures(feed.Uri)).Throws <KeyNotFoundException>(); _feedCacheMock.Setup(x => x.Add(feed.Uri, data)); _feedCacheMock.Setup(x => x.GetFeed(feed.Uri)).Returns(feed); // ReSharper disable once AccessToDisposedClosure _trustManagerMock.Setup(x => x.CheckTrust(data, feed.Uri, null)) .Returns(new ValidSignature("fingerprint", new DateTime(2000, 1, 1))); Assert.AreEqual(feed, Target.GetFeed(feed.Uri)); } }
/// <summary> /// Tests the sync logic with custom <see cref="AppList"/>s. /// </summary> /// <param name="resetMode">The <see cref="SyncResetMode"/> to pass to <see cref="SyncIntegrationManager.Sync"/>.</param> /// <param name="appListLocal">The current local <see cref="AppList"/>.</param> /// <param name="appListLast">The state of the <see cref="AppList"/> after the last successful sync.</param> /// <param name="appListServer">The current server-side <see cref="AppList"/>.</param> private static void TestSync(SyncResetMode resetMode, AppList appListLocal, AppList?appListLast, AppList appListServer) { string appListLocalPath = AppList.GetDefaultPath(); appListLocal.SaveXml(appListLocalPath); appListLast?.SaveXml(appListLocalPath + SyncIntegrationManager.AppListLastSyncSuffix); using var appListServerPath = new TemporaryFile("0install-test-applist"); { using (var stream = File.Create(appListServerPath)) appListServer.SaveXmlZip(stream, CryptoKey); using (var appListServerFile = File.OpenRead(appListServerPath)) { using var syncServer = new MicroServer("app-list", appListServerFile); var config = new Config { SyncServer = new(syncServer.ServerUri), SyncServerUsername = "******", SyncServerPassword = "******", SyncCryptoKey = CryptoKey }; using (var integrationManager = new SyncIntegrationManager(config, _ => new Feed(), new SilentTaskHandler())) integrationManager.Sync(resetMode); appListServer = AppList.LoadXmlZip(syncServer.FileContent, CryptoKey); } } appListLocal = XmlStorage.LoadXml <AppList>(appListLocalPath); appListLast = XmlStorage.LoadXml <AppList>(appListLocalPath + SyncIntegrationManager.AppListLastSyncSuffix); appListServer.Should().Be(appListLocal, because: "Server and local data should be equal after sync"); appListLast.Should().Be(appListLocal, because: "Last sync snapshot and local data should be equal after sync"); }
private void UseKeyInfoServer(MicroServer keyInfoServer) { var config = Resolve <Config>(); config.AutoApproveKeys = true; config.KeyInfoServer = keyInfoServer.ServerUri; }
public void DownloadRecipe() { _storeMock.Setup(x => x.Flush()); using (var serverArchive = new MicroServer("archive.zip", TestData.ZipArchiveStream)) using (var serverSingleFile = new MicroServer("regular", TestData.RegularString.ToStream())) { TestDownload( dirPath => File.Exists(Path.Combine(dirPath, "regular")) && !File.Exists(Path.Combine(dirPath, "executable")) && File.Exists(Path.Combine(dirPath, "executable2")) && File.Exists(Path.Combine(dirPath, "regular2")), new Recipe { Steps = { new Archive { Href = serverArchive.FileUri, MimeType = Archive.MimeTypeZip, Size = TestData.ZipArchiveStream.Length }, new RenameStep { Source = "executable", Destination = "executable2" }, new SingleFile { Href = serverSingleFile.FileUri, Size = TestData.RegularString.Length, Destination = "regular2" } } }); } }
/// <summary> /// Tests the sync logic with custom <see cref="AppList"/>s. /// </summary> /// <param name="resetMode">The <see cref="SyncResetMode"/> to pass to <see cref="SyncIntegrationManager.Sync"/>.</param> /// <param name="appListLocal">The current local <see cref="AppList"/>.</param> /// <param name="appListLast">The state of the <see cref="AppList"/> after the last successful sync.</param> /// <param name="appListServer">The current server-side <see cref="AppList"/>.</param> private void TestSync(SyncResetMode resetMode, AppList appListLocal, AppList appListLast, AppList appListServer) { appListLocal.SaveXml(_appListPath); if (appListLast != null) { appListLast.SaveXml(_appListPath + SyncIntegrationManager.AppListLastSyncSuffix); } using (var stream = File.Create(_appListPath + ".zip")) appListServer.SaveXmlZip(stream); using (var appListServerFile = File.OpenRead(_appListPath + ".zip")) using (var syncServer = new MicroServer("app-list", appListServerFile)) { using (var integrationManager = new SyncIntegrationManager(_appListPath, new SyncServer { Uri = syncServer.ServerUri }, interfaceId => new Feed(), new SilentTaskHandler())) integrationManager.Sync(resetMode); appListServer = AppList.LoadXmlZip(syncServer.FileContent); } appListLocal = XmlStorage.LoadXml <AppList>(_appListPath); appListLast = XmlStorage.LoadXml <AppList>(_appListPath + SyncIntegrationManager.AppListLastSyncSuffix); Assert.AreEqual(appListLocal, appListServer, "Server and local data should be equal after sync"); Assert.AreEqual(appListLocal, appListLast, "Last sync snapshot and local data should be equal after sync"); }
public void ShouldDownloadMissing() { using var server = new MicroServer("icon.png", "data".ToStream()); var icon = PngIcon(server.FileUri); Verify(icon, "data"); }
public void ShouldReturnStaleOnRefreshFailure() { using var server = new MicroServer("_", new MemoryStream()); var icon = PngIcon(new Uri(server.FileUri + "-invalid")); Inject(icon, "data", timestamp: new DateTime(1980, 1, 1)); Verify(icon, "data"); }
public void DownloadKeyAndReject() { ExpectKeyImport(); MockHandler.AnswerQuestionWith = false; using (var server = new MicroServer(_signature.Fingerprint + ".gpg", new MemoryStream(_keyData))) Assert.Throws <SignatureException>(() => Target.CheckTrust(_combinedBytes, new FeedUri(server.ServerUri + "test.xml"))); Assert.IsFalse(IsKeyTrusted, "Key should not be trusted"); }
public void DownloadSingleArchive() { StoreMock.Setup(x => x.Flush()); using var server = new MicroServer("archive.zip", ZipArchiveStream); TestDownloadArchives( new Archive { Href = server.FileUri, MimeType = Archive.MimeTypeZip, Size = ZipArchiveStream.Length, Extract = "extract", Destination = "destination" }); }
public void DownloadKeyFromMirrorAndApprove() { ExpectKeyImport(); MockHandler.AnswerQuestionWith = true; using (var server = new MicroServer(_signature.Fingerprint + ".gpg", new MemoryStream(_keyData))) Assert.AreEqual(_signature, Target.CheckTrust(_combinedBytes, new FeedUri("http://localhost/test.xml"), mirrorUrl: new FeedUri(server.ServerUri.AbsoluteUri + "test.xml"))); Assert.IsTrue(IsKeyTrusted, "Key should be trusted"); }
public void DownloadKeyAndReject() { ExpectKeyImport(); _handler.AnswerQuestionWith = false; using (var server = new MicroServer(OpenPgpUtilsTest.TestKeyIDString + ".gpg", new MemoryStream(_keyData))) Assert.Throws <SignatureException>(() => _trustManager.CheckTrust(_combinedBytes, new FeedUri(server.ServerUri + "test.xml"))); IsKeyTrusted().Should().BeFalse(because: "Key should not be trusted"); }
public void TestGetIconDownload() { const string iconData = "test"; using (var server = new MicroServer("icon.png", iconData.ToStream())) { string path = _cache.GetIcon(server.FileUri, new SilentTaskHandler()); File.ReadAllText(path).Should().Be(iconData); } }
public void DownloadSingleArchiveMirror() { StoreMock.Setup(x => x.Flush()); using var mirrorServer = new MicroServer("archive/http/invalid/directory%23archive.zip", ZipArchiveStream); Config.FeedMirror = new FeedUri(mirrorServer.ServerUri); TestDownloadArchives( new Archive { Href = new Uri("http://invalid/directory/archive.zip"), MimeType = Archive.MimeTypeZip, Size = ZipArchiveStream.Length, Extract = "extract", Destination = "destination" }); }
public void ShouldRefreshStale() { Skip.IfNot(NetUtils.IsInternetConnected, "Icon cache is not refresh when offline"); using var server = new MicroServer("icon.png", "new".ToStream()); var icon = PngIcon(server.FileUri); Inject(icon, "old", timestamp: new DateTime(1980, 1, 1)); Verify(icon, "new"); }
public void DownloadAndApplySingleFile() { using var stream = SingleFileData.ToStream(); using var microServer = new MicroServer(SingleFileName, stream); var file = new SingleFile { Href = microServer.FileUri, Destination = SingleFileName }; file.DownloadAndApply(new SilentTaskHandler()).Dispose(); file.Size.Should().Be(stream.Length); }
public void AddMissingExceptions() { using var stream = typeof(ImplementationUtilsTest).GetEmbeddedStream("testArchive.zip"); using var microServer = new MicroServer("archive.zip", stream); var implementation = new Implementation { ManifestDigest = new ManifestDigest(sha1New: "invalid"), RetrievalMethods = { new Archive { Href = microServer.FileUri } } }; Assert.Throws <DigestMismatchException>(() => implementation.AddMissing(new SilentTaskHandler())); }
public void DownloadSingleFile() { _storeMock.Setup(x => x.Flush()); using (var server = new MicroServer("regular", TestData.RegularString.ToStream())) { TestDownload( dirPath => File.Exists(Path.Combine(dirPath, "regular")), new SingleFile { Href = server.FileUri, Size = TestData.RegularString.Length, Destination = "regular" }); } }
public void ExistingKeyAndAutoTrust() { RegisterKey(); _feedCacheMock.Setup(x => x.Contains(new FeedUri("http://localhost/test.xml"))).Returns(false); using (var keyInfoServer = new MicroServer("key/" + _signature.Fingerprint, KeyInfoResponse.ToStream())) { UseKeyInfoServer(keyInfoServer); Assert.AreEqual(_signature, Target.CheckTrust(_combinedBytes, new FeedUri("http://localhost/test.xml"))); } Assert.IsTrue(IsKeyTrusted, "Key should be trusted"); }
public void DownloadKeyAndApprove() { ExpectKeyImport(); _handler.AnswerQuestionWith = true; using (var server = new MicroServer(OpenPgpUtilsTest.TestKeyIDString + ".gpg", new MemoryStream(_keyData))) { _trustManager.CheckTrust(_combinedBytes, new FeedUri(server.ServerUri + "test.xml")) .Should().Be(OpenPgpUtilsTest.TestSignature); } IsKeyTrusted().Should().BeTrue(because: "Key should be trusted"); }
public void DownloadAndApplyArchive() { using var stream = typeof(RetrievalMethodUtilsTest).GetEmbeddedStream("testArchive.zip"); using var microServer = new MicroServer("archive.zip", stream); var archive = new Archive { Href = microServer.FileUri }; archive.DownloadAndApply(new SilentTaskHandler()).Dispose(); archive.MimeType.Should().Be(Archive.MimeTypeZip); archive.Size.Should().Be(stream.Length); }
public void ExistingKeyAndNoAutoTrust() { RegisterKey(); _feedCacheMock.Setup(x => x.Contains(new FeedUri("http://localhost/test.xml"))).Returns(true); _handler.AnswerQuestionWith = false; using (var keyInfoServer = new MicroServer("key/" + OpenPgpUtilsTest.TestSignature.FormatFingerprint(), KeyInfoResponse.ToStream())) { UseKeyInfoServer(keyInfoServer); Assert.Throws <SignatureException>(() => _trustManager.CheckTrust(_combinedBytes, new FeedUri("http://localhost/test.xml"))); } IsKeyTrusted().Should().BeFalse(because: "Key should not be trusted"); }
public void ExistingKeyAndAutoTrust() { RegisterKey(); _feedCacheMock.Setup(x => x.Contains(new FeedUri("http://localhost/test.xml"))).Returns(false); using (var keyInfoServer = new MicroServer("key/" + OpenPgpUtilsTest.TestSignature.FormatFingerprint(), KeyInfoResponse.ToStream())) { UseKeyInfoServer(keyInfoServer); _trustManager.CheckTrust(_combinedBytes, new FeedUri("http://localhost/test.xml")) .Should().Be(OpenPgpUtilsTest.TestSignature); } IsKeyTrusted().Should().BeTrue(because: "Key should be trusted"); }
public void DownloadKeyFromMirrorAndApprove() { ExpectKeyImport(); _handler.AnswerQuestionWith = true; using (var server = new MicroServer("keys/" + OpenPgpUtilsTest.TestKeyIDString + ".gpg", new MemoryStream(_keyData))) { _config.FeedMirror = new FeedUri(server.ServerUri); _trustManager.CheckTrust(_combinedBytes, new FeedUri("http://localhost:9999/test/feed.xml")) .Should().Be(OpenPgpUtilsTest.TestSignature); } IsKeyTrusted().Should().BeTrue(because: "Key should be trusted"); }
public void ExistingKeyAndNoAutoTrust() { RegisterKey(); _feedCacheMock.Setup(x => x.Contains(new FeedUri("http://localhost/test.xml"))).Returns(true); MockHandler.AnswerQuestionWith = false; using (var keyInfoServer = new MicroServer("key/" + _signature.Fingerprint, KeyInfoResponse.ToStream())) { UseKeyInfoServer(keyInfoServer); Assert.Throws <SignatureException>(() => Target.CheckTrust(_combinedBytes, new FeedUri("http://localhost/test.xml"))); } Assert.IsFalse(IsKeyTrusted, "Key should not be trusted"); }
public void DownloadAndApplySingleFile() { using (var stream = SingleFileData.ToStream()) using (var microServer = new MicroServer(SingleFileName, stream)) { var file = new SingleFile { Href = microServer.FileUri, Destination = SingleFileName }; file.DownloadAndApply(new SilentTaskHandler()).Dispose(); Assert.AreEqual(stream.Length, file.Size); } }
public void DownloadMultipleArchives() { StoreMock.Setup(x => x.Flush()); using var server1 = new MicroServer("archive.zip", ZipArchiveStream); using var server2 = new MicroServer("archive.zip", ZipArchiveStream); TestDownloadArchives( new Archive { Href = server1.FileUri, MimeType = Archive.MimeTypeZip, Size = ZipArchiveStream.Length, Extract = "extract1", Destination = "destination1" }, new Archive { Href = server2.FileUri, MimeType = Archive.MimeTypeZip, Size = ZipArchiveStream.Length, Extract = "extract2", Destination = "destination2" }); }
public void AddMissingExceptions() { using (var stream = typeof(ArchiveExtractorTest).GetEmbeddedStream("testArchive.zip")) using (var microServer = new MicroServer("archive.zip", stream)) { var implementation = new Implementation { ManifestDigest = new ManifestDigest(sha1New: "invalid"), RetrievalMethods = { new Archive { Href = microServer.FileUri } } }; implementation.Invoking(x => x.AddMissing(new SilentTaskHandler())).ShouldThrow <DigestMismatchException>(); } }
public void DownloadAndApplyArchive() { using (var stream = typeof(ExtractorTest).GetEmbeddedStream("testArchive.zip")) using (var microServer = new MicroServer("archive.zip", stream)) { var archive = new Archive { Href = microServer.FileUri }; archive.DownloadAndApply(new SilentTaskHandler()).Dispose(); Assert.AreEqual(Archive.MimeTypeZip, archive.MimeType); Assert.AreEqual(stream.Length, archive.Size); } }
public void BuildSingleFile() { using (var originalStream = SingleFileData.ToStream()) using (var microServer = new MicroServer(SingleFileName, originalStream)) { var implementation = ImplementationUtils.Build(new SingleFile {Href = microServer.FileUri, Destination = SingleFileName}, new SilentTaskHandler()); Assert.AreEqual(_singleFileSha256Digest, "sha256new_" + implementation.ManifestDigest.Sha256New); var file = (SingleFile)implementation.RetrievalMethods[0]; Assert.AreEqual(originalStream.Length, file.Size); } }
public void TestGetIconDownloadFail() { const string iconData = "test"; using (var server = new MicroServer("empty", new MemoryStream())) { // Write a file to the cache directory, mark it as outdated, use an unreachable/invalid URI string prePath = Path.Combine(_tempDir, new FeedUri(server.FileUri + "-invalid").Escape()); File.WriteAllText(prePath, iconData); File.SetLastWriteTimeUtc(prePath, new DateTime(1980, 1, 1)); string path = _cache.GetIcon(new Uri(server.FileUri + "-invalid"), new SilentTaskHandler()); Assert.AreEqual(iconData, File.ReadAllText(path)); } }
public void DownloadAndApplySingleFile() { using (var stream = SingleFileData.ToStream()) using (var microServer = new MicroServer(SingleFileName, stream)) { var file = new SingleFile {Href = microServer.FileUri, Destination = SingleFileName}; file.DownloadAndApply(new SilentTaskHandler()).Dispose(); Assert.AreEqual(stream.Length, file.Size); } }
public void TestGetIconDownload() { const string iconData = "test"; using (var server = new MicroServer("icon.png", iconData.ToStream())) { string path = _cache.GetIcon(server.FileUri, new SilentTaskHandler()); Assert.AreEqual(iconData, File.ReadAllText(path)); } }