/************************************************************************* * * * CESeCore: CE Security Core * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package org.cesecore.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.spec.InvalidKeySpecException; import java.util.Arrays; import java.util.Collections; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.apache.commons.lang.StringEscapeUtils; import org.apache.log4j.Logger; import org.cesecore.config.ConfigurationHolder; import org.junit.Test; /** * Tests the StringTools class . * * @version $Id: StringToolsTest.java 30976 2019-01-03 14:47:14Z samuellb $ */ public class StringToolsTest { private static Logger log = Logger.getLogger(StringToolsTest.class); /** * tests stripping whitespace * * @throws Exception * error */ @Test public void test01StripWhitespace() throws Exception { log.trace(">test01StripWhitespace()"); String test = " foo \t bar \r\n\r\n \f\f\f quu x "; assertEquals("foobarquux", StringTools.stripWhitespace(test)); log.trace(">test01StripWhitespace()"); } @Test public void test02IpStringToOctets() throws Exception { log.trace(">test02IpStringToOctets()"); String ip = "23.34.45.167"; byte[] octs = StringTools.ipStringToOctets(ip); for (int i = 0; i < octs.length; i++) { log.debug("octs[" + i + "]=" + (int) octs[i]); } log.trace(">test02IpStringToOctets()"); } @Test public void test03Strip() throws Exception { log.trace(">test03Strip()"); String strip1 = "foo$bar:far%"; String stripped = StringTools.strip(strip1); assertFalse("String has chars that should be stripped!", StringTools.hasSqlStripChars(strip1).isEmpty()); assertEquals("String not stripped correctly!", stripped, "foo/bar:far/"); log.trace("test04Strip()"); String strip1 = "CN=foo, O=Acme\\, Inc, OU=;\\/\\<\\>bar"; String stripped = StringTools.strip(strip1); assertFalse("String has chars that should be stripped!", StringTools.hasSqlStripChars(strip1).isEmpty()); assertEquals("String not stripped correctly! " + stripped, "CN=foo, O=Acme\\, Inc, OU=//\\<\\>bar", stripped); strip1 = "CN=foo, O=Acme\\, Inc, OU=;\\/<>\"bar"; stripped = StringTools.strip(strip1); assertFalse("String has chars that should be stripped!", StringTools.hasSqlStripChars(strip1).isEmpty()); assertEquals("String not stripped correctly! " + stripped, "CN=foo, O=Acme\\, Inc, OU=//<>\"bar", stripped); strip1 = "CN=foo\\+bar, O=Acme\\, Inc"; stripped = StringTools.strip(strip1); assertTrue("String does not have chars to be stripped!", StringTools.hasSqlStripChars(strip1).isEmpty()); assertEquals("String not stripped correctly! " + stripped, "CN=foo\\+bar, O=Acme\\, Inc", stripped); // Multi-valued.. not supported by EJBCA yet.. let it through for backwards compatibility. strip1 = "CN=foo+CN=bar, O=Acme\\, Inc"; stripped = StringTools.strip(strip1); assertTrue("String does not have chars to be stripped!", StringTools.hasSqlStripChars(strip1).isEmpty()); assertEquals("String not stripped correctly! " + stripped, "CN=foo+CN=bar, O=Acme\\, Inc", stripped); log.trace("test05Strip()"); final Object originalValue = ConfigurationHolder.instance().getProperty(FORBIDDEN_CHARS_KEY); try { final String input = "|\n|\r|;|foo bar|!|\u0000|`|?|$|~|\\<|\\>|\\\"|\\\\"; final String defaultOutput = "|/|/|/|foo bar|/|/|/|/|/|/|\\<|\\>|\\\"|\\\\"; forbiddenTest(null, input, defaultOutput); forbiddenTest("\n\r;!\u0000%`?$~", input, defaultOutput); forbiddenTest("", input, input); forbiddenTest("ABCDEF", input, input); forbiddenTest("rab| oof<>\"\\", input, "/\n/\r/;/////////!/\u0000/`/?/$/~////////"); forbiddenTest("\"", input, "|\n|\r|;|foo bar|!|\u0000|`|?|$|~|\\<|\\>|/|\\\\"); forbiddenTest("f", input, "|\n|\r|;|/oo bar|!|\u0000|`|?|$|~|\\<|\\>|\\\"|\\\\"); } finally { ConfigurationHolder.instance().setProperty(FORBIDDEN_CHARS_KEY, originalValue); } log.trace(" should not have been stripped, but ! should have: ", "footag/", ret); ret = StringTools.stripUsername(str); assertEquals("<> should have been stripped and so should !", "foo/tag/tag//tag//", ret); } @Test public void testCleanXForwardedFor() { assertEquals("192.0.2.43, 2001:db8:cafe::17", StringTools.getCleanXForwardedFor("192.0.2.43, 2001:db8:cafe::17")); assertEquals("192.0.2.43", StringTools.getCleanXForwardedFor("192.0.2.43")); assertEquals("2001:db8:cafe::17", StringTools.getCleanXForwardedFor("2001:db8:cafe::17")); assertEquals("192.0.2.43, 2001:db8:cafe::17", StringTools.getCleanXForwardedFor(" 192.0.2.43, 2001:db8:cafe::17 ")); assertEquals("192.0.2.43, 2001:db8:cafe::17", StringTools.getCleanXForwardedFor("192.0.2.43, 2001:DB8:CAFE::17")); assertEquals(null, StringTools.getCleanXForwardedFor(null)); assertEquals("??c?????a?e????a?e???????????????", StringTools.getCleanXForwardedFor("