OpenMU

This project aims to create an easy to use, extendable and customizable server for a MMORPG called "MU Online".

View on GitHub

Packet Structure Tests Implementation

Overview

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).

Files Created

1. src/Network/Packets/GenerateTests.xslt

XSLT transformation that generates C# test code from XML packet definitions. Features:

2. tests/MUnique.OpenMU.Network.Packets.Tests/

New test project that:

3. Test Files Generated

Types of Validation

Fixed-Length Validation

// 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));

Variable-Length Validation

// 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));

Field Boundary Validation

// Ensures fields don't exceed packet boundaries
Assert.That(fieldIndex + fieldSize, Is.LessThanOrEqualTo(packetLength));

How It Works

  1. Build Process: During build, XSLT transformations read XML packet definitions
  2. Code Generation: Generates comprehensive test methods for each packet/structure
  3. Validation: Tests run during normal test execution, catching definition errors
  4. Integration: Fully integrated with existing CI/CD pipeline

Benefits

Usage

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/

Example Issues Caught

The generated tests would catch issues like:

This addresses the core issue mentioned in PR #622 where packet structures were defined incorrectly.