### Question:
BCrypt { public static String gensalt(int log_rounds, SecureRandom random) { if (log_rounds < MIN_LOG_ROUNDS || log_rounds > MAX_LOG_ROUNDS) { throw new IllegalArgumentException("Bad number of rounds"); } StringBuilder rs = new StringBuilder(); byte rnd[] = new byte[BCRYPT_SALT_LEN]; random.nextBytes(rnd); rs.append("$2a$"); if (log_rounds < 10) { rs.append("0"); } rs.append(log_rounds); rs.append("$"); encode_base64(rnd, rnd.length, rs); return rs.toString(); }  static String hashpw(String password, String salt); static String gensalt(int log_rounds, SecureRandom random); static String gensalt(int log_rounds); static String gensalt(); static boolean checkpw(String plaintext, String hashed);  }### Answer:
@PrepareForTest({BCrypt.class, SecureRandom.class}) @Test public void testGensalt() throws Exception { PowerMockito.whenNew(SecureRandom.class).withNoArguments() .thenReturn(PowerMockito.mock(SecureRandom.class)); Assert.assertEquals("$2a$10$......................", BCrypt.gensalt()); Assert.assertEquals("$2a$09$......................", BCrypt.gensalt(9)); }
@Test public void testGensaltTooLittleRounds() throws IllegalArgumentException { thrown.expect(IllegalArgumentException.class); BCrypt.gensalt(3); }
@Test public void testGensaltTooManyRounds() throws IllegalArgumentException { thrown.expect(IllegalArgumentException.class); BCrypt.gensalt(32); }