<commit_before>package org.owasp.esapi;

import static org.junit.Assert.*;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestCase;
import org.junit.Test;
import org.owasp.esapi.util.CryptoHelperTest;

public class PlainTextTest {
	
	private String unicodeStr = "A\u00ea\u00f1\u00fcC";	// I.e., "AêñüC"
	private String altString  = "AêñüC";				// Same as above.
	
	@Test
	public final void testUnicodeString() {
		assertTrue(unicodeStr.equals(altString));
		try {
			byte[] utf8Bytes = unicodeStr.getBytes("UTF-8");
			PlainText pt1 = new PlainText(unicodeStr);
			PlainText pt2 = new PlainText(altString);
			
			assertTrue( pt1.equals(pt2) );
			assertFalse( pt1.equals( unicodeStr ) );
			assertTrue( pt1.length() == utf8Bytes.length );
			assertTrue( Arrays.equals(utf8Bytes, pt1.asBytes()) );			
			assertTrue( pt1.hashCode() == unicodeStr.hashCode() );
			
		} catch (UnsupportedEncodingException e) {
			fail("No UTF-8 byte encoding: " + e);
			e.printStackTrace(System.err);
		}
	}

	@Test
	public final void testEmptyString() {
		PlainText mt  = new PlainText("");
		assertTrue( mt.length() == 0 );
		byte[] ba = mt.asBytes();
		assertTrue( ba != null && ba.length == 0 );
	}

	@Test
	public final void testOverwrite() {
		try {
			byte[] utf8Bytes = altString.getBytes("UTF-8");
			PlainText pt = new PlainText(utf8Bytes);
			assertTrue( pt.toString().equals(altString) );
			assertTrue( Arrays.equals(utf8Bytes, pt.asBytes()) );			
			assertTrue( pt.hashCode() == unicodeStr.hashCode() );
			
			int origLen = utf8Bytes.length;<commit_msg>Changes so that the testUnicodeString() test case will be skipped if unicodeStr and altString are not equal at runtime. (They should be.) This is meant to deal with the Windows-1252 vs. UTF-8 encoding issue on Windows OS. Also changed testOverwrite() so it did not depend on both unicodeStr and altString. Now only uses unicodeStr.<commit_after>package org.owasp.esapi;

import static org.junit.Assert.*;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestCase;
import org.junit.Test;
import org.owasp.esapi.util.CryptoHelperTest;

public class PlainTextTest {
	
	private String unicodeStr = "A\u00ea\u00f1\u00fcC";	// I.e., "AêñüC"
	private String altString  = "AêñüC";				// Same as above.
	
	/* NOTE: This test will not work on Windows unless executed under
	 * Eclipse and the file is stored / treated as a UTF-8 encoded file
	 * rather than the Windows native OS encoding of Windows-1252 (aka,
	 * CP-1252). Therefore this test case has a check to not run the test
	 * unless
	 *     unicodeStr.equals(altString)
	 * is true. If not the test is skipped and a message is printed to stderr.
	 * <NAME> made an attempt to address this (see private email to
	 * <EMAIL> on 11/26/2009, subject "Re: [OWASP-ESAPI] Unit
	 * Tests Status") to correct this problem by setting some SVN attribute
	 * to standardize all source files to UTF-8, but not all Subversion clients
	 * are either honoring this or perhaps Windows just overrides this. Either
	 * way, this test (which used to be an assertTrue() expression) was
	 * introduced to account for this.
	 */
	@Test
	public final void testUnicodeString() {
	    // These 2 strings are *meant* to be equal. If they are not, please
	    // do *NOT* change the test. It's a Windows thing. Sorry. Change your
	    // OS instead. ;-)
	    if ( ! unicodeStr.equals(altString) ) {
	        System.err.println("Skipping JUnit test case " +
	                           "PlainTextTest.testUnicodeString() on OS " +
	                           System.getProperty("os.name") );
	        return;
	    }
		try {
			byte[] utf8Bytes = unicodeStr.getBytes("UTF-8");
			PlainText pt1 = new PlainText(unicodeStr);
			PlainText pt2 = new PlainText(altString);
			
			assertTrue( pt1.equals(pt2) );
			assertFalse( pt1.equals( unicodeStr ) );
			assertTrue( pt1.length() == utf8Bytes.length );
			assertTrue( Arrays.equals(utf8Bytes, pt1.asBytes()) );			
			assertTrue( pt1.hashCode() == unicodeStr.hashCode() );
			
		} catch (UnsupportedEncodingException e) {
			fail("No UTF-8 byte encoding: " + e);
			e.printStackTrace(System.err);
		}
	}

	@Test
	public final void testEmptyString() {
		PlainText mt  = new PlainText("");
		assertTrue( mt.length() == 0 );
		byte[] ba = mt.asBytes();
		assertTrue( ba != null && ba.length == 0 );
	}

	@Test
	public final void testOverwrite() {
		try {
			byte[] utf8Bytes = unicodeStr.getBytes("UTF-8");
			PlainText pt = new PlainText(utf8Bytes);
			assertTrue( pt.toString().equals(unicodeStr) );
			assertTrue( Arrays.equals(utf8Bytes, pt.asBytes()) );			
			assertTrue( pt.hashCode() == unicodeStr.hashCode() );
			
			int origLen = utf8Bytes.length;
{'fraction_non_alphanumeric': 0.1183749226644669, 'fraction_numerical': 0.016498247061249742, 'mean_word_length': 3.5284780578898225, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '247600', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1527, 'n_words': 456}