|
| 1 | +package com.bencodez.advancedcore.api.javascript; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +class JavascriptPlaceholderBinderTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + void placeholderOutputIsBoundInsteadOfCopiedIntoSource() { |
| 15 | + HashMap<String, Object> bindings = new HashMap<>(); |
| 16 | + String injection = "Bukkit.dispatchCommand(Console, 'op attacker')"; |
| 17 | + |
| 18 | + String prepared = JavascriptPlaceholderBinder.bind("%name% == 'safe'", ignored -> injection, bindings::put); |
| 19 | + |
| 20 | + assertEquals("__advancedCorePlaceholder0 == 'safe'", prepared); |
| 21 | + assertEquals(injection, bindings.get("__advancedCorePlaceholder0")); |
| 22 | + assertFalse(prepared.contains(injection)); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void exactQuotedPlaceholderUsesTheBoundValue() { |
| 27 | + HashMap<String, Object> bindings = new HashMap<>(); |
| 28 | + |
| 29 | + String prepared = JavascriptPlaceholderBinder.bind("'%name%' == 'Ben'", ignored -> "Ben", bindings::put); |
| 30 | + |
| 31 | + assertEquals("__advancedCorePlaceholder0 == 'Ben'", prepared); |
| 32 | + assertEquals("Ben", bindings.get("__advancedCorePlaceholder0")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void encodedCustomPlaceholderValueIsBoundAsData() { |
| 37 | + HashMap<String, Object> bindings = new HashMap<>(); |
| 38 | + String injection = "'; Bukkit.dispatchCommand(Console, 'op attacker'); '"; |
| 39 | + String encoded = JavascriptPlaceholderValue.encode(injection); |
| 40 | + |
| 41 | + String prepared = JavascriptPlaceholderBinder.bind("'" + encoded + "'", token -> token, bindings::put); |
| 42 | + |
| 43 | + assertEquals("__advancedCorePlaceholder0", prepared); |
| 44 | + assertEquals(injection, bindings.get("__advancedCorePlaceholder0")); |
| 45 | + assertFalse(prepared.contains(injection)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void preservesPrimitiveTypes() { |
| 50 | + HashMap<String, Object> bindings = new HashMap<>(); |
| 51 | + |
| 52 | + String prepared = JavascriptPlaceholderBinder.bind("%allowed% && %count% > 2 && %ratio% < 2.0", token -> { |
| 53 | + if (token.equals("%allowed%")) { |
| 54 | + return "true"; |
| 55 | + } |
| 56 | + if (token.equals("%count%")) { |
| 57 | + return "5"; |
| 58 | + } |
| 59 | + return "1.5"; |
| 60 | + }, bindings::put); |
| 61 | + |
| 62 | + assertEquals("__advancedCorePlaceholder0 && __advancedCorePlaceholder1 > 2 && __advancedCorePlaceholder2 < 2.0", |
| 63 | + prepared); |
| 64 | + assertEquals(Boolean.TRUE, bindings.get("__advancedCorePlaceholder0")); |
| 65 | + assertEquals(Long.valueOf(5), bindings.get("__advancedCorePlaceholder1")); |
| 66 | + assertEquals(Double.valueOf(1.5), bindings.get("__advancedCorePlaceholder2")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void unresolvedTokensRemainUntouched() { |
| 71 | + HashMap<String, Object> bindings = new HashMap<>(); |
| 72 | + |
| 73 | + String prepared = JavascriptPlaceholderBinder.bind("%unknown% == true", token -> token, bindings::put); |
| 74 | + |
| 75 | + assertEquals("%unknown% == true", prepared); |
| 76 | + assertTrue(bindings.isEmpty()); |
| 77 | + } |
| 78 | +} |
0 commit comments