Author: bago Date: Tue Oct 6 17:50:37 2009 New Revision: 822374 URL: http://svn.apache.org/viewvc?rev=822374&view=rev Log: Some test for the main library.
Added: james/jdkim/trunk/main/src/test/ james/jdkim/trunk/main/src/test/java/ james/jdkim/trunk/main/src/test/java/org/ james/jdkim/trunk/main/src/test/java/org/apache/ james/jdkim/trunk/main/src/test/java/org/apache/james/ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java (with props) james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java (with props) james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java (with props) james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java (with props) james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java (with props) james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java (with props) james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java (with props) Added: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java?rev=822374&view=auto ============================================================================== --- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java (added) +++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java Tue Oct 6 17:50:37 2009 @@ -0,0 +1,112 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jdkim.canon; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +import junit.framework.TestCase; + +/** + * Base class useful when testing outputstreams + * It simplify the job of testing any weird chunking during the + * streamin. + */ +public abstract class AbstractOutputStreamTestCase extends TestCase { + + protected AbstractOutputStreamTestCase() { + } + + public void chunker(BufferedInputStream is, OutputStream os) + throws IOException { + byte[] buffer = new byte[307]; + int read; + int chunksCounter = 0; // + int bytesCounter = 0; // + while ((read = is.read(buffer, 0, (buffer.length / (chunksCounter % 8 + 1)))) > 0) { + if (read == buffer.length && chunksCounter % 13 % 7 % 2 == 1) { + os.write(buffer); + } else if (chunksCounter % 11 != 0){ + os.write(buffer, 0, read); + } else for (int i = 0; i < read; i++) { + os.write(buffer[i]); + } + if (chunksCounter % 3 == 2) os.flush(); + chunksCounter++; + bytesCounter+=read; + } + os.close(); + } + + public void chunker(byte[] data, OutputStream os) throws IOException { + BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(data)); + chunker(is, os); + } + + public void writeChunk(OutputStream os, byte[] data, int from, int len) + throws IOException { + if (len == 1) os.write(data[from]); + else if (len == data.length) os.write(data); + else os.write(data, from, len); + } + + public void assertArrayEquals(String explanation, byte[] expected, byte[] actual) { + if (!Arrays.equals(expected, actual)) { + assertEquals(explanation, new String(expected), new String(actual)); + } + } + + public void assertArrayEquals(byte[] expected, byte[] actual) { + if (!Arrays.equals(expected, actual)) { + assertEquals(new String(expected), new String(actual)); + } + } + + protected OutputStream newInstance(ByteArrayOutputStream bos) { + throw new IllegalStateException("Implement newInstance in order to use extensive chunker"); + } + + /** + * An extensive checker for streams. + * It split the buffer every possibile 1, to and 3 part sequences and check the results. + * + * @throws NoSuchAlgorithmException + * @throws IOException + */ + public void extensiveChunker(byte[] data, byte[] expectedData) throws IOException { + for (int i = 0; i < data.length; i++) for (int j = i; j < data.length; j++) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + OutputStream os = newInstance(bos); + + writeChunk(os, data, 0, i); + writeChunk(os, data, i, j-i); + writeChunk(os, data, j, data.length-j); + os.close(); + + assertArrayEquals("i="+i+", j="+j+", l="+data.length, expectedData, bos.toByteArray()); + } + } + +} Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java?rev=822374&view=auto ============================================================================== --- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java (added) +++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java Tue Oct 6 17:50:37 2009 @@ -0,0 +1,70 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jdkim.canon; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + + +public class CompoundOutputStreamTest extends AbstractOutputStreamTestCase { + + private byte[] testData; + + protected void setUp() throws Exception { + testData = new byte[4096]; + for (int i = 0; i < testData.length; i++) { + testData[i] = (byte)((i*i*4095+(testData.length-i)*17) % 128 ); + } + } + + public void testSingleBytes() throws NoSuchAlgorithmException, IOException { + List/* ByteArrayOutputStream */ oss = new LinkedList(); + for (int i = 0; i < 5; i++) { + oss.add(new ByteArrayOutputStream()); + } + CompoundOutputStream os = new CompoundOutputStream(oss); + for (int i = 0; i < testData.length; i++) { + os.write(testData[i]); + } + os.close(); + for (Iterator i = oss.iterator(); i.hasNext(); ) { + ByteArrayOutputStream bos = (ByteArrayOutputStream) i.next(); + assertArrayEquals(testData, bos.toByteArray()); + } + } + + public void testChunks() throws NoSuchAlgorithmException, IOException { + List/* ByteArrayOutputStream */ oss = new LinkedList(); + for (int i = 0; i < 5; i++) { + oss.add(new ByteArrayOutputStream()); + } + CompoundOutputStream os = new CompoundOutputStream(oss); + chunker(testData, os); + for (Iterator i = oss.iterator(); i.hasNext(); ) { + ByteArrayOutputStream bos = (ByteArrayOutputStream) i.next(); + assertArrayEquals(testData, bos.toByteArray()); + } + } + +} Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java?rev=822374&view=auto ============================================================================== --- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java (added) +++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java Tue Oct 6 17:50:37 2009 @@ -0,0 +1,68 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jdkim.canon; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +public class DigestOutputStreamTest extends AbstractOutputStreamTestCase { + + private byte[] testData; + private byte[] expectedDigest; + + protected void setUp() throws Exception { + testData = new byte[4096]; + for (int i = 0; i < testData.length; i++) { + testData[i] = (byte)((i*i*4095+(testData.length-i)*17) % 128 ); + } + MessageDigest md = MessageDigest.getInstance("sha-256"); + md.update(testData); + expectedDigest = md.digest(); + } + + public void testSingleBytes() throws NoSuchAlgorithmException, IOException { + DigestOutputStream dos = new DigestOutputStream(MessageDigest.getInstance("sha-256")); + for (int i = 0; i < testData.length; i++) { + dos.write(testData[i]); + } + dos.close(); + byte[] digest = dos.getDigest(); + assertTrue(Arrays.equals(expectedDigest, digest)); + } + + public void testChunks() throws NoSuchAlgorithmException, IOException { + DigestOutputStream dos = new DigestOutputStream(MessageDigest.getInstance("sha-256")); + chunker(testData, dos); + byte[] digest = dos.getDigest(); + assertTrue(Arrays.equals(expectedDigest, digest)); + } + + public void testChunksAndPassthrough() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DigestOutputStream dos = new DigestOutputStream(MessageDigest.getInstance("sha-256"), bos); + chunker(testData, dos); + byte[] digest = dos.getDigest(); + assertTrue(Arrays.equals(expectedDigest, digest)); + assertTrue(Arrays.equals(testData, bos.toByteArray())); + } +} Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java?rev=822374&view=auto ============================================================================== --- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java (added) +++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java Tue Oct 6 17:50:37 2009 @@ -0,0 +1,68 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jdkim.canon; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.security.NoSuchAlgorithmException; + +public class LimitedOutputStreamTest extends AbstractOutputStreamTestCase { + + private byte[] testData; + private byte[] expectedData; + + protected void setUp() throws Exception { + testData = "this is a \r\n canonicalization \ttest\r\n\r\n\r\n".getBytes(); + expectedData = "this is a \r\n canonicalizatio".getBytes(); + } + + public void testSingleBytes() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + LimitedOutputStream os = new LimitedOutputStream(bos, 30); + for (int i = 0; i < testData.length; i++) { + assertEquals(i >= 30, os.isLimited()); + if (i == 30) { + assertArrayEquals(expectedData, bos.toByteArray()); + } + os.write(testData[i]); + } + os.close(); + assertEquals(30, os.getComputedBytes()); + assertArrayEquals(expectedData, bos.toByteArray()); + } + + public void testChunks() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + LimitedOutputStream os = new LimitedOutputStream(bos, 30); + chunker(testData, os); + assertEquals(30, os.getComputedBytes()); + assertArrayEquals(expectedData, bos.toByteArray()); + } + + protected OutputStream newInstance(ByteArrayOutputStream bos) { + return new LimitedOutputStream(bos, 30); + } + + public void testExtensiveChunks() throws NoSuchAlgorithmException, IOException { + extensiveChunker(testData, expectedData); + } + +} Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java?rev=822374&view=auto ============================================================================== --- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java (added) +++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java Tue Oct 6 17:50:37 2009 @@ -0,0 +1,52 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jdkim.canon; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; + +public class RelaxedBodyCanonicalizerTest extends AbstractOutputStreamTestCase { + + private byte[] testData; + private byte[] expectedData; + + protected void setUp() throws Exception { + testData = "this is a \r\n canonicalization \ttest\r\n\r\n\r\n".getBytes(); + expectedData = "this is a\r\n canonicalization test\r\n".getBytes(); + } + + public void testSingleBytes() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + RelaxedBodyCanonicalizer os = new RelaxedBodyCanonicalizer(bos); + for (int i = 0; i < testData.length; i++) { + os.write(testData[i]); + } + os.close(); + assertArrayEquals(expectedData, bos.toByteArray()); + } + + public void testChunks() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + RelaxedBodyCanonicalizer os = new RelaxedBodyCanonicalizer(bos); + chunker(testData, os); + assertArrayEquals(expectedData, bos.toByteArray()); + } +} Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java?rev=822374&view=auto ============================================================================== --- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java (added) +++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java Tue Oct 6 17:50:37 2009 @@ -0,0 +1,141 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jdkim.canon; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.security.NoSuchAlgorithmException; + +public class SimpleBodyCanonicalizerTest extends AbstractOutputStreamTestCase { + + private byte[] testData; + private byte[] expectedData; + + protected void setUp() throws Exception { + testData = "this is a \r\n canonicalization \ttest\r\n\r\n\r\n".getBytes(); + expectedData = "this is a \r\n canonicalization \ttest\r\n".getBytes(); + } + + public void testSingleBytes() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SimpleBodyCanonicalizer os = new SimpleBodyCanonicalizer(bos); + for (int i = 0; i < testData.length; i++) { + os.write(testData[i]); + } + os.close(); + assertArrayEquals(expectedData, bos.toByteArray()); + } + + public void testChunks() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SimpleBodyCanonicalizer os = new SimpleBodyCanonicalizer(bos); + chunker(testData, os); + assertArrayEquals(expectedData, bos.toByteArray()); + } + + public void testCRLFchunk() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SimpleBodyCanonicalizer os = new SimpleBodyCanonicalizer(bos); + writeChunk(os, testData, 0, 37); + // a buffer consisting of only CRLF was not handled correctly. + // this test checks this. + writeChunk(os, testData, 37, 6); + os.close(); + assertArrayEquals(expectedData, bos.toByteArray()); + } + + public void testProblematicChunks() throws NoSuchAlgorithmException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SimpleBodyCanonicalizer os = new SimpleBodyCanonicalizer(bos); + writeChunk(os, testData, 0, 38); + writeChunk(os, testData, 38, 2); + // a buffer consisting of only LFCR after a previous chunk + // ended with CR was not handled correctly. + writeChunk(os, testData, 40, 3); + os.close(); + assertArrayEquals(expectedData, bos.toByteArray()); + } + + + protected OutputStream newInstance(ByteArrayOutputStream bos) { + return new SimpleBodyCanonicalizer(bos); + } + + public void testExtensiveChunks() throws NoSuchAlgorithmException, IOException { + extensiveChunker(testData, expectedData); + } + + public void testWrongCRSequences() throws NoSuchAlgorithmException, IOException { + // byte[] test = "this is a \r\n canonica\rlizati\r\ron \ttest\r\n\r\n\r\r".getBytes(); + // byte[] expected = "this is a \r\n canonica\rlizati\r\ron \ttest\r\n\r\n\r\r\n".getBytes(); + byte[] test = "this is a \r\n canonica\rlizati".getBytes(); + byte[] expected = "this is a \r\n canonica\rlizati\r\n".getBytes(); + extensiveChunker(test, expected); + } + + public void testProblematicCRSequences() throws NoSuchAlgorithmException, IOException { + byte[] test = "this is a \r\n canonica\rlizati".getBytes(); + byte[] expected = "this is a \r\n canonica\rlizati\r\n".getBytes(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SimpleBodyCanonicalizer os = new SimpleBodyCanonicalizer(bos); + writeChunk(os, test, 0, 24); + // this created a problem where a single byte write after a line + // ending with \r was buggy + writeChunk(os, test, 24, 1); + writeChunk(os, test, 25, 5); + os.close(); + assertArrayEquals(expected, bos.toByteArray()); + } + + + public void testWrongCRSequencesAdv() throws NoSuchAlgorithmException, IOException { + // byte[] test = "this is a \r\n canonica\rlizati\r\ron \ttest\r\n\r\n\r\r".getBytes(); + // byte[] expected = "this is a \r\n canonica\rlizati\r\ron \ttest\r\n\r\n\r\r\n".getBytes(); + byte[] test = "this is a \r\n canonica\rlizati\r\ron\r\n\r\n\r".getBytes(); + byte[] expected = "this is a \r\n canonica\rlizati\r\ron\r\n".getBytes(); + extensiveChunker(test, expected); + } + + public void testProblematicEndingCRLFCR() throws NoSuchAlgorithmException, IOException { + byte[] test = "this is a \r\n canonica\rlizati\r\ron\r\n\r\n\r".getBytes(); + byte[] expected = "this is a \r\n canonica\rlizati\r\ron\r\n".getBytes(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SimpleBodyCanonicalizer os = new SimpleBodyCanonicalizer(bos); + // checks a bug with an buffer ending with \r\n\r + writeChunk(os, test, 0, 39); + os.close(); + assertArrayEquals(expected, bos.toByteArray()); + } + + public void testProblematicEndingCR() throws NoSuchAlgorithmException, IOException { + byte[] test = "this is a \r\n canonica\rlizati\r\ron\r\n\r\n\r".getBytes(); + byte[] expected = "this is a \r\n canonica\rlizati\r\ron\r\n".getBytes(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + SimpleBodyCanonicalizer os = new SimpleBodyCanonicalizer(bos); + // checks a bug with an buffer ending with \r\n\r + writeChunk(os, test, 0, 31); + writeChunk(os, test, 31, 1); + writeChunk(os, test, 32, 7); + os.close(); + assertArrayEquals(expected, bos.toByteArray()); + } + +} Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java?rev=822374&view=auto ============================================================================== --- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java (added) +++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java Tue Oct 6 17:50:37 2009 @@ -0,0 +1,171 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jdkim.tagvalue; + +import java.util.Set; + +import junit.framework.TestCase; + +public class TagValueTest extends TestCase { + + public void testEmpty() { + new TagValue(""); + } + + public void testValid() { + new TagValue("v=DKIM1; p=ciao; s=cips;"); + new TagValue("v="); + new TagValue("v=;"); + assertTrue(tagValuesEquals("v=", "v=;")); + assertTrue(tagValuesEquals("v=", "v= ;")); + assertTrue(tagValuesEquals("v=", "v=\r\n ;")); + assertFalse(tagValuesEquals("", "v=;")); + } + + public void testInvalidSyntax() { + try { + new TagValue("_p=ciao; s=cips; v=DKIM1;"); + fail("expected invalid tag exception"); + } catch (IllegalStateException e) { + } + } + + public void testDoubleTag() { + try { + new TagValue("s=ciao; s=cips; v=DKIM1;"); + fail("expected duplicate tag exception"); + } catch (IllegalStateException e) { + } + } + + public void testInvalidFWS() { + try { + new TagValue("\r\n"); + fail("we only expect WSP/FWS withing a tag-value. No FWS/WSP allowed with no tag"); + } catch (IllegalStateException e) { + } + } + + public void testInvalidFWSSyntax() { + try { + new TagValue("p=test \r\n\r\n "); + fail("expecting WSP after CRLF to handle it as FWS"); + } catch (IllegalStateException e) { + } + try { + new TagValue("p=\r\n\r\n test"); + fail("expecting WSP after CRLF to handle it as FWS"); + } catch (IllegalStateException e) { + } + } + + public void testInvalidFWSStartSyntax() { + try { + new TagValue("\r\np=ciao; s=cips; v=DKIM1;"); + fail("\\r\\n at the beginning is not valid FWS"); + } catch (IllegalStateException e) { + } + try { + new TagValue("\t\r\np=ciao; s=cips; v=DKIM1;"); + fail("\\t\\r\\n at the beginning is not valid FWS"); + } catch (IllegalStateException e) { + } + } + + public void testInvalidFWSEndSyntax() { + try { + new TagValue("p\r\n=ciao; s=cips; v=DKIM1;"); + fail("\\r\\n at the end is not valid FWS"); + } catch (IllegalStateException e) { + } + try { + new TagValue("p \r\n=ciao; s=cips; v=DKIM1;"); + fail("\\r\\n at the end is not valid FWS"); + } catch (IllegalStateException e) { + } + } + + public void testValidFWSTags() { + assertTrue(tagValuesEquals("\r\n\tp=ciao; s=cips; v=DKIM1;", "p=ciao;s=cips;v=DKIM1;")); + assertTrue(tagValuesEquals("p\r\n =ciao; s=cips; v=DKIM1;", "p=ciao;s=cips;v=DKIM1;")); + assertTrue(tagValuesEquals("p\r\n = \r\n\tciao; s=cips; v=DKIM1;", "p=ciao;s=cips;v=DKIM1;")); + assertTrue(tagValuesEquals("p\r\n = ciao; s=cips\r\n\t; v=DKIM1;", "p=ciao;s=cips;v=DKIM1;")); + } + + public void testNoTermination() { + TagValue t = new TagValue("\r\n\tp=ciao; s=cips; v=DKIM1\r\n\t"); + assertEquals("DKIM1", t.getValue("v")); + } + + // spaces around the value have to be stripped + public void testSingleValue() { + TagValue t = new TagValue("\r\n\tp = hi\t"); + assertEquals("hi", t.getValue("p")); + } + + // spaces withing the value needs to be retained. + public void testWSPinValue() { + TagValue t = new TagValue("\r\n\tp = \r\n hi \thi hi \t hi\t"); + assertEquals("hi \thi hi \t hi", t.getValue("p")); + } + + // FWS withing the value needs to be retained. + public void testFWSinValue() { + TagValue t = new TagValue("\r\n\tp = \r\n hi \thi\r\n hi \t hi\t"); + assertEquals("hi \thi\r\n hi \t hi", t.getValue("p")); + } + + public void testNoEqual() { + try { + new TagValue("\r\n\tp hi\t"); + fail("Expected value"); + } catch (IllegalStateException e) { + } + try { + new TagValue("v=DKIM1; pciao; s=cips;"); + fail("Expected value"); + } catch (IllegalStateException e) { + } + } + + /** + * TODO currently checking with the expert group to see if this is correct + */ + public void testEndingWSP() { + new TagValue("t=value; "); + } + + public void testTagSetWithEquals() { + TagValue tv = new TagValue("t=value; v=encoded=40value"); + Set tags = tv.getTags(); + assertEquals(2, tags.size()); + assertTrue(tags.contains("t")); + assertTrue(tags.contains("v")); + } + + public boolean tagValuesEquals(String t1, String t2) { + TagValue tv1 = new TagValue(t1); + TagValue tv2 = new TagValue(t2); + boolean eq = tv1.equals(tv2); + if (eq) assertTrue(tv1.hashCode() == tv2.hashCode()); + return eq; + } + +} Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org