TlsVersionParser { ProtocolVersion parse( final CharSequence buffer, final Tokenizer.Cursor cursor, final BitSet delimiters) throws ParseException { final int lowerBound = cursor.getLowerBound(); final int upperBound = cursor.getUpperBound(); int pos = cursor.getPos(); if (pos + 4 > cursor.getUpperBound()) { throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos); } if (buffer.charAt(pos) != 'T' || buffer.charAt(pos + 1) != 'L' || buffer.charAt(pos + 2) != 'S' || buffer.charAt(pos + 3) != 'v') { throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos); } pos = pos + 4; cursor.updatePos(pos); if (cursor.atEnd()) { throw new ParseException("Invalid TLS version", buffer, lowerBound, upperBound, pos); } final String s = this.tokenizer.parseToken(buffer, cursor, delimiters); final int idx = s.indexOf('.'); if (idx == -1) { final int major; try { major = Integer.parseInt(s); } catch (final NumberFormatException e) { throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos); } return new ProtocolVersion("TLS", major, 0); } else { final String s1 = s.substring(0, idx); final int major; try { major = Integer.parseInt(s1); } catch (final NumberFormatException e) { throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos); } final String s2 = s.substring(idx + 1); final int minor; try { minor = Integer.parseInt(s2); } catch (final NumberFormatException e) { throw new ParseException("Invalid TLS minor version", buffer, lowerBound, upperBound, pos); } return new ProtocolVersion("TLS", major, minor); } } TlsVersionParser();  final static TlsVersionParser INSTANCE; }
@Test public void testParseBasic() throws Exception { Assert.assertThat(impl.parse("TLSv1"), CoreMatchers.equalTo(TLS.V_1_0.version)); Assert.assertThat(impl.parse("TLSv1.1"), CoreMatchers.equalTo(TLS.V_1_1.version)); Assert.assertThat(impl.parse("TLSv1.2"), CoreMatchers.equalTo(TLS.V_1_2.version)); Assert.assertThat(impl.parse("TLSv1.3"), CoreMatchers.equalTo(TLS.V_1_3.version)); Assert.assertThat(impl.parse("TLSv22.356"), CoreMatchers.equalTo(new ProtocolVersion("TLS", 22, 356))); }
@Test public void testParseBuffer() throws Exception { final Tokenizer.Cursor cursor = new Tokenizer.Cursor(1, 13); Assert.assertThat(impl.parse(" TLSv1.2,0000", cursor, Tokenizer.INIT_BITSET(',')), CoreMatchers.equalTo(TLS.V_1_2.version)); Assert.assertThat(cursor.getPos(), CoreMatchers.equalTo(8)); }
@Test(expected = ParseException.class) public void testParseFailure1() throws Exception { impl.parse("Tlsv1"); }
@Test(expected = ParseException.class) public void testParseFailure2() throws Exception { impl.parse("TLSV1"); }
@Test(expected = ParseException.class) public void testParseFailure3() throws Exception { impl.parse("TLSv"); }
@Test(expected = ParseException.class) public void testParseFailure4() throws Exception { impl.parse("TLSv1A"); }
@Test(expected = ParseException.class) public void testParseFailure5() throws Exception { impl.parse("TLSv1.A"); }