You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2009/10/11 17:35:33 UTC
svn commit: r824090 [4/4] - in /james/jdkim/trunk: ./
mailets/src/main/java/org/apache/james/jdkim/mailets/
mailets/src/test/java/org/apache/james/jdkim/mailets/
main/src/main/java/org/apache/james/jdkim/
main/src/main/java/org/apache/james/jdkim/api/ ...
Modified: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/PublicKeyRecordTest.java
URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/PublicKeyRecordTest.java?rev=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/PublicKeyRecordTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/PublicKeyRecordTest.java Sun Oct 11 15:35:32 2009
@@ -32,195 +32,218 @@
public class PublicKeyRecordTest extends TestCase {
- public void testValidate() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("");
- try {
- pkr.validate();
- fail("Expected failure: missing mandatory parameters");
- } catch (IllegalStateException e) {
- }
- pkr = new PublicKeyRecordImpl("k=rsa; p=XXXXXXXX=;");
- pkr.validate();
- pkr = new PublicKeyRecordImpl("v=DKIM1; k=rsa; p=XXXXXX=");
- pkr.validate();
- pkr = new PublicKeyRecordImpl(" v=DKIM1; k=rsa; p=XXXXXX=");
- pkr.validate();
- pkr = new PublicKeyRecordImpl("k=rsa; v=DKIM1; p=XXXXXX=");
- try {
- pkr.validate();
- fail("Expected failure: v should be the first");
- } catch (IllegalStateException e) {
- }
- pkr = new PublicKeyRecordImpl("v=DKIM2; k=rsa; p=XXXXXX=");
- try {
- pkr.validate();
- fail("Expected failure: wrong version");
- } catch (IllegalStateException e) {
- }
- pkr = new PublicKeyRecordImpl("v=DKIM1; k=rsa; p=");
- try {
- pkr.validate();
- fail("Expected failure: revoked key");
- } catch (IllegalStateException e) {
- }
- }
-
- public void testIsHashMethodSupported() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; p=XXXXXXXX=;");
- pkr.validate();
- assertTrue(pkr.isHashMethodSupported("sha1"));
- assertTrue(pkr.isHashMethodSupported("sha256"));
- pkr = new PublicKeyRecordImpl("k=rsa; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- assertTrue(pkr.isHashMethodSupported("sha1"));
- assertFalse(pkr.isHashMethodSupported("sha128"));
- assertTrue(pkr.isHashMethodSupported("sha256"));
- }
-
- public void testIsKeyTypeSupported() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; p=XXXXXXXX=;");
- pkr.validate();
- assertTrue(pkr.isKeyTypeSupported("rsa"));
- assertFalse(pkr.isKeyTypeSupported("dsa"));
- }
-
- public void testGetAcceptableHashMethods() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- List methods = pkr.getAcceptableHashMethods();
- assertEquals("[sha1, sha256]", methods.toString());
- pkr = new PublicKeyRecordImpl("k=rsa; p=XXXXXXXX=;");
- pkr.validate();
- methods = pkr.getAcceptableHashMethods();
- assertNull(methods);
- }
-
- public void testGetAcceptableKeyTypes() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- List methods = pkr.getAcceptableKeyTypes();
- assertEquals("[rsa]", methods.toString());
- pkr = new PublicKeyRecordImpl("k=rsa:dsa; p=XXXXXXXX=;");
- pkr.validate();
- methods = pkr.getAcceptableKeyTypes();
- assertEquals("[rsa, dsa]", methods.toString());
- }
-
- public void testGetGranularityPattern() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- Pattern pattern = pkr.getGranularityPattern();
- assertEquals("^\\Q\\E.*\\Q\\E$", pattern.pattern());
- assertTrue(pattern.matcher("something").matches());
- assertTrue(pattern.matcher("").matches());
- pkr = new PublicKeyRecordImpl("k=rsa; g=; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- pattern = pkr.getGranularityPattern();
- assertEquals("@", pattern.pattern());
- assertFalse(pattern.matcher("something").matches());
- assertFalse(pattern.matcher("").matches());
- pkr = new PublicKeyRecordImpl("k=rsa; g=some*; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- pattern = pkr.getGranularityPattern();
- assertTrue(pattern.matcher("something").matches());
- assertTrue(pattern.matcher("some").matches());
- assertFalse(pattern.matcher("som").matches());
- assertFalse(pattern.matcher("awesome").matches());
- assertEquals("^\\Qsome\\E.*\\Q\\E$", pattern.pattern());
- pkr = new PublicKeyRecordImpl("k=rsa; g=*+test; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- pattern = pkr.getGranularityPattern();
- assertEquals("^\\Q\\E.*\\Q+test\\E$", pattern.pattern());
- assertTrue(pattern.matcher("a+test").matches());
- assertTrue(pattern.matcher("+test").matches());
- assertFalse(pattern.matcher("atest").matches());
- assertFalse(pattern.matcher("+tested").matches());
- pkr = new PublicKeyRecordImpl("k=rsa; g=test; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- pattern = pkr.getGranularityPattern();
- assertEquals("^\\Qtest\\E$", pattern.pattern());
- assertTrue(pattern.matcher("test").matches());
- assertFalse(pattern.matcher("atest").matches());
- assertFalse(pattern.matcher("testa").matches());
- try {
- pkr = new PublicKeyRecordImpl("k=rsa; g=*\\+test; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- pattern = pkr.getGranularityPattern();
- fail("Expected syntax error");
- } catch ( IllegalStateException e) {
- }
- try {
- pkr = new PublicKeyRecordImpl("k=rsa; g=*test*; h=sha1:sha256; p=XXXXXXXX=;");
- pkr.validate();
- pattern = pkr.getGranularityPattern();
- fail("Expected syntax error");
- } catch ( IllegalStateException e) {
- }
- }
-
- public void testGetPublicKey() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- PublicKey pk = pkr.getPublicKey();
- assertEquals("RSA", pk.getAlgorithm());
- // On older jvm this is X509
- // assertEquals("X.509", pk.getFormat());
- assertEquals(new BigInteger("140815480285950232210124449496973988135931539914762288985377502488754711434253259186192434865594456027796377309280714060984552676169392598862819043219650259702261370701494928576447797673342985377518637829874968725582762257956980427968667812066816497848410406856165942400151628259779523949079651036806330485849"), ((RSAKey) pk).getModulus());
-
- try {
- pkr = new PublicKeyRecordImpl("k=dsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- pk = pkr.getPublicKey();
- fail("Expected invalid key spec. DSA is not supported");
- } catch (IllegalStateException e) {
- }
-
- try {
- pkr = new PublicKeyRecordImpl("k=unknown; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- pk = pkr.getPublicKey();
- fail("Expected invalid algorythm. 'unknown' is not supported");
- } catch (IllegalStateException e) {
- }
- }
-
- public void testGetFlags() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; t=y:s; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- List flags = pkr.getFlags();
- assertEquals("[y, s]", flags.toString());
- pkr = new PublicKeyRecordImpl("k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- flags = pkr.getFlags();
- assertEquals("[y]", flags.toString());
- pkr = new PublicKeyRecordImpl("k=rsa; t=; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- flags = pkr.getFlags();
- assertEquals("[]", flags.toString());
- }
-
- public void testIsTesting() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; t=y:s; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- assertTrue(pkr.isTesting());
- pkr = new PublicKeyRecordImpl("k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- assertTrue(pkr.isTesting());
- pkr = new PublicKeyRecordImpl("k=rsa; t=; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- assertFalse(pkr.isTesting());
- }
-
- public void testIsDenySubdomains() {
- PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; t=y:s; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- assertTrue(pkr.isDenySubdomains());
- pkr = new PublicKeyRecordImpl("k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- assertFalse(pkr.isDenySubdomains());
- pkr = new PublicKeyRecordImpl("k=rsa; t=; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
- pkr.validate();
- assertFalse(pkr.isDenySubdomains());
- }
+ public void testValidate() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl("");
+ try {
+ pkr.validate();
+ fail("Expected failure: missing mandatory parameters");
+ } catch (IllegalStateException e) {
+ }
+ pkr = new PublicKeyRecordImpl("k=rsa; p=XXXXXXXX=;");
+ pkr.validate();
+ pkr = new PublicKeyRecordImpl("v=DKIM1; k=rsa; p=XXXXXX=");
+ pkr.validate();
+ pkr = new PublicKeyRecordImpl(" v=DKIM1; k=rsa; p=XXXXXX=");
+ pkr.validate();
+ pkr = new PublicKeyRecordImpl("k=rsa; v=DKIM1; p=XXXXXX=");
+ try {
+ pkr.validate();
+ fail("Expected failure: v should be the first");
+ } catch (IllegalStateException e) {
+ }
+ pkr = new PublicKeyRecordImpl("v=DKIM2; k=rsa; p=XXXXXX=");
+ try {
+ pkr.validate();
+ fail("Expected failure: wrong version");
+ } catch (IllegalStateException e) {
+ }
+ pkr = new PublicKeyRecordImpl("v=DKIM1; k=rsa; p=");
+ try {
+ pkr.validate();
+ fail("Expected failure: revoked key");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ public void testIsHashMethodSupported() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; p=XXXXXXXX=;");
+ pkr.validate();
+ assertTrue(pkr.isHashMethodSupported("sha1"));
+ assertTrue(pkr.isHashMethodSupported("sha256"));
+ pkr = new PublicKeyRecordImpl("k=rsa; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ assertTrue(pkr.isHashMethodSupported("sha1"));
+ assertFalse(pkr.isHashMethodSupported("sha128"));
+ assertTrue(pkr.isHashMethodSupported("sha256"));
+ }
+
+ public void testIsKeyTypeSupported() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl("k=rsa; p=XXXXXXXX=;");
+ pkr.validate();
+ assertTrue(pkr.isKeyTypeSupported("rsa"));
+ assertFalse(pkr.isKeyTypeSupported("dsa"));
+ }
+
+ public void testGetAcceptableHashMethods() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl(
+ "k=rsa; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ List methods = pkr.getAcceptableHashMethods();
+ assertEquals("[sha1, sha256]", methods.toString());
+ pkr = new PublicKeyRecordImpl("k=rsa; p=XXXXXXXX=;");
+ pkr.validate();
+ methods = pkr.getAcceptableHashMethods();
+ assertNull(methods);
+ }
+
+ public void testGetAcceptableKeyTypes() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl(
+ "k=rsa; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ List methods = pkr.getAcceptableKeyTypes();
+ assertEquals("[rsa]", methods.toString());
+ pkr = new PublicKeyRecordImpl("k=rsa:dsa; p=XXXXXXXX=;");
+ pkr.validate();
+ methods = pkr.getAcceptableKeyTypes();
+ assertEquals("[rsa, dsa]", methods.toString());
+ }
+
+ public void testGetGranularityPattern() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl(
+ "k=rsa; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ Pattern pattern = pkr.getGranularityPattern();
+ assertEquals("^\\Q\\E.*\\Q\\E$", pattern.pattern());
+ assertTrue(pattern.matcher("something").matches());
+ assertTrue(pattern.matcher("").matches());
+ pkr = new PublicKeyRecordImpl("k=rsa; g=; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ pattern = pkr.getGranularityPattern();
+ assertEquals("@", pattern.pattern());
+ assertFalse(pattern.matcher("something").matches());
+ assertFalse(pattern.matcher("").matches());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; g=some*; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ pattern = pkr.getGranularityPattern();
+ assertTrue(pattern.matcher("something").matches());
+ assertTrue(pattern.matcher("some").matches());
+ assertFalse(pattern.matcher("som").matches());
+ assertFalse(pattern.matcher("awesome").matches());
+ assertEquals("^\\Qsome\\E.*\\Q\\E$", pattern.pattern());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; g=*+test; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ pattern = pkr.getGranularityPattern();
+ assertEquals("^\\Q\\E.*\\Q+test\\E$", pattern.pattern());
+ assertTrue(pattern.matcher("a+test").matches());
+ assertTrue(pattern.matcher("+test").matches());
+ assertFalse(pattern.matcher("atest").matches());
+ assertFalse(pattern.matcher("+tested").matches());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; g=test; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ pattern = pkr.getGranularityPattern();
+ assertEquals("^\\Qtest\\E$", pattern.pattern());
+ assertTrue(pattern.matcher("test").matches());
+ assertFalse(pattern.matcher("atest").matches());
+ assertFalse(pattern.matcher("testa").matches());
+ try {
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; g=*\\+test; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ pattern = pkr.getGranularityPattern();
+ fail("Expected syntax error");
+ } catch (IllegalStateException e) {
+ }
+ try {
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; g=*test*; h=sha1:sha256; p=XXXXXXXX=;");
+ pkr.validate();
+ pattern = pkr.getGranularityPattern();
+ fail("Expected syntax error");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ public void testGetPublicKey() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ PublicKey pk = pkr.getPublicKey();
+ assertEquals("RSA", pk.getAlgorithm());
+ // On older jvm this is X509
+ // assertEquals("X.509", pk.getFormat());
+ assertEquals(
+ new BigInteger(
+ "140815480285950232210124449496973988135931539914762288985377502488754711434253259186192434865594456027796377309280714060984552676169392598862819043219650259702261370701494928576447797673342985377518637829874968725582762257956980427968667812066816497848410406856165942400151628259779523949079651036806330485849"),
+ ((RSAKey) pk).getModulus());
+
+ try {
+ pkr = new PublicKeyRecordImpl(
+ "k=dsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ pk = pkr.getPublicKey();
+ fail("Expected invalid key spec. DSA is not supported");
+ } catch (IllegalStateException e) {
+ }
+
+ try {
+ pkr = new PublicKeyRecordImpl(
+ "k=unknown; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ pk = pkr.getPublicKey();
+ fail("Expected invalid algorythm. 'unknown' is not supported");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ public void testGetFlags() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=y:s; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ List flags = pkr.getFlags();
+ assertEquals("[y, s]", flags.toString());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ flags = pkr.getFlags();
+ assertEquals("[y]", flags.toString());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ flags = pkr.getFlags();
+ assertEquals("[]", flags.toString());
+ }
+
+ public void testIsTesting() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=y:s; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ assertTrue(pkr.isTesting());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ assertTrue(pkr.isTesting());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ assertFalse(pkr.isTesting());
+ }
+
+ public void testIsDenySubdomains() {
+ PublicKeyRecord pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=y:s; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ assertTrue(pkr.isDenySubdomains());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ assertFalse(pkr.isDenySubdomains());
+ pkr = new PublicKeyRecordImpl(
+ "k=rsa; t=; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIhyR3oItOy22ZOaBrIVe9m/iME3RqOJeasANSpg2YTHTYV+Xtp4xwf5gTjCmHQEMOs0qYu0FYiNQPQogJ2t0Mfx9zNu06rfRBDjiIU9tpx2T+NGlWZ8qhbiLo5By8apJavLyqTLavyPSrvsx0B3YzC63T4Age2CDqZYA+OwSMWQIDAQAB");
+ pkr.validate();
+ assertFalse(pkr.isDenySubdomains());
+ }
}
Modified: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/SignatureRecordImplTest.java
URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/SignatureRecordImplTest.java?rev=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/SignatureRecordImplTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/SignatureRecordImplTest.java Sun Oct 11 15:35:32 2009
@@ -26,124 +26,120 @@
public class SignatureRecordImplTest extends TestCase {
- public void testQPDecode() {
- assertEquals("",SignatureRecordImpl.dkimQuotedPrintableDecode(""));
- assertEquals("@",SignatureRecordImpl.dkimQuotedPrintableDecode("=40"));
- assertEquals("\r\n",SignatureRecordImpl.dkimQuotedPrintableDecode("=0D=0A"));
- assertEquals("\0CIAO\0",SignatureRecordImpl.dkimQuotedPrintableDecode("=00CIAO=00"));
- assertEquals("thisisatest",SignatureRecordImpl.dkimQuotedPrintableDecode("this\r\n\tis\r\n a\r\n \t test"));
- }
-
- public void testQPWhiteSpaces() {
- assertEquals("thisisatest",SignatureRecordImpl.dkimQuotedPrintableDecode("this is a test"));
- assertEquals("thisisatest",SignatureRecordImpl.dkimQuotedPrintableDecode("this\r\n is a test"));
- }
-
- public void testQPInvalid() {
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("=");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("==");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("=2 3");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("=3");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("=3a");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("==20");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("=20=");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("=3x");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- try {
- SignatureRecordImpl.dkimQuotedPrintableDecode("this\r\nis a test");
- fail("invalid sequence parsed.");
- } catch (IllegalArgumentException e) {
- }
- }
-
- // TODO check bytes > 128
- /*
- public void test8bit() {
- assertEquals("PROVA\144CIAO\144",Main.dkimQuotedPrintableDecode("PROVA=90CIAO=90"));
- }
- */
-
-
- /* when we moved from Sun's Base64 to CommonsCodec the decoding changed behaviour.
- * it does no more fail on bad encoded data.
- public void testWrongBase64Encoding() {
- SignatureRecord sr = new SignatureRecordImpl("v=1; bh=0012=GG; b==GG;");
- try {
- sr.getBodyHash();
- fail("expected failure");
- } catch (Exception e) {
- assertTrue(e.getMessage().toLowerCase().contains("decod"));
- }
- try {
- sr.getSignature();
- fail("expected failure");
- } catch (Exception e) {
- assertTrue(e.getMessage().toLowerCase().contains("decod"));
- }
- }
- */
-
- public void testWrongHashSyntaxes() {
- SignatureRecord sr = new SignatureRecordImpl("v=1; a=nothyphenedword;");
- try {
- sr.getHashAlgo();
- fail("expected failure");
- } catch (Exception e) {
- assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
- }
- try {
- sr.getHashMethod();
- fail("expected failure");
- } catch (Exception e) {
- assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
- }
- try {
- sr.getHashAlgo();
- fail("expected failure");
- } catch (Exception e) {
- assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
- }
- }
-
- public void testExpired() {
- SignatureRecord sr = new SignatureRecordImpl("v=1; c=simple; h=from:to; s=select; d=example.com; a=rsa-sha1; x=0; bh=abcdef; b=1235345987;");
- try {
- sr.validate();
- fail("expected failure");
- } catch (Exception e) {
- assertTrue(e.getMessage().indexOf("expired") != -1);
- }
- }
-
+ public void testQPDecode() {
+ assertEquals("", SignatureRecordImpl.dkimQuotedPrintableDecode(""));
+ assertEquals("@", SignatureRecordImpl.dkimQuotedPrintableDecode("=40"));
+ assertEquals("\r\n", SignatureRecordImpl
+ .dkimQuotedPrintableDecode("=0D=0A"));
+ assertEquals("\0CIAO\0", SignatureRecordImpl
+ .dkimQuotedPrintableDecode("=00CIAO=00"));
+ assertEquals("thisisatest", SignatureRecordImpl
+ .dkimQuotedPrintableDecode("this\r\n\tis\r\n a\r\n \t test"));
+ }
+
+ public void testQPWhiteSpaces() {
+ assertEquals("thisisatest", SignatureRecordImpl
+ .dkimQuotedPrintableDecode("this is a test"));
+ assertEquals("thisisatest", SignatureRecordImpl
+ .dkimQuotedPrintableDecode("this\r\n is a test"));
+ }
+
+ public void testQPInvalid() {
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("=");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("==");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("=2 3");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("=3");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("=3a");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("==20");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("=20=");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("=3x");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ SignatureRecordImpl.dkimQuotedPrintableDecode("this\r\nis a test");
+ fail("invalid sequence parsed.");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ // TODO check bytes > 128
+ /*
+ * public void test8bit() {
+ * assertEquals("PROVA\144CIAO\144",Main.dkimQuotedPrintableDecode("PROVA=90CIAO=90")); }
+ */
+
+ /*
+ * when we moved from Sun's Base64 to CommonsCodec the decoding changed
+ * behaviour. it does no more fail on bad encoded data. public void
+ * testWrongBase64Encoding() { SignatureRecord sr = new
+ * SignatureRecordImpl("v=1; bh=0012=GG; b==GG;"); try { sr.getBodyHash();
+ * fail("expected failure"); } catch (Exception e) {
+ * assertTrue(e.getMessage().toLowerCase().contains("decod")); } try {
+ * sr.getSignature(); fail("expected failure"); } catch (Exception e) {
+ * assertTrue(e.getMessage().toLowerCase().contains("decod")); } }
+ */
+
+ public void testWrongHashSyntaxes() {
+ SignatureRecord sr = new SignatureRecordImpl("v=1; a=nothyphenedword;");
+ try {
+ sr.getHashAlgo();
+ fail("expected failure");
+ } catch (Exception e) {
+ assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
+ }
+ try {
+ sr.getHashMethod();
+ fail("expected failure");
+ } catch (Exception e) {
+ assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
+ }
+ try {
+ sr.getHashAlgo();
+ fail("expected failure");
+ } catch (Exception e) {
+ assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
+ }
+ }
+
+ public void testExpired() {
+ SignatureRecord sr = new SignatureRecordImpl(
+ "v=1; c=simple; h=from:to; s=select; d=example.com; a=rsa-sha1; x=0; bh=abcdef; b=1235345987;");
+ try {
+ sr.validate();
+ fail("expected failure");
+ } catch (Exception e) {
+ assertTrue(e.getMessage().indexOf("expired") != -1);
+ }
+ }
+
}
Modified: james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/SignatureRecordTest.java
URL: http://svn.apache.org/viewvc/james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/SignatureRecordTest.java?rev=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/SignatureRecordTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/SignatureRecordTest.java Sun Oct 11 15:35:32 2009
@@ -25,76 +25,80 @@
import junit.framework.TestCase;
public class SignatureRecordTest extends TestCase {
-
- public void testBasic() {
- SignatureRecord sign = new SignatureRecordImpl("v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n"
- +" d=gmail.com; s=beta;\r\n"
- +" h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
- +" bh=9sd6eO/xnGLInYGPFN86r9q27iClGpwfkl4PBc5XEuQ=;\r\n"
- +" b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
- sign.validate();
- }
-
- public void testWrongOrMissingVersion() {
- try {
- SignatureRecord sign = new SignatureRecordImpl("a=rsa-sha1; c=relaxed/relaxed;\r\n"
- +" d=gmail.com; s=beta;\r\n"
- +" h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
- +" b=Kw/TqnjB4L5ZC7DX1ibiNkuIw630uHZvzuozn/e6yTm3U8ObWEz/rJK5GO8RSrF56JrCA/xo8W2CGmyNmpQYbEpLl5P9/NcJSYUmln/O6GSa4Usyv4FdEU4FVjkyW0ToGFHNkw9Mm0urveA4Lcfk9gClJczXnvGBdiv/bkVBEJk=");
- sign.validate();
- fail("expected error on missing v=");
- } catch (IllegalStateException e) {
- }
- try {
- SignatureRecord sign = new SignatureRecordImpl("v=2; a=rsa-sha256; c=relaxed/relaxed;\r\n"
- +" d=gmail.com; s=beta;\r\n"
- +" h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
- +" bh=9sd6eO/xnGLInYGPFN86r9q27iClGpwfkl4PBc5XEuQ=;\r\n"
- +" b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
- sign.validate();
- fail("expected error on wrong v=");
- } catch (IllegalStateException e) {
- }
- }
-
-
- public void testMissingRequired() {
- try {
- SignatureRecord sign = new SignatureRecordImpl("v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n"
- +" d=gmail.com; s=beta;\r\n"
- +" h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
- +" b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
- sign.validate();
- fail("expected error on missing bh=");
- } catch (IllegalStateException e) {
- }
- }
-
- public void testDomainMismatch() {
- try {
- SignatureRecord sign = new SignatureRecordImpl("v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n"
- +" d=gmail.com; s=beta; i=@agmail.com;\r\n"
- +" h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
- +" bh=9sd6eO/xnGLInYGPFN86r9q27iClGpwfkl4PBc5XEuQ=;\r\n"
- +" b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
- sign.validate();
- fail("expected error on domain mismatch");
- } catch (IllegalStateException e) {
- }
- }
-
- public void testMissingFrom() {
- try {
- SignatureRecord sign = new SignatureRecordImpl("v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n"
- +" d=gmail.com; s=beta; i=@subdomain.gmail.com;\r\n"
- +" h=domainkey-signature:received:received:message-id:date:fram:to:subject:mime-version:content-type;\r\n"
- +" bh=9sd6eO/xnGLInYGPFN86r9q27iClGpwfkl4PBc5XEuQ=;\r\n"
- +" b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
- sign.validate();
- fail("expected error on missing 'from' header");
- } catch (IllegalStateException e) {
- }
- }
+ public void testBasic() {
+ SignatureRecord sign = new SignatureRecordImpl(
+ "v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n"
+ + " d=gmail.com; s=beta;\r\n"
+ + " h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
+ + " bh=9sd6eO/xnGLInYGPFN86r9q27iClGpwfkl4PBc5XEuQ=;\r\n"
+ + " b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
+ sign.validate();
+ }
+
+ public void testWrongOrMissingVersion() {
+ try {
+ SignatureRecord sign = new SignatureRecordImpl(
+ "a=rsa-sha1; c=relaxed/relaxed;\r\n"
+ + " d=gmail.com; s=beta;\r\n"
+ + " h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
+ + " b=Kw/TqnjB4L5ZC7DX1ibiNkuIw630uHZvzuozn/e6yTm3U8ObWEz/rJK5GO8RSrF56JrCA/xo8W2CGmyNmpQYbEpLl5P9/NcJSYUmln/O6GSa4Usyv4FdEU4FVjkyW0ToGFHNkw9Mm0urveA4Lcfk9gClJczXnvGBdiv/bkVBEJk=");
+ sign.validate();
+ fail("expected error on missing v=");
+ } catch (IllegalStateException e) {
+ }
+ try {
+ SignatureRecord sign = new SignatureRecordImpl(
+ "v=2; a=rsa-sha256; c=relaxed/relaxed;\r\n"
+ + " d=gmail.com; s=beta;\r\n"
+ + " h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
+ + " bh=9sd6eO/xnGLInYGPFN86r9q27iClGpwfkl4PBc5XEuQ=;\r\n"
+ + " b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
+ sign.validate();
+ fail("expected error on wrong v=");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ public void testMissingRequired() {
+ try {
+ SignatureRecord sign = new SignatureRecordImpl(
+ "v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n"
+ + " d=gmail.com; s=beta;\r\n"
+ + " h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
+ + " b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
+ sign.validate();
+ fail("expected error on missing bh=");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ public void testDomainMismatch() {
+ try {
+ SignatureRecord sign = new SignatureRecordImpl(
+ "v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n"
+ + " d=gmail.com; s=beta; i=@agmail.com;\r\n"
+ + " h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type;\r\n"
+ + " bh=9sd6eO/xnGLInYGPFN86r9q27iClGpwfkl4PBc5XEuQ=;\r\n"
+ + " b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
+ sign.validate();
+ fail("expected error on domain mismatch");
+ } catch (IllegalStateException e) {
+ }
+ }
+
+ public void testMissingFrom() {
+ try {
+ SignatureRecord sign = new SignatureRecordImpl(
+ "v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n"
+ + " d=gmail.com; s=beta; i=@subdomain.gmail.com;\r\n"
+ + " h=domainkey-signature:received:received:message-id:date:fram:to:subject:mime-version:content-type;\r\n"
+ + " bh=9sd6eO/xnGLInYGPFN86r9q27iClGpwfkl4PBc5XEuQ=;\r\n"
+ + " b=tGQtBQg1sO+JKopOylApWLngylEqeMcXwCEUQN+S2PSpi9c1G9Nm5df9pMShus3iFaQb0PPvTfpw++cAC8/N0p3Gi/lVLc+Yh7xWEIPZ3Nxd3xqTQy7grIkBpV0q6559dEhhfFoEyLS0OK/IrqFIUVDRIMnsMjimXV7u+Sgoi7Q=");
+ sign.validate();
+ fail("expected error on missing 'from' header");
+ } catch (IllegalStateException e) {
+ }
+ }
}
Modified: 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=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/AbstractOutputStreamTestCase.java Sun Oct 11 15:35:32 2009
@@ -30,83 +30,94 @@
import junit.framework.TestCase;
/**
- * Base class useful when testing outputstreams
- * It simplify the job of testing any weird chunking during the
- * streamin.
+ * 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());
- }
- }
+
+ 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());
+ }
+ }
}
Modified: 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=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/CompoundOutputStreamTest.java Sun Oct 11 15:35:32 2009
@@ -26,45 +26,44 @@
import java.util.LinkedList;
import java.util.List;
-
public class CompoundOutputStreamTest extends AbstractOutputStreamTestCase {
- private byte[] testData;
+ 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());
- }
- }
+ 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());
+ }
+ }
}
Modified: 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=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/DigestOutputStreamTest.java Sun Oct 11 15:35:32 2009
@@ -27,42 +27,46 @@
public class DigestOutputStreamTest extends AbstractOutputStreamTestCase {
- private byte[] testData;
- private byte[] expectedDigest;
+ 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()));
- }
+ 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()));
+ }
}
Modified: 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=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/LimitedOutputStreamTest.java Sun Oct 11 15:35:32 2009
@@ -26,43 +26,45 @@
public class LimitedOutputStreamTest extends AbstractOutputStreamTestCase {
- private byte[] testData;
- private byte[] expectedData;
+ 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);
- }
+ 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);
+ }
}
Modified: 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=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/RelaxedBodyCanonicalizerTest.java Sun Oct 11 15:35:32 2009
@@ -25,28 +25,29 @@
public class RelaxedBodyCanonicalizerTest extends AbstractOutputStreamTestCase {
- private byte[] testData;
- private byte[] expectedData;
+ 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();
- }
+ 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 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());
- }
+ public void testChunks() throws NoSuchAlgorithmException, IOException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ RelaxedBodyCanonicalizer os = new RelaxedBodyCanonicalizer(bos);
+ chunker(testData, os);
+ assertArrayEquals(expectedData, bos.toByteArray());
+ }
}
Modified: 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=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/canon/SimpleBodyCanonicalizerTest.java Sun Oct 11 15:35:32 2009
@@ -26,116 +26,133 @@
public class SimpleBodyCanonicalizerTest extends AbstractOutputStreamTestCase {
- private byte[] testData;
- private byte[] expectedData;
+ 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());
- }
+ 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());
+ }
}
Modified: 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=824090&r1=824089&r2=824090&view=diff
==============================================================================
--- james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java (original)
+++ james/jdkim/trunk/main/src/test/java/org/apache/james/jdkim/tagvalue/TagValueTest.java Sun Oct 11 15:35:32 2009
@@ -25,147 +25,152 @@
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;
- }
+ 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;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org