Skip to content

Commit

Permalink
/tool/traceroute entities + test #48
Browse files Browse the repository at this point in the history
  • Loading branch information
danikf committed Sep 26, 2018
1 parent 522ffde commit 03d3c2c
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 67 deletions.
27 changes: 27 additions & 0 deletions tik4net.objects/Tool/ToolPing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,32 @@ public override string ToString()
{
return string.Format("{0} ....... {1}", Host, TikTimeHelper.FromTikTimeToSeconds(Time));
}

/// <summary>
/// Pings given <see paramref="address"/>. Returns <paramref name="cnt"/> of ping results to the <paramref name="address"/>.
/// </summary>
public static IEnumerable<ToolPing> Execute(ITikConnection connection, string address, int cnt)
{
return ToolPingConnectionExtensions.Ping(connection, address, cnt);
}
}

/// <summary>
/// Connection extension class for <see cref="ToolPing"/>
/// </summary>
public static class ToolPingConnectionExtensions
{
/// <summary>
/// Pings given <see paramref="address"/>. Returns <paramref name="cnt"/> of ping results to the <paramref name="address"/>.
/// </summary>
public static IEnumerable<ToolPing> Ping(this ITikConnection connection, string address, int cnt)
{
var result = connection.LoadList<ToolPing>(
connection.CreateParameter("address", address, TikCommandParameterFormat.NameValue),
connection.CreateParameter("count", cnt.ToString(), TikCommandParameterFormat.NameValue));

return result;
}
}

}
91 changes: 91 additions & 0 deletions tik4net.objects/Tool/ToolTraceroute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace tik4net.Objects.Tool
{
/// <summary>
/// Traceroute displays the list of the routers that packet travels through to get to a remote host. The traceroute or tracepath tool is available on practically all Unix-like operating systems and tracert on Microsoft Windows operating systems.
/// Traceroute operation is based on TTL value and ICMP “Time Exceeded” message.Remember that TTL value in IP header is used to avoid routing loops.Each hop decrements TTL value by 1. If the TTL reaches zero, the packet is discarded and ICMP Time Exceeded message is sent back to the sender when this occurs.
/// Initially by traceroute, the TTL value is set to 1 when next router finds a packet with TTL = 1 it sets TTL value to zero, and responds with an ICMP "time exceeded" message to the source. This message lets the source know that the packet traverses that particular router as a hop. Next time TTL value is incremented by 1 and so on.Typically, each router in the path towards the destination decrements the TTL field by one unit TTL reaches zero.
/// Using this command you can see how packets travel through the network and where it may fail or slow down. Using this information you can determine the computer, router, switch or other network device that possibly causing network issues or failures.
/// </summary>
[TikEntity("/tool/traceroute", LoadCommand = "", LoadDefaultParameneterFormat = TikCommandParameterFormat.NameValue, IsReadOnly = true, IncludeProplist = false)]
public class ToolTraceroute
{
/// <summary>
/// address
/// </summary>
[TikProperty("address", IsReadOnly = true)]
public string Address { get; private set; }

/// <summary>
/// loss
/// </summary>
[TikProperty("loss", IsReadOnly = true)]
public int Loss { get; private set; }

/// <summary>
/// sent
/// </summary>
[TikProperty("sent", IsReadOnly = true)]
public int Sent { get; private set; }

/// <summary>
/// last
/// </summary>
[TikProperty("last", IsReadOnly = true)]
public string Last { get; private set; }

/// <summary>
/// status
/// </summary>
[TikProperty("status", IsReadOnly = true)]
public string Status { get; private set; }

/// <summary>
/// avg
/// </summary>
[TikProperty("avg", IsReadOnly = true, IsMandatory = false)]
public string Avg { get; private set; }

/// <summary>
/// best
/// </summary>
[TikProperty("best", IsReadOnly = true, IsMandatory = false)]
public string Best { get; private set; }

/// <summary>
/// worst
/// </summary>
[TikProperty("worst", IsReadOnly = true, IsMandatory = false)]
public string Worst { get; private set; }

/// <summary>
/// Traceroutes given <see paramref="address"/>.
/// </summary>
public static IEnumerable<ToolTraceroute> Execute(ITikConnection connection, string address)
{
return ToolTracerouteConnectionExtensions.Traceroute(connection, address);
}
}

/// <summary>
/// Connection extension class for <see cref="ToolPing"/>
/// </summary>
public static class ToolTracerouteConnectionExtensions
{
/// <summary>
/// Traceroutes given <see paramref="address"/>.
/// </summary>
public static IEnumerable<ToolTraceroute> Traceroute(this ITikConnection connection, string address)
{
var result = connection.LoadList<ToolTraceroute>(
connection.CreateParameter("address", address, TikCommandParameterFormat.NameValue),
connection.CreateParameter("count", "1", TikCommandParameterFormat.NameValue));

return result;
}
}
}
66 changes: 0 additions & 66 deletions tik4net.tests/PingTest.cs

