use of javax.crypto.CipherInputStream 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 + ".");
}
}
use of javax.crypto.CipherInputStream 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.");
}
}
use of javax.crypto.CipherInputStream in project robovm by robovm.
the class CipherInputStream1Test method testCipherInputStream.
/**
* CipherInputStream(InputStream is) method testing. Tests that
* CipherInputStream uses NullCipher if Cipher is not specified
* in the constructor.
*/
public void testCipherInputStream() 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) {
};
for (int i = 0; i < data.length; i++) {
if ((byte) cis.read() != data[i]) {
fail("NullCipher should be used " + "if Cipher is not specified.");
}
}
if (cis.read() != -1) {
fail("NullCipher should be used if Cipher is not specified.");
}
}
use of javax.crypto.CipherInputStream in project robovm by robovm.
the class CipherInputStreamTest method testSkip.
public void testSkip() throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream in = new CipherInputStream(new ByteArrayInputStream(cipherText), cipher);
assertTrue(in.skip(5) > 0);
}
use of javax.crypto.CipherInputStream in project robovm by robovm.
the class CipherInputStreamTest method testCipherInputStream_TruncatedInput_Failure.
public void testCipherInputStream_TruncatedInput_Failure() throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(new byte[16], "AES"), new IvParameterSpec(new byte[16]));
InputStream is = new CipherInputStream(new ByteArrayInputStream(new byte[31]), cipher);
is.read(new byte[4]);
is.close();
}
Aggregations