Skip to content

Commit

Permalink
Add pics info test packets
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Aug 29, 2024
1 parent f92d371 commit d9cb187
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
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

0 comments on commit d9cb187

Please sign in to comment.