Skip to content
Draft
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
90 changes: 90 additions & 0 deletions lua/gluatest/extensions/holytest_tools.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
AddCSLuaFile()

-- Helper function in case the setup required changes in the future
function CreateLuaNetWriteBuffer( size, messageName )
size = size or 128 -- 128 bytes by default

local writeBuffer = bitbuf.CreateWriteBuffer( 128 )
writeBuffer:WriteUBitLong( 0, 8 ) -- The message type. 0 = Lua net message

-- This is the default size of the outside net message.
-- net.Receive's len argument still includes this but these are not part the Lua net message and for testing, we remove these
local defaultSize = writeBuffer:GetNumBitsWritten()

-- Header for net.ReadHeader
-- We don't include it in defaultSize since inside net.Income the len argument is updated to remove these 16 bits
writeBuffer:WriteUBitLong( util.AddNetworkString( messageName ), 16 )

return writeBuffer, defaultSize
end

function SendBufferAsNetMessage( receivePlayer, writeBuffer )
local userID = receivePlayer:IsPlayer() and receivePlayer:UserID() or -1
local readBuffer = bitbuf.CreateReadBuffer( writeBuffer:GetData() )
holytest.ReceiveClientMessage( userID, receivePlayer, readBuffer, readBuffer:GetNumBits() )
end

-- For use when wanting to write for example a table using net.WriteTable
-- NOTE: Tests for net.Write* cannot be made as currently we do not have a way to properly test the written buffer.
function HookBufferIntoNetWriteFunctions( writeBuffer )
_original_net = _original_net or {}

for name, func in ipairs(net) do
if not name:StartsWith("Write") then continue end
if _original_net[name] then continue end

_original_net[name] = func
end

net.WriteString = function( val )
writeBuffer:WriteString( val )
end

net.WriteData = function( val )
writeBuffer:WriteData( val )
end

net.WriteBit = function( val )
writeBuffer:WriteOneBit( val )
end

net.WriteInt = function( val, bits )
writeBuffer:WriteBitLong( val, bits )
end

net.WriteUInt = function( val, bits )
writeBuffer:WriteUBitLong( val, bits )
end

net.WriteNormal = function( val )
writeBuffer:WriteBitVec3Normal( val )
end

net.WriteVector = function( val )
writeBuffer:WriteBitVec3Coord( val )
end

net.WriteAngle = function( val )
writeBuffer:WriteBitAngles( val )
end

net.WriteDouble = function( val )
writeBuffer:WriteDouble( val )
end

net.WriteUInt64 = function( val )
writeBuffer:WriteVarInt64( val )
end

net.WriteMatrix = function( val )
writeBuffer:WriteVMatrix( val )
end
end

function RemoveHookFromNetWriteFunctions()
for name, func in ipairs(_original_net) do
if not name:StartsWith("Write") then continue end

