Package org.apache.harmony.security.asn1

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


                }

            }
        };

        BerInputStream berIn = new BerInputStream(in);
        berIn.readContent();

        assertTrue(Arrays.equals(encoding, berIn.getEncoded()));

        //
        // negative test case: the stream returns only 4 bytes of content
        //
        in = new ByteArrayInputStream(encoding) {

            int i = 0;

            public int read(byte[] b, int off, int len) {
                if (i == 0) {
                    i++;
                    return super.read(b, off, 4);
                } else {
                    return 0;
                }

            }
        };
        berIn = new BerInputStream(in);
        try {
            berIn.readContent();
            fail("No expected ASN1Exception");
        } catch (ASN1Exception e) {
        }
    }
View Full Code Here


                        BigInteger.valueOf(0xFFFFFF) }, };

        // positive testcases
        for (int i = 0; i < testcase.length; i++) {
            try {
                BerInputStream in = new BerInputStream(
                        new ByteArrayInputStream((byte[]) testcase[i][0]));

                int expected = ((BigInteger) testcase[i][1]).intValue();

                assertEquals(expected, in.getLength());
            } catch (IOException e) {
                e.printStackTrace();
                fail("Testcase: " + i + "\nUnexpected exception." + e);
            }
        }

        // negative testcase
        try {
            new BerInputStream(new ByteArrayInputStream(new byte[] { 0x30,
                    (byte) 0x84, 0x01, 0x01, 0x01, 0x01 }));
            fail("No expected ASN1Exception");
        } catch (ASN1Exception e) {
            assertTrue(e.getMessage().startsWith("Too long"));
        }

        //
        // Test for correct internal array reallocation
        // Regression for HARMONY-5054
        //

        // must be greater then buffer initial size (16K)
        int arrayLength = 17000;

        // 1 byte for tag and 3 for length
        byte[] encoding = new byte[arrayLength + 4];

        // fill tag and length bytes
        encoding[0] = ASN1Constants.TAG_OCTETSTRING;
        encoding[1] = (byte) 0x82; // length is encoded in two bytes
        encoding[2] = (byte) (arrayLength >> 8);
        encoding[3] = (byte) (arrayLength & 0xFF);

        BerInputStream in = new BerInputStream(new ByteArrayInputStream(
                encoding));
        assertEquals(encoding.length, in.getBuffer().length);
    }
View Full Code Here

                0x06, 0x02, 0x01, 0x03, // oid bytes
                0x01, 0x00 // just random bytes
        };

        // pass boolean encoding
        BerInputStream in = new BerInputStream(encoded, 0, 3);
        assertEquals("boolean", 1, in.getLength());

        // pass oid encoding
        in = new BerInputStream(encoded, 3, 4);
        assertEquals("boolean", 2, in.getLength());

        // pass random encoding (equals to ANY)
        in = new BerInputStream(encoded, 7, 2);
        assertEquals("any", 0, in.getLength());

        // extra bytes for oid
        try {
            new BerInputStream(encoded, 3, 5);
            fail("No expected ASN1Exception");
        } catch (ASN1Exception e) {
            assertEquals("Wrong content length", e.getMessage());
        }

        // less bytes for oid
        try {
            new BerInputStream(encoded, 3, 3);
            fail("No expected ASN1Exception");
        } catch (ASN1Exception e) {
            assertEquals("Wrong content length", e.getMessage());
        }
    }
