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));
            }
        }
        public override void TestImportSelections()
        {
            var testFeed1 = FeedTest.CreateTestFeed();

            testFeed1.Uri  = FeedTest.Sub1Uri;
            testFeed1.Name = "Sub 1";
            FeedCacheMock.Setup(x => x.GetFeed(FeedTest.Sub1Uri)).Returns(testFeed1);
            var testImplementation1 = (Implementation)testFeed1.Elements[0];

            var testImplementation2 = new Implementation {
                ID = "id2", ManifestDigest = new ManifestDigest(sha256: "abc"), Version = new ImplementationVersion("1.0")
            };
            var testFeed2 = new Feed
            {
                Uri      = FeedTest.Sub2Uri,
                Name     = "Sub 2",
                Elements = { testImplementation2 }
            };

            FeedCacheMock.Setup(x => x.GetFeed(FeedTest.Sub2Uri)).Returns(testFeed2);

            var selections = SelectionsTest.CreateTestSelections();

            // Download uncached implementations
            StoreMock.Setup(x => x.Contains(It.IsAny <ManifestDigest>())).Returns(false);
            FetcherMock.Setup(x => x.Fetch(new[] { testImplementation1, testImplementation2 }));

            using (var tempFile = new TemporaryFile("0install-unit-tests"))
            {
                selections.SaveXml(tempFile);
                RunAndAssert(Resources.AllComponentsDownloaded, 0, selections, tempFile);
            }
        }
        public override void TestNormal()
        {
            var requirements = RequirementsTest.CreateTestRequirements();
            var selections   = SelectionsTest.CreateTestSelections();

            var testFeed1 = FeedTest.CreateTestFeed();

            testFeed1.Uri  = FeedTest.Sub1Uri;
            testFeed1.Name = "Sub 1";
            var testImplementation1 = testFeed1[selections.Implementations[0].ID];

            FeedCacheMock.Setup(x => x.GetFeed(FeedTest.Sub1Uri)).Returns(testFeed1);

            var testImplementation2 = new Implementation {
                ID = "id2", ManifestDigest = new ManifestDigest(sha256: "abc"), Version = new ImplementationVersion("1.0")
            };
            var testFeed2 = new Feed
            {
                Uri      = FeedTest.Sub2Uri,
                Name     = "Sub 2",
                Elements = { testImplementation2 }
            };

            FeedCacheMock.Setup(x => x.GetFeed(FeedTest.Sub2Uri)).Returns(testFeed2);

            SolverMock.Setup(x => x.Solve(requirements)).Returns(selections);

            // Download uncached implementations
            StoreMock.Setup(x => x.Contains(It.IsAny <ManifestDigest>())).Returns(false);
            FetcherMock.Setup(x => x.Fetch(new[] { testImplementation1, testImplementation2 }));

            RunAndAssert(Resources.AllComponentsDownloaded, 0, selections,
                         "http://0install.de/feeds/test/test1.xml", "--command=command", "--os=Windows", "--cpu=i586", "--not-before=1.0", "--before=2.0", "--version-for=http://0install.de/feeds/test/test2.xml", "2.0..!3.0");
        }
Beispiel #4
0
        public void TestNativeFeed()
        {
            var mainFeed = FeedTest.CreateTestFeed();

            mainFeed.Elements.RemoveAt(1);
            mainFeed.Feeds.Clear();
            _feedManagerMock.Setup(x => x.GetFeed(FeedTest.Test1Uri)).Returns(mainFeed);

            using (new LocationsRedirect("0install-unit-tests"))
            {
                var localUri = new FeedUri(Locations.GetSaveDataPath("0install.net", true, "native_feeds", mainFeed.Uri.PrettyEscape()));

                var subFeed = mainFeed.Clone();
                subFeed.Uri = FeedTest.Sub1Uri;
                subFeed.Elements[0].Version = new ImplementationVersion("2.0");
                subFeed.SaveXml(localUri.LocalPath);
                _feedManagerMock.Setup(x => x.GetFeed(localUri)).Returns(subFeed);

                var requirements = new Requirements(FeedTest.Test1Uri, Command.NameRun);
                CollectionAssert.AreEqual(
                    expected: new[]
                {
                    new SelectionCandidate(localUri, new FeedPreferences(), (Implementation)subFeed.Elements[0], requirements),
                    new SelectionCandidate(FeedTest.Test1Uri, new FeedPreferences(), (Implementation)mainFeed.Elements[0], requirements)
                },
                    actual: Target.GetSortedCandidates(requirements));
            }
        }
