2018-09-29 22:49:12 -04:00
using IW4MAdmin.Application ;
using SharedLibraryCore ;
2018-10-02 13:39:08 -04:00
using SharedLibraryCore.Commands ;
2018-11-05 22:01:29 -05:00
using SharedLibraryCore.Database.Models ;
2019-06-24 12:01:34 -04:00
using SharedLibraryCore.Events ;
2018-09-02 17:59:27 -04:00
using System ;
using System.Collections.Generic ;
2018-09-29 22:49:12 -04:00
using System.Linq ;
2018-09-02 17:59:27 -04:00
using System.Text ;
2018-09-29 22:49:12 -04:00
using System.Threading ;
2019-06-24 12:01:34 -04:00
using System.Threading.Tasks ;
2018-09-02 17:59:27 -04:00
using Xunit ;
namespace Tests
{
2018-09-29 22:49:12 -04:00
[Collection("ManagerCollection")]
2018-09-02 17:59:27 -04:00
public class ClientTests
{
2019-06-24 12:01:34 -04:00
private readonly ApplicationManager _manager ;
2018-10-02 13:39:08 -04:00
const int TestTimeout = 10000 ;
2018-09-29 22:49:12 -04:00
public ClientTests ( ManagerFixture fixture )
{
2019-06-24 12:01:34 -04:00
_manager = fixture . Manager ;
2018-09-29 22:49:12 -04:00
}
2018-09-02 17:59:27 -04:00
[Fact]
public void SetAdditionalPropertyShouldSucceed ( )
{
2018-11-05 22:01:29 -05:00
var client = new EFClient ( ) ;
2018-09-02 17:59:27 -04:00
int newProp = 5 ;
client . SetAdditionalProperty ( "NewProp" , newProp ) ;
}
[Fact]
public void GetAdditionalPropertyShouldSucceed ( )
{
2018-11-05 22:01:29 -05:00
var client = new EFClient ( ) ;
2018-09-02 17:59:27 -04:00
int newProp = 5 ;
client . SetAdditionalProperty ( "NewProp" , newProp ) ;
Assert . True ( client . GetAdditionalProperty < int > ( "NewProp" ) = = 5 , "added property does not match retrieved property" ) ;
}
2018-09-29 22:49:12 -04:00
2019-06-24 12:01:34 -04:00
[Fact]
public void BanEvasionShouldLink ( )
{
var server = _manager . Servers [ 0 ] ;
var waiter = new ManualResetEventSlim ( ) ;
_manager . GetApplicationSettings ( ) . Configuration ( ) . RConPollRate = 5000 ;
while ( ! server . IsInitialized )
{
Thread . Sleep ( 100 ) ;
}
var e = new GameEvent ( )
{
Type = GameEvent . EventType . PreConnect ,
Owner = server ,
Origin = new EFClient ( )
{
NetworkId = 1337 ,
ClientNumber = 0 ,
CurrentAlias = new EFAlias ( )
{
Name = "Ban Me" ,
IPAddress = 1337
}
}
} ;
_manager . GetEventHandler ( ) . AddEvent ( e ) ;
e . OnProcessed . Wait ( ) ;
e = new GameEvent ( )
{
Type = GameEvent . EventType . PreConnect ,
Owner = server ,
Origin = new EFClient ( )
{
NetworkId = 1338 ,
ClientNumber = 1 ,
CurrentAlias = new EFAlias ( )
{
Name = "Ban Me" ,
IPAddress = null
}
}
} ;
_manager . GetEventHandler ( ) . AddEvent ( e ) ;
e . OnProcessed . Wait ( ) ;
e = new GameEvent ( )
{
Type = GameEvent . EventType . Update ,
Owner = server ,
Origin = new EFClient ( )
{
NetworkId = 1338 ,
ClientNumber = 1 ,
CurrentAlias = new EFAlias ( )
{
Name = "Ban Me" ,
IPAddress = 1337
}
}
} ;
_manager . GetEventHandler ( ) . AddEvent ( e ) ;
e . OnProcessed . Wait ( ) ;
}
2018-09-29 22:49:12 -04:00
[Fact]
2018-10-02 13:39:08 -04:00
public void WarnClientShouldSucceed ( )
2018-09-29 22:49:12 -04:00
{
2019-06-24 12:01:34 -04:00
var onJoined = new ManualResetEventSlim ( ) ;
var server = _manager . Servers [ 0 ] ;
while ( ! server . IsInitialized )
2018-09-29 22:49:12 -04:00
{
Thread . Sleep ( 100 ) ;
}
2019-06-24 12:01:34 -04:00
_manager . OnServerEvent + = ( sender , eventArgs ) = >
{
if ( eventArgs . Event . Type = = GameEvent . EventType . Connect )
{
onJoined . Set ( ) ;
}
} ;
server . EmulateClientJoinLog ( ) ;
onJoined . Wait ( ) ;
2018-09-29 22:49:12 -04:00
2019-06-24 12:01:34 -04:00
var client = server . Clients [ 0 ] ;
2018-09-29 22:49:12 -04:00
2019-06-24 12:01:34 -04:00
var warnEvent = client . Warn ( "test warn" , Utilities . IW4MAdminClient ( server ) ) ;
warnEvent . OnProcessed . Wait ( 5000 ) ;
2018-09-29 22:49:12 -04:00
2019-06-24 12:01:34 -04:00
Assert . False ( warnEvent . Failed ) ;
2018-09-29 22:49:12 -04:00
2018-11-05 22:01:29 -05:00
warnEvent = client . Warn ( "test warn" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2019-06-24 12:01:34 -04:00
warnEvent . OnProcessed . Wait ( 5000 ) ;
2018-09-29 22:49:12 -04:00
Assert . True ( warnEvent . FailReason = = GameEvent . EventFailReason . Permission & &
client . Warnings = = 1 , "warning was applied without proper permissions" ) ;
2018-10-02 13:39:08 -04:00
// warn clear
2018-11-05 22:01:29 -05:00
var warnClearEvent = client . WarnClear ( new EFClient { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2019-06-24 12:01:34 -04:00
warnClearEvent . OnProcessed . Wait ( 5000 ) ;
2018-10-02 13:39:08 -04:00
Assert . True ( warnClearEvent . FailReason = = GameEvent . EventFailReason . Permission & &
client . Warnings = = 1 , "warning was removed without proper permissions" ) ;
2019-06-24 12:01:34 -04:00
warnClearEvent = client . WarnClear ( Utilities . IW4MAdminClient ( server ) ) ;
warnClearEvent . OnProcessed . Wait ( 5000 ) ;
2018-10-02 13:39:08 -04:00
Assert . True ( ! warnClearEvent . Failed & & client . Warnings = = 0 , "warning was not cleared" ) ;
2018-09-29 22:49:12 -04:00
}
[Fact]
2018-10-02 13:39:08 -04:00
public void ReportClientShouldSucceed ( )
2018-09-29 22:49:12 -04:00
{
2019-06-24 12:01:34 -04:00
while ( ! _manager . IsInitialized )
2018-09-29 22:49:12 -04:00
{
Thread . Sleep ( 100 ) ;
}
2019-06-24 12:01:34 -04:00
var client = _manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-09-29 22:49:12 -04:00
Assert . False ( client = = null , "no client found to report" ) ;
2018-10-03 22:20:49 -04:00
// fail
2018-11-05 22:01:29 -05:00
var player = new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ;
2018-10-03 22:20:49 -04:00
player . SetAdditionalProperty ( "_reportCount" , 3 ) ;
var reportEvent = client . Report ( "test report" , player ) ;
reportEvent . OnProcessed . Wait ( TestTimeout ) ;
Assert . True ( reportEvent . FailReason = = GameEvent . EventFailReason . Throttle &
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 0 , $"too many reports were applied [{reportEvent.FailReason.ToString()}]" ) ;
2018-09-29 22:49:12 -04:00
// succeed
2018-11-05 22:01:29 -05:00
reportEvent = client . Report ( "test report" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-09-29 22:49:12 -04:00
reportEvent . OnProcessed . Wait ( TestTimeout ) ;
Assert . True ( ! reportEvent . Failed & &
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 1 , $"report was not applied [{reportEvent.FailReason.ToString()}]" ) ;
// fail
2018-11-05 22:01:29 -05:00
reportEvent = client . Report ( "test report" , new EFClient ( ) { ClientId = 1 , NetworkId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-09-29 22:49:12 -04:00
Assert . True ( reportEvent . FailReason = = GameEvent . EventFailReason . Permission & &
2018-10-02 13:39:08 -04:00
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 1 ,
$"report was applied without proper permission [{reportEvent.FailReason.ToString()},{ client.CurrentServer.Reports.Count(r => r.Target.NetworkId == client.NetworkId)}]" ) ;
2018-09-29 22:49:12 -04:00
// fail
reportEvent = client . Report ( "test report" , client ) ;
Assert . True ( reportEvent . FailReason = = GameEvent . EventFailReason . Invalid & &
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 1 , $"report was applied to self" ) ;
// fail
2018-11-05 22:01:29 -05:00
reportEvent = client . Report ( "test report" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-09-29 22:49:12 -04:00
Assert . True ( reportEvent . FailReason = = GameEvent . EventFailReason . Exception & &
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 1 , $"duplicate report was applied" ) ;
}
2018-10-02 13:39:08 -04:00
[Fact]
public void FlagClientShouldSucceed ( )
{
2019-06-24 12:01:34 -04:00
while ( ! _manager . IsInitialized )
2018-10-02 13:39:08 -04:00
{
Thread . Sleep ( 100 ) ;
}
2019-06-24 12:01:34 -04:00
var client = _manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client = = null , "no client found to flag" ) ;
2018-11-05 22:01:29 -05:00
var flagEvent = client . Flag ( "test flag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
flagEvent . OnProcessed . Wait ( ) ;
// succeed
Assert . True ( ! flagEvent . Failed & &
2018-11-05 22:01:29 -05:00
client . Level = = EFClient . Permission . Flagged , $"player is not flagged [{flagEvent.FailReason.ToString()}]" ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client . ReceivedPenalties . FirstOrDefault ( p = > p . Offense = = "test flag" ) = = null , "flag was not applied" ) ;
2018-11-05 22:01:29 -05:00
flagEvent = client . Flag ( "test flag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
flagEvent . OnProcessed . Wait ( ) ;
// fail
Assert . True ( client . ReceivedPenalties . Count = = 1 , "flag was applied without permisions" ) ;
2018-11-05 22:01:29 -05:00
flagEvent = client . Flag ( "test flag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
flagEvent . OnProcessed . Wait ( ) ;
// fail
Assert . True ( client . ReceivedPenalties . Count = = 1 , "duplicate flag was applied" ) ;
2018-11-05 22:01:29 -05:00
var unflagEvent = client . Unflag ( "test unflag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
unflagEvent . OnProcessed . Wait ( ) ;
// fail
2018-11-05 22:01:29 -05:00
Assert . False ( client . Level = = EFClient . Permission . User , "user was unflagged without permissions" ) ;
2018-10-02 13:39:08 -04:00
2018-11-05 22:01:29 -05:00
unflagEvent = client . Unflag ( "test unflag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
unflagEvent . OnProcessed . Wait ( ) ;
// succeed
2018-11-05 22:01:29 -05:00
Assert . True ( client . Level = = EFClient . Permission . User , "user was not unflagged" ) ;
2018-10-02 13:39:08 -04:00
2018-11-05 22:01:29 -05:00
unflagEvent = client . Unflag ( "test unflag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
unflagEvent . OnProcessed . Wait ( ) ;
// succeed
Assert . True ( unflagEvent . FailReason = = GameEvent . EventFailReason . Invalid , "user was not flagged" ) ;
}
[Fact]
void KickClientShouldSucceed ( )
{
2019-06-24 12:01:34 -04:00
while ( ! _manager . IsInitialized )
2018-10-02 13:39:08 -04:00
{
Thread . Sleep ( 100 ) ;
}
2019-06-24 12:01:34 -04:00
var client = _manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client = = null , "no client found to kick" ) ;
2018-11-05 22:01:29 -05:00
var kickEvent = client . Kick ( "test kick" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
kickEvent . OnProcessed . Wait ( ) ;
Assert . True ( kickEvent . FailReason = = GameEvent . EventFailReason . Permission , "client was kicked without permission" ) ;
2018-11-05 22:01:29 -05:00
kickEvent = client . Kick ( "test kick" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
kickEvent . OnProcessed . Wait ( ) ;
2019-06-24 12:01:34 -04:00
Assert . True ( _manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( c = > c . NetworkId = = client . NetworkId ) = = null , "client was not kicked" ) ;
2018-10-02 13:39:08 -04:00
}
[Fact]
void TempBanClientShouldSucceed ( )
{
2019-06-24 12:01:34 -04:00
while ( ! _manager . IsInitialized )
2018-10-02 13:39:08 -04:00
{
Thread . Sleep ( 100 ) ;
}
2019-06-24 12:01:34 -04:00
var client = _manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client = = null , "no client found to tempban" ) ;
var tbCommand = new CTempBan ( ) ;
tbCommand . ExecuteAsync ( new GameEvent ( )
{
2018-11-05 22:01:29 -05:00
Origin = new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ,
2018-10-02 13:39:08 -04:00
Target = client ,
Data = "5days test tempban" ,
Type = GameEvent . EventType . Command ,
Owner = client . CurrentServer
} ) . Wait ( ) ;
2019-06-24 12:01:34 -04:00
Assert . True ( _manager . GetPenaltyService ( ) . GetActivePenaltiesAsync ( client . AliasLinkId ) . Result . Count ( p = > p . Type = = EFPenalty . PenaltyType . TempBan ) = = 1 ,
2018-10-02 13:39:08 -04:00
"tempban was not added" ) ;
}
[Fact]
void BanUnbanClientShouldSucceed ( )
{
2019-06-24 12:01:34 -04:00
while ( ! _manager . IsInitialized )
2018-10-02 13:39:08 -04:00
{
Thread . Sleep ( 100 ) ;
}
2019-06-24 12:01:34 -04:00
var client = _manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client = = null , "no client found to ban" ) ;
var banCommand = new CBan ( ) ;
banCommand . ExecuteAsync ( new GameEvent ( )
{
2018-11-05 22:01:29 -05:00
Origin = new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ,
2018-10-02 13:39:08 -04:00
Target = client ,
Data = "test ban" ,
Type = GameEvent . EventType . Command ,
Owner = client . CurrentServer
} ) . Wait ( ) ;
2019-06-24 12:01:34 -04:00
Assert . True ( _manager . GetPenaltyService ( ) . GetActivePenaltiesAsync ( client . AliasLinkId ) . Result . Count ( p = > p . Type = = EFPenalty . PenaltyType . Ban ) = = 1 ,
2018-10-02 13:39:08 -04:00
"ban was not added" ) ;
var unbanCommand = new CUnban ( ) ;
unbanCommand . ExecuteAsync ( new GameEvent ( )
{
2018-11-05 22:01:29 -05:00
Origin = new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ,
2019-06-11 09:00:14 -04:00
//Target = Manager.GetClientService().Find(c => c.NetworkId == client.NetworkId).Result.First(),
2018-10-02 13:39:08 -04:00
Data = "test unban" ,
Type = GameEvent . EventType . Command ,
Owner = client . CurrentServer
} ) . Wait ( ) ;
2019-06-24 12:01:34 -04:00
Assert . True ( _manager . GetPenaltyService ( ) . GetActivePenaltiesAsync ( client . AliasLinkId ) . Result . Count ( p = > p . Type = = EFPenalty . PenaltyType . Ban ) = = 0 ,
2018-10-02 13:39:08 -04:00
"ban was not removed" ) ;
}
2018-09-02 17:59:27 -04:00
}
}