Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test incoming packets #1403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Samples/999_TestBed/999_TestBed.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\SteamKit2\SteamKit2\SteamKit2.csproj" />
</ItemGroup>

</Project>
58 changes: 58 additions & 0 deletions Samples/999_TestBed/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Threading;
using SteamKit2;

// This is just a test bed to nethook various packets to later be added in tests

var steamClient = new SteamClient();
var manager = new CallbackManager( steamClient );
using var cts = new CancellationTokenSource();

steamClient.DebugNetworkListener = new NetHookNetworkListener();

var steamUser = steamClient.GetHandler<SteamUser>();
var steamApps = steamClient.GetHandler<SteamApps>();

manager.Subscribe<SteamClient.ConnectedCallback>( OnConnected );
manager.Subscribe<SteamClient.DisconnectedCallback>( OnDisconnected );
manager.Subscribe<SteamUser.LoggedOnCallback>( OnLoggedOn );

Console.WriteLine( "Connecting to Steam..." );

steamClient.Connect();

while ( !cts.IsCancellationRequested )
{
await manager.RunWaitCallbackAsync( cts.Token );
}

void OnConnected( SteamClient.ConnectedCallback callback )
{
Console.WriteLine( "Connected to Steam! Logging in..." );

steamUser.LogOnAnonymous();
}

void OnDisconnected( SteamClient.DisconnectedCallback callback )
{
Console.WriteLine( "Disconnected from Steam" );

cts.Cancel();
}

async void OnLoggedOn( SteamUser.LoggedOnCallback callback )
{
if ( callback.Result != EResult.OK )
{
Console.WriteLine( $"Unable to logon to Steam: {callback.Result}" );
cts.Cancel();
return;
}

Console.WriteLine( "Successfully logged on!" );

await steamApps.PICSGetProductInfo( new SteamApps.PICSRequest( 480 ), null );
await steamApps.PICSGetProductInfo( new SteamApps.PICSRequest( 480 ), null, true );

steamUser.LogOff();
}
14 changes: 14 additions & 0 deletions Samples/Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "022_DotaMatchRequest", "022
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SteamKit2", "..\SteamKit2\SteamKit2\SteamKit2.csproj", "{4B2B0365-DE37-4B65-B614-3E4E7C05147D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "999_TestBed", "999_TestBed\999_TestBed.csproj", "{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -209,6 +211,18 @@ Global
{4B2B0365-DE37-4B65-B614-3E4E7C05147D}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{4B2B0365-DE37-4B65-B614-3E4E7C05147D}.Release|x86.ActiveCfg = Release|Any CPU
{4B2B0365-DE37-4B65-B614-3E4E7C05147D}.Release|x86.Build.0 = Release|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Debug|x86.ActiveCfg = Debug|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Debug|x86.Build.0 = Debug|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Release|Any CPU.Build.0 = Release|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Release|x86.ActiveCfg = Release|Any CPU
{A5DDFB34-6FFC-4CE5-8575-8D600DE9D547}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions SteamKit2/SteamKit2/Steam/CMClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ void NetMsgReceived( object? sender, NetMsgEventArgs e )
OnClientMsgReceived( GetPacketMsg( e.Data, this ) );
}

#if DEBUG
internal void ReceiveTestPacketMsg( IPacketMsg packetMsg ) => OnClientMsgReceived( packetMsg );
#endif

void Connected( object? sender, EventArgs e )
{
DebugLog.Assert( connection != null, nameof( CMClient ), "No connection object after connecting." );
Expand Down
81 changes: 81 additions & 0 deletions SteamKit2/Tests/PacketFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using SteamKit2;
using SteamKit2.Internal;
using Xunit;

#nullable enable
namespace Tests
{
public class PacketFacts
{
internal record struct TestPacket( EMsg EMsg, byte[] Data );

private static Type? GetCallback( EMsg msg ) => msg switch
{
EMsg.ClientPICSProductInfoResponse => typeof( SteamApps.PICSProductInfoCallback ),
_ => null,
};

[Fact]
public async Task PostsExpectedCallbacks()
{
var steamClient = new SteamClient();

await foreach ( var (eMsg, data) in GetPackets( "in" ) )
{
var expectedCallback = GetCallback( eMsg );
Assert.NotNull( expectedCallback );

var packetMsg = CMClient.GetPacketMsg( data, steamClient );
Assert.NotNull( packetMsg );
Assert.IsAssignableFrom<PacketClientMsgProtobuf>( packetMsg );

Assert.Null( steamClient.GetCallback() ); // There must be no callbacks queued

steamClient.ReceiveTestPacketMsg( packetMsg );

var callback = steamClient.GetCallback();
Assert.NotNull( callback );
Assert.Equal( expectedCallback, callback.GetType() );
}
}

private static async IAsyncEnumerable<TestPacket> GetPackets( string direction )
{
var folder = Path.Join( AppDomain.CurrentDomain.BaseDirectory, "Packets" );
var files = Directory.GetFiles( folder, "*.bin" );

foreach ( var filename in files )
{
var packet = await GetPacket( filename, direction );

if ( packet.HasValue )
{
yield return packet.Value;
}
}
}

private static async Task<TestPacket?> GetPacket( string filename, string direction )
{
var parts = Path.GetFileNameWithoutExtension( filename ).Split( '_' );

Assert.True( parts.Length > 3 );

if ( parts[ 1 ] != direction )
{
return null;
}

var emsg = ( EMsg )uint.Parse( parts[ 2 ] );

var data = await File.ReadAllBytesAsync( filename );

return new( emsg, data );
}
}
}
#nullable disable
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions SteamKit2/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

<ItemGroup>
<EmbeddedResource Include="Files\*" />
<None Update="Packets\*.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
Loading