Beispiel #5
0
        public void TestNormal()
        {
            var requirements  = RequirementsTest.CreateTestRequirements();
            var selectionsOld = SelectionsTest.CreateTestSelections();
            var selectionsNew = SelectionsTest.CreateTestSelections();

            selectionsNew.Implementations[1].Version = new ImplementationVersion("2.0");
            selectionsNew.Implementations.Add(new ImplementationSelection {
                InterfaceUri = FeedTest.Sub3Uri, ID = "id3", Version = new ImplementationVersion("0.1")
            });

            GetMock <ISolver>().SetupSequence(x => x.Solve(requirements))
            .Returns(selectionsOld)
            .Returns(selectionsNew);

            // Download uncached implementations
            ExpectFetchUncached(selectionsNew,
                                new Implementation {
                ID = "id1"
            },
                                new Implementation {
                ID = "id2"
            },
                                new Implementation {
                ID = "id3"
            });

            // Check for <replaced-by>
            GetMock <IFeedCache>().Setup(x => x.GetFeed(FeedTest.Test1Uri)).Returns(FeedTest.CreateTestFeed());

            RunAndAssert("http://0install.de/feeds/test/test2.xml: 1.0 -> 2.0" + Environment.NewLine + "http://0install.de/feeds/test/sub3.xml: new -> 0.1" + Environment.NewLine, 0, selectionsNew,
                         "http://0install.de/feeds/test/test1.xml", "--command=command", "--os=Windows", "--cpu=i586", "--not-before=1.0", "--before=2.0", "--version-for=http://0install.de/feeds/test/test2.xml", "2.0..!3.0");
        }
Beispiel #6
0
        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);
            }
        }
        /// <summary>
        /// Ensures <see cref="DiskFeedCache"/> can handle feed URIs longer than the OSes maximum supported file path length.
        /// </summary>
        // This feature has been removed to improve performance
        //[Test]
        public void TestTooLongFilename()
        {
            if (!WindowsUtils.IsWindows)
            {
                Assert.Ignore("Windows systems have a specific upper limit to file path lengths");
            }

            var longHttpUrlBuilder = new StringBuilder(255);

            for (int i = 0; i < 255; i++)
            {
                longHttpUrlBuilder.Append("x");
            }

            var feed = FeedTest.CreateTestFeed();

            feed.Uri = new FeedUri("http://0install.de/feeds/test-" + longHttpUrlBuilder);

            _cache.Add(feed.Uri, feed.ToArray());

            feed.Normalize(feed.Uri);
            Assert.AreEqual(feed, _cache.GetFeed(feed.Uri));

            Assert.IsTrue(_cache.Contains(feed.Uri));
            _cache.Remove(feed.Uri);
            Assert.IsFalse(_cache.Contains(feed.Uri));
        }
Beispiel #8
0
        public void TestSitePackages()
        {
            var mainFeed = FeedTest.CreateTestFeed();

            mainFeed.Feeds.Clear();
            FeedManagerMock.Setup(x => x[FeedTest.Test1Uri]).Returns(mainFeed);

            using (new LocationsRedirect("0install-unit-tests"))
            {
                var pathComponents = mainFeed.Uri.EscapeComponent()
                                     .Prepend("site-packages")
                                     .Concat(new[] { "xyz", "0install", "feed.xml" });
                var localUri = new FeedUri(Locations.GetSaveDataPath("0install.net", isFile: true, resource: pathComponents.ToArray()));

                var subFeed = mainFeed.Clone();
                subFeed.Uri = FeedTest.Sub1Uri;
                subFeed.Elements[0].Version = new ImplementationVersion("2.0");
                subFeed.SaveXml(localUri.LocalPath);
                FeedManagerMock.Setup(x => x[localUri]).Returns(subFeed);
                PackageManagerMock.Setup(x => x.Query((PackageImplementation)mainFeed.Elements[1])).Returns(Enumerable.Empty <ExternalImplementation>());

                var requirements = new Requirements(FeedTest.Test1Uri, Command.NameRun);
                Target.GetSortedCandidates(requirements).Should().Equal(
                    new SelectionCandidate(localUri, new FeedPreferences(), (Implementation)subFeed.Elements[0], requirements),
                    new SelectionCandidate(FeedTest.Test1Uri, new FeedPreferences(), (Implementation)mainFeed.Elements[0], requirements));
            }
        }
