Skip to content

Commit

Permalink
Make use of new ipfscore package. Various analyzer updates
Browse files Browse the repository at this point in the history
  • Loading branch information
erikmav committed Apr 17, 2023
1 parent c54af88 commit acddd18
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 94 deletions.
2 changes: 0 additions & 2 deletions src/CoreApi/PubSubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,5 @@ void ProcessMessages(Action<PublishedMessage> handler, StreamReader sr, Cancella
sr.Dispose();
}
}

}

}
9 changes: 5 additions & 4 deletions src/IpfsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ namespace Ipfs.Http
/// </remarks>
public partial class IpfsClient : ICoreApi
{
const string unknownFilename = "unknown";
private const string unknownFilename = "unknown";

static readonly object safe = new object();
static HttpClient? api = null;
private static readonly object safe = new object();
private static HttpClient? api;

/// <summary>
/// The default URL to the IPFS HTTP API server.
Expand All @@ -42,7 +42,7 @@ public partial class IpfsClient : ICoreApi
/// </remarks>
public static Uri DefaultApiUri = new Uri(
Environment.GetEnvironmentVariable("IpfsHttpApi")
?? "http://localhost:5001");
?? "http://localhost:1206");

/// <summary>
/// Creates a new instance of the <see cref="IpfsClient"/> class and sets the
Expand Down Expand Up @@ -493,6 +493,7 @@ public async Task<String> UploadAsync(string command, CancellationToken cancel,
return json;
}
}

/// <summary>
/// Perform an <see href="https://ipfs.io/docs/api/">IPFS API command</see> that
/// requires uploading of a "file".
Expand Down
4 changes: 2 additions & 2 deletions src/IpfsHttpClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<DebugType>full</DebugType>

<!-- https://semver.org/spec/v2.0.0.html -->
<Version>0.0.6</Version>
<Version>0.0.7</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>

