Skip to content

Commit

Permalink
[core] Remove some dead code and add a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alanmcgovern committed Jan 16, 2020
1 parent d6c14c5 commit 2ad4a7f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
30 changes: 25 additions & 5 deletions src/MonoTorrent.Tests/Common/MagnetLinkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public void InvalidMagnetLink4()
});
}

[Test]
public void InvalidMagnetLink_DoubleEquals()
{
Assert.Throws<FormatException>(() => MagnetLink.FromUri(new Uri("magnet://btih:?xt=urn=:btih:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C")));
}

[Test]
public void NullMagnetLink()
{
Expand Down Expand Up @@ -169,17 +175,31 @@ public void ToUriV1 ()
"http://whatever2.com/file.exe",
};
var magnetLink = new MagnetLink (infoHash, name, announces, webSeeds);
var uri = magnetLink.ToV1String ();
var uriString = magnetLink.ToV1String ();

Assert.IsFalse (uri.Contains (announces[0]), "The uri should be Uri encoded");
Assert.IsFalse (uri.Contains (webSeeds[0]), "The uri should be Uri encoded");
Assert.IsFalse (uri.Contains (name), "The name should be Uri encoded");
Assert.IsFalse (uriString.Contains (announces[0]), "The uri should be Uri encoded");
Assert.IsFalse (uriString.Contains (webSeeds[0]), "The uri should be Uri encoded");
Assert.IsFalse (uriString.Contains (name), "The name should be Uri encoded");

magnetLink = MagnetLink.Parse (uri);
magnetLink = MagnetLink.Parse (uriString);
Assert.AreEqual (infoHash, magnetLink.InfoHash, "#1");
Assert.AreEqual (name, magnetLink.Name, "#2");
CollectionAssert.AreEquivalent (announces, magnetLink.AnnounceUrls, "#3");
CollectionAssert.AreEquivalent (webSeeds, magnetLink.Webseeds, "#4");

Assert.AreEqual(magnetLink.ToV1String (), MagnetLink.FromUri(magnetLink.ToV1Uri()).ToV1String (), "#5");
}

[Test]
public void TwoInfoHashes ()
{
Assert.Throws<FormatException>(() => MagnetLink.FromUri(new Uri("magnet://btih:?foo=bar&xt=urn:btih:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt=urn:btih:ANCKHTQCWBTRNJIV4WNAE52SJUQCZO5C")));
}

[Test]
public void UnsupportedScheme ()
{
Assert.Throws<FormatException> (() => MagnetLink.FromUri(new Uri("http://not_a_magnet_link.com")));
}
}
}
6 changes: 6 additions & 0 deletions src/MonoTorrent.Tests/Common/SpeedMonitorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ namespace MonoTorrent.Common
[TestFixture]
public class SpeedMonitorTest
{
[Test]
public void InvalidConstructor ()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new SpeedMonitor(-1));
}

[Test]
public void VeryFastRate ()
{
Expand Down
7 changes: 4 additions & 3 deletions src/MonoTorrent/MonoTorrent/HashAlgoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Cryptography;

namespace MonoTorrent
{
// FIXME: Make this internal in the future
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HashAlgoFactory
{
static Dictionary<Type, Type> algos = new Dictionary<Type, Type>();
Expand Down Expand Up @@ -62,9 +65,7 @@ public static void Register(Type baseType, Type specificType)
public static T Create<T>()
where T : HashAlgorithm
{
if (algos.ContainsKey(typeof(T)))
return (T)Activator.CreateInstance(algos[typeof(T)]);
return null;
return (T)Activator.CreateInstance(algos[typeof(T)]);
}
}
}
27 changes: 10 additions & 17 deletions src/MonoTorrent/MonoTorrent/MagnetLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ public static MagnetLink FromUri (Uri uri)
case "xl"://exact length
size = long.Parse (keyval [1]);
break;
case "xs":// eXact Source - P2P link.
case "kt"://keyword topic
case "mt"://manifest topic
//not supported for moment
break;
//case "xs":// eXact Source - P2P link.
//case "kt"://keyword topic
//case "mt"://manifest topic
// Unused
//break;
default:
//not supported
// Unknown/unsupported
break;
}
}
Expand All @@ -156,24 +156,17 @@ public static MagnetLink FromUri (Uri uri)
}

public string ToV1String ()
=> ToString (1);
=> ToString ();

public Uri ToV1Uri ()
=> new Uri (ToV1String ());

string ToString (int formatVersion)
string ToString ()
{
var sb = new StringBuilder ();
sb.Append ("magnet:?");
if (formatVersion == 1) {
sb.Append ("xt=urn:btih:");
sb.Append (InfoHash.ToHex ());
} else if (formatVersion == 2) {
sb.Append ("xt=urn:btmh");
throw new NotSupportedException ("Need to add support for the new 'multihash' thing");
} else {
throw new NotSupportedException ();
}
sb.Append ("xt=urn:btih:");
sb.Append (InfoHash.ToHex ());

if (!string.IsNullOrEmpty (Name)) {
sb.Append ("&dn=");
Expand Down

0 comments on commit 2ad4a7f

Please sign in to comment.