Beispiel #9
0
        public void TestInterfacePreferences()
        {
            var mainFeed = FeedTest.CreateTestFeed();

            mainFeed.Elements.RemoveAt(1);
            mainFeed.Feeds.Clear();
            FeedManagerMock.Setup(x => x[FeedTest.Test1Uri]).Returns(mainFeed);

            new InterfacePreferences {
                Feeds = { new FeedReference {
                              Source = FeedTest.Sub1Uri
                          } }
            }.SaveFor(mainFeed.Uri);

            var subFeed = mainFeed.Clone();

            subFeed.Uri = FeedTest.Sub1Uri;
            subFeed.Elements[0].Version = new ImplementationVersion("2.0");
            FeedManagerMock.Setup(x => x[FeedTest.Sub1Uri]).Returns(subFeed);

            var requirements = new Requirements(FeedTest.Test1Uri, Command.NameRun);

            Target.GetSortedCandidates(requirements).Should().Equal(
                new SelectionCandidate(FeedTest.Sub1Uri, new FeedPreferences(), (Implementation)subFeed.Elements[0], requirements),
                new SelectionCandidate(FeedTest.Test1Uri, new FeedPreferences(), (Implementation)mainFeed.Elements[0], requirements));
        }
Beispiel #10
0
        public override void SetUp()
        {
            base.SetUp();

            _feedPreNormalize = FeedTest.CreateTestFeed();

            _feedPostNormalize = _feedPreNormalize.Clone();
            _feedPostNormalize.Normalize(_feedPreNormalize.Uri);
        }
        public void TestBuildStubNeedsTerminal()
        {
            Skip.IfNot(WindowsUtils.IsWindows, "StubBuilder is only used on Windows");

            var target = new FeedTarget(FeedTest.Test1Uri, FeedTest.CreateTestFeed());

            using var tempFile = new TemporaryFile("0install-unit-tests");
            StubBuilder.BuildRunStub(target, tempFile, _iconStoreMock.Object, needsTerminal: true);
        }
