Exemple #1
0
        public void GetLatestVersion_PaketFileCorrupt_DownloadPaketFile()
        {
            //arrange
            var content  = Guid.NewGuid().ToByteArray();
            var sha      = new SHA256Managed();
            var checksum = sha.ComputeHash(new MemoryStream(content));
            var hash     = BitConverter.ToString(checksum).Replace("-", String.Empty);
            var hashFile = new PaketHashFile(new List <string> {
                hash + " paket.exe"
            });

            var pathInCache = Path.Combine(CacheDownloadStrategy.PaketCacheDir, "any", "paket.exe");

            mockEffectiveStrategy.Setup(x => x.GetLatestVersion(true)).Throws <WebException>().Verifiable();
            mockFileProxy.Setup(x => x.OpenRead(pathInCache)).Returns(() => new MemoryStream(Guid.NewGuid().ToByteArray()));
            mockFileProxy.Setup(x => x.CreateExclusive(It.IsAny <string>())).Returns(() => new MemoryStream());
            mockFileProxy.Setup(x => x.OpenRead(It.Is <string>(s => s.StartsWith("C:\\temp"))))
            .Returns(() => new MemoryStream(content));
            mockFileProxy.Setup(x => x.FileExists(pathInCache)).Returns(true);
            mockFileProxy.Setup(x => x.GetTempPath()).Returns("C:\\temp");

            //act
            sut.DownloadVersion("any", "any", hashFile);

            //assert
            mockEffectiveStrategy.Verify(x => x.DownloadVersion(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <PaketHashFile>()), Times.Once);
            mockFileProxy.Verify(x => x.CreateExclusive("any"));
        }
