|
| 1 | +package io.vertx.pgclient.data; |
| 2 | + |
| 3 | +import io.vertx.ext.unit.TestContext; |
| 4 | +import io.vertx.pgclient.PgConnection; |
| 5 | +import io.vertx.sqlclient.*; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.net.Inet4Address; |
| 9 | +import java.net.Inet6Address; |
| 10 | +import java.net.InetAddress; |
| 11 | +import java.util.function.BiFunction; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertThrows; |
| 15 | + |
| 16 | +public class CidrCodecTest extends DataTypeTestBase{ |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testValidIPv4() throws Exception { |
| 20 | + InetAddress address = InetAddress.getByName("192.168.1.1"); |
| 21 | + Cidr cidr = new Cidr(); |
| 22 | + cidr.setAddress(address); |
| 23 | + cidr.setNetmask(24); |
| 24 | + assertEquals(address, cidr.getAddress()); |
| 25 | + assertEquals(Integer.valueOf(24), cidr.getNetmask()); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testValidIPv6() throws Exception { |
| 30 | + InetAddress address = InetAddress.getByName("fe80::f03c:91ff:feae:e944"); |
| 31 | + Cidr cidr = new Cidr(); |
| 32 | + cidr.setAddress(address); |
| 33 | + cidr.setNetmask(64); |
| 34 | + assertEquals(address, cidr.getAddress()); |
| 35 | + assertEquals(Integer.valueOf(64), cidr.getNetmask()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testInvalidNetmaskIPv4() throws Exception { |
| 40 | + InetAddress address = InetAddress.getByName("192.168.1.1"); |
| 41 | + Cidr cidr = new Cidr(); |
| 42 | + cidr.setAddress(address); |
| 43 | + assertThrows(IllegalArgumentException.class, () -> cidr.setNetmask(33)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testInvalidNetmaskIPv6() throws Exception { |
| 48 | + InetAddress address = InetAddress.getByName("fe80::f03c:91ff:feae:e944"); |
| 49 | + Cidr cidr = new Cidr(); |
| 50 | + cidr.setAddress(address); |
| 51 | + assertThrows(IllegalArgumentException.class, () -> cidr.setNetmask(129)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testBinaryDecodeCIDR(TestContext ctx) throws Exception { |
| 56 | + testDecodeCIDR(ctx, SqlClient::preparedQuery); |
| 57 | + } |
| 58 | + |
| 59 | + private void testDecodeCIDR(TestContext ctx, BiFunction<SqlClient, String, Query<RowSet<Row>>> a) throws Exception { |
| 60 | + InetAddress addr1 = Inet4Address.getByName("128.0.0.0"); |
| 61 | + InetAddress addr2 = Inet6Address.getByName("2001:0db8:1234:0000:0000:0000:0000:0000"); |
| 62 | + PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(conn -> { |
| 63 | + a.apply(conn, "SELECT " + |
| 64 | + "'128.0.0.0'::CIDR," + |
| 65 | + "'128.0.0.0/4'::CIDR," + |
| 66 | + "'2001:0db8:1234:0000:0000:0000:0000:0000'::CIDR," + |
| 67 | + "'2001:0db8:1234:0000:0000:0000:0000:0000/56'::CIDR") |
| 68 | + .execute() |
| 69 | + .onComplete(ctx.asyncAssertSuccess(rows -> { |
| 70 | + ctx.assertEquals(1, rows.size()); |
| 71 | + Row row = rows.iterator().next(); |
| 72 | + Cidr v1 = (Cidr) row.getValue(0); |
| 73 | + Cidr v2 = (Cidr) row.getValue(1); |
| 74 | + Cidr v3 = (Cidr) row.getValue(2); |
| 75 | + Cidr v4 = (Cidr) row.getValue(3); |
| 76 | + ctx.assertEquals(addr1, v1.getAddress()); |
| 77 | + ctx.assertEquals(32,v1.getNetmask()); |
| 78 | + ctx.assertEquals(addr1, v2.getAddress()); |
| 79 | + ctx.assertEquals(4, v2.getNetmask()); |
| 80 | + ctx.assertEquals(addr2, v3.getAddress()); |
| 81 | + ctx.assertEquals(128, v3.getNetmask()); |
| 82 | + ctx.assertEquals(addr2, v4.getAddress()); |
| 83 | + ctx.assertEquals(56, v4.getNetmask()); |
| 84 | + })); |
| 85 | + })); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testBinaryEncodeCIDR(TestContext ctx) throws Exception { |
| 90 | + InetAddress addr1 = Inet4Address.getByName("128.0.0.0"); |
| 91 | + InetAddress addr2 = Inet6Address.getByName("2001:0db8:1234:0000:0000:0000:0000:0000"); |
| 92 | + PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(conn -> { |
| 93 | + conn |
| 94 | + .preparedQuery("SELECT ($1::CIDR)::VARCHAR, ($2::CIDR)::VARCHAR, ($3::CIDR)::VARCHAR, ($4::CIDR)::VARCHAR") |
| 95 | + .execute(Tuple.of( |
| 96 | + new Cidr().setAddress(addr1), |
| 97 | + new Cidr().setAddress(addr1).setNetmask(4), |
| 98 | + new Cidr().setAddress(addr2), |
| 99 | + new Cidr().setAddress(addr2).setNetmask(56) |
| 100 | + )) |
| 101 | + .onComplete(ctx.asyncAssertSuccess(rows -> { |
| 102 | + ctx.assertEquals(1, rows.size()); |
| 103 | + Row row = rows.iterator().next(); |
| 104 | + String v1 = row.getString(0); |
| 105 | + String v2 = row.getString(1); |
| 106 | + String v3 = row.getString(2); |
| 107 | + String v4 = row.getString(3); |
| 108 | + ctx.assertEquals("128.0.0.0/32", v1); |
| 109 | + ctx.assertEquals("128.0.0.0/4", v2); |
| 110 | + ctx.assertEquals("2001:db8:1234::/128", v3); |
| 111 | + ctx.assertEquals("2001:db8:1234::/56", v4); |
| 112 | + })); |
| 113 | + })); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments