public void AddPackageAsFiles() { string file1Content = "file 1 content"; string file2Content = "file 2 content"; Stream file1 = StreamsHelper.StreamFromString(file1Content); Stream file2 = StreamsHelper.StreamFromString(file2Content); string expectedFullhash = HashServiceHelper.Instance().FromString( HashServiceHelper.Instance().FromString("folder1/file1.txt") + HashServiceHelper.Instance().FromString(file1Content) + HashServiceHelper.Instance().FromString("folder2/file2.txt") + HashServiceHelper.Instance().FromString(file2Content)); PackageCreateArguments postArgs = new PackageCreateArguments { Id = Guid.NewGuid().ToString(), Files = new PackageCreateItem[] { new PackageCreateItem(file1, "folder1/file1.txt"), new PackageCreateItem(file2, "folder2/file2.txt") } }; PackageCreateResult result = _packageService.CreatePackage(postArgs); Assert.True(result.Success); Assert.Equal(2, TestingWorkspace.Repository.Count()); Assert.Empty(TestingWorkspace.Incoming); Assert.Equal(expectedFullhash, result.PackageHash); }
public Base() { PackageCreate = new Core.PackageCreate( IndexReader, Settings, new TestLogger <IPackageCreate>(), new Core.Workspace(Settings, new TestLogger <IWorkspace>(), HashServiceHelper.Instance()), HashServiceHelper.Instance()); }
public void AddPackageAsArchive() { Dictionary <string, string> files = new Dictionary <string, string>(); string file1Content = "file 1 content"; string file2Content = "file 2 content"; string expectedFullhash = HashServiceHelper.Instance().FromString( HashServiceHelper.Instance().FromString("folder1/file1.txt") + HashServiceHelper.Instance().FromString(file1Content) + HashServiceHelper.Instance().FromString("folder2/file2.txt") + HashServiceHelper.Instance().FromString(file2Content)); files.Add("folder1/file1.txt", file1Content); files.Add("folder2/file2.txt", file2Content); MemoryStream zipStream = new MemoryStream(); using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true)) { foreach (var file in files) { ZipArchiveEntry fileEntry = archive.CreateEntry(file.Key); using (var entryStream = fileEntry.Open()) using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write(file.Value); } } } PackageCreateArguments postArgs = new PackageCreateArguments { Id = Guid.NewGuid().ToString(), IsArchive = true, Files = new PackageCreateItem[] { new PackageCreateItem(zipStream, "folder/archive.zip") } }; PackageCreateResult result = _packageService.CreatePackage(postArgs); if (!result.Success) { throw new Exception(result.PublicError); } Assert.True(result.Success); Assert.Equal(2, TestingWorkspace.Repository.Count()); Assert.Empty(TestingWorkspace.Incoming); Assert.Equal(expectedFullhash, result.PackageHash); }
public void GetFile() { // create package, files folder and item location in one byte[] content = Encoding.ASCII.GetBytes("some content"); string hash = HashServiceHelper.Instance().FromByteArray(content); string packageFolder = Path.Combine(this.Settings.RepositoryPath, "path", "to", "file", hash); Directory.CreateDirectory(packageFolder); File.WriteAllBytes(Path.Join(packageFolder, "bin"), content); Stream stream = this.IndexReader.GetFile(Core.FileIdentifier.Cloak("path/to/file", hash)).Content; byte[] testContent = Core.StreamsHelper.StreamToByteArray(stream); Assert.Equal(content, testContent); }
public void PurgeWithException() { IFileSystem fileSystem = Mock.Of <IFileSystem>(); Mock.Get(fileSystem).Setup(f => f.File.Delete(It.IsAny <string>())).Throws <IOException>(); IIndexReader indexReader = new Core.IndexReader(Settings, new ThreadDefault(), TagService, Logger, fileSystem, HashServiceHelper.Instance()); // force an archive and ensure that all archives will be purged Settings.MaxArchives = 0; File.WriteAllText(Path.Join(Settings.ArchivePath, "test"), string.Empty); indexReader.PurgeOldArchives(); // no assert! exception is throttled internally, we just need coverage here }
public void Wipe() { string testFolder = Path.Join(AppDomain.CurrentDomain.BaseDirectory, this.GetType().Name); if (Directory.Exists(testFolder)) { Directory.Delete(testFolder, true); } Directory.CreateDirectory(testFolder); Core.ITetriSettings settings = new Core.TetriSettings(new TestLogger <Core.TetriSettings>()) { TempPath = Path.Join(testFolder, "Temp") }; Directory.CreateDirectory(settings.TempPath); string testFilePath = Path.Join(settings.TempPath, "test"); File.WriteAllText(testFilePath, string.Empty); Core.ITagsService tagService = new Core.TagsService( settings, new TestLogger <Core.ITagsService>(), new Core.PackageListCache(MemoryCacheHelper.GetInstance())); Core.IIndexReader reader = new Core.IndexReader(settings, new Core.ThreadDefault(), tagService, new TestLogger <Core.IIndexReader>(), new FileSystem(), HashServiceHelper.Instance()); reader.Initialize(); Assert.False(File.Exists(testFilePath)); }
public void GetAfterWaiting() { // we need a valid package first TestPackage testPackage = PackageHelper.CreatePackage(this.Settings); // create a fake archive temp file so GetPackageAsArchive() goes into wait state string tempArchivePath = this.IndexReader.GetPackageArchiveTempPath(testPackage.Name); File.WriteAllText(tempArchivePath, string.Empty); // seeing as our fake temp archive bypasses archive creation, we should make a fake archive too, it should just be a file string archivePath = this.IndexReader.GetPackageArchivePath(testPackage.Name); File.WriteAllText(archivePath, "some-fake-archive-content"); // lock the temp archive so it doesn't get auto-clobbered using (Stream lockStream = new FileStream(tempArchivePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { // mock IThread.Sleep, in this method, we will release and delete our temp archive lock, so the archive fetch can proceed IThread mockThread = Mock.Of <IThread>(); Mock.Get(mockThread) .Setup(r => r.Sleep(It.IsAny <int>())) .Callback <int>(time => { lockStream.Close(); File.Delete(tempArchivePath); }); // make a custom reader with our mocked Thread IIndexReader indexReader = new Core.IndexReader(this.Settings, mockThread, this.TagService, this.Logger, new FileSystem(), HashServiceHelper.Instance()); using (Stream zipStream = indexReader.GetPackageAsArchive(testPackage.Name)) { // confirm we got our fake archive content back Assert.Equal("some-fake-archive-content", StreamsHelper.StreamToString(zipStream)); } } }
public Base() { Workspace = new Core.Workspace(Settings, base.WorkspaceLogger, HashServiceHelper.Instance()); Workspace.Initialize(); }