This implementation provides automatically generated tests for packet structures defined in XML files. The tests validate that packet definitions are correct and would catch issues like incorrect packet lengths (as mentioned in PR #622).
XSLT transformation that generates C# test code from XML packet definitions. Features:
- Validates fixed-length packets/structures against their declared lengths
- Tests variable-length packets' GetRequiredSize methods
- Validates field boundaries to prevent buffer overruns
- Generates syntactically correct C# test code with proper naming
New test project that:
- Automatically generates test files during build (when
ciis not set) - Integrates with existing test infrastructure (NUnit, StyleCop, etc.)
- Added to main solution file for CI/CD integration
ClientToServerPacketTests.cs- Tests for client-to-server packetsServerToClientPacketTests.cs- Tests for server-to-client packetsChatServerPacketTests.cs- Tests for chat server packetsConnectServerPacketTests.cs- Tests for connect server packets
// Validates declared length matches calculated size
const int expectedLength = 20; // From XML
const int actualLength = PlayerShopItem.Length; // From generated struct
Assert.That(actualLength, Is.EqualTo(expectedLength));
// Validates field boundaries
Assert.That(fieldIndex + fieldSize, Is.LessThanOrEqualTo(expectedLength));// Tests GetRequiredSize method accuracy
const string testString = "TestData";
var calculatedSize = StoredItem.GetRequiredSize(testString);
var expectedSize = Encoding.UTF8.GetByteCount(testString) + 1 + baseOffset;
Assert.That(calculatedSize, Is.EqualTo(expectedSize));// Ensures fields don't exceed packet boundaries
Assert.That(fieldIndex + fieldSize, Is.LessThanOrEqualTo(packetLength));- Build Process: During build, XSLT transformations read XML packet definitions
- Code Generation: Generates comprehensive test methods for each packet/structure
- Validation: Tests run during normal test execution, catching definition errors
- Integration: Fully integrated with existing CI/CD pipeline
- Automatic Detection: Catches packet definition errors at build time
- Comprehensive Coverage: Tests all packet types and structures
- Zero Maintenance: Tests are automatically updated when XML definitions change
- Early Error Detection: Prevents runtime issues from malformed packets
The tests run automatically as part of the normal build and test process. No manual intervention required.
To run tests manually:
dotnet test tests/MUnique.OpenMU.Network.Packets.Tests/The generated tests would catch issues like:
- Packet length declared as 10 but fields require 12 bytes
- Field starting at index 8 with size 4 in a 10-byte packet
- Incorrect GetRequiredSize calculations
- Overlapping field definitions
This addresses the core issue mentioned in PR #622 where packet structures were defined incorrectly.