aboutsummaryrefslogtreecommitdiff
path: root/esapi/src/test/java/org/owasp/encoder/esapi/ESAPIEncoderTest.java
blob: 7443996921c10ec908ea7b1406a40dc33d886b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.owasp.encoder.esapi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.Encoder;

/**
 * ESAPIEncoderTest
 *
 * @author jeffi
 */
public class ESAPIEncoderTest extends TestCase {
    public static Test suite() {
        return new TestSuite(ESAPIEncoderTest.class);
    }

    public void testEncode() throws Exception {
        // Note: ESAPI reference encodes as: "<>&Ω"
        // That's 25 characters to OWASP Java Encoder's 14.
        assertEquals("&lt;&gt;&amp;\u03a9", ESAPI.encoder().encodeForXML("<>&\u03a9"));
    }

    public void testSerialization() throws Exception {
        // Note: ESAPI reference implementation is NOT serializable.  Maybe
        // it will be in the future.  Our implementation is however
        // guaranteed serializable.

        Encoder encoder = ESAPI.encoder();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(encoder);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(
            new ByteArrayInputStream(baos.toByteArray()));

        Encoder deserializedEncoder = (Encoder)ois.readObject();

        assertSame(encoder, deserializedEncoder);
    }
}