/** * Generates new CSRF token and puts it in the session. Returns generated token value. */ @SuppressWarnings({"unchecked"}) public static String prepareCsrfToken(final HttpSession session, final int timeToLive) { Set<Token> tokenSet = (Set<Token>) session.getAttribute(CSRF_TOKEN_SET); if (tokenSet == null) { tokenSet = new HashSet<>(); session.setAttribute(CSRF_TOKEN_SET, tokenSet); } String value; boolean unique; do { value = RandomString.get().randomAlphaNumeric(32); assureSize(tokenSet); unique = tokenSet.add(new Token(value, timeToLive)); } while (!unique); return value; }
@Test void testNaturalComparatorContract() { NaturalOrderComparator<String> comparator = new NaturalOrderComparator<>(); int loop = 1000; while(loop-- > 0) { String s1 = RandomString.get().randomAscii(2); String s2 = RandomString.get().randomAscii(5); String s3 = RandomString.get().randomAscii(4); assertReflexivity(comparator, s1, s2); assertTransitivity(comparator, s1, s2, s3); } }
@Test void testNaturalComparatorContract2() { NaturalOrderComparator<String> comparator = new NaturalOrderComparator<>(); int loop = 1000; while(loop-- > 0) { String s1 = RandomString.get().randomNumeric(2); String s2 = RandomString.get().randomNumeric(5); String s3 = RandomString.get().randomNumeric(4); assertReflexivity(comparator, s1, s2); assertTransitivity(comparator, s1, s2, s3); } }
@Test void testReadString_with_unknown_path() throws Exception { assertThrows(IOException.class, () -> { PathUtil.readString(new File(BASE_DIR, RandomString.get().randomAlpha(8)).toPath()); }); }
@Test void testRandom() { int total = 100000; ArrayList<String> randomList = new ArrayList<>(); for (int i = 0; i < total; i++) { randomList.add(RandomString.get().random(20, 'a', 'z')); } SortedArrayList<String> sortedList = new SortedArrayList<>(randomList); Collections.sort(randomList); for (int i = 0; i < total; i++) { assertEquals(randomList.get(i), sortedList.get(i)); } }
@Test void testRandomAppends() { StringBuilder sb = new StringBuilder(10); FastCharBuffer fcb = new FastCharBuffer(10); Random rnd = new Random(); int loop = 100; while (loop-- > 0) { String s = RandomString.get().randomAlphaNumeric(rnd.nextInt(20)); sb.append(s); fcb.append(s); } assertEquals(sb.toString(), fcb.toString()); }
@Test void testBCryptRandom() { for (int rounds = 0; rounds < 1000; rounds++) { String text = RandomString.get().randomAlphaNumeric(10); String hash = BCrypt.hashpw(text, BCrypt.gensalt(4)); assertTrue(BCrypt.checkpw(text, hash)); } }
@Test void testLoop() { long reps = 10000; while (reps-- > 0) { String s = RandomString.get().randomAscii(MathUtil.randomInt(1, 1024)); byte[] encrypted = threefish.encryptString(s); String s2 = threefish.decryptString(encrypted); assertEquals(s, s2); } } }
@Test void testString() { JsonParsers.forEachParser(jsonParser -> { assertEquals("123", jsonParser.parse("\"" + "123" + "\"")); assertEquals("12\n3", jsonParser.parse("\"" + "12\\n3" + "\"")); String big = RandomString.get().randomAlpha(510); String jbig = big + "\\n"; String rbig = big + "\n"; assertEquals(512, jbig.length()); assertEquals(rbig, jsonParser.parse("\"" + jbig + "\"")); jbig += "x"; rbig += "x"; assertEquals(rbig, jsonParser.parse("\"" + jbig + "\"")); jbig = "12" + jbig; rbig = "12" + rbig; assertEquals(rbig, jsonParser.parse("\"" + jbig + "\"")); }); }
String boundary = StringUtil.repeat('-', 10) + RandomString.get().randomAlphaNumeric(10);
String boundary = StringUtil.repeat('-', 10) + RandomString.get().randomAlphaNumeric(10);