static void assertHashByteBufferMatchesBytes(HashFunction hashFunction) { Random rng = new Random(0L); byte[] bytes = new byte[rng.nextInt(256) + 1]; rng.nextBytes(bytes); assertEquals(hashFunction.hashBytes(bytes), hashFunction.hashBytes(ByteBuffer.wrap(bytes))); }
static void assertHashByteBufferExhaustsBuffer(HashFunction hashFunction) { Random rng = new Random(0L); byte[] bytes = new byte[rng.nextInt(256) + 1]; rng.nextBytes(bytes); ByteBuffer buffer = ByteBuffer.wrap(bytes); HashCode unused = hashFunction.hashBytes(buffer); assertFalse(buffer.hasRemaining()); }
@Test public void testInsertLobAsLastValue() { doInHibernate( this::sessionFactory, session -> { byte[] details = new byte[4000]; byte[] title = new byte[2000]; Random random = new Random(); random.nextBytes( details ); random.nextBytes( title ); // This insert will fail on Oracle without the fix to ModelBinder flagging SimpleValue and Property as Lob // because the fields will not be placed at the end of the insert, resulting in an Oracle failure. final LobAsLastValueEntity entity = new LobAsLastValueEntity( "Test", new String( details ), new String( title ) ); session.save( entity ); } ); }
@Test public void testPeekLength() throws IOException { HLLCounterOld hllc = new HLLCounterOld(10); HLLCounterOld copy = new HLLCounterOld(10); byte[] value = new byte[10]; for (int i = 0; i < 200000; i++) { rand1.nextBytes(value); hllc.add(value); buf.clear(); hllc.writeRegisters(buf); int len = buf.position(); buf.position(0); assertEquals(len, hllc.peekLength(buf)); copy.readRegisters(buf); assertEquals(len, buf.position()); assertEquals(hllc, copy); } buf.clear(); }
private static void assertHashBytesEquivalence(HashFunction hashFunction, Random random) { int size = random.nextInt(2048); byte[] bytes = new byte[size]; random.nextBytes(bytes); assertEquals( hashFunction.hashBytes(bytes), hashFunction.newHasher(size).putBytes(bytes).hash()); int off = random.nextInt(size); int len = random.nextInt(size - off); assertEquals( hashFunction.hashBytes(bytes, off, len), hashFunction.newHasher(size).putBytes(bytes, off, len).hash()); }
@Test public void testEncodeDecodeSmall() { for (int i = 0; i < 12; i++) { final byte[] data = new byte[i]; this.getRandom().nextBytes(data); final byte[] enc = Base64.encodeBase64(data); assertTrue("\"" + new String(enc) + "\" is Base64 data.", Base64.isBase64(enc)); final byte[] data2 = Base64.decodeBase64(enc); assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data, data2)); } }
@Override public void run() { byte[] value = new byte[100]; Put[] in = new Put[1]; // iterate for the specified number of operations for (int i=0; i<numOps; i++) { // generate random bytes rand.nextBytes(value); // put the randombytes and verify that we can read it. This is one // way of ensuring that rwcc manipulation in HRegion.put() is fine. Put put = new Put(rowkey); put.addColumn(fam1, qual1, value); in[0] = put; try { OperationStatus[] ret = region.batchMutate(in); assertEquals(1, ret.length); assertEquals(OperationStatusCode.SUCCESS, ret[0].getOperationStatusCode()); assertGet(this.region, rowkey, fam1, qual1, value); } catch (IOException e) { assertTrue("Thread id " + threadNumber + " operation " + i + " failed.", false); } } } }
private byte[] getRandomByteArray(int numWords) { Assert.assertTrue(numWords >= 0); final int lengthInBytes = numWords * 8; final byte[] bytes = new byte[lengthInBytes]; rand.nextBytes(bytes); return bytes; }
static void assertHashByteBufferPreservesByteOrder(HashFunction hashFunction) { Random rng = new Random(0L); byte[] bytes = new byte[rng.nextInt(256) + 1]; rng.nextBytes(bytes); ByteBuffer littleEndian = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); ByteBuffer bigEndian = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); assertEquals(hashFunction.hashBytes(littleEndian), hashFunction.hashBytes(littleEndian)); assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.order()); assertEquals(ByteOrder.BIG_ENDIAN, littleEndian.order()); }
@Test public void testPeekLength() throws IOException { HLLCounter hllc = new HLLCounter(10); HLLCounter copy = new HLLCounter(10); byte[] value = new byte[10]; for (int i = 0; i < 200000; i++) { rand1.nextBytes(value); hllc.add(value); buf.clear(); hllc.writeRegisters(buf); int len = buf.position(); buf.position(0); assertEquals(len, hllc.peekLength(buf)); copy.readRegisters(buf); assertEquals(len, buf.position()); assertEquals(hllc, copy); } buf.clear(); }
private void createFiles(FileSystem fs, Path parentDir, int numOfFiles) throws IOException { Random random = new Random(); for (int i = 0; i < numOfFiles; i++) { int xMega = 1 + random.nextInt(3); // size of each file is between 1~3M try (FSDataOutputStream fsdos = fs.create(new Path(parentDir, "file-" + i))) { for (int m = 0; m < xMega; m++) { byte[] M = new byte[1024 * 1024]; random.nextBytes(M); fsdos.write(M); } } } }
private static void assertHashByteBufferEquivalence(HashFunction hashFunction, Random random) { int size = random.nextInt(2048); byte[] bytes = new byte[size]; random.nextBytes(bytes); assertEquals( hashFunction.hashBytes(ByteBuffer.wrap(bytes)), hashFunction.newHasher(size).putBytes(ByteBuffer.wrap(bytes)).hash()); int off = random.nextInt(size); int len = random.nextInt(size - off); assertEquals( hashFunction.hashBytes(ByteBuffer.wrap(bytes, off, len)), hashFunction.newHasher(size).putBytes(ByteBuffer.wrap(bytes, off, len)).hash()); }
@Test public void authentication_with_sha1_with_correct_password_should_work() { String password = randomAlphanumeric(60); byte[] saltRandom = new byte[20]; RANDOM.nextBytes(saltRandom); String salt = DigestUtils.sha1Hex(saltRandom); UserDto user = newUserDto() .setHashMethod(SHA1.name()) .setCryptedPassword(DigestUtils.sha1Hex("--" + salt + "--" + password + "--")) .setSalt(salt); underTest.authenticate(db.getSession(), user, password, AuthenticationEvent.Method.BASIC); }
@Override void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) { byte[] value = new byte[random.nextInt(128)]; random.nextBytes(value); int off = random.nextInt(value.length + 1); int len = random.nextInt(value.length - off + 1); for (PrimitiveSink sink : sinks) { sink.putBytes(value, off, len); } } },
private byte[] getRandomByteArray(int numWords) { Assert.assertTrue(numWords >= 0); final int lengthInBytes = numWords * 8; final byte[] bytes = new byte[lengthInBytes]; rand.nextBytes(bytes); return bytes; }