This file was deleted.

71 changes: 71 additions & 0 deletions tik4net.tests/ToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using tik4net.Objects;
using tik4net.Objects.Tool;
Expand All @@ -12,6 +13,67 @@ namespace tik4net.tests
[TestClass]
public class ToolTests : TestBase
{
#region ping
[TestMethod]
public void PingLocalhostWillNotFail()
{
const string HOST = "127.0.0.1";
var result = ToolPing.Execute(Connection, HOST, 4);
Assert.IsTrue(result.Count() == 4);
}

[TestMethod]
public void PingLocalhostAsyncWillNotFail()
{
const string HOST = "127.0.0.1";

List<ToolPing> responseList = new List<ToolPing>();
Exception responseException = null;

ITikCommand pingCommand = Connection.LoadAsync<ToolPing>(
ping => responseList.Add(ping), //read callback
exception => responseException = exception, //exception callback
Connection.CreateParameter("address", HOST),
Connection.CreateParameter("count", "1"),
Connection.CreateParameter("size", "64"));

Thread.Sleep(2 * 1000);

Assert.IsNull(responseException);
Assert.AreEqual(responseList.Count, 1);
Assert.AreEqual(responseList.Single().Host, HOST);
}

[TestMethod]
public void PingLocalhostAsyncWithCloseWillNotFail()
{
const string HOST = "127.0.0.1";
const int MAX_CNT = 100;

List<ToolPing> responseList = new List<ToolPing>();
Exception responseException = null;

ITikCommand pingCommand = Connection.LoadAsync<ToolPing>(
ping => responseList.Add(ping), //read callback
exception => responseException = exception, //exception callback
Connection.CreateParameter("address", HOST),
Connection.CreateParameter("count", MAX_CNT.ToString()),
Connection.CreateParameter("size", "64"));

Thread.Sleep(3 * 1000);
Connection.Close();
Thread.Sleep(2 * 1000);

Assert.IsTrue(!Connection.IsOpened);
Assert.IsNull(responseException);
Assert.IsTrue(responseList.Count < MAX_CNT);
Assert.IsTrue(!responseList.Any(ping => ping.Host != HOST));

RecreateConnection(); //Cleanup
}
#endregion


#region --- WOL ---
[TestMethod]
public void WolWillNotFail()
Expand Down Expand Up @@ -55,6 +117,15 @@ public void WolWithBadMacWillFail()

[TestMethod]
public void TracerouteToLocalhostWillNotFail()
{
const string IP = "127.0.0.1";

var result = ToolTraceroute.Execute(Connection, IP);
Assert.IsTrue(result.Count() == 2);
}

[TestMethod]
public void TracerouteToLocalhostWillNotFail_2()
{
const string IP = "127.0.0.1";

Expand Down
1 change: 0 additions & 1 deletion tik4net.tests/tik4net.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<Compile Include="CrudTest.cs" />
<Compile Include="InterfaceMonitorTrafficTest.cs" />
<Compile Include="InterfaceWirelessTest.cs" />
<Compile Include="PingTest.cs" />
<Compile Include="ConnectionTest.cs" />
<Compile Include="InterfaceEthernetTest.cs" />
<Compile Include="IpHotspotActiveTest.cs" />
Expand Down

0 comments on commit 03d3c2c

Please sign in to comment.