<!-- Nuget specs -->
Expand Down Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="IpfsShipyard.Ipfs.Core" Version="0.0.4" />
<PackageReference Include="IpfsShipyard.Ipfs.Core" Version="0.0.5" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Multiformats.Base" Version="2.0.2" />
Expand Down
1 change: 0 additions & 1 deletion test/BlockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ public void DataStream()
Assert.AreEqual(3, stream.ReadByte());
Assert.AreEqual(-1, stream.ReadByte(), "at eof");
}

}
}
106 changes: 67 additions & 39 deletions test/CoreApi/BitswapApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,71 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ipfs.Http
{
[TestClass]
public class BitswapApiTest
public sealed class BitswapApiTest
{
private readonly IpfsClient ipfs = TestFixture.Ipfs;

[TestMethod]
public async Task Wants()
{
var block = new DagNode(Encoding.UTF8.GetBytes("BitswapApiTest unknown block"));
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(() => ipfs.Bitswap.GetAsync(block.Id).Wait());

var endTime = DateTime.Now.AddSeconds(10);
while (DateTime.Now < endTime)
{
await Task.Delay(100);
var wants = await ipfs.Bitswap.WantsAsync();
if (wants.Contains(block.Id))
return;
}
Assert.Fail("wanted block is missing");
await RunAsyncTaskAndTestAsync(
ct => ipfs.Bitswap.GetAsync(block.Id, ct),
async () =>
{
var endTime = DateTime.Now.AddSeconds(10);
while (DateTime.Now < endTime)
{
await Task.Delay(100);
var wants = await ipfs.Bitswap.WantsAsync();
if (wants.Contains(block.Id))
{
return;
}
}
Assert.Fail("wanted block is missing");
});
}

[TestMethod]
[Ignore("https://github.com/ipfs/go-ipfs/issues/5295")]
public async Task Unwant()
{
var block = new DagNode(Encoding.UTF8.GetBytes("BitswapApiTest unknown block 2"));
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(() => ipfs.Bitswap.GetAsync(block.Id).Wait());
await RunAsyncTaskAndTestAsync(
ct => ipfs.Bitswap.GetAsync(block.Id, ct),
async () =>
{
var endTime = DateTime.Now.AddSeconds(10);
while (true)
{
if (DateTime.Now > endTime)
Assert.Fail("wanted block is missing");
await Task.Delay(100);
var wants = await ipfs.Bitswap.WantsAsync();
if (wants.Contains(block.Id))
break;
}

var endTime = DateTime.Now.AddSeconds(10);
while (true)
{
if (DateTime.Now > endTime)
Assert.Fail("wanted block is missing");
await Task.Delay(100);
var wants = await ipfs.Bitswap.WantsAsync();
if (wants.Contains(block.Id))
break;
}

await ipfs.Bitswap.UnwantAsync(block.Id);
endTime = DateTime.Now.AddSeconds(10);
while (true)
{
if (DateTime.Now > endTime)
Assert.Fail("unwanted block is present");
await Task.Delay(100);
var wants = await ipfs.Bitswap.WantsAsync();
if (!wants.Contains(block.Id))
break;
}
await ipfs.Bitswap.UnwantAsync(block.Id);
endTime = DateTime.Now.AddSeconds(10);
while (true)
{
if (DateTime.Now > endTime)
Assert.Fail("unwanted block is present");
await Task.Delay(100);
var wants = await ipfs.Bitswap.WantsAsync();
if (!wants.Contains(block.Id))
break;
}
});
}

[TestMethod]
Expand All @@ -70,5 +77,26 @@ public async Task Ledger()
Assert.IsNotNull(ledger.Peer);
Assert.AreEqual(peer.Id, ledger.Peer!.Id);
}

private static async Task RunAsyncTaskAndTestAsync(Func<CancellationToken, Task> asyncTaskWork, Func<Task> testWork)
{
var cts = new CancellationTokenSource();
var asyncTask = Task.Run(async () => await asyncTaskWork(cts.Token));
try
{
await testWork();
}
finally
{
cts.Cancel();
try
{
await asyncTask;
}
catch
{
}
}
}
}
}
76 changes: 38 additions & 38 deletions test/CoreApi/BlockApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,108 +11,108 @@ namespace Ipfs.Http
public class BlockApiTest
{
private readonly IpfsClient ipfs = TestFixture.Ipfs;
private const string id = "QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ";
private const string id = "bafkreiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu";
private readonly byte[] blob = Encoding.UTF8.GetBytes("blorb");

[TestMethod]
public void Put_Bytes()
public async Task Put_Bytes()
{
var cid = ipfs.Block.PutAsync(blob).Result;
var cid = await ipfs.Block.PutAsync(blob);
Assert.AreEqual(id, (string)cid);

var data = ipfs.Block.GetAsync(cid).Result;
var data = await ipfs.Block.GetAsync(cid);
Assert.AreEqual(blob.Length, data.Size);
CollectionAssert.AreEqual(blob, data.DataBytes);
}

[TestMethod]
public void Put_Bytes_ContentType()
public async Task Put_Bytes_ContentType()
{
var cid = ipfs.Block.PutAsync(blob, contentType: "raw").Result;
var cid = await ipfs.Block.PutAsync(blob, contentType: "raw");
Assert.AreEqual("bafkreiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu", (string)cid);

var data = ipfs.Block.GetAsync(cid).Result;
var data = await ipfs.Block.GetAsync(cid);
Assert.AreEqual(blob.Length, data.Size);
CollectionAssert.AreEqual(blob, data.DataBytes);
}

[TestMethod]
public void Put_Bytes_Hash()
public async Task Put_Bytes_Hash()
{
var cid = ipfs.Block.PutAsync(blob, "raw", "sha2-512").Result;
var cid = await ipfs.Block.PutAsync(blob, "raw", "sha2-512");
Assert.AreEqual("bafkrgqelljziv4qfg5mefz36m2y3h6voaralnw6lwb4f53xcnrf4mlsykkn7vt6eno547tw5ygcz62kxrle45wnbmpbofo5tvu57jvuaf7k7e", (string)cid);

var data = ipfs.Block.GetAsync(cid).Result;
var data = await ipfs.Block.GetAsync(cid);
Assert.AreEqual(blob.Length, data.Size);
CollectionAssert.AreEqual(blob, data.DataBytes);
}

[TestMethod]
public void Put_Bytes_Pinned()
public async Task Put_Bytes_Pinned()
{
var data1 = new byte[] { 23, 24, 127 };
var cid1 = ipfs.Block.PutAsync(data1, contentType: "raw", pin: true).Result;
var pins = ipfs.Pin.ListAsync().Result;
var cid1 = await ipfs.Block.PutAsync(data1, contentType: "raw", pin: true);
var pins = await ipfs.Pin.ListAsync();
Assert.IsTrue(pins.Any(pin => pin == cid1));

var data2 = new byte[] { 123, 124, 27 };
var cid2 = ipfs.Block.PutAsync(data2, contentType: "raw", pin: false).Result;
pins = ipfs.Pin.ListAsync().Result;
var cid2 = await ipfs.Block.PutAsync(data2, contentType: "raw", pin: false);
pins = await ipfs.Pin.ListAsync();
Assert.IsFalse(pins.Any(pin => pin == cid2));
}

[TestMethod]
public void Put_Stream()
public async Task Put_Stream()
{
var cid = ipfs.Block.PutAsync(new MemoryStream(blob)).Result;
var cid = await ipfs.Block.PutAsync(new MemoryStream(blob));
Assert.AreEqual(id, (string)cid);

var data = ipfs.Block.GetAsync(cid).Result;
var data = await ipfs.Block.GetAsync(cid);
Assert.AreEqual(blob.Length, data.Size);
CollectionAssert.AreEqual(blob, data.DataBytes);
}

[TestMethod]
public void Put_Stream_ContentType()
public async Task Put_Stream_ContentType()
{
var cid = ipfs.Block.PutAsync(new MemoryStream(blob), contentType: "raw").Result;
var cid = await ipfs.Block.PutAsync(new MemoryStream(blob), contentType: "raw");
Assert.AreEqual("bafkreiaxnnnb7qz2focittuqq3ya25q7rcv3bqynnczfzako47346wosmu", (string)cid);

var data = ipfs.Block.GetAsync(cid).Result;
var data = await ipfs.Block.GetAsync(cid);
Assert.AreEqual(blob.Length, data.Size);
CollectionAssert.AreEqual(blob, data.DataBytes);
}

[TestMethod]
public void Put_Stream_Hash()
public async Task Put_Stream_Hash()
{
var cid = ipfs.Block.PutAsync(new MemoryStream(blob), "raw", "sha2-512").Result;
var cid = await ipfs.Block.PutAsync(new MemoryStream(blob), "raw", "sha2-512");
Assert.AreEqual("bafkrgqelljziv4qfg5mefz36m2y3h6voaralnw6lwb4f53xcnrf4mlsykkn7vt6eno547tw5ygcz62kxrle45wnbmpbofo5tvu57jvuaf7k7e", (string)cid);

var data = ipfs.Block.GetAsync(cid).Result;
var data = await ipfs.Block.GetAsync(cid);
Assert.AreEqual(blob.Length, data.Size);
CollectionAssert.AreEqual(blob, data.DataBytes);
}

[TestMethod]
public void Put_Stream_Pinned()
public async Task Put_Stream_Pinned()
{
var data1 = new MemoryStream(new byte[] { 23, 24, 127 });
var cid1 = ipfs.Block.PutAsync(data1, contentType: "raw", pin: true).Result;
var pins = ipfs.Pin.ListAsync().Result;
var cid1 = await ipfs.Block.PutAsync(data1, contentType: "raw", pin: true);
var pins = await ipfs.Pin.ListAsync();
Assert.IsTrue(pins.Any(pin => pin == cid1));

var data2 = new MemoryStream(new byte[] { 123, 124, 27 });
var cid2 = ipfs.Block.PutAsync(data2, contentType: "raw", pin: false).Result;
pins = ipfs.Pin.ListAsync().Result;
var cid2 = await ipfs.Block.PutAsync(data2, contentType: "raw", pin: false);
pins = await ipfs.Pin.ListAsync();
Assert.IsFalse(pins.Any(pin => pin == cid2));
}

[TestMethod]
public void Get()
public async Task Get()
{
var _ = ipfs.Block.PutAsync(blob).Result;
var block = ipfs.Block.GetAsync(id).Result;
var _ = await ipfs.Block.PutAsync(blob);
var block = await ipfs.Block.GetAsync(id);
Assert.AreEqual(id, (string)block.Id);
CollectionAssert.AreEqual(blob, block.DataBytes);
var blob1 = new byte[blob.Length];
Expand All @@ -121,18 +121,18 @@ public void Get()
}

[TestMethod]
public void Stat()
public async Task Stat()
{
var _ = ipfs.Block.PutAsync(blob).Result;
var info = ipfs.Block.StatAsync(id).Result;
var _ = await ipfs.Block.PutAsync(blob);
var info = await ipfs.Block.StatAsync(id);
Assert.AreEqual(id, (string)info.Id);
Assert.AreEqual(5, info.Size);
}

[TestMethod]
public async Task Remove()
{
var _ = ipfs.Block.PutAsync(blob).Result;
var _ = await ipfs.Block.PutAsync(blob);
var cid = await ipfs.Block.RemoveAsync(id);
Assert.IsNotNull(cid);
Assert.AreEqual(id, (string)cid!);
Expand All @@ -141,13 +141,13 @@ public async Task Remove()
[TestMethod]
public void Remove_Unknown()
{
ExceptionAssert.Throws<Exception>(() => { var _ = ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF").Result; });
ExceptionAssert.Throws<Exception>(() => { var _ = ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF").GetAwaiter().GetResult(); });
}

[TestMethod]
public async Task Remove_Unknown_OK()
{
var cid = await ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF", true);
var cid = await ipfs.Block.RemoveAsync("QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rFF", ignoreNonexistent: true);
Assert.IsNull(cid);
}
}
Expand Down
1 change: 0 additions & 1 deletion test/CoreApi/BlockRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ public async Task Version()
var version = await ipfs.BlockRepository.VersionAsync();
Assert.IsFalse(string.IsNullOrWhiteSpace(version));
}

}
}
4 changes: 2 additions & 2 deletions test/CoreApi/BootstrapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Ipfs.Http
[TestClass]
public class BootstapApiTest
{
IpfsClient ipfs = TestFixture.Ipfs;
MultiAddress somewhere = "/ip4/127.0.0.1/tcp/4009/ipfs/QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ";
private readonly IpfsClient ipfs = TestFixture.Ipfs;
private readonly MultiAddress somewhere = "/ip4/127.0.0.1/tcp/4009/ipfs/QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ";

[TestMethod]
public async Task Add_Remove()
Expand Down
Loading

0 comments on commit acddd18

Please sign in to comment.