Search in sources :

Example 1 with NullCipher

use of javax.crypto.NullCipher in project robovm by robovm.

the class CipherInputStreamTest method testReadBII.

/**
     * javax.crypto.CipherInputStream#read(byte[] b, int off, int len)
     */
public void testReadBII() throws Exception {
    // Regression for HARMONY-1080
    CipherInputStream stream = new CipherInputStream(null, new NullCipher());
    try {
        stream.read(new byte[1], 1, 0);
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 2 with NullCipher

use of javax.crypto.NullCipher in project robovm by robovm.

the class CipherOutputStream1Test method testWrite3.

/**
     * write(byte[] b, int off, int len) method testing.
     */
public void testWrite3() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestOutputStream tos = new TestOutputStream();
    CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
    for (int i = 0; i < data.length; i++) {
        cos.write(data, i, 1);
    }
    cos.flush();
    byte[] result = tos.toByteArray();
    if (!Arrays.equals(result, data)) {
        fail("CipherOutputStream wrote incorrect data.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) NullCipher(javax.crypto.NullCipher)

Example 3 with NullCipher

use of javax.crypto.NullCipher in project robovm by robovm.

the class CipherInputStream1Test method testSkip.

/**
     * skip(long n) method testing. Tests that the method correctly skips the
     * bytes.
     */
public void testSkip() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestInputStream tis = new TestInputStream(data);
    CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
    int expected = data.length;
    byte[] result = new byte[expected];
    int skipped = (int) cis.skip(2);
    int ind = skipped;
    // the number of got bytes
    int got = skipped + cis.read(result, 0, 1);
    while (true) {
        for (int j = 0; j < got - ind; j++) {
            if (result[j] != data[ind + j]) {
                fail("read(byte[] b, int off, int len) " + "returned incorrect data: Expected " + data[ind + j] + ", got: " + result[j]);
            }
        }
        if (got == expected) {
            break;
        } else if (got > expected) {
            fail("The data returned by " + "read(byte[] b, int off, int len) " + "is larger than expected.");
        } else {
            ind = got;
            got += cis.read(result, 0, 1);
        }
    }
    if ((got = cis.read(result, 0, 1)) != -1) {
        fail("read() should return -1 at the end of the stream. " + "Output is: " + got + ".");
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 4 with NullCipher

use of javax.crypto.NullCipher in project robovm by robovm.

the class CipherInputStream1Test method testRead3.

/**
     * read(byte[] b, int off, int len) method testing. Tests that method
     * returns the correct value (related to the InputStream), that it discards
     * bytes in the case of null buffer, and that it returns -1 at the end of
     * stream.
     */
public void testRead3() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestInputStream tis = new TestInputStream(data);
    CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
    int expected = data.length;
    byte[] result = new byte[expected];
    int skip = 2;
    // index into the data array (to check the got data)
    int ind = skip;
    // should read and discard bytes;
    cis.read(null, 0, skip);
    // the number of got bytes
    int got = skip + cis.read(result, 0, 1);
    while (true) {
        for (int j = 0; j < got - ind; j++) {
            assertEquals("read(byte[] b, int off, int len) " + "returned incorrect data.", result[j], data[ind + j]);
        }
        if (got == expected) {
            break;
        } else if (got > expected) {
            fail("The data returned by " + "read(byte[] b, int off, int len) " + "is larger than expected.");
        } else {
            ind = got;
            got += cis.read(result, 0, 3);
        }
    }
    if (cis.read(result, 0, 1) != -1) {
        fail("read() should return -1 at the end of the stream.");
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 5 with NullCipher

use of javax.crypto.NullCipher in project robovm by robovm.

the class SealedObjectTest method testSealedObject2.

/**
     * SealedObject(SealedObject so) method testing. Tests if the
     * NullPointerException is thrown in the case of null SealedObject.
     */
public void testSealedObject2() throws Exception {
    try {
        new SealedObject(null) {
        };
        fail("NullPointerException should be thrown in the case " + "of null SealedObject.");
    } catch (NullPointerException e) {
    }
    String secret = "secret string";
    Cipher cipher = new NullCipher();
    SealedObject so1 = new SealedObject(secret, cipher);
    SealedObject so2 = new SealedObject(so1) {
    };
    assertEquals("The secret content of the object should equals " + "to the secret content of initial object.", secret, so2.getObject(cipher));
    assertEquals("The algorithm which was used to seal the object " + "should be the same as the algorithm used to seal the " + "initial object", so1.getAlgorithm(), so2.getAlgorithm());
}
Also used : NullCipher(javax.crypto.NullCipher) SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher)

Aggregations

NullCipher (javax.crypto.NullCipher)25 CipherInputStream (javax.crypto.CipherInputStream)9 Cipher (javax.crypto.Cipher)6 SealedObject (javax.crypto.SealedObject)5 CipherOutputStream (javax.crypto.CipherOutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Key (java.security.Key)2 KeyGenerator (javax.crypto.KeyGenerator)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 FilterInputStream (java.io.FilterInputStream)1 InputStream (java.io.InputStream)1 LineNumberInputStream (java.io.LineNumberInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 PushbackInputStream (java.io.PushbackInputStream)1 DigestInputStream (java.security.DigestInputStream)1