net[name] = func
end
end
36 changes: 36 additions & 0 deletions lua/tests/gmod/unit/libraries/net/readdata.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
return WithTestEntity( {
groupName = "net.ReadData",
cases = {
{
name = "Exists on the string table",
func = function()
expect( net.ReadData ).to.beA( "function" )
end
},
{
name = "Returns the right value",
when = holytest ~= nil,
func = function()
local writeBuffer, defaultSize = CreateLuaNetWriteBuffer( 128, "gmod_test_ReceiveTest" )

-- Message content (with a random null byte)
local testData = "12345-Hello World-" .. string.char(0) .. "-12345"
writeBuffer:WriteBytes( testData )

local receivePlayer = player.GetAll()[1]
net.Receive( "gmod_test_ReceiveTest", function( len, ply )
len = len - defaultSize -- Removed as its part of the outside message

expect( receivePlayer ).to.equal( ply )
expect( len ).to.equal( testData:len() * 8 )

expect( net.ReadData( testData:len() ) ).to.equal( testData )

expect( net.ReadData( 123 ) ).to.equal( string.char(0) ) -- Returns an null byte string on failure
end )

SendBufferAsNetMessage( receivePlayer, writeBuffer )
end
},
}
} )
35 changes: 35 additions & 0 deletions lua/tests/gmod/unit/libraries/net/readentity.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
return WithTestEntity( {
groupName = "net.ReadEntity",
cases = {
{
name = "Exists on the string table",
func = function()
expect( net.ReadEntity ).to.beA( "function" )
end
},
{
name = "Returns the right value",
when = holytest ~= nil,
func = function()
local writeBuffer, defaultSize = CreateLuaNetWriteBuffer( 128, "gmod_test_ReceiveTest" )

-- Message content
local receivePlayer = player.GetAll()[1]
writeBuffer:WriteUBitLong( receivePlayer:EntIndex(), MAX_EDICT_BITS )

net.Receive( "gmod_test_ReceiveTest", function( len, ply )
len = len - defaultSize -- Removed as its part of the outside message

expect( receivePlayer ).to.equal( ply )
expect( len ).to.equal( MAX_EDICT_BITS )

expect( net.ReadEntity() ).to.equal( receivePlayer )

expect( net.ReadEntity() ).to.beNil() -- Returns nil on failure
end )

SendBufferAsNetMessage( receivePlayer, writeBuffer )
end
},
}
} )
35 changes: 35 additions & 0 deletions lua/tests/gmod/unit/libraries/net/readfloat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
return WithTestEntity( {
groupName = "net.ReadFloat",
cases = {
{
name = "Exists on the string table",
func = function()
expect( net.ReadFloat ).to.beA( "function" )
end
},
{
name = "Returns the right value",
when = holytest ~= nil,
func = function()
local writeBuffer, defaultSize = CreateLuaNetWriteBuffer( 128, "gmod_test_ReceiveTest" )

-- Message content
writeBuffer:WriteFloat( 12.34 )

local receivePlayer = player.GetAll()[1]
net.Receive( "gmod_test_ReceiveTest", function( len, ply )
len = len - defaultSize -- Removed as its part of the outside message

expect( receivePlayer ).to.equal( ply )
expect( len ).to.equal( 16 ) -- We wrote 16 bits

expect( net.ReadFloat() ).to.equal( 12.34 )

expect( net.ReadFloat() ).to.equal( 0 ) -- Returns 0 on failure
end )

SendBufferAsNetMessage( receivePlayer, writeBuffer )
end
},
}
} )
35 changes: 35 additions & 0 deletions lua/tests/gmod/unit/libraries/net/readint.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
return WithTestEntity( {
groupName = "net.ReadInt",
cases = {
{
name = "Exists on the string table",
func = function()
expect( net.ReadInt ).to.beA( "function" )
end
},
{
name = "Returns the right value",
when = holytest ~= nil,
func = function()
local writeBuffer, defaultSize = CreateLuaNetWriteBuffer( 128, "gmod_test_ReceiveTest" )

-- Message content
writeBuffer:WriteBitLong( 1234, 16 )

local receivePlayer = player.GetAll()[1]
net.Receive( "gmod_test_ReceiveTest", function( len, ply )
len = len - defaultSize -- Removed as its part of the outside message

expect( receivePlayer ).to.equal( ply )
expect( len ).to.equal( 16 ) -- We wrote 16 bits

expect( net.ReadInt( 16 ) ).to.equal( 1234 )

expect( net.ReadInt( 16 ) ).to.equal( 0 ) -- Returns 0 on failure
end )

SendBufferAsNetMessage( receivePlayer, writeBuffer )
end
},
}
} )
36 changes: 36 additions & 0 deletions lua/tests/gmod/unit/libraries/net/readstring.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
return WithTestEntity( {
groupName = "net.ReadString",
cases = {
{
name = "Exists on the string table",
func = function()
expect( net.ReadString ).to.beA( "function" )
end
},
{
name = "Returns the right value",
when = holytest ~= nil,
func = function()
local writeBuffer, defaultSize = CreateLuaNetWriteBuffer( 128, "gmod_test_ReceiveTest" )

-- Message content
local testMessage = "Hello World"
writeBuffer:WriteString( testMessage )

local receivePlayer = player.GetAll()[1]
net.Receive( "gmod_test_ReceiveTest", function( len, ply )
len = len - defaultSize -- Removed as its part of the outside message

expect( receivePlayer ).to.equal( ply )
expect( len ).to.equal( ( testMessage:len() + 1 ) * 8 ) -- + 1 due to null terminator byte

expect( net.ReadString() ).to.equal( testMessage )

expect( net.ReadString() ).to.equal( "" ) -- Returns an empty string when nothing could be read
end )

SendBufferAsNetMessage( receivePlayer, writeBuffer )
end
},
}
} )