View Full Code Here

        // decode from byte array
        for (int i = 0; i < validBitstring.length; i++) {
            DerInputStream in = new DerInputStream(
                    (byte[]) validBitstring[i][1]);

            BitString expected = (BitString) validBitstring[i][0];
            BitString decoded = (BitString) asn1.decode(in);

            assertEquals("Testcase: " + i, expected.unusedBits,
                    decoded.unusedBits);

            assertTrue("Testcase: " + i, Arrays.equals(expected.bytes,
                    decoded.bytes));
        }

        // decode from input stream
        for (int i = 0; i < validBitstring.length; i++) {
            DerInputStream in = new DerInputStream(new ByteArrayInputStream(
                    (byte[]) validBitstring[i][1]));

            BitString expected = (BitString) validBitstring[i][0];
            BitString decoded = (BitString) asn1.decode(in);

            assertEquals("Testcase: " + i, expected.unusedBits,
                    decoded.unusedBits);

            assertTrue("Testcase: " + i, Arrays.equals(expected.bytes,
View Full Code Here

        throws IOException, ParseException {
        // full fractional seconds
        Date myDate = new Date(1101980374187L);
        byte[] encoded =
            new DerOutputStream(gTime, myDate).encoded;
        DerInputStream dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("full", myDate, gTime.decode(dis));

        // 2 digit fractional seconds (last 0 must be trimmed out)
        myDate = new Date(1101980374180L);
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("2 fraction", myDate, gTime.decode(dis));

        // 1 digit fractional seconds (last 2 0s must be trimmed out)
        myDate = new Date(1101980374100L);
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("1 fraction", myDate, gTime.decode(dis));

        // no fractional seconds (last 3 0s and "." must be trimmed out)
        myDate = new Date(1101980374000L);
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("no fraction", myDate, gTime.decode(dis));

        // midnight
        myDate = new SimpleDateFormat("MM.dd.yyyy HH:mm").
            parse("06.06.2004 00:00");
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("midnight", myDate, gTime.decode(dis));

        // date 100 ms
        myDate = new Date(100L);
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("100ms", myDate, gTime.decode(dis));
    }
View Full Code Here

    private final void runTest(boolean useInputStream)
        throws IOException, ParseException {
        Date myDate = new Date(1101980374187L);
        byte[] encoded =
            new DerOutputStream(uTime, myDate).encoded;
        DerInputStream dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        // the difference only fractional-seconds
        assertEquals(187, (myDate.getTime()-((Date)uTime.decode(dis)).getTime()));

        // midnight
        myDate = new SimpleDateFormat("MM.dd.yyyy HH:mm").
            parse("06.06.2004 00:00");
        encoded =
            new DerOutputStream(uTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals(myDate, uTime.decode(dis));
    }
View Full Code Here

        throws IOException, ParseException {
        // full fractional seconds
        Date myDate = new Date(1101980374187L);
        byte[] encoded =
            new DerOutputStream(gTime, myDate).encoded;
        DerInputStream dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("full", myDate, gTime.decode(dis));

        // 2 digit fractional seconds (last 0 must be trimmed out)
        myDate = new Date(1101980374180L);
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("2 fraction", myDate, gTime.decode(dis));

        // 1 digit fractional seconds (last 2 0s must be trimmed out)
        myDate = new Date(1101980374100L);
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("1 fraction", myDate, gTime.decode(dis));

        // no fractional seconds (last 3 0s and "." must be trimmed out)
        myDate = new Date(1101980374000L);
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("no fraction", myDate, gTime.decode(dis));

        // midnight
        myDate = new SimpleDateFormat("MM.dd.yyyy HH:mm").
            parse("06.06.2004 00:00");
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("midnight", myDate, gTime.decode(dis));

        // date 100 ms
        myDate = new Date(100L);
        encoded =
            new DerOutputStream(gTime, myDate).encoded;
        dis = useInputStream
        ? new DerInputStream(new ByteArrayInputStream(encoded))
        : new DerInputStream(encoded);
        assertEquals("100ms", myDate, gTime.decode(dis));
    }
View Full Code Here

        this.hexString = hexString;
        this.encoded = encoded;

        try {
            DerInputStream in = new DerInputStream(encoded);

            tag = in.tag;

            if (DirectoryString.ASN1.checkTag(tag)) {
                // has string representation
View Full Code Here

                socket.receive(resp);

                bytesRead = resp.getLength();
                out.write(buf, resp.getOffset(), bytesRead);
            }
            DerInputStream in = new DerInputStream(out.toByteArray());

            if (in.tag == KDCReply.AS_REP_ASN1.constrId) { //TODO AS reply
                throw new RuntimeException();//FIXME
            } else if (in.tag == KerberosErrorMessage.ASN1.constrId) {
                KerberosErrorMessage errMsg = KerberosErrorMessage.decode(in);
View Full Code Here

        this.hexString = hexString;
        this.encoded = encoded;

        try {
            DerInputStream in = new DerInputStream(encoded);

            tag = in.tag;

            if (DirectoryString.ASN1.checkTag(tag)) {
                // has string representation
View Full Code Here

TOP

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

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.