|
| 1 | +package org.owasp.esapi; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.UnsupportedEncodingException; |
| 6 | +import java.util.Arrays; |
| 7 | + |
| 8 | +import junit.framework.JUnit4TestAdapter; |
| 9 | +import junit.framework.TestCase; |
| 10 | +import org.junit.Test; |
| 11 | +import org.owasp.esapi.util.CryptoHelperTest; |
| 12 | + |
| 13 | +public class PlainTextTest { |
| 14 | + |
| 15 | + private String unicodeStr = "A\u00ea\u00f1\u00fcC"; // I.e., "AêñüC" |
| 16 | + private String altString = "AêñüC"; // Same as above. |
| 17 | + |
| 18 | + @Test |
| 19 | + public final void testUnicodeString() { |
| 20 | + assertTrue(unicodeStr.equals(altString)); |
| 21 | + try { |
| 22 | + byte[] utf8Bytes = unicodeStr.getBytes("UTF-8"); |
| 23 | + PlainText pt1 = new PlainText(unicodeStr); |
| 24 | + PlainText pt2 = new PlainText(altString); |
| 25 | + |
| 26 | + assertTrue( pt1.equals(pt2) ); |
| 27 | + assertFalse( pt1.equals( unicodeStr ) ); |
| 28 | + assertTrue( pt1.length() == utf8Bytes.length ); |
| 29 | + assertTrue( Arrays.equals(utf8Bytes, pt1.asBytes()) ); |
| 30 | + assertTrue( pt1.hashCode() == unicodeStr.hashCode() ); |
| 31 | + |
| 32 | + } catch (UnsupportedEncodingException e) { |
| 33 | + fail("No UTF-8 byte encoding: " + e); |
| 34 | + e.printStackTrace(System.err); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public final void testEmptyString() { |
| 40 | + PlainText mt = new PlainText(""); |
| 41 | + assertTrue( mt.length() == 0 ); |
| 42 | + byte[] ba = mt.asBytes(); |
| 43 | + assertTrue( ba != null && ba.length == 0 ); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public final void testOverwrite() { |
| 48 | + try { |
| 49 | + byte[] utf8Bytes = altString.getBytes("UTF-8"); |
| 50 | + PlainText pt = new PlainText(utf8Bytes); |
| 51 | + assertTrue( pt.toString().equals(altString) ); |
| 52 | + assertTrue( Arrays.equals(utf8Bytes, pt.asBytes()) ); |
| 53 | + assertTrue( pt.hashCode() == unicodeStr.hashCode() ); |
| 54 | + |
| 55 | + int origLen = utf8Bytes.length; |
| 56 | + |
| 57 | + pt.overwrite(); |
| 58 | + assertTrue( utf8Bytes != null ); |
| 59 | + assertTrue( pt.asBytes() != null ); |
| 60 | + assertTrue( utf8Bytes == pt.asBytes() ); |
| 61 | + |
| 62 | +// DISCUSS: |
| 63 | +// See discussion note regarding method PlainText.overwrite(). Uncomment this |
| 64 | +// try / catch block if we set 'rawBytes' in this method to null. |
| 65 | +// |
| 66 | +// try { |
| 67 | +// @SuppressWarnings("unused") |
| 68 | +// int len = pt.length(); |
| 69 | +// } catch( NullPointerException npe ) { |
| 70 | +// assertTrue( "Caught expected NullPointerException", true ); |
| 71 | +// } |
| 72 | + int afterLen = utf8Bytes.length; |
| 73 | + assertTrue( origLen == afterLen ); |
| 74 | + System.out.println("Length after overwritting: " + afterLen); |
| 75 | + for (int i = 0; i < afterLen; i++ ) { |
| 76 | + System.out.println("utf8Bytes[" + i + "] = " + utf8Bytes[i]); |
| 77 | + } |
| 78 | + } catch (UnsupportedEncodingException e) { |
| 79 | + fail("No UTF-8 byte encoding: " + e); |
| 80 | + e.printStackTrace(System.err); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Run all the test cases in this suite. |
| 86 | + * This is to allow running from {@code org.owasp.esapi.AllTests} which |
| 87 | + * uses a JUnit 3 test runner. |
| 88 | + */ |
| 89 | + public static junit.framework.Test suite() { |
| 90 | + return new JUnit4TestAdapter(PlainTextTest.class); |
| 91 | + } |
| 92 | +} |
0 commit comments