public void Successful_Add_File_Can_Respond_With_Finished_Code() { _nodeFileTransferFactory.RegisterTransfer(Arg.Any <IDownloadFileInformation>()) .Returns(FileTransferResponseCodeTypes.Successful); var expectedCid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("expectedHash")); _fakeDfs.AddAsync(Arg.Any <Stream>(), Arg.Any <string>()).Returns(expectedCid); var protocolMessage = GenerateProtocolMessage(); AddFileToDfsResponse addFileToDfsResponse = null; _nodeFileTransferFactory.RegisterTransfer(Arg.Do <IDownloadFileInformation>(async information => { information.RecipientChannel = Substitute.For <IChannel>(); information.UpdateChunkIndicator(0, true); information.Dispose(); await information.RecipientChannel.WriteAndFlushAsync(Arg.Do <MessageDto>(x => { addFileToDfsResponse = x.Content.FromProtocolMessage <AddFileToDfsResponse>(); _manualResetEvent.Set(); })); })); protocolMessage.SendToHandler(_fakeContext, _addFileToDfsRequestObserver); _manualResetEvent.WaitOne(); addFileToDfsResponse.ResponseCode[0].Should().Be((byte)FileTransferResponseCodeTypes.Finished.Id); addFileToDfsResponse.DfsHash.Should().Be(expectedCid); }
public async Task ReadAsync_Should_Point_To_The_Correct_File() { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("file")); await _dfs.ReadAsync(cid); _fileSystem.File.Received(1) .OpenRead(Arg.Is <string>(s => s.Equals(Path.Combine(_baseFolder, cid.Encode())))); }
public async Task ReadTextAsync_Should_Point_To_The_Correct_File() { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("file")); await _dfs.ReadTextAsync(cid); await _fileSystem.File.Received(1).ReadAllTextAsync( Arg.Is <string>(s => s.Equals(Path.Combine(_baseFolder, cid.Encode()))), Arg.Any <Encoding>(), Arg.Any <CancellationToken>()); }
public async Task ReadTextAsync_Should_Assume_UTF8_Content() { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("file")); await _dfs.ReadTextAsync(cid); await _fileSystem.File.Received(1).ReadAllTextAsync( Arg.Any <string>(), Arg.Is(Encoding.UTF8), Arg.Any <CancellationToken>()); }
/// <inheritdoc /> public async Task <Cid> AddTextAsync(string utf8Content, CancellationToken cancellationToken = default) { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash(utf8Content)); var filePath = Path.Combine(_baseFolder.FullName, cid); await _fileSystem.File.WriteAllTextAsync( filePath, utf8Content, Encoding.UTF8, cancellationToken); return(cid); }
public async Task ReadTextAsync_Should_Rely_On_IpfsEngine_And_Return_Text_Content() { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("file")); _ipfsEngine.FileSystem .ReadAllTextAsync(cid.Encode(), Arg.Any <CancellationToken>()) .Returns(c => "the other content"); var text = await _dfs.ReadTextAsync(cid.Encode()); text.Should().Be("the other content"); }
public async Task ReadAsync_Should_Rely_On_IpfsEngine_And_Return_Streamed_Content() { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("file")); _ipfsEngine.FileSystem .ReadFileAsync(cid.Encode(), Arg.Any <CancellationToken>()) .Returns(c => "the content".ToMemoryStream()); using (var stream = await _dfs.ReadAsync(cid.Encode())) { stream.ReadAllAsUtf8String(false).Should().Be("the content"); } }
public void TryReadDeltaFromDfs_Should_Return_True_When_Hash_Found_On_Dfs_And_Delta_Is_Valid() { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("good hash")); var matchingDelta = DeltaHelper.GetDelta(_hashProvider); _dfs.ReadAsync(cid, CancellationToken.None) .Returns(matchingDelta.ToByteArray().ToMemoryStream()); var found = _dfsReader.TryReadDeltaFromDfs(cid, out var delta, CancellationToken.None); found.Should().BeTrue(); delta.Should().Be(matchingDelta); }
public async Task AddAsync_Should_Save_File_In_Subfolder_With_Hash_As_Name() { _fileSystem.File.Create(Arg.Any <string>()).Returns(new MemoryStream()); var contentBytes = BitConverter.GetBytes(123456); var contentStream = contentBytes.ToMemoryStream(); var expectedCid = CidHelper.CreateCid(_hashProvider.ComputeMultiHash(contentBytes)); var cid = await _dfs.AddAsync(contentStream); cid.Should().Be(expectedCid); }
public void Cli_Can_Request_Node_Info() { var hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256")); var hash = CidHelper.CreateCid(hashProvider.ComputeUtf8MultiHash("hello")); var result = Shell.ParseCommand("getdelta", "-h", hash, NodeArgumentPrefix, ServerNodeName); result.Should().BeTrue(); var request = AssertSentMessageAndGetMessageContent <GetDeltaRequest>(); MultiBase.Encode(request.DeltaDfsHash.ToByteArray(), "base32").Should().Be(hash); }
public void TryReadDeltaFromDfs_Should_Return_False_And_Log_When_Hash_Not_Found_On_Dfs() { var exception = new FileNotFoundException("that hash is not good"); _dfs.ReadAsync(Arg.Any <Cid>(), Arg.Any <CancellationToken>()) .Throws(exception); var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("bad hash")); _dfsReader.TryReadDeltaFromDfs(cid, out _, CancellationToken.None).Should().BeFalse(); _logger.Received(1).Error(exception, Arg.Any <string>(), Arg.Is <Cid>(s => s == cid)); }
public void TryReadDeltaFromDfs_Should_Pass_Cancellation_Token() { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("good hash")); var cancellationToken = new CancellationToken(); var matchingDelta = DeltaHelper.GetDelta(_hashProvider); _dfs.ReadAsync(cid, CancellationToken.None) .Returns(matchingDelta.ToByteArray().ToMemoryStream()); _dfsReader.TryReadDeltaFromDfs(cid, out _, CancellationToken.None); _dfs.Received(1).ReadAsync(Arg.Is(cid), Arg.Is(cancellationToken)); }
/// <inheritdoc /> public async Task <Cid> AddAsync(Stream content, string name = "", CancellationToken cancellationToken = default) { var cid = CidHelper.CreateCid(_hashProvider.ComputeMultiHash(content)); var filePath = Path.Combine(_baseFolder.FullName, cid); using (var file = _fileSystem.File.Create(filePath)) { content.Position = 0; await content.CopyToAsync(file, cancellationToken).ConfigureAwait(false); } return(cid); }
public async Task AddTextAsync_Should_Save_File_In_Subfolder_With_Hash_As_Name() { var someGoodUtf8Content = "some good utf8 content!"; var cid = await _dfs.AddTextAsync(someGoodUtf8Content); var expectedCid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash(someGoodUtf8Content)); await _fileSystem.File.Received(1).WriteAllTextAsync( Arg.Is(Path.Combine(_baseFolder, expectedCid.Encode())), Arg.Any <string>(), Arg.Is(Encoding.UTF8), Arg.Any <CancellationToken>()); cid.Should().Be(expectedCid); }
public void GetDeltaRequest_Can_Be_Sent() { //Arrange var hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256")); var deltaMultiHash = CidHelper.CreateCid(hashProvider.ComputeUtf8MultiHash("previous")); var commandContext = TestCommandHelpers.GenerateCliRequestCommandContext(); var connectedNode = commandContext.GetConnectedNode(null); var command = new GetDeltaCommand(commandContext, _logger); //Act TestCommandHelpers.GenerateRequest(commandContext, command, "-n", "node1", "-h", deltaMultiHash); //Assert var requestSent = TestCommandHelpers.GetRequest <GetDeltaRequest>(connectedNode); requestSent.Should().BeOfType(typeof(GetDeltaRequest)); }
public void TryReadDeltaFromDfs_Should_Return_False_When_Hash_Found_On_Dfs_And_Delta_Is_Not_Valid() { var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("good hash")); var matchingDelta = DeltaHelper.GetDelta(_hashProvider); matchingDelta.PreviousDeltaDfsHash = ByteString.Empty; new Action(() => matchingDelta.IsValid()).Should() .Throw <InvalidDataException>("otherwise this test is useless"); _dfs.ReadAsync(cid, CancellationToken.None) .Returns(matchingDelta.ToByteArray().ToMemoryStream()); var found = _dfsReader.TryReadDeltaFromDfs(cid, out var delta, CancellationToken.None); found.Should().BeFalse(); delta.Should().BeNull(); }
public DfsTests() { _hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256")); _ipfsEngine = Substitute.For <ICoreApi>(); var fileSystem = Substitute.For <IFileSystemApi>(); _ipfsEngine.FileSystem.Returns(fileSystem); var logger = Substitute.For <ILogger>(); _expectedCid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("data")); _addedRecord = Substitute.For <IFileSystemNode>(); _addedRecord.Id.ReturnsForAnyArgs(_expectedCid); _cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(DelayInMs)); _dfs = new Dfs(_ipfsEngine, _hashProvider, logger); }
public void TryGetDelta_Should_Not_Hit_The_Dfs_Or_Store_Delta_When_Delta_Is_In_Cache() { _memoryCache.ClearReceivedCalls(); // needed because of the CreateEntry call from the DeltaCache .ctor var deltaFromCache = DeltaHelper.GetDelta(_hashProvider); var cid = CidHelper.CreateCid(_hashProvider.ComputeUtf8MultiHash("abc")); _memoryCache.TryGetValue(Arg.Is(cid.Hash), out Arg.Any <Delta>()) .Returns(ci => { ci[1] = deltaFromCache; return(true); }); var found = _deltaCache.TryGetOrAddConfirmedDelta(cid.Hash, out var delta); delta.Should().Be(deltaFromCache); found.Should().BeTrue(); _dfsReader.DidNotReceiveWithAnyArgs().TryReadDeltaFromDfs(default, out _);