Skip to content

Commit

Permalink
✅ Update TCP tests + Fix NetworkStream Write
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Dec 5, 2023
1 parent 51bb237 commit 4b4e809
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
52 changes: 51 additions & 1 deletion Tests/Kernels/NetworkTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,57 @@ private void TestDhcpConnection()

private void TestTcpConnection()
{

Global.debugger.Send("Creating TCP client...");

using (var xClient = new TcpClient())
{
Global.debugger.Send("Creating IPAddress...");
var address = new IPAddress(new byte[] { 127, 0, 0, 1 });
Global.debugger.Send("Connecting to TCP server...");
xClient.Connect(address, 12345);
Global.debugger.Send("TcpClient connected.");
NetworkStream stream = xClient.GetStream();
Assert.IsTrue(xClient.Connected, "TCP connexion established.");

byte[] receivedData = new byte[xClient.ReceiveBufferSize];
int bytesRead = stream.Read(receivedData, 0, receivedData.Length);
string receivedMessage = Encoding.ASCII.GetString(receivedData, 0, bytesRead);
Assert.AreEqual(receivedMessage, "Hello from the testrunner!", "TCP receive works");

Global.debugger.Send("TcpClient sending IP " + NetworkConfiguration.CurrentAddress.ToString());
stream.Write(Encoding.ASCII.GetBytes(NetworkConfiguration.CurrentAddress.ToString()));
Global.debugger.Send("TcpClient IP sent");

// Envoyer un message au serveur
string messageToSend = "cosmos is the best operating system uwu";
byte[] dataToSend = Encoding.ASCII.GetBytes(messageToSend);
Global.debugger.Send("Sending: " + messageToSend);
stream.Write(dataToSend, 0, dataToSend.Length);

byte[] receivedData2 = new byte[xClient.ReceiveBufferSize];
int bytesRead2 = stream.Read(receivedData2, 0, receivedData2.Length);
string receivedMessage2 = Encoding.ASCII.GetString(receivedData2, 0, bytesRead2);
Assert.AreEqual(receivedMessage2, "COSMOS IS THE BEST OPERATING SYSTEM UWU", "TCP send works");

string baseMessage = "This is a long TCP message for sequencing test...";
string paddedMessage = baseMessage.PadRight(6000, '.');
stream.Write(Encoding.ASCII.GetBytes(paddedMessage));
Global.debugger.Send("Sent long data packet.");

byte[] receivedData3 = new byte[xClient.ReceiveBufferSize];
int bytesRead3 = stream.Read(receivedData3, 0, receivedData3.Length);
Assert.AreEqual(bytesRead3, 6000, "TCP paquet sequencing works.");

stream.Close();
}

Global.debugger.Send("Creating TCP server...");

var xServer = new TcpListener(IPAddress.Any, 4343);
xServer.Start();

var client = xServer.AcceptTcpClient(); //blocking
Assert.IsTrue(client.Connected, "Received new client! TCP connexion established.");
}

private void TestDnsConnection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public static int Read(NetworkStream aThis, byte[] buffer, int offset, int count
return _streamSocket.Receive(buffer, offset, count, 0);
}

public static void Write(NetworkStream aThis, ReadOnlySpan<byte> buffer)
{
Write(aThis, buffer.ToArray(), 0, buffer.Length);
}

public static void Write(NetworkStream aThis, byte[] buffer, int offset, int count)
{
_streamSocket.Send(buffer, offset, count, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public static void Connect(TcpClient aThis, string hostname, int port, [FieldAcc
var address = IPAddress.Parse(hostname);
var endpoint = new IPEndPoint(address, port);

Cosmos.HAL.Global.debugger.Send("address - " + address.ToString());
_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_clientSocket.Bind(endpoint);
_clientSocket.Connect(address, port);
Expand All @@ -53,6 +52,10 @@ public static void Connect(TcpClient aThis, string hostname, int port, [FieldAcc

public static void Connect(TcpClient aThis, IPAddress address, int port, [FieldAccess(Name = "System.Boolean System.Net.Sockets.TcpClient._active")] ref bool _active)
{
var endpoint = new IPEndPoint(address, port);

_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_clientSocket.Bind(endpoint);
_clientSocket.Connect(address, port);
_active = true;
}
Expand Down

0 comments on commit 4b4e809

Please sign in to comment.