@Test public void testTunWriteInvalidIP() throws Exception { boolean thrownexception = false; TunDevice tun = null; log.info("test tunnel write invalid IP"); try { tun = openTun("test"); tun.open(); Assert.assertTrue(tun.getFd() > 0); log.info("device: {} fd: {}", tun.getDevice(), tun.getFd()); int size = 1000; ByteBuffer ipPacket = utils.getBadIpPacket(ICMPPacket.PROTOCOL, size); tun.writeByteBuffer(ipPacket, size); } catch (libc.ErrnoException errno) { // invalid argument log.info("got exeception (OK) : {}", errno.getMessage()); Assert.assertEquals(22, errno.getErrno()); thrownexception = true; } finally { if (tun != null) { tun.close(); Assert.assertEquals(0, tun.getFd()); Assert.assertTrue(thrownexception); } } }
@Test public void testTunWrite() throws Exception { boolean thrownexception = false; TunDevice tun = null; log.info("test tunnel write"); try { tun = openTun("test"); tun.open(); Assert.assertTrue(tun.getFd() > 0); log.info("device: {} fd: {}", tun.getDevice(), tun.getFd()); // we need to write some bytes to tun device ByteBuffer buf = ByteBuffer.allocateDirect(60); // create dummy ICMP packet, buf.clear(); buf.put((byte) 0x45); // IPv4 + header size buf.put((byte) 0x00); // dscp buf.put((byte) 60); // size buf.putShort((byte) 0x00); buf.putShort((byte) 0x00); buf.put((byte) 0x40); // TTL buf.put((byte) 0x01); // protocol int x = tun.writeByteBuffer(buf, 60); Assert.assertEquals(60, x); } finally { if (tun != null) { tun.close(); Assert.assertEquals(0, tun.getFd()); } } }