Exemple #2
0
        protected override PaketHashFile DownloadHashFileCore(string latestVersion)
        {
            var url = string.Format(Constants.PaketCheckSumDownloadUrlTemplate, latestVersion);

            ConsoleImpl.WriteInfo("Starting download from {0}", url);

            var content = WebRequestProxy.DownloadString(url);

            return(PaketHashFile.FromString(content));
        }
        protected override PaketHashFile DownloadHashFileCore(string latestVersion)
        {
            if (!EffectiveStrategy.CanDownloadHashFile)
            {
                return(null);
            }

            var cachedPath = GetHashFilePathInCache(latestVersion);

            if (FileSystemProxy.FileExists(cachedPath))
            {
                // Maybe there's another bootstraper process running
                // We trust it to close the file with the correct content
                FileSystemProxy.WaitForFileFinished(cachedPath);
                ConsoleImpl.WriteInfo("Hash file of version {0} found in cache.", latestVersion);
            }
            else
            {
                FileSystemProxy.CreateDirectory(Path.GetDirectoryName(cachedPath));
                try
                {
                    ConsoleImpl.WriteInfo("Hash file of version {0} not found in cache.", latestVersion);
                    var hashFile = EffectiveStrategy.DownloadHashFile(latestVersion);
                    Debug.Assert(hashFile != null,
                                 "'EffectiveStrategy.CanDownloadHashFile' but DownloadHashFile returned null");

                    ConsoleImpl.WriteTrace("Writing hashFile file in cache.");
                    ConsoleImpl.WriteTrace("hashFile -> {0}", cachedPath);
                    using (var finalStream = FileSystemProxy.CreateExclusive(cachedPath))
                    {
                        hashFile.WriteToStream(finalStream);
                    }

                    return(hashFile);
                }
                catch (IOException ex)
                {
                    if (ex.HResult == HelperProxies.FileSystemProxy.HRESULT_ERROR_SHARING_VIOLATION)
                    {
                        ConsoleImpl.WriteTrace("Can't lock hashFile file, another instance might be writing it. Waiting.");
                        // Same as before let's trust other bootstraper processes
                        FileSystemProxy.WaitForFileFinished(cachedPath);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(PaketHashFile.FromStrings(FileSystemProxy.ReadAllLines(cachedPath)));
        }
Exemple #4
0
        public void GetLatestVersion_PaketHashFileExistsButIsCorrupt_ThrowException()
        {
            var hashFile = new PaketHashFile(new List <string> {
                Guid.NewGuid().ToString().Replace("-", String.Empty) + " not-paket.exe"
            });

            //arrange
            mockEffectiveStrategy.Setup(x => x.GetLatestVersion(true)).Throws <WebException>().Verifiable();
            mockFileProxy.Setup(x => x.OpenRead(It.IsAny <string>())).Returns(new MemoryStream(Guid.NewGuid().ToByteArray()));
            mockFileProxy.Setup(x => x.FileExists(It.IsAny <string>())).Returns(true);

            //act
            Assert.Throws <InvalidDataException>(() => sut.DownloadVersion("any", "any", hashFile));

            //assert
            mockEffectiveStrategy.Verify(x => x.DownloadVersion(It.IsAny <string>(), It.IsAny <string>(), null), Times.Never);
        }
Exemple #5
0
        public void GetLatestVersion_PaketFileNotCorrupt_DontDownloadPaketFile()
        {
            //arrange
            var content  = Guid.NewGuid().ToByteArray();
            var sha      = new SHA256Managed();
            var checksum = sha.ComputeHash(new MemoryStream(content));
            var hash     = BitConverter.ToString(checksum).Replace("-", String.Empty);
            var hashFile = new PaketHashFile(new List <string> {
                hash + " paket.exe"
            });

            mockEffectiveStrategy.Setup(x => x.GetLatestVersion(true)).Throws <WebException>().Verifiable();
            mockFileProxy.Setup(x => x.OpenRead(It.IsAny <string>())).Returns(() => new MemoryStream(content));
            mockFileProxy.Setup(x => x.CreateExclusive(It.IsAny <string>())).Returns(() => new MemoryStream());
            mockFileProxy.Setup(x => x.FileExists(It.IsAny <string>())).Returns(true);

            //act
            sut.DownloadVersion("any", "any", hashFile);

            //assert
            mockEffectiveStrategy.Verify(x => x.DownloadVersion(It.IsAny <string>(), It.IsAny <string>(), null), Times.Never);
            mockFileProxy.Verify(x => x.CreateExclusive("any"));
        }
        private void DownloadAndPlaceInCache(string latestVersion, string target, string cached, PaketHashFile hashFile)
        {
            FileSystemProxy.CreateDirectory(Path.GetDirectoryName(cached));

            var tempFile = Path.Combine(FileSystemProxy.GetTempPath(), Guid.NewGuid().ToString());

            EffectiveStrategy.DownloadVersion(latestVersion, tempFile, hashFile);

            if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashFile, latestVersion, tempFile))
            {
                throw new InvalidOperationException(
                          string.Format("paket.exe was corrupted after download by {0}: Invalid hash",
                                        EffectiveStrategy.Name));
            }

            ConsoleImpl.WriteTrace("Caching version {0} for later, hash is ok", latestVersion);
            using (var targetStream = FileSystemProxy.CreateExclusive(target))
                using (var cachedStream = FileSystemProxy.CreateExclusive(cached))
                {
                    using (var tempStream = FileSystemProxy.OpenRead(tempFile))
                    {
                        tempStream.CopyTo(targetStream);
                        tempStream.Seek(0, SeekOrigin.Begin);
                        tempStream.CopyTo(cachedStream);
                    }
                }

            FileSystemProxy.DeleteFile(tempFile);
        }
        protected override void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashFile)
        {
            var cached = Path.Combine(PaketCacheDir, latestVersion, "paket.exe");

            if (!FileSystemProxy.FileExists(cached))
            {
                ConsoleImpl.WriteInfo("Version {0} not found in cache.", latestVersion);

                DownloadAndPlaceInCache(latestVersion, target, cached, hashFile);
                return;
            }

            FileSystemProxy.WaitForFileFinished(cached);

            if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashFile, latestVersion, cached))
            {
                ConsoleImpl.WriteWarning("Version {0} found in cache but it's hashFile isn't valid.", latestVersion);

                DownloadAndPlaceInCache(latestVersion, target, cached, hashFile);
                return;
            }

            ConsoleImpl.WriteInfo("Copying version {0} from cache.", latestVersion);
            ConsoleImpl.WriteTrace("{0} -> {1}", cached, target);
            using (var targetStream = FileSystemProxy.CreateExclusive(target))
                using (var cachedStream = FileSystemProxy.OpenRead(cached))
                {
                    cachedStream.CopyTo(targetStream);
                }
        }
