|
| 1 | +package org.jsoup.safety; |
| 2 | + |
| 3 | +import org.jsoup.nodes.Attribute; |
| 4 | +import org.jsoup.nodes.Attributes; |
| 5 | +import org.jsoup.nodes.Element; |
| 6 | +import org.jsoup.parser.Tag; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 11 | + |
| 12 | +public class SafelistTest { |
| 13 | + private static final String TEST_TAG = "testTag"; |
| 14 | + private static final String TEST_ATTRIBUTE = "testAttribute"; |
| 15 | + private static final String TEST_SCHEME = "valid-scheme"; |
| 16 | + private static final String TEST_VALUE = TEST_SCHEME + "://testValue"; |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testCopyConstructor_noSideEffectOnTags() { |
| 20 | + Safelist safelist1 = Safelist.none().addTags(TEST_TAG); |
| 21 | + Safelist safelist2 = new Safelist(safelist1); |
| 22 | + safelist1.addTags("invalidTag"); |
| 23 | + |
| 24 | + assertFalse(safelist2.isSafeTag("invalidTag")); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testCopyConstructor_noSideEffectOnAttributes() { |
| 29 | + Safelist safelist1 = Safelist.none().addAttributes(TEST_TAG, TEST_ATTRIBUTE); |
| 30 | + Safelist safelist2 = new Safelist(safelist1); |
| 31 | + safelist1.addAttributes(TEST_TAG, "invalidAttribute"); |
| 32 | + |
| 33 | + assertFalse(safelist2.isSafeAttribute(TEST_TAG, null, new Attribute("invalidAttribute", TEST_VALUE))); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testCopyConstructor_noSideEffectOnEnforcedAttributes() { |
| 38 | + Safelist safelist1 = Safelist.none().addEnforcedAttribute(TEST_TAG, TEST_ATTRIBUTE, TEST_VALUE); |
| 39 | + Safelist safelist2 = new Safelist(safelist1); |
| 40 | + safelist1.addEnforcedAttribute(TEST_TAG, TEST_ATTRIBUTE, "invalidValue"); |
| 41 | + |
| 42 | + for (Attribute enforcedAttribute : safelist2.getEnforcedAttributes(TEST_TAG)) { |
| 43 | + assertNotEquals("invalidValue", enforcedAttribute.getValue()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testCopyConstructor_noSideEffectOnProtocols() { |
| 49 | + final String invalidScheme = "invalid-scheme"; |
| 50 | + Safelist safelist1 = Safelist.none() |
| 51 | + .addAttributes(TEST_TAG, TEST_ATTRIBUTE) |
| 52 | + .addProtocols(TEST_TAG, TEST_ATTRIBUTE, TEST_SCHEME); |
| 53 | + Safelist safelist2 = new Safelist(safelist1); |
| 54 | + safelist1.addProtocols(TEST_TAG, TEST_ATTRIBUTE, invalidScheme); |
| 55 | + |
| 56 | + Attributes attributes = new Attributes(); |
| 57 | + Attribute invalidAttribute = new Attribute(TEST_ATTRIBUTE, invalidScheme + "://someValue"); |
| 58 | + attributes.put(invalidAttribute); |
| 59 | + Element invalidElement = new Element(Tag.valueOf(TEST_TAG), "", attributes); |
| 60 | + |
| 61 | + assertFalse(safelist2.isSafeAttribute(TEST_TAG, invalidElement, invalidAttribute)); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +} |
0 commit comments