1 13 14 package se.anatom.ejbca.util; 15 16 import junit.framework.TestCase; 17 18 import org.apache.log4j.Logger; 19 import org.ejbca.util.CertTools; 20 import org.ejbca.util.StringTools; 21 22 23 28 public class TestStringTools extends TestCase { 29 private static Logger log = Logger.getLogger(TestStringTools.class); 30 31 36 public TestStringTools(String name) { 37 super(name); 38 } 39 40 protected void setUp() throws Exception { 41 log.debug(">setUp()"); 42 log.debug("<setUp()"); 43 } 44 45 protected void tearDown() throws Exception { 46 log.debug(">tearDown()"); 47 log.debug("<tearDown()"); 48 } 49 50 55 public void test01StripWhitespace() throws Exception { 56 log.debug(">test01StripWhitespace()"); 57 String test = " foo \t bar \r\n\r\n \f\f\f quu x "; 58 assertEquals("foobarquux", StringTools.stripWhitespace(test)); 59 log.debug(">test01StripWhitespace()"); 60 } 61 62 public void test02IpStringToOctets() throws Exception { 63 log.debug(">test02IpStringToOctets()"); 64 String ip = "23.34.45.167"; 65 byte[] octs = StringTools.ipStringToOctets(ip); 66 for (int i=0;i<octs.length;i++) { 67 log.debug("octs["+i+"]="+(int)octs[i]); 68 } 69 log.debug(">test02IpStringToOctets()"); 70 } 71 public void test03Strip() throws Exception { 72 log.debug(">test03Strip()"); 73 String strip1 = "foo$bar:far%"; 74 String stripped = StringTools.strip(strip1); 75 assertTrue("String has chars that should be stripped!", StringTools.hasSqlStripChars(strip1)); 76 assertEquals("String not stripped correctly!", stripped, "foo/bar:far/"); 77 log.debug("<test03Strip()"); 78 } 79 public void test04Strip() throws Exception { 80 log.debug(">test04Strip()"); 81 String strip1 = "CN=foo, O=Acme\\, Inc, OU=;\\/<>bar"; 82 String stripped = StringTools.strip(strip1); 83 assertTrue("String has chars that should be stripped!", StringTools.hasSqlStripChars(strip1)); 84 assertEquals("String not stripped correctly!", stripped, "CN=foo, O=Acme\\, Inc, OU=/////bar"); 85 log.debug("<test04Strip()"); 86 } 87 public void testBase64() throws Exception { 88 String s1 = "C=SE, O=abc, CN=def"; 89 String b1 = StringTools.putBase64String(s1); 90 String s2 = StringTools.getBase64String(b1); 91 assertEquals(s2,s1); 92 93 s1 = "C=SE, O=ÅÄÖ, CN=åäö"; 94 b1 = StringTools.putBase64String(s1); 95 s2 = StringTools.getBase64String(b1); 96 assertEquals(s2,s1); 97 } 98 public void testObfuscate() throws Exception { 99 String obf = StringTools.obfuscate("foo123"); 100 String deobf = StringTools.deobfuscate(obf); 101 assertEquals("foo123", deobf); 102 } 103 public void testPbe() throws Exception { 104 CertTools.installBCProvider(); 105 String enc = StringTools.pbeEncryptStringWithSha256Aes192("foo123"); 106 String dec = StringTools.pbeDecryptStringWithSha256Aes192(enc); 107 assertEquals("foo123", dec); 108 } 109 } 110 | Popular Tags |