Exemple #8
0
        protected override void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashfile)
        {
            var apiHelper = new NugetApiHelper(PaketNugetPackageName, NugetSource);

            const string paketNupkgFile         = "paket.latest.nupkg";
            const string paketNupkgFileTemplate = "paket.{0}.nupkg";

            var paketDownloadUrl = apiHelper.GetLatestPackage();
            var paketFile        = paketNupkgFile;

            if (!String.IsNullOrWhiteSpace(latestVersion))
            {
                paketDownloadUrl = apiHelper.GetSpecificPackageVersion(latestVersion);
                paketFile        = String.Format(paketNupkgFileTemplate, latestVersion);
            }

            var randomFullPath = Path.Combine(Folder, Path.GetRandomFileName());

            FileSystemProxy.CreateDirectory(randomFullPath);
            var paketPackageFile = Path.Combine(randomFullPath, paketFile);

            if (FileSystemProxy.DirectoryExists(NugetSource))
            {
                if (String.IsNullOrWhiteSpace(latestVersion))
                {
                    latestVersion = GetLatestVersion(false);
                }
                var sourcePath = Path.Combine(NugetSource, String.Format(paketNupkgFileTemplate, latestVersion));

                ConsoleImpl.WriteInfo("Starting download from {0}", sourcePath);

                FileSystemProxy.CopyFile(sourcePath, paketPackageFile);
            }
            else
            {
                ConsoleImpl.WriteInfo("Starting download from {0}", paketDownloadUrl);

                try
                {
                    WebRequestProxy.DownloadFile(paketDownloadUrl, paketPackageFile);
                }
                catch (WebException webException)
                {
                    if (webException.Status == WebExceptionStatus.ProtocolError && !string.IsNullOrEmpty(latestVersion))
                    {
                        var response = (HttpWebResponse)webException.Response;
                        if (response.StatusCode == HttpStatusCode.NotFound)
                        {
                            throw new WebException(String.Format("Version {0} wasn't found (404)", latestVersion),
                                                   webException);
                        }
                        if (response.StatusCode == HttpStatusCode.BadRequest)
                        {
                            // For cases like "The package version is not a valid semantic version"
                            throw new WebException(String.Format("Unable to get version '{0}': {1}", latestVersion, response.StatusDescription),
                                                   webException);
                        }
                    }
                    Console.WriteLine(webException.ToString());
                    throw;
                }
            }

            FileSystemProxy.ExtractToDirectory(paketPackageFile, randomFullPath);
            var paketSourceFile = Path.Combine(randomFullPath, "tools", "paket.exe");

            FileSystemProxy.CopyFile(paketSourceFile, target, true);
            FileSystemProxy.DeleteDirectory(randomFullPath, true);
        }
Exemple #9
0
        protected override void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashfile)
        {
            string url = String.Format(Constants.PaketNupkgDownloadUrlTemplate, latestVersion);

            ConsoleImpl.WriteInfo("Starting download from {0}", url);

            var tmpFile = BootstrapperHelper.GetTempFile("paketnupkg");

            WebRequestProxy.DownloadFile(url, tmpFile);

            string packageName = Path.GetFileName(url);

            var randomFullPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            FileSystemProxy.CreateDirectory(randomFullPath);

            string packagePath = Path.Combine(randomFullPath, packageName);

            FileSystemProxy.CopyFile(tmpFile, packagePath, true);
            FileSystemProxy.DeleteFile(tmpFile);

            var installAsTool = new InstallKind.InstallAsTool(FileSystemProxy);

            installAsTool.Run(randomFullPath, target, latestVersion);

            FileSystemProxy.DeleteDirectory(randomFullPath, true);
        }
Exemple #10
0
        protected override void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashfile)
        {
            var url = String.Format(Constants.PaketExeDownloadUrlTemplate, latestVersion);

            ConsoleImpl.WriteInfo("Starting download from {0}", url);

            var tmpFile = BootstrapperHelper.GetTempFile("paket");

            WebRequestProxy.DownloadFile(url, tmpFile);

            if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashfile, latestVersion, tmpFile))
            {
                ConsoleImpl.WriteWarning("Hash of downloaded paket.exe is invalid, retrying once");

                WebRequestProxy.DownloadFile(url, tmpFile);

                if (!BootstrapperHelper.ValidateHash(FileSystemProxy, hashfile, latestVersion, tmpFile))
                {
                    ConsoleImpl.WriteWarning("Hash of downloaded paket.exe still invalid (Using the file anyway)");
                }
                else
                {
                    ConsoleImpl.WriteTrace("Hash of downloaded file successfully found in {0}", hashfile);
                }
            }
            else
            {
                ConsoleImpl.WriteTrace("Hash of downloaded file successfully found in {0}", hashfile);
            }

            FileSystemProxy.CopyFile(tmpFile, target, true);
            FileSystemProxy.DeleteFile(tmpFile);
        }
 protected abstract void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashfile);
 public void DownloadVersion(string latestVersion, string target, PaketHashFile hashfile)
 {
     Wrap(() => DownloadVersionCore(latestVersion, target, hashfile), "DownloadVersion");
 }
 protected override void DownloadVersionCore(string latestVersion, string target, PaketHashFile hashfile)
 {
     _effectiveStrategy.DownloadVersion(latestVersion, target, hashfile);
     TouchTarget(target);
 }