From ca0191d050b3dac6e65815ad5662ed8e796ef9d9 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Thu, 29 Oct 2020 12:44:07 +0100 Subject: fix for the Android Java SDK bug in getDeclaredFields nor providing the field order --- src/net/gcdc/asn1/datatypes/FieldOrder.java | 13 +++ src/net/gcdc/asn1/test/UperEncodeBooleanTest.java | 3 +- .../asn1/test/UperEncodeChoiceExtensionTest.java | 3 + src/net/gcdc/asn1/test/UperEncodeChoiceTest.java | 3 + .../asn1/test/UperEncodeEnumExtensionTest.java | 2 + src/net/gcdc/asn1/test/UperEncodeEnumTest.java | 2 + .../test/UperEncodeExtensionFieldOrderTest.java | 102 +++++++++++++++++++++ .../gcdc/asn1/test/UperEncodeFieldOrderTest.java | 68 ++++++++++++++ .../test/UperEncodeIntegerConstrainedTest.java | 4 + .../asn1/test/UperEncodeIntegerExtensionTest.java | 28 ++---- .../gcdc/asn1/test/UperEncodeIntegerSmallTest.java | 3 + src/net/gcdc/asn1/test/UperEncodeIntegerTest.java | 2 + .../asn1/test/UperEncodeObjectIdentifierTest.java | 4 + .../gcdc/asn1/test/UperEncodeOctetStringTest.java | 2 + .../UperEncodeOptionalSequenceExtensionTest.java | 3 + .../asn1/test/UperEncodeRestrictedIntegerTest.java | 2 + .../asn1/test/UperEncodeSequenceExtensionTest.java | 4 + .../asn1/test/UperEncodeSequenceOfIntegerTest.java | 3 +- .../UperEncodeSequenceOfRestrictedIntegerTest.java | 2 + .../test/UperEncodeSequenceOfStringListTest.java | 2 + .../asn1/test/UperEncodeSequenceOfStringTest.java | 3 +- .../test/UperEncodeSequenceOfUtf8StringTest.java | 3 +- .../asn1/test/UperEncodeStringDefaultTest.java | 3 + .../gcdc/asn1/test/UperEncodeStringLengthTest.java | 2 + src/net/gcdc/asn1/test/UperEncodeStringTest.java | 33 +++++-- src/net/gcdc/asn1/uper/UperEncoder.java | 29 +++++- 26 files changed, 297 insertions(+), 31 deletions(-) create mode 100644 src/net/gcdc/asn1/datatypes/FieldOrder.java create mode 100644 src/net/gcdc/asn1/test/UperEncodeExtensionFieldOrderTest.java create mode 100644 src/net/gcdc/asn1/test/UperEncodeFieldOrderTest.java (limited to 'src/net/gcdc') diff --git a/src/net/gcdc/asn1/datatypes/FieldOrder.java b/src/net/gcdc/asn1/datatypes/FieldOrder.java new file mode 100644 index 0000000..23fb8fa --- /dev/null +++ b/src/net/gcdc/asn1/datatypes/FieldOrder.java @@ -0,0 +1,13 @@ +package net.gcdc.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface FieldOrder { + int order() default -1; +} + diff --git a/src/net/gcdc/asn1/test/UperEncodeBooleanTest.java b/src/net/gcdc/asn1/test/UperEncodeBooleanTest.java index 9716474..b9ca590 100644 --- a/src/net/gcdc/asn1/test/UperEncodeBooleanTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeBooleanTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Optional; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.uper.UperEncoder; @@ -25,7 +26,7 @@ public class UperEncodeBooleanTest { @Sequence public static class TestRecord { - + @FieldOrder(order = 0) @Asn1Optional() Boolean value; public TestRecord() { diff --git a/src/net/gcdc/asn1/test/UperEncodeChoiceExtensionTest.java b/src/net/gcdc/asn1/test/UperEncodeChoiceExtensionTest.java index acbbd0b..19cdb63 100644 --- a/src/net/gcdc/asn1/test/UperEncodeChoiceExtensionTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeChoiceExtensionTest.java @@ -7,6 +7,7 @@ import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Optional; import net.gcdc.asn1.datatypes.CharacterRestriction; import net.gcdc.asn1.datatypes.Choice; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.HasExtensionMarker; import net.gcdc.asn1.datatypes.IsExtension; import net.gcdc.asn1.datatypes.RestrictedString; @@ -30,9 +31,11 @@ public class UperEncodeChoiceExtensionTest { @HasExtensionMarker public static class TestRecordExtended { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.IA5String) String value1 = null; + @FieldOrder(order = 1) @IsExtension @RestrictedString(CharacterRestriction.IA5String) String value2 = "extension"; diff --git a/src/net/gcdc/asn1/test/UperEncodeChoiceTest.java b/src/net/gcdc/asn1/test/UperEncodeChoiceTest.java index d35e717..8dc8ce1 100644 --- a/src/net/gcdc/asn1/test/UperEncodeChoiceTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeChoiceTest.java @@ -6,6 +6,7 @@ import java.util.logging.Level; import net.gcdc.asn1.datatypes.CharacterRestriction; import net.gcdc.asn1.datatypes.Choice; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.RestrictedString; import net.gcdc.asn1.uper.UperEncoder; @@ -31,9 +32,11 @@ public class UperEncodeChoiceTest { @Choice public static class TestRecord { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.UTF8String) String valueUtf8; + @FieldOrder(order = 1) @RestrictedString(CharacterRestriction.IA5String) String valueIA5; diff --git a/src/net/gcdc/asn1/test/UperEncodeEnumExtensionTest.java b/src/net/gcdc/asn1/test/UperEncodeEnumExtensionTest.java index c098839..4b91f8e 100644 --- a/src/net/gcdc/asn1/test/UperEncodeEnumExtensionTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeEnumExtensionTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Optional; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.HasExtensionMarker; import net.gcdc.asn1.datatypes.IsExtension; import net.gcdc.asn1.datatypes.Sequence; @@ -35,6 +36,7 @@ public class UperEncodeEnumExtensionTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @Asn1Optional EnumType value = EnumType.value1; public TestRecord() {} public void setValue(EnumType value) { diff --git a/src/net/gcdc/asn1/test/UperEncodeEnumTest.java b/src/net/gcdc/asn1/test/UperEncodeEnumTest.java index 66fbc05..ce41e7a 100644 --- a/src/net/gcdc/asn1/test/UperEncodeEnumTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeEnumTest.java @@ -6,6 +6,7 @@ import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Default; import net.gcdc.asn1.datatypes.Asn1Optional; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.uper.UperEncoder; @@ -32,6 +33,7 @@ public class UperEncodeEnumTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @Asn1Default(value="value2") @Asn1Optional EnumType value = EnumType.value2; diff --git a/src/net/gcdc/asn1/test/UperEncodeExtensionFieldOrderTest.java b/src/net/gcdc/asn1/test/UperEncodeExtensionFieldOrderTest.java new file mode 100644 index 0000000..3c60e9c --- /dev/null +++ b/src/net/gcdc/asn1/test/UperEncodeExtensionFieldOrderTest.java @@ -0,0 +1,102 @@ +package net.gcdc.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import net.gcdc.asn1.datatypes.Asn1BigInteger; +import net.gcdc.asn1.datatypes.FieldOrder; +import net.gcdc.asn1.datatypes.HasExtensionMarker; +import net.gcdc.asn1.datatypes.IsExtension; +import net.gcdc.asn1.datatypes.Sequence; + +import net.gcdc.asn1.uper.UperEncoder; + +import org.junit.Test; + + +public class UperEncodeExtensionFieldOrderTest { + + /** + * Example from the Standard on UPER. +
+     TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE {
+           number1 INTEGER,
+           ...,
+           number2 INTEGER,
+           number3 INTEGER 
+    }
+    
+    value TestRecord ::=  {     
+        value1       12345678909999899,
+        value2       5555555555,
+        value3       32001      
+       }
+    
+Encoding to the file 'data.uper' using PER UNALIGNED encoding rule...
+TestRecord SEQUENCE [root fieldcount (not encoded) = 1]
+  value1 INTEGER [length = 7.0]
+    12345678909999899
+  value2 INTEGER [length = 5.0]
+    5555555555
+  value3 INTEGER [length = 2.0]
+    32001
+Total encoded length = 20.2
+Encoded successfully in 21 bytes:
+8395EE2A 2EF8858D 81C18140 52C8C338 C0C09F40 40
+    
+    
+    
+ */ + @Sequence + @HasExtensionMarker + public static class TestRecord { + + @FieldOrder(order = 0) + Asn1BigInteger value1; + + @FieldOrder(order = 1) + @IsExtension + Asn1BigInteger value2; + + @FieldOrder(order = 2) + @IsExtension + Asn1BigInteger value3; + + public TestRecord() { + value1 = new Asn1BigInteger(12345678909999899L); + value2 = new Asn1BigInteger(5555555555L); + value3 = new Asn1BigInteger(32001L); + } + + + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("8395EE2A2EF8858D81C1814052C8C338C0C09F4040",hex); + + + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("8395EE2A2EF8858D81C1814052C8C338C0C09F4040",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + assertEquals(result.value3.longValue(),record.value3.longValue()); + + } + +} diff --git a/src/net/gcdc/asn1/test/UperEncodeFieldOrderTest.java b/src/net/gcdc/asn1/test/UperEncodeFieldOrderTest.java new file mode 100644 index 0000000..e535356 --- /dev/null +++ b/src/net/gcdc/asn1/test/UperEncodeFieldOrderTest.java @@ -0,0 +1,68 @@ +package net.gcdc.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import net.gcdc.asn1.datatypes.Asn1Optional; +import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; +import net.gcdc.asn1.datatypes.RestrictedString; +import net.gcdc.asn1.datatypes.Sequence; + +import net.gcdc.asn1.uper.UperEncoder; + +import org.junit.Test; + + +public class UperEncodeFieldOrderTest { + + /** + * Example from the Standard on UPER. +
+        World-Schema DEFINITIONS AUTOMATIC TAGS ::= 
+        BEGIN
+        TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE {
+            testString1 UTF8String OPTIONAL,
+            testString2 IA5String OPTIONAL
+         }                                   
+        END
+    
+ */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 1) + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Optional() String string2; + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.UTF8String) + @Asn1Optional() String string1; + + + public TestRecord() { + } + + public TestRecord(String utf8, String ia5) { + this.string1 = utf8; + this.string2 = ia5; + } + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord("String1", "String2"); + byte[] encoded = UperEncoder.encode(record); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.string1,"String1"); + assertEquals(result.string2,"String2"); + } + + + + + +} diff --git a/src/net/gcdc/asn1/test/UperEncodeIntegerConstrainedTest.java b/src/net/gcdc/asn1/test/UperEncodeIntegerConstrainedTest.java index 9450406..6c6a4af 100644 --- a/src/net/gcdc/asn1/test/UperEncodeIntegerConstrainedTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeIntegerConstrainedTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.IntRange; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.uper.UperEncoder; @@ -26,12 +27,15 @@ public class UperEncodeIntegerConstrainedTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @IntRange(minValue=1, maxValue=999) public Long value1; + @FieldOrder(order = 1) @IntRange(minValue=0, maxValue=999) public Long value2; + @FieldOrder(order = 2) @IntRange(minValue=63, maxValue=999) public Long value3; diff --git a/src/net/gcdc/asn1/test/UperEncodeIntegerExtensionTest.java b/src/net/gcdc/asn1/test/UperEncodeIntegerExtensionTest.java index 5a33368..4c87bae 100644 --- a/src/net/gcdc/asn1/test/UperEncodeIntegerExtensionTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeIntegerExtensionTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1BigInteger; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.HasExtensionMarker; import net.gcdc.asn1.datatypes.IsExtension; import net.gcdc.asn1.datatypes.Sequence; @@ -51,14 +52,17 @@ Encoded successfully in 21 bytes: @HasExtensionMarker public static class TestRecord { - + @FieldOrder(order = 0) Asn1BigInteger value1; + @FieldOrder(order = 2) + @IsExtension + Asn1BigInteger value3; + + @FieldOrder(order = 1) @IsExtension Asn1BigInteger value2; - @IsExtension - Asn1BigInteger value3; public TestRecord() { value1 = new Asn1BigInteger(12345678909999899L); @@ -74,26 +78,14 @@ Encoded successfully in 21 bytes: TestRecord record = new TestRecord(); byte[] encoded = UperEncoder.encode(record); - String hex = UperEncoder.hexStringFromBytes(encoded); - UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); - assertEquals("8395EE2A2EF8858D81C1814052C8C338C0C09F4040",hex); - - - } - - @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { - - TestRecord record = new TestRecord(); - byte[] encoded = UperEncoder.encode(record); - String hex = UperEncoder.hexStringFromBytes(encoded); - UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); - assertEquals("8395EE2A2EF8858D81C1814052C8C338C0C09F4040",hex); - TestRecord result = UperEncoder.decode(encoded, TestRecord.class); assertEquals(result.value1.longValue(),record.value1.longValue()); assertEquals(result.value2.longValue(),record.value2.longValue()); assertEquals(result.value3.longValue(),record.value3.longValue()); + } + + } diff --git a/src/net/gcdc/asn1/test/UperEncodeIntegerSmallTest.java b/src/net/gcdc/asn1/test/UperEncodeIntegerSmallTest.java index 9ad0e63..c34918f 100644 --- a/src/net/gcdc/asn1/test/UperEncodeIntegerSmallTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeIntegerSmallTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.uper.UperEncoder; @@ -25,8 +26,10 @@ public class UperEncodeIntegerSmallTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) public Long value1; + @FieldOrder(order = 1) public Integer value2; public TestRecord() { diff --git a/src/net/gcdc/asn1/test/UperEncodeIntegerTest.java b/src/net/gcdc/asn1/test/UperEncodeIntegerTest.java index 4eab78a..f4f56f6 100644 --- a/src/net/gcdc/asn1/test/UperEncodeIntegerTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeIntegerTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1BigInteger; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.uper.UperEncoder; @@ -25,6 +26,7 @@ public class UperEncodeIntegerTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) Asn1BigInteger value; public TestRecord() { diff --git a/src/net/gcdc/asn1/test/UperEncodeObjectIdentifierTest.java b/src/net/gcdc/asn1/test/UperEncodeObjectIdentifierTest.java index c46751d..d36cfe6 100644 --- a/src/net/gcdc/asn1/test/UperEncodeObjectIdentifierTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeObjectIdentifierTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import net.gcdc.asn1.datatypes.Asn1Optional; import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.RestrictedString; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.uper.UperEncoder; @@ -34,12 +35,15 @@ class UperEncodeObjectIdentifierTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.ObjectIdentifier) String value1 = "2.16.840.1.101.3.4.3.1"; //DSA SHA224 + @FieldOrder(order = 1) @RestrictedString(CharacterRestriction.ObjectIdentifier) String value2 = "2.16.840.1.101.3.4.3.2"; //DSA SHA248 + @FieldOrder(order = 2) @RestrictedString(CharacterRestriction.ObjectIdentifier) String value3 = "1.2.840.10045.3.1.7"; //ECC diff --git a/src/net/gcdc/asn1/test/UperEncodeOctetStringTest.java b/src/net/gcdc/asn1/test/UperEncodeOctetStringTest.java index 7604d6a..eb92373 100644 --- a/src/net/gcdc/asn1/test/UperEncodeOctetStringTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeOctetStringTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.datatypesimpl.OctetString; @@ -39,6 +40,7 @@ public class UperEncodeOctetStringTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) OctetString value; public TestRecord() { diff --git a/src/net/gcdc/asn1/test/UperEncodeOptionalSequenceExtensionTest.java b/src/net/gcdc/asn1/test/UperEncodeOptionalSequenceExtensionTest.java index 1b2fa09..4e82a82 100644 --- a/src/net/gcdc/asn1/test/UperEncodeOptionalSequenceExtensionTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeOptionalSequenceExtensionTest.java @@ -6,6 +6,7 @@ import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Optional; import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.HasExtensionMarker; import net.gcdc.asn1.datatypes.IsExtension; import net.gcdc.asn1.datatypes.RestrictedString; @@ -32,9 +33,11 @@ public class UperEncodeOptionalSequenceExtensionTest { @HasExtensionMarker public static class TestRecordExtended { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.IA5String) String value1; + @FieldOrder(order = 1) @IsExtension @RestrictedString(CharacterRestriction.IA5String) @Asn1Optional() String value2; diff --git a/src/net/gcdc/asn1/test/UperEncodeRestrictedIntegerTest.java b/src/net/gcdc/asn1/test/UperEncodeRestrictedIntegerTest.java index 27dc5f4..7463214 100644 --- a/src/net/gcdc/asn1/test/UperEncodeRestrictedIntegerTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeRestrictedIntegerTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.IntRange; import net.gcdc.asn1.datatypes.Sequence; @@ -25,6 +26,7 @@ TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @IntRange(maxValue = 63000, minValue = 33000) Long value; diff --git a/src/net/gcdc/asn1/test/UperEncodeSequenceExtensionTest.java b/src/net/gcdc/asn1/test/UperEncodeSequenceExtensionTest.java index 7934354..8c5131b 100644 --- a/src/net/gcdc/asn1/test/UperEncodeSequenceExtensionTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeSequenceExtensionTest.java @@ -6,6 +6,7 @@ import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Optional; import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.HasExtensionMarker; import net.gcdc.asn1.datatypes.IsExtension; import net.gcdc.asn1.datatypes.RestrictedString; @@ -29,13 +30,16 @@ public class UperEncodeSequenceExtensionTest { @HasExtensionMarker public static class TestRecordExtended { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.IA5String) @Asn1Optional() String value1 = "regular"; + @FieldOrder(order = 1) @IsExtension @RestrictedString(CharacterRestriction.IA5String) @Asn1Optional() String value2 = "extension"; + public TestRecordExtended() { } } diff --git a/src/net/gcdc/asn1/test/UperEncodeSequenceOfIntegerTest.java b/src/net/gcdc/asn1/test/UperEncodeSequenceOfIntegerTest.java index 6028a29..40a3772 100644 --- a/src/net/gcdc/asn1/test/UperEncodeSequenceOfIntegerTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeSequenceOfIntegerTest.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import java.util.logging.Level; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.datatypesimpl.SequenceOfUnrestrictedLong; @@ -27,7 +28,7 @@ TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { @Sequence public static class TestRecord { - + @FieldOrder(order = 0) SequenceOfUnrestrictedLong numbers; public TestRecord() { diff --git a/src/net/gcdc/asn1/test/UperEncodeSequenceOfRestrictedIntegerTest.java b/src/net/gcdc/asn1/test/UperEncodeSequenceOfRestrictedIntegerTest.java index 5ac9834..c1feece 100644 --- a/src/net/gcdc/asn1/test/UperEncodeSequenceOfRestrictedIntegerTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeSequenceOfRestrictedIntegerTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.IntRange; import net.gcdc.asn1.datatypes.Sequence; @@ -25,6 +26,7 @@ TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @IntRange(minValue=9500000,maxValue=99900001) TestSequenceOfLong numbers = null;; diff --git a/src/net/gcdc/asn1/test/UperEncodeSequenceOfStringListTest.java b/src/net/gcdc/asn1/test/UperEncodeSequenceOfStringListTest.java index 1a8f68e..4bc0cb9 100644 --- a/src/net/gcdc/asn1/test/UperEncodeSequenceOfStringListTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeSequenceOfStringListTest.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.logging.Level; import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.RestrictedString; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.uper.UperEncoder; @@ -27,6 +28,7 @@ public class UperEncodeSequenceOfStringListTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.IA5String) ArrayList strings = new ArrayList(); diff --git a/src/net/gcdc/asn1/test/UperEncodeSequenceOfStringTest.java b/src/net/gcdc/asn1/test/UperEncodeSequenceOfStringTest.java index c7c82f7..3ddf93e 100644 --- a/src/net/gcdc/asn1/test/UperEncodeSequenceOfStringTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeSequenceOfStringTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.RestrictedString; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.datatypesimpl.SequenceOfStringIA5; @@ -26,7 +27,7 @@ public class UperEncodeSequenceOfStringTest { @Sequence public static class TestRecord { - + @FieldOrder(order = 0) SequenceOfStringIA5 strings = new SequenceOfStringIA5(); public TestRecord() { diff --git a/src/net/gcdc/asn1/test/UperEncodeSequenceOfUtf8StringTest.java b/src/net/gcdc/asn1/test/UperEncodeSequenceOfUtf8StringTest.java index b2c855d..88d5050 100644 --- a/src/net/gcdc/asn1/test/UperEncodeSequenceOfUtf8StringTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeSequenceOfUtf8StringTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.util.logging.Level; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.datatypesimpl.SequenceOfStringUTF8; import net.gcdc.asn1.uper.UperEncoder; @@ -46,7 +47,7 @@ Encoded successfully in 19 bytes: @Sequence public static class TestRecord { - + @FieldOrder(order = 0) SequenceOfStringUTF8 strings = new SequenceOfStringUTF8(); public TestRecord() { diff --git a/src/net/gcdc/asn1/test/UperEncodeStringDefaultTest.java b/src/net/gcdc/asn1/test/UperEncodeStringDefaultTest.java index e43d76d..5abaa37 100644 --- a/src/net/gcdc/asn1/test/UperEncodeStringDefaultTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeStringDefaultTest.java @@ -7,6 +7,7 @@ import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Default; import net.gcdc.asn1.datatypes.Asn1Optional; import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.RestrictedString; import net.gcdc.asn1.datatypes.Sequence; import net.gcdc.asn1.uper.UperEncoder; @@ -28,9 +29,11 @@ public class UperEncodeStringDefaultTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.UTF8String) @Asn1Optional() String valueUtf8; + @FieldOrder(order = 1) @RestrictedString(CharacterRestriction.IA5String) @Asn1Default(value="testString") String valueIA5; diff --git a/src/net/gcdc/asn1/test/UperEncodeStringLengthTest.java b/src/net/gcdc/asn1/test/UperEncodeStringLengthTest.java index 378bc06..9db6229 100644 --- a/src/net/gcdc/asn1/test/UperEncodeStringLengthTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeStringLengthTest.java @@ -6,6 +6,7 @@ import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Optional; import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.RestrictedString; import net.gcdc.asn1.datatypes.Sequence; @@ -38,6 +39,7 @@ public class UperEncodeStringLengthTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.UTF8String) @Asn1Optional() String valueUtf8; diff --git a/src/net/gcdc/asn1/test/UperEncodeStringTest.java b/src/net/gcdc/asn1/test/UperEncodeStringTest.java index 521075c..29e6c99 100644 --- a/src/net/gcdc/asn1/test/UperEncodeStringTest.java +++ b/src/net/gcdc/asn1/test/UperEncodeStringTest.java @@ -6,6 +6,7 @@ import java.util.logging.Level; import net.gcdc.asn1.datatypes.Asn1Optional; import net.gcdc.asn1.datatypes.CharacterRestriction; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.RestrictedString; import net.gcdc.asn1.datatypes.Sequence; @@ -31,9 +32,11 @@ public class UperEncodeStringTest { @Sequence public static class TestRecord { + @FieldOrder(order = 0) @RestrictedString(CharacterRestriction.UTF8String) @Asn1Optional() String valueUtf8; + @FieldOrder(order = 1) @RestrictedString(CharacterRestriction.IA5String) @Asn1Optional() String valueIA5; @@ -48,29 +51,41 @@ public class UperEncodeStringTest { @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { - TestRecord record = new TestRecord("Müller", "Meier"); + + //Teststring: AêñüC + String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"); + + TestRecord record = new TestRecord(original, "Meier"); byte[] encoded = UperEncoder.encode(record); String hex = UperEncoder.hexStringFromBytes(encoded); UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); - assertEquals("C1D370EF1B1B195C8166E5D39790",hex); + assertEquals("C21070EAB0EC70EF10C166E5D39790",hex); + } @Test public void testEncodeUtf8() throws IllegalArgumentException, IllegalAccessException { - TestRecord record = new TestRecord("你好吗", "Meier"); + + //"你好吗" + String original = new String("\u00e4" + "\u00bd" + "\u00a0" + "\u00e5" + "\u00a5" + "\u00bd" + "\u00e5" + "\u0090" + "\u0097"); + TestRecord record = new TestRecord(original, "Meier"); byte[] encoded = UperEncoder.encode(record); String hex = UperEncoder.hexStringFromBytes(encoded); UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); - assertEquals("C2792F6839696F796425C166E5D39790",hex); + assertEquals("C4B0E930AF70A830E970A970AF70E970A430A5C166E5D39790",hex); } @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { - TestRecord record = new TestRecord("Müller", "Meier"); + + //Teststring: AêñüC + String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"); + + TestRecord record = new TestRecord(original, "Meier"); byte[] encoded = UperEncoder.encode(record); String hex = UperEncoder.hexStringFromBytes(encoded); UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); - assertEquals("C1D370EF1B1B195C8166E5D39790",hex); + assertEquals("C21070EAB0EC70EF10C166E5D39790",hex); TestRecord result = UperEncoder.decode(encoded, TestRecord.class); assertEquals(result.valueUtf8,record.valueUtf8); assertEquals(result.valueIA5,record.valueIA5); @@ -78,11 +93,13 @@ public class UperEncodeStringTest { @Test public void testDecodeUtf8() throws IllegalArgumentException, IllegalAccessException { - TestRecord record = new TestRecord("你好吗", "Meier"); + //"你好吗" + String original = new String("\u00e4" + "\u00bd" + "\u00a0" + "\u00e5" + "\u00a5" + "\u00bd" + "\u00e5" + "\u0090" + "\u0097"); + TestRecord record = new TestRecord(original, "Meier"); byte[] encoded = UperEncoder.encode(record); String hex = UperEncoder.hexStringFromBytes(encoded); UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); - assertEquals("C2792F6839696F796425C166E5D39790",hex); + assertEquals("C4B0E930AF70A830E970A970AF70E970A430A5C166E5D39790",hex); TestRecord result = UperEncoder.decode(encoded, TestRecord.class); assertEquals(result.valueUtf8,record.valueUtf8); assertEquals(result.valueIA5,record.valueIA5); diff --git a/src/net/gcdc/asn1/uper/UperEncoder.java b/src/net/gcdc/asn1/uper/UperEncoder.java index f9c2f2a..30aa82b 100644 --- a/src/net/gcdc/asn1/uper/UperEncoder.java +++ b/src/net/gcdc/asn1/uper/UperEncoder.java @@ -7,6 +7,8 @@ import java.lang.reflect.InvocationTargetException; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,6 +19,7 @@ import logger.LoggerFactory; import net.gcdc.asn1.datatypes.Asn1Default; import net.gcdc.asn1.datatypes.Asn1Optional; +import net.gcdc.asn1.datatypes.FieldOrder; import net.gcdc.asn1.datatypes.HasExtensionMarker; import net.gcdc.asn1.datatypes.IntRange; import net.gcdc.asn1.datatypes.IsExtension; @@ -405,7 +408,31 @@ public final class UperEncoder { Map originalAccess = new HashMap<>(); Asn1ContainerFieldSorter(Class type) { - for (Field f : type.getDeclaredFields()) { + + /* + * + * sorting of the fields added to compensate the error + * in the java SDK on android where getDeclaredFields does + * not return the fields in the order of the class definition + * + */ + List fields = Arrays.asList(type.getDeclaredFields()); + Collections.sort(fields, new Comparator() { + @Override + public int compare(Field o1, Field o2) { + FieldOrder ao1 = o1.getAnnotation(FieldOrder.class); + FieldOrder ao2 = o2.getAnnotation(FieldOrder.class); + int order1 = ao1 == null ? Integer.MAX_VALUE : ao1.order(); + int order2 = ao2 == null ? Integer.MAX_VALUE : ao2.order(); + if (order1 == Integer.MAX_VALUE || order2 == Integer.MAX_VALUE || order1 < 0 || order2 < 0 || order1 == order2) { + logger.debug(String.format("field order error for %s",type.getSimpleName())); + } + return Integer.compare(order1, order2); + } + }); + + + for (Field f : fields) { if (isTestInstrumentation(f) || isNonAsn1Field(f) ) { continue; } -- cgit v1.2.3