Skip to content

Commit

Permalink
fix(MultiAddress): allow empty MultiAddress, fixes #69
Browse files Browse the repository at this point in the history
BREAKING CHANGE

MultiAddress(byte) and MultiAddress(string) do not throw on empty input.  They
simply create an empty Protocols.
  • Loading branch information
richardschneider committed Nov 29, 2018
1 parent 3e26a9e commit aed5f2e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/MultiAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public MultiAddress()
public MultiAddress(string s) : this()
{
if (string.IsNullOrWhiteSpace(s))
throw new ArgumentNullException("s");
return;

Read(new StringReader(s));
}
Expand Down Expand Up @@ -104,6 +104,9 @@ public MultiAddress Clone()
public MultiAddress(byte[] buffer)
: this()
{
if (buffer == null || buffer.Length == 0)
return;

Read(new MemoryStream(buffer, false));
}

Expand Down
18 changes: 4 additions & 14 deletions test/MultiAddressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public void Parsing()
Assert.AreEqual("ipfs", a.Protocols[2].Name);
Assert.AreEqual("QmVcSqVEsvm5RR9mBLjwpb2XjFVn5bPdPL69mL8PH45pPC", a.Protocols[2].Value);

ExceptionAssert.Throws<ArgumentNullException>(() => new MultiAddress((string)null));
ExceptionAssert.Throws<ArgumentNullException>(() => new MultiAddress(""));
ExceptionAssert.Throws<ArgumentNullException>(() => new MultiAddress(" "));
Assert.AreEqual(0, new MultiAddress((string)null).Protocols.Count);
Assert.AreEqual(0, new MultiAddress("").Protocols.Count);
Assert.AreEqual(0, new MultiAddress(" ").Protocols.Count);
}

[TestMethod]
Expand Down Expand Up @@ -189,18 +189,10 @@ public void Reading_Invalid_Code()
ExceptionAssert.Throws<InvalidDataException>(() => new MultiAddress(new byte[] { 0x7F }));
}

[TestMethod]
public void Reading_Empty()
{
ExceptionAssert.Throws<Exception>(() => new MultiAddress(new byte[0]));
}

[TestMethod]
public void Reading_Invalid_Text()
{
ExceptionAssert.Throws<ArgumentNullException>(() => new MultiAddress((string)null));
ExceptionAssert.Throws<ArgumentNullException>(() => new MultiAddress(""));
ExceptionAssert.Throws<ArgumentNullException>(() => new MultiAddress(" "));
ExceptionAssert.Throws<FormatException>(() => new MultiAddress("tcp/80"));
}

Expand Down Expand Up @@ -312,8 +304,6 @@ public void Ipv6ScopeId_Ignored()
public void TryCreate_FromString()
{
Assert.IsNotNull(MultiAddress.TryCreate("/ip4/1.2.3.4/tcp/80"));
Assert.IsNull(MultiAddress.TryCreate((string)null));
Assert.IsNull(MultiAddress.TryCreate(" "));
Assert.IsNull(MultiAddress.TryCreate("/tcp/alpha")); // bad port
Assert.IsNull(MultiAddress.TryCreate("/foobar")); // bad protocol
}
Expand All @@ -325,7 +315,7 @@ public void TryCreate_FromBytes()
var good1 = MultiAddress.TryCreate(good.ToArray());
Assert.AreEqual(good, good1);

Assert.IsNull(MultiAddress.TryCreate(new byte[0]));
Assert.IsNull(MultiAddress.TryCreate(new byte[] { 0x7f }));
}

[TestMethod]
Expand Down

0 comments on commit aed5f2e

Please sign in to comment.