Package org.apache.harmony.security.asn1

Examples of org.apache.harmony.security.asn1.ASN1Choice


    //
    //

    public void test_ExplicitChoice() throws IOException {

        ASN1Choice choice = new ASN1Choice(new ASN1Type[] { ASN1Boolean
                .getInstance() }) {

            public Object getObjectToEncode(Object obj) {
                return obj;
            }
View Full Code Here


    /**
     * TODO Put method description here
     */
    public void testChoiceOfChoice() throws Exception {

        ASN1Choice choice1 = new ASN1Choice(new ASN1Type[] {
                ASN1Oid.getInstance(), // first
                ASN1Boolean.getInstance(),// second: decoded component
                ASN1Integer.getInstance() // third
                }) {

            public Object getDecodedObject(BerInputStream in)
                    throws IOException {

                assertEquals("choice1", 1, in.choiceIndex);

                return in.content;
            }

            public Object getObjectToEncode(Object obj) {
                return obj;
            }

            public int getIndex(Object obj) {
                return 0;
            }
        };

        ASN1Choice choice2 = new ASN1Choice(new ASN1Type[] { choice1, // first: decoded component
                ASN1BitString.getInstance() // second
                }) {

            public Object getDecodedObject(BerInputStream in)
                    throws IOException {

                assertEquals("choice2", 0, in.choiceIndex);

                return in.content;
            }

            public Object getObjectToEncode(Object obj) {
                return obj;
            }

            public int getIndex(Object obj) {
                return 0;
            }
        };

        Boolean b = (Boolean) choice2.decode(new byte[] { 0x01, 0x01, 0x00 });

        assertTrue(b == Boolean.FALSE);
    }
View Full Code Here

    /**
     * TODO Put method description here
     */
    public void testDistinctTags() throws Exception {

        ASN1Choice choice1 = new ASN1Choice(new ASN1Type[] {
                ASN1Boolean.getInstance(),// component to be checked
                ASN1Oid.getInstance(), ASN1Integer.getInstance() }) {

            public Object getObjectToEncode(Object obj) {
                return obj;
            }

            public int getIndex(Object obj) {
                return 0;
            }
        };

        // two ASN.1 booleans
        try {
            new ASN1Choice(new ASN1Type[] { choice1, //
                    ASN1Boolean.getInstance() // component to be checked
                    }) {

                public Object getObjectToEncode(Object obj) {
                    return obj;
                }

                public int getIndex(Object obj) {
                    return 0;
                }
            };
            fail("No expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
        }

        // ASN.1 ANY
        try {
            new ASN1Choice(new ASN1Type[] { choice1,//
                    ASN1Any.getInstance() // component to be checked
                    }) {

                public Object getObjectToEncode(Object obj) {
                    return obj;
                }

                public int getIndex(Object obj) {
                    return 0;
                }
            };
            fail("No expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
        }

        // two choices
        ASN1Choice choice2 = new ASN1Choice(new ASN1Type[] {
                ASN1BitString.getInstance(), //
                ASN1Boolean.getInstance() //component to be checked
                }) {

            public Object getObjectToEncode(Object obj) {
                return obj;
            }

            public int getIndex(Object obj) {
                return 0;
            }
        };

        try {
            new ASN1Choice(new ASN1Type[] { choice1, choice2 }) {

                public Object getObjectToEncode(Object obj) {
                    return obj;
                }

View Full Code Here

        }
    }

    public void testChoiceInSequenceOf() throws IOException {

        ASN1Choice choice = new ASN1Choice(new ASN1Type[] {
                ASN1Boolean.getInstance(), ASN1Integer.getInstance() }) {

            public int getIndex(Object object) {
                if (object instanceof Boolean) {
                    return 0; // ASN1Boolean
View Full Code Here

    //
    //

    public void test_ExplicitChoice() throws IOException {

        ASN1Choice choice = new ASN1Choice(new ASN1Type[] { ASN1Boolean
                .getInstance() }) {

            public Object getObjectToEncode(Object obj) {
                return obj;
            }
View Full Code Here

    /**
     * TODO Put method description here
     */
    public void testChoiceOfChoice() throws Exception {

        ASN1Choice choice1 = new ASN1Choice(new ASN1Type[] {
                ASN1Oid.getInstance(), // first
                ASN1Boolean.getInstance(),// second: decoded component
                ASN1Integer.getInstance() // third
                }) {

            public Object getDecodedObject(BerInputStream in)
                    throws IOException {

                assertEquals("choice1", 1, in.choiceIndex);

                return in.content;
            }

            public Object getObjectToEncode(Object obj) {
                return obj;
            }

            public int getIndex(Object obj) {
                return 0;
            }
        };

        ASN1Choice choice2 = new ASN1Choice(new ASN1Type[] { choice1, // first: decoded component
                ASN1BitString.getInstance() // second
                }) {

            public Object getDecodedObject(BerInputStream in)
                    throws IOException {

                assertEquals("choice2", 0, in.choiceIndex);

                return in.content;
            }

            public Object getObjectToEncode(Object obj) {
                return obj;
            }

            public int getIndex(Object obj) {
                return 0;
            }
        };

        Boolean b = (Boolean) choice2.decode(new byte[] { 0x01, 0x01, 0x00 });

        assertTrue(b == Boolean.FALSE);
    }
View Full Code Here

    /**
     * TODO Put method description here
     */
    public void testDistinctTags() throws Exception {

        ASN1Choice choice1 = new ASN1Choice(new ASN1Type[] {
                ASN1Boolean.getInstance(),// component to be checked
                ASN1Oid.getInstance(), ASN1Integer.getInstance() }) {

            public Object getObjectToEncode(Object obj) {
                return obj;
            }

            public int getIndex(Object obj) {
                return 0;
            }
        };

        // two ASN.1 booleans
        try {
            new ASN1Choice(new ASN1Type[] { choice1, //
                    ASN1Boolean.getInstance() // component to be checked
                    }) {

                public Object getObjectToEncode(Object obj) {
                    return obj;
                }

                public int getIndex(Object obj) {
                    return 0;
                }
            };
            fail("No expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
        }

        // ASN.1 ANY
        try {
            new ASN1Choice(new ASN1Type[] { choice1,//
                    ASN1Any.getInstance() // component to be checked
                    }) {

                public Object getObjectToEncode(Object obj) {
                    return obj;
                }

                public int getIndex(Object obj) {
                    return 0;
                }
            };
            fail("No expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
        }

        // two choices
        ASN1Choice choice2 = new ASN1Choice(new ASN1Type[] {
                ASN1BitString.getInstance(), //
                ASN1Boolean.getInstance() //component to be checked
                }) {

            public Object getObjectToEncode(Object obj) {
                return obj;
            }

            public int getIndex(Object obj) {
                return 0;
            }
        };

        try {
            new ASN1Choice(new ASN1Type[] { choice1, choice2 }) {

                public Object getObjectToEncode(Object obj) {
                    return obj;
                }

View Full Code Here

                // [UNIVERSAL 5] Boolean
                new Object[] {
                        Boolean.FALSE,
                        new byte[] { 0x25, 0x03, 0x01, 0x01, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL, 5,
                                ASN1Boolean.getInstance()) },

                // [APPLICATION 5] Boolean
                new Object[] {
                        Boolean.FALSE,
                        new byte[] { 0x65, 0x03, 0x01, 0x01, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_APPLICATION, 5,
                                ASN1Boolean.getInstance()) },

                // [CONTEXT-SPECIFIC 5] Boolean
                new Object[] {
                        Boolean.FALSE,
                        new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
                                5, ASN1Boolean.getInstance()) },

                // [5] Boolean (default = CONTEXT-SPECIFIC)
                new Object[] { Boolean.FALSE,
                        new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01, 0x00 },
                        new ASN1Explicit(5, ASN1Boolean.getInstance()) },

                // [PRIVATE 5] Boolean
                new Object[] {
                        Boolean.FALSE,
                        new byte[] { (byte) 0xE5, 0x03, 0x01, 0x01, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_PRIVATE, 5,
                                ASN1Boolean.getInstance()) },

                //
                // Boolean = true
                //

                // [UNIVERSAL 5] Boolean
                new Object[] {
                        Boolean.TRUE,
                        new byte[] { 0x25, 0x03, 0x01, 0x01, (byte) 0xFF },
                        new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL, 5,
                                ASN1Boolean.getInstance()) },

                // [APPLICATION 5] Boolean
                new Object[] {
                        Boolean.TRUE,
                        new byte[] { 0x65, 0x03, 0x01, 0x01, (byte) 0xFF },
                        new ASN1Explicit(ASN1Constants.CLASS_APPLICATION, 5,
                                ASN1Boolean.getInstance()) },

                // [CONTEXT-SPECIFIC 5] Boolean
                new Object[] {
                        Boolean.TRUE,
                        new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01, (byte) 0xFF },
                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
                                5, ASN1Boolean.getInstance()) },

                // [5] Boolean (default = CONTEXT-SPECIFIC)
                new Object[] {
                        Boolean.TRUE,
                        new byte[] { (byte) 0xA5, 0x03, 0x01, 0x01, (byte) 0xFF },
                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
                                5, ASN1Boolean.getInstance()) },

                // [PRIVATE 5] Boolean
                new Object[] {
                        Boolean.TRUE,
                        new byte[] { (byte) 0xE5, 0x03, 0x01, 0x01, (byte) 0xFF },
                        new ASN1Explicit(ASN1Constants.CLASS_PRIVATE, 5,
                                ASN1Boolean.getInstance()) },
                //
                // SequenceOf - testing constructed ASN.1 type
                //

                // [UNIVERSAL 5] SequenceOf
                new Object[] {
                        new ArrayList(),
                        new byte[] { 0x25, 0x02, 0x30, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_UNIVERSAL, 5,
                                sequence) },

                // [APPLICATION 5] SequenceOf
                new Object[] {
                        new ArrayList(),
                        new byte[] { 0x65, 0x02, 0x30, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_APPLICATION, 5,
                                sequence) },

                // [CONTEXT-SPECIFIC 5] SequenceOf
                new Object[] {
                        new ArrayList(),
                        new byte[] { (byte) 0xA5, 0x02, 0x30, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
                                5, sequence) },

                // [5] SequenceOf (default = CONTEXT-SPECIFIC)
                new Object[] {
                        new ArrayList(),
                        new byte[] { (byte) 0xA5, 0x02, 0x30, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_CONTEXTSPECIFIC,
                                5, sequence) },

                // [PRIVATE 5] SequenceOf
                new Object[] {
                        new ArrayList(),
                        new byte[] { (byte) 0xE5, 0x02, 0x30, 0x00 },
                        new ASN1Explicit(ASN1Constants.CLASS_PRIVATE, 5,
                                sequence) } };
    }
View Full Code Here

            public int getIndex(Object obj) {
                return 0;
            }
        };

        ASN1Explicit explicit = new ASN1Explicit(0, choice);

        byte[] encoded = new byte[] { (byte) 0xA0, 0x03, 0x01, 0x01, 0x00 };

        assertEquals("False: ", Boolean.FALSE, explicit.decode(encoded));

        encoded[4] = (byte) 0xFF;

        assertEquals("True: ", Boolean.TRUE, explicit.decode(encoded));
    }
View Full Code Here

     *
     * TYPE1 = [1] IMPLICIT OCTET STRING
     * TYPE2 = [2] IMPLICIT TYPE1
     */
    public void testConsecutiveStringTagging() throws Exception {
        ASN1Implicit type1 = new ASN1Implicit(1, ASN1OctetString.getInstance());

        ASN1Implicit type2 = new ASN1Implicit(2, type1);

        byte[] primitiveEncoding = new byte[] {
        // tag: class(CONTEXT SPECIFIC) + number (2)
                (byte) 0x82,
                // length
                0x03,
                // value
                0x00, 0x01, 0x02 };

        byte[] constructedEncoding = new byte[] {
        // tag: class(CONTEXT SPECIFIC) + constructed +number (2)
                (byte) 0xA2,
                // length
                0x00 };

        byte[] array = new byte[] { 0x00, 0x01, 0x02 };

        // decode primitive
        assertTrue(Arrays.equals(array, (byte[]) type2
                .decode(primitiveEncoding)));

        // encode primitive
        assertTrue(Arrays.equals(primitiveEncoding, type2.encode(array)));

        // decode constructed
        try {
            type2.decode(constructedEncoding);
            fail("No expected ASN1Exception");
        } catch (ASN1Exception e) {
            // FIXME any other check instead of comparing the message???
            assertEquals(
                    "ASN.1 octetstring: constructed identifier at [0]. Not valid for DER.",
View Full Code Here

TOP

Related Classes of org.apache.harmony.security.asn1.ASN1Choice

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.