2121import org .junit .jupiter .api .Assertions ;
2222import org .junit .jupiter .api .Test ;
2323
24- import java .util .Arrays ;
2524import java .util .Collection ;
2625import java .util .Collections ;
2726import java .util .EnumSet ;
27+ import java .util .LinkedList ;
2828import java .util .function .BiFunction ;
2929import java .util .function .Consumer ;
3030import java .util .function .Function ;
3131import java .util .function .Supplier ;
3232import java .util .regex .Pattern ;
3333
34+ import static java .util .Arrays .asList ;
35+ import static java .util .Collections .singletonList ;
36+
3437public final class TestCmdEvaluation {
3538
39+ public TestCmdEvaluation () {
40+ TEST_CHAINS .addAll (
41+ asList (
42+ new Chain ("0" , true , Policy .DENY , asList ("/cd reload *" , "/" ), false , EvalCause .setValues ()),
43+ new Chain ("1" , true , Policy .ALLOW , singletonList ("/cd reload" ), false , EvalCause .setValues ()),
44+ new Chain ("2" , true , Policy .DENY , singletonList ("/cd" ), false , EvalCause .setValues ()),
45+ new Chain ("3" , true , Policy .DENY , asList (TEST_PLUGIN_CMDS ), false , EvalCause .setValues ()),
46+ new Chain ("4" , true , Policy .ALLOW , singletonList ("/es version" ), false , EvalCause .setValues ()),
47+ new Chain ("5" , true , Policy .DENY , asList ("/es give" , "/es enchant" ), false , EvalCause .setValues ()),
48+ new Chain ("6" , true , Policy .ALLOW , singletonList ("^(/heywhats(up)?(?:$|\\ W)cool(beans)?(?:$|\\ W).*)" ), true , EvalCause .setValues ()),
49+ new Chain ("7" , true , Policy .DENY , singletonList ("^(/heywhats(up)?(?:$|\\ W).*)" ), true , EvalCause .setValues ()),
50+ new Chain ("8" , false , Policy .DENY , singletonList ("/thisshouldnotbedenied" ), false , EvalCause .setValues ()),
51+ new Chain ("9" , true , Policy .DENY , singletonList ("/blocksuggestionsonly" ), false , EnumSet .of (EvalCause .CMD_SUGGESTION )),
52+ new Chain ("10" , true , Policy .DENY , singletonList ("/UPPERCASE" ), false , EvalCause .setValues ()),
53+ new Chain ("11" , true , Policy .DENY , singletonList ("/suggestion argument" ), false , Collections .singleton (EvalCause .CMD_SUGGESTION ))
54+ )
55+ );
56+ }
57+
3658 // Various 'plugin checking' commands
3759 private static final String [] TEST_PLUGIN_CMDS = {
3860 "/plugins" , "/pl" , "/version" , "/ver" , "/icanhasbukkit" ,
@@ -41,26 +63,16 @@ public final class TestCmdEvaluation {
4163
4264 // Various programatically defined chains of rules, some regex ones provided at the bottom.
4365 // Chains should NOT contain 'donotuseinchains', doing so will make some tests fail (by design).
44- private static final Collection <Chain > TEST_CHAINS = Arrays .asList (
45- new Chain ("0" , true , Policy .DENY , Arrays .asList ("/cd reload *" , "/" ), false , EvalCause .setValues ()),
46- new Chain ("1" , true , Policy .ALLOW , Collections .singletonList ("/cd reload" ), false , EvalCause .setValues ()),
47- new Chain ("2" , true , Policy .DENY , Collections .singletonList ("/cd" ), false , EvalCause .setValues ()),
48- new Chain ("3" , true , Policy .DENY , Arrays .asList (TEST_PLUGIN_CMDS ), false , EvalCause .setValues ()),
49- new Chain ("4" , true , Policy .ALLOW , Collections .singletonList ("/es version" ), false , EvalCause .setValues ()),
50- new Chain ("5" , true , Policy .DENY , Arrays .asList ("/es give" , "/es enchant" ), false , EvalCause .setValues ()),
51- new Chain ("6" , true , Policy .ALLOW , Collections .singletonList ("^(/heywhats(up)?(?:$|\\ W)cool(beans)?(?:$|\\ W).*)" ), true , EvalCause .setValues ()),
52- new Chain ("7" , true , Policy .DENY , Collections .singletonList ("^(/heywhats(up)?(?:$|\\ W).*)" ), true , EvalCause .setValues ()),
53- new Chain ("8" , false , Policy .DENY , Collections .singletonList ("/thisshouldnotbedenied" ), false , EvalCause .setValues ()),
54- new Chain ("9" , true , Policy .DENY , Collections .singletonList ("/blocksuggestionsonly" ), false , EnumSet .of (EvalCause .CMD_SUGGESTION ))
55- );
66+ private static final Collection <Chain > TEST_CHAINS = new LinkedList <>();
5667
5768 // Commands to be tested which are expected to be evaluated with a DENY policy.
5869 public static final String [] TEST_CMDS_EXPECTING_DENY = {
5970 "/cd abc" , "/cd reloada" , "/cd reloa" , "/cd reload abc" ,
6071 "/plugins" , "/pl" , "/version" , "/ver" , "/icanhasbukkit" ,
6172 "/about" , "/?" , "/help" , "/ehelp" , "/paper" , "/spigot" ,
6273 "/es give" , "/es enchant" , "/cd" , "/" , "/:" , "/hello:how" ,
63- "/the:quick brown fox" , "/bukkit:help" ,
74+ "/the:quick brown fox" , "/bukkit:help" , "/uppercase" ,
75+ "/UPPERCASE" ,
6476 // regex:
6577 "/heywhats" ,
6678 "/heywhats coolio" ,
@@ -162,11 +174,11 @@ public void testWildcardBaseCmd() {
162174 final BiFunction <String , Policy , Boolean > test = (cmd , policy ) -> {
163175 final Evaluation eval = Evaluator .evaluate (
164176 "/hello" ,
165- Collections . singletonList (new Chain (
177+ singletonList (new Chain (
166178 "wildcard-base-cmd" ,
167179 true ,
168180 Policy .DENY ,
169- Collections . singletonList ("/*" ),
181+ singletonList ("/*" ),
170182 false ,
171183 EvalCause .setValues ()
172184 )),
@@ -195,11 +207,11 @@ public void testWildcardArgs() {
195207 final BiFunction <String , Policy , Boolean > test = (cmd , policy ) -> {
196208 final Evaluation eval = Evaluator .evaluate (
197209 cmd ,
198- Collections . singletonList (new Chain (
210+ singletonList (new Chain (
199211 "wildcard-arg-cmd" ,
200212 true ,
201213 policy ,
202- Collections . singletonList ("/hello * world *" ),
214+ singletonList ("/hello * world *" ),
203215 false ,
204216 EvalCause .setValues ()
205217 )),
@@ -233,7 +245,7 @@ public void testRulesExpectingDeny() {
233245 for (final String cmd : TEST_CMDS_EXPECTING_DENY ) {
234246 final Evaluation eval = testEvaluation (cmd , true );
235247 Assertions .assertFalse (eval .dueToException (), "Exception not expected here" );
236- Assertions .assertEquals (eval .policy (), Policy . DENY , "Wrong policy evaluated for cmd='" +
248+ Assertions .assertEquals (Policy . DENY , eval .policy (), "Wrong policy evaluated for cmd='" +
237249 cmd + "'; description='" + eval .description () + "'" );
238250 }
239251 }
@@ -249,7 +261,7 @@ public void testRulesExpectingAllow() {
249261 for (final String cmd : TEST_CMDS_EXPECTING_ALLOW ) {
250262 final Evaluation eval = testEvaluation (cmd , true );
251263 Assertions .assertFalse (eval .dueToException (), "Exception not expected here" );
252- Assertions .assertEquals (eval .policy (), Policy . ALLOW , "Wrong policy evaluated for cmd='" +
264+ Assertions .assertEquals (Policy . ALLOW , eval .policy (), "Wrong policy evaluated for cmd='" +
253265 cmd + "'; description='" + eval .description () + "'" );
254266 }
255267 }
@@ -269,7 +281,7 @@ public void testColonBlocking() {
269281
270282 Assertions .assertFalse (eval .dueToException (), "Exception not expected here" );
271283 Assertions .assertTrue (eval .dueToColonInFirstArg (), "Colon in first arg expected to be cause" );
272- Assertions .assertEquals (eval .policy (), Policy . DENY ,
284+ Assertions .assertEquals (Policy . DENY , eval .policy (),
273285 "Wrong policy evaluated for cmd='" + cmd + "'; description='" +
274286 eval .description () + "'" );
275287 }
@@ -279,7 +291,7 @@ public void testColonBlocking() {
279291
280292 Assertions .assertFalse (eval .dueToException (), "Exception not expected here" );
281293 Assertions .assertFalse (eval .dueToColonInFirstArg (), "Colon in first arg unexpected to be cause" );
282- Assertions .assertEquals (eval .policy (), Policy . ALLOW ,
294+ Assertions .assertEquals (Policy . ALLOW , eval .policy (),
283295 "Wrong policy evaluated for cmd='" + cmd + "'; description='" +
284296 eval .description () + "'" );
285297 }
@@ -291,7 +303,7 @@ public void testColonBlocking() {
291303
292304 Assertions .assertFalse (eval .dueToException (), "Exception not expected here" );
293305 Assertions .assertFalse (eval .dueToColonInFirstArg (), "Colon in first arg unexpected to be cause" );
294- Assertions .assertEquals (eval .policy (), Policy . ALLOW ,
306+ Assertions .assertEquals (Policy . ALLOW , eval .policy (),
295307 "Wrong policy evaluated for cmd='" + cmd + "'; description='" +
296308 eval .description () + "'" );
297309 }
@@ -300,7 +312,7 @@ public void testColonBlocking() {
300312 final Evaluation eval = testEvaluation (cmd , false );
301313 Assertions .assertFalse (eval .dueToException (), "Exception not expected here" );
302314 Assertions .assertFalse (eval .dueToColonInFirstArg (), "Colon in first arg unexpected to be cause" );
303- Assertions .assertEquals (eval .policy (), Policy . ALLOW ,
315+ Assertions .assertEquals (Policy . ALLOW , eval .policy (),
304316 "Wrong policy evaluated for cmd='" + cmd + "'; description='" +
305317 eval .description () + "'" );
306318 }
@@ -351,7 +363,7 @@ public void testDenyDefaultPolicy() {
351363 );
352364
353365 Assertions .assertFalse (eval .dueToException (), "Exception not expected here" );
354- Assertions .assertEquals (eval .policy (), Policy . DENY , "Wrong policy evaluated" );
366+ Assertions .assertEquals (Policy . DENY , eval .policy (), "Wrong policy evaluated" );
355367 }
356368
357369 /**
@@ -372,7 +384,7 @@ public void testExceptionDeniesCmd() {
372384 final Evaluation eval = testEvaluation ("DoesNotHaveStartingSlash" , true );
373385
374386 Assertions .assertTrue (eval .dueToException (), "Intentional exception was expected here" );
375- Assertions .assertEquals (eval .policy (), Policy . DENY , "Wrong security policy evaluated" );
387+ Assertions .assertEquals (Policy . DENY , eval .policy (), "Wrong security policy evaluated" );
376388
377389 Evaluator .SUPPRESS_EXCEPTION_MESSAGES .set (false );
378390 }
@@ -403,12 +415,51 @@ public void testEvalCauseFiltering() {
403415 final Evaluation evalOther = eval .apply (EvalCause .OTHER );
404416
405417 Assertions .assertTrue (evalExecution .dueToDefaultPolicy ());
406- Assertions .assertSame (evalExecution .policy (), Policy . ALLOW );
418+ Assertions .assertSame (Policy . ALLOW , evalExecution .policy ());
407419
408420 Assertions .assertFalse (evalSuggestion .dueToDefaultPolicy ()); // << notice
409- Assertions .assertSame (evalSuggestion .policy (), Policy . DENY ); // << notice
421+ Assertions .assertSame (Policy . DENY , evalSuggestion .policy ()); // << notice
410422
411423 Assertions .assertTrue (evalOther .dueToDefaultPolicy ());
412- Assertions .assertSame (evalOther .policy (), Policy .ALLOW );
424+ Assertions .assertSame (Policy .ALLOW , evalOther .policy ());
425+ }
426+
427+ /**
428+ * Tests that command suggestion filtering is working with arguments too.
429+ *
430+ * @author lokka30
431+ * @since 1.1.6
432+ */
433+ @ Test
434+ public void testSuggestionFilteringArgumentsWorks () {
435+ final Collection <String > cmdsShouldAllow = asList (
436+ "/suggestion argumento" ,
437+ "/suggestion argumen argument" ,
438+ "/suggestion"
439+ );
440+ final Collection <String > cmdsShouldDeny = asList (
441+ "/suggestion argument" ,
442+ "/suggestion argument test"
443+ );
444+
445+ cmdsShouldAllow .forEach (cmd -> Assertions .assertSame (Policy .ALLOW , Evaluator .evaluate (
446+ cmd ,
447+ TEST_CHAINS ,
448+ TEST_DEFAULT_POLICY ,
449+ true ,
450+ EvalCause .CMD_SUGGESTION ,
451+ debugLogger ,
452+ warningLogger
453+ ).policy ()));
454+
455+ cmdsShouldDeny .forEach (cmd -> Assertions .assertSame (Policy .DENY , Evaluator .evaluate (
456+ cmd ,
457+ TEST_CHAINS ,
458+ TEST_DEFAULT_POLICY ,
459+ true ,
460+ EvalCause .CMD_SUGGESTION ,
461+ debugLogger ,
462+ warningLogger
463+ ).policy ()));
413464 }
414465}
0 commit comments