Beispiel #12
0
        public void TestBuildStubNeedsTerminal()
        {
            if (!WindowsUtils.IsWindows)
            {
                Assert.Ignore("StubBuilder is only used on Windows");
            }

            using (var tempFile = new TemporaryFile("0install-unit-tests"))
                new FeedTarget(FeedTest.Test1Uri, FeedTest.CreateTestFeed()).BuildRunStub(tempFile, new SilentTaskHandler(), needsTerminal: true);
        }
    public void InterfacePreferences()
    {
        var mainFeed = FeedTest.CreateTestFeed();

        mainFeed.Elements.RemoveAt(1);
        mainFeed.Feeds.Clear();
        _feedManagerMock.Setup(x => x[FeedTest.Test1Uri]).Returns(mainFeed);

        new InterfacePreferences {
            Feeds = { new() { Source = FeedTest.Sub1Uri } }
        }.SaveFor(mainFeed.Uri);
Beispiel #14
0
        public FeedManagerTest()
        {
            _feedCacheMock    = CreateMock <IFeedCache>();
            _trustManagerMock = CreateMock <ITrustManager>();
            _feedManager      = new FeedManager(_config, _feedCacheMock.Object, _trustManagerMock.Object, new SilentTaskHandler());

            _feedPreNormalize = FeedTest.CreateTestFeed();

            _feedPostNormalize = _feedPreNormalize.Clone();
            _feedPostNormalize.Normalize(_feedPreNormalize.Uri);
        }
        public override void SetUp()
        {
            base.SetUp();

            _backingCacheMock = MockRepository.Create <IFeedCache>();
            _cache            = new MemoryFeedCache(_backingCacheMock.Object);

            // Create a dummy feed
            _feed     = FeedTest.CreateTestFeed();
            _feed.Uri = FeedTest.Test1Uri;
        }
        public void TestAdd()
        {
            var feed = FeedTest.CreateTestFeed();

            feed.Uri = FeedTest.Test3Uri;

            _cache.Add(feed.Uri, feed.ToArray());

            feed.Normalize(feed.Uri);
            Assert.AreEqual(feed, _cache.GetFeed(feed.Uri));
        }
Beispiel #17
0
        public void TestAdd()
        {
            var feed = FeedTest.CreateTestFeed();

            feed.Uri = FeedTest.Test3Uri;

            _cache.Add(feed.Uri, ToArray(feed));

            _cache.GetFeed(feed.Uri)
            .Should().Be(feed);
        }
    public void GetSortedCandidates()
    {
        var mainFeed = FeedTest.CreateTestFeed();

        mainFeed.Feeds.Clear();
        _feedManagerMock.Setup(x => x[FeedTest.Test1Uri]).Returns(mainFeed);
        _packageManagerMock.Setup(x => x.Query((PackageImplementation)mainFeed.Elements[1])).Returns(Enumerable.Empty <ExternalImplementation>());

        var requirements = new Requirements(FeedTest.Test1Uri, Command.NameRun);

        _provider.GetSortedCandidates(requirements).Should().Equal(
            new SelectionCandidate(FeedTest.Test1Uri, new FeedPreferences(), (Implementation)mainFeed.Elements[0], requirements));
    }
Beispiel #19
0
        [Fact] // Ensures valid feeds are correctly imported.
        public void Import()
        {
            var feed = FeedTest.CreateTestFeed();
            var data = SignFeed(feed);

            // No previous feed
            _feedCacheMock.Setup(x => x.GetSignatures(feed.Uri)).Throws <KeyNotFoundException>();

            _feedCacheMock.Setup(x => x.Add(feed.Uri, data));
            using var feedFile = new TemporaryFile("0install-unit-tests");
            File.WriteAllBytes(feedFile, data);
            _feedManager.ImportFeed(feedFile);
        }
        public void ImportIncorrectUri()
        {
            var feed = FeedTest.CreateTestFeed();
            var data = feed.ToXmlString().ToStream().ToArray();

            _trustManagerMock.Setup(x => x.CheckTrust(data, new FeedUri("http://invalid/"), null))
            .Returns(new ValidSignature("a", new DateTime(2000, 1, 1)));
            using (var feedFile = new TemporaryFile("0install-unit-tests"))
            {
                File.WriteAllBytes(feedFile, data);
                Assert.Throws <UriFormatException>(() => Target.ImportFeed(feedFile, new FeedUri("http://invalid/")));
            }
        }
Beispiel #21
0
        /// <summary>
        /// Configures the <see cref="ISolver"/> mock to expect a call with <see cref="RequirementsTest.CreateTestRequirements"/>.
        /// </summary>
        /// <returns>The selections returned by the mock; <see cref="SelectionsTest.CreateTestSelections"/>.</returns>
        protected Selections ExpectSolve()
        {
            var requirements = RequirementsTest.CreateTestRequirements();
            var selections   = SelectionsTest.CreateTestSelections();

            GetMock <ISolver>().Setup(x => x.Solve(requirements)).Returns(selections);

            var feed = FeedTest.CreateTestFeed();

            GetMock <IFeedCache>().Setup(x => x.GetFeed(FeedTest.Test1Uri)).Returns(feed);

            selections.Name = feed.Name;
            return(selections);
        }
        public void ImportReplayAttack()
        {
            var feed = FeedTest.CreateTestFeed();
            var data = SignFeed(feed);

            // Newer signautre present => replay attack
            _feedCacheMock.Setup(x => x.GetSignatures(feed.Uri)).Returns(new[] { new ValidSignature("fingerprint", new DateTime(2002, 1, 1)) });

            using (var feedFile = new TemporaryFile("0install-unit-tests"))
            {
                File.WriteAllBytes(feedFile, data);
                Assert.Throws <ReplayAttackException>(() => Target.ImportFeed(feedFile, feed.Uri));
            }
        }
Beispiel #23
0
        public void Refresh()
        {
            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 feedData = feedStream.ToArray();

            feedStream.Position = 0;

            AssertRefreshData(feed, feedData);
        }
Beispiel #24
0
        [Fact] // Ensures replay attacks are detected.
        public void ImportReplayAttack()
        {
            var feed = FeedTest.CreateTestFeed();
            var data = SignFeed(feed);

            // Newer signature present => replay attack
            _feedCacheMock.Setup(x => x.GetSignatures(feed.Uri)).Returns(new[]
            {
                new ValidSignature(OpenPgpUtilsTest.TestKeyID, OpenPgpUtilsTest.TestFingerprint, new DateTime(2002, 1, 1, 0, 0, 0, DateTimeKind.Utc))
            });

            using var feedFile = new TemporaryFile("0install-unit-tests");
            File.WriteAllBytes(feedFile, data);
            Assert.Throws <ReplayAttackException>(() => _feedManager.ImportFeed(feedFile));
        }
Beispiel #25
0
        public void TestLookupOriginalImplementation()
        {
            var mainFeed = FeedTest.CreateTestFeed();

            mainFeed.Feeds.Clear();
            FeedManagerMock.Setup(x => x[FeedTest.Test1Uri]).Returns(mainFeed);
            PackageManagerMock.Setup(x => x.Query((PackageImplementation)mainFeed.Elements[1])).Returns(Enumerable.Empty <ExternalImplementation>());

            var requirements = new Requirements(FeedTest.Test1Uri, Command.NameRun);
            var candidates   = Target.GetSortedCandidates(requirements);
            var candidate    = candidates.Single().ToSelection(candidates, requirements);

            Target.LookupOriginalImplementation(candidate)
            .Should().Be(mainFeed.Elements[0]);
        }
Beispiel #26
0
        public void DownloadIncorrectUri()
        {
            var feed       = FeedTest.CreateTestFeed();
            var feedStream = new MemoryStream();

            feed.SaveXml(feedStream);
            feedStream.Position = 0;

            using var server = new MicroServer("feed.xml", feedStream);
            var feedUri = new FeedUri(server.FileUri);

            // No previous feed
            _feedCacheMock.Setup(x => x.Contains(feedUri)).Returns(false);

            Assert.Throws <InvalidDataException>(() => _feedManager[feedUri]);
        }
Beispiel #27
0
        public void TestGetFeeds()
        {
            var feed1 = FeedTest.CreateTestFeed();
            var feed3 = FeedTest.CreateTestFeed();

            feed3.Uri = FeedTest.Test3Uri;

            var cacheMock = MockRepository.Create <IFeedCache>();

            cacheMock.Setup(x => x.ListAll()).Returns(new[] { FeedTest.Test1Uri, FeedTest.Test2Uri, FeedTest.Test3Uri });
            cacheMock.Setup(x => x.GetFeed(FeedTest.Test1Uri)).Returns(feed1);
            cacheMock.Setup(x => x.GetFeed(FeedTest.Test2Uri)).Throws(new InvalidDataException("Fake exception for testing"));
            cacheMock.Setup(x => x.GetFeed(FeedTest.Test3Uri)).Returns(feed3);

            CollectionAssert.AreEqual(new[] { feed1, feed3 }, cacheMock.Object.GetAll());
        }
Beispiel #28
0
        public void TestLookupOriginalImplementation()
        {
            var mainFeed = FeedTest.CreateTestFeed();

            mainFeed.Feeds.Clear();
            _feedManagerMock.Setup(x => x.GetFeed(FeedTest.Test1Uri)).Returns(mainFeed);
            _packageManagerMock.Setup(x => x.Query((PackageImplementation)mainFeed.Elements[1])).Returns(Enumerable.Empty <ExternalImplementation>());

            var requirements = new Requirements(FeedTest.Test1Uri, Command.NameRun);
            var candidates   = Target.GetSortedCandidates(requirements);
            var candidate    = candidates.Single().ToSelection(candidates, requirements);

            Assert.AreEqual(
                expected: mainFeed.Elements[0],
                actual: Target.LookupOriginalImplementation(candidate));
        }
        public void TestGetFeeds()
        {
            var feed1 = FeedTest.CreateTestFeed();
            var feed3 = FeedTest.CreateTestFeed();

            feed3.Uri = FeedTest.Test3Uri;

            var cacheMock = CreateMock <IFeedCache>();

            cacheMock.Setup(x => x.ListAll()).Returns(new[] { FeedTest.Test1Uri, FeedTest.Test2Uri, FeedTest.Test3Uri });
            cacheMock.Setup(x => x.GetFeed(FeedTest.Test1Uri)).Returns(feed1);
            cacheMock.Setup(x => x.GetFeed(FeedTest.Test2Uri)).Throws(new InvalidDataException("Fake exception for testing"));
            cacheMock.Setup(x => x.GetFeed(FeedTest.Test3Uri)).Returns(feed3);

            cacheMock.Object.GetAll().Should().Equal(feed1, feed3);
        }
    public DiskFeedCacheTest()
    {
        // Create a temporary cache
        _tempDir = new TemporaryDirectory("0install-test-feeds");
        _cache   = new FeedCache(_tempDir, new Mock <IOpenPgp>().Object);

        // Add some dummy feeds to the cache
        _feed1     = FeedTest.CreateTestFeed();
        _feed1.Uri = FeedTest.Test1Uri;
        _feed1.SaveXml(Path.Combine(_tempDir, _feed1.Uri.Escape()));

        var feed2 = FeedTest.CreateTestFeed();

        feed2.Uri = FeedTest.Test2Uri;
        feed2.SaveXml(Path.Combine(_tempDir, feed2.Uri.Escape()));
        File.WriteAllText(Path.Combine(_tempDir, "http_invalid"), "");
    }