Compare commits

..

11 Commits

Author SHA1 Message Date
ed2b34add0 Fix net.i2p.util.Addresses::getIP empty string
The standard library behavior is to return the localhost when null or an empty string is passed.
getIP seeked to override that behavior, but didn't treat the empty string case.
2021-01-25 18:32:19 +01:00
8f931f3d16 Add @since 0.9.49 to net.i2p.util.Addresses 2021-01-25 18:27:31 +01:00
453dd4076f test net.i2p.util.Addresses::getPort 2021-01-21 21:34:59 +01:00
0263ba23b0 test net.i2p.util.Addresses::toString 2021-01-21 00:08:26 +01:00
61ffbce1c1 test net.i2p.util.Addresses::isDeprecated 2021-01-20 23:19:59 +01:00
78a09c27f7 test net.i2p.util.Addresses::isDynamic 2021-01-20 23:19:59 +01:00
30f3e8c6ed test net.i2p.util.Addresses::isIP{v6,v4,}Address 2021-01-20 23:19:59 +01:00
23352c2d76 test net.i2p.util.Addresses::getIP with IP string 2021-01-20 23:15:50 +01:00
6b3bf4cb78 test net.i2p.util.Addresses::getIP with empty string 2021-01-20 23:15:50 +01:00
b37c718785 test net.i2p.util.Addresses::getIP with null input 2021-01-20 23:15:49 +01:00
3276cfca82 Correct @return of Addresses.getAddresses 2021-01-20 19:44:44 +01:00
6 changed files with 216 additions and 111 deletions

View File

@@ -1,31 +0,0 @@
.idea
.git
Dockerfile
# Gradle
.gradle
build
apps/BOB/build
apps/addressbook/build
apps/desktopgui/build
apps/i2pcontrol/build
apps/i2psnark/build
apps/i2ptunnel/build
apps/imagegen/build
apps/jetty/build
apps/jrobin/build
apps/ministreaming/java/build
apps/ministreaming/build
apps/routerconsole/build
apps/sam/build
apps/streaming/build
apps/susidns/build
apps/susimail/build
apps/systray/build
core/java/build
core/build
installer/build
router/java/build
router/build

View File

@@ -22,45 +22,3 @@ test:
only:
- merge_requests
- tags
# Make sure we can build a docker image
# It's cached for later jobs
build_docker:
stage: test
image: docker:19.03.12
services:
- docker:19.03.12-dind
script:
# Try to load latest branch image from local tar or from registry
- docker load -i ci-exports/$CI_COMMIT_REF_SLUG.tar || docker pull $CI_REGISTRY_IMAGE:latest || true
- docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:latest .
- mkdir -p ci-exports/
- docker save $CI_REGISTRY_IMAGE:latest > ci-exports/$CI_COMMIT_REF_SLUG.tar
variables:
# When using dind service, we need to instruct docker to talk with
# the daemon started inside of the service. The daemon is available
# with a network connection instead of the default
# /var/run/docker.sock socket. Docker 19.03 does this automatically
# by setting the DOCKER_HOST in
# https://github.com/docker-library/docker/blob/d45051476babc297257df490d22cbd806f1b11e4/19.03/docker-entrypoint.sh#L23-L29
#
# The 'docker' hostname is the alias of the service container as described at
# https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services.
#
# Specify to Docker where to create the certificates, Docker will
# create them automatically on boot, and will create
# `/certs/client` that will be shared between the service and job
# container, thanks to volume mount from config.toml
DOCKER_TLS_CERTDIR: "/certs"
# Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
DOCKER_HOST: tcp://docker:2376
cache:
# The same key should be used across branches
key: "$CI_COMMIT_REF_SLUG"
paths:
- ci-exports/*.tar
only:
- master
- merge_requests
- tags

14
Docker.entrypoint.sh Normal file
View File

@@ -0,0 +1,14 @@
#!/bin/sh
export JAVA_HOME=/opt/jdk/jre
# Ensure user rights
chown -R i2p:nobody /opt/i2p
chmod -R u+rwx /opt/i2p
gosu i2p /opt/i2p/i2psvc /opt/i2p/wrapper.config wrapper.pidfile=/var/tmp/i2p.pid \
wrapper.name=i2p \
wrapper.displayname="I2P Service" \
wrapper.statusfile=/var/tmp/i2p.status \
wrapper.java.statusfile=/var/tmp/i2p.java.status \
wrapper.logfile=/var/tmp/wrapper.log

View File

@@ -1,40 +1,62 @@
# Use a multi-stage build to reduce the size of the resulting image
# We need alpine >v3 in order to install an apache-ant > 1.9
FROM debian:buster-slim as builder
FROM meeh/java8server:latest
# Docker image based on Alpine with Java.
ENV DEBIAN_FRONTEND=noninteractive
# We use Oracle Java to run I2P, but uses the openjdk to build it.
# Build installer
RUN apt-get update \
# Workaround for installing openjdk-11-jre-headless
&& mkdir -p /usr/share/man/man1 \
&& apt-get -qqqy install debhelper ant debconf gettext libgmp-dev po-debconf fakeroot \
build-essential quilt dh-apparmor dh-systemd libservice-wrapper-java libjson-simple-java \
devscripts libjetty9-java libtomcat9-java libtaglibs-standard-jstlel-java libgetopt-java \
bash-completion
WORKDIR /tmp/build
COPY . ./
MAINTAINER Mikal Villa <mikal@sigterm.no>
RUN ant debian
ENV GIT_BRANCH="master"
ENV I2P_PREFIX="/opt/i2p"
ENV PATH=${I2P_PREFIX}/bin:$PATH
ENV JAVA_HOME=/usr/lib/jvm/default-jvm
# Second stage only using the installer from the last stage
# ---------------------------------------------------------
# We can't use alpine here as the java service wrapper is built with glibc
# alpine uses musl
FROM openjdk:11.0-jre-slim
ENV GOSU_VERSION=1.7
ENV GOSU_SHASUM="34049cfc713e8b74b90d6de49690fa601dc040021980812b2f1f691534be8a50 /usr/local/bin/gosu"
RUN mkdir /user && adduser -S -h /user i2p && chown -R i2p:nobody /user
# Adding files first, since Docker.expt is required for installation
ADD Docker.expt /tmp/Docker.expt
ADD Docker.entrypoint.sh /entrypoint.sh
# Required for wget https
RUN apk add --no-cache openssl
# Gosu is a replacement for su/sudo in docker and not a backdoor :) See https://github.com/tianon/gosu
RUN wget -O /usr/local/bin/gosu https://github.com/tianon/gosu/releases/download/${GOSU_VERSION}/gosu-amd64 \
&& echo "${GOSU_SHASUM}" | sha256sum -c && chmod +x /usr/local/bin/gosu
#
# Each RUN is a layer, adding the dependencies and building i2pd in one layer takes around 8-900Mb, so to keep the
# image under 200mb we need to remove all the build dependencies in the same "RUN" / layer.
#
# The main layer
RUN apk --no-cache add build-base git gettext tar bzip2 apache-ant openjdk8 expect \
&& mkdir -p /usr/src/build \
&& cd /usr/src/build \
&& git clone -b ${GIT_BRANCH} https://github.com/i2p/i2p.i2p.git \
&& cd /usr/src/build/i2p.i2p \
&& echo "noExe=true" >> build.properties \
&& ant installer-linux \
&& cp i2pinstall*.jar /tmp/i2pinstall.jar \
&& mkdir -p /opt \
&& chown i2p:root /opt \
&& chmod u+rw /opt \
&& gosu i2p expect -f /tmp/Docker.expt \
&& cd ${I2P_PREFIX} \
&& rm -fr man docs *.bat *.command *.app /tmp/i2pinstall.jar /tmp/Docker.expt \
&& rm -fr /usr/src/build \
&& apk --purge del build-base apache-ant expect tcl expat git openjdk8 openjdk8-jre openjdk8-jre-base openjdk8-jre-lib bzip2 tar \
binutils-libs binutils pkgconfig libcurl libc-dev musl-dev g++ make fortify-headers pkgconf giflib libssh2 libxdmcp libxcb \
libx11 pcre alsa-lib libxi libxrender libxml2 readline bash openssl \
&& rm -fr /usr/lib/jvm/default-jre \
&& ln -sf /opt/jdk/jre /usr/lib/jvm/default-jre \
&& chmod a+x /entrypoint.sh
# "install" i2p by copying over installed files
COPY --from=builder /tmp/*.deb /tmp/
# Install and configure
RUN apt-get update -qqq \
&& apt-get -qqqy install geoip-database famfamfam-flag-png \
&& dpkb -i /tmp/*
EXPOSE 7654 7656 7657 7658 4444 6668 8998 7659 7660 4445 15000-20000
USER i2psvc
ENTRYPOINT [ "/usr/bin/i2prouter" ]
CMD start
ENTRYPOINT [ "/entrypoint.sh" ]

View File

@@ -40,7 +40,7 @@ import net.i2p.data.DataHelper;
* @author zzz
*/
public abstract class Addresses {
private static final File IF_INET6_FILE = new File("/proc/net/if_inet6");
private static final long INET6_CACHE_EXPIRE = 10*60*1000;
private static final boolean INET6_CACHE_ENABLED = !SystemVersion.isMac() && !SystemVersion.isWindows() &&
@@ -122,10 +122,9 @@ public abstract class Addresses {
*
* Warning, very slow on Windows, appx. 200ms + 50ms/interface
*
* @return a sorted set of all addresses including wildcard
* @param includeLocal whether to include local
* @param includeIPv6 whether to include IPV6
* @return a Set of all addresses
* @return a sorted set of all addresses including wildcard
* @since 0.8.3
*/
public static SortedSet<String> getAddresses(boolean includeLocal, boolean includeIPv6) {
@@ -141,11 +140,10 @@ public abstract class Addresses {
*
* Warning, very slow on Windows, appx. 200ms + 50ms/interface
*
* @return a sorted set of all addresses
* @param includeSiteLocal whether to include private like 192.168.x.x
* @param includeLoopbackAndWildcard whether to include 127.x.x.x and 0.0.0.0
* @param includeIPv6 whether to include IPV6
* @return a Set of all addresses
* @return a sorted set of all addresses
* @since 0.9.4
*/
public static SortedSet<String> getAddresses(boolean includeSiteLocal,
@@ -163,12 +161,11 @@ public abstract class Addresses {
*
* Warning, very slow on Windows, appx. 200ms + 50ms/interface
*
* @return a sorted set of all addresses
* @param includeSiteLocal whether to include private like 192.168.x.x
* @param includeLoopbackAndWildcard whether to include 127.x.x.x and 0.0.0.0
* @param includeIPv6 whether to include IPV6
* @param includeIPv6Temporary whether to include IPV6 temporary addresses
* @return a Set of all addresses
* @return a sorted set of all addresses
* @since 0.9.46
*/
public static SortedSet<String> getAddresses(boolean includeSiteLocal,
@@ -343,7 +340,7 @@ public abstract class Addresses {
return "(bad IP length " + addr.length + "):" + port;
}
}
/**
* Convenience method to convert and validate a port String
* without throwing an exception.
@@ -402,7 +399,7 @@ public abstract class Addresses {
* @since 0.9.3
*/
public static byte[] getIP(String host) {
if (host == null)
if (host == null || host.isEmpty())
return null;
byte[] rv;
synchronized (_IPAddress) {

View File

@@ -0,0 +1,145 @@
package net.i2p.util;
import org.junit.Test;
import java.net.Inet6Address;
import java.net.UnknownHostException;
import static org.junit.Assert.*;
/**
* @since 0.9.49
*/
public class AddressesTest {
@Test
public void getIPNull() {
assertNull(Addresses.getIP(null));
}
@Test
public void getIPEmptyString() {
assertNull(Addresses.getIP(""));
}
@Test
public void getIPWithIPString() {
byte[] address = {
1, 2, 3, 4
};
assertArrayEquals(address, Addresses.getIP("1.2.3.4"));
}
@Test
public void getPort() {
assertEquals(80, Addresses.getPort("80"));
}
@Test
public void getPort__invalidPort() {
String[] strings = {
"",
" 80",
"-100",
"a",
"99999",
null
};
for (String string : strings) {
assertEquals(0, Addresses.getPort(string));
}
}
@Test
public void isIPAddress() {
assertTrue(Addresses.isIPAddress("127.0.0.1"));
assertTrue(Addresses.isIPAddress("::1"));
}
@Test
public void isIPv6Address() {
assertTrue(Addresses.isIPv6Address("::1"));
assertFalse(Addresses.isIPv6Address(""));
}
@Test
public void isIPv4Address() {
assertTrue(Addresses.isIPv4Address("127.0.0.1"));
assertFalse(Addresses.isIPv4Address(""));
}
/**
* Should always return false when the address isn't in the cache
*/
@Test
public void isDynamic() throws UnknownHostException {
String host = "localhost";
byte[] address = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
assertFalse(Addresses.isDynamic((Inet6Address) Inet6Address.getByAddress(host, address)));
}
/**
* Should always return false when the address isn't in the cache
*/
@Test
public void isDeprecated() throws UnknownHostException {
String host = "localhost";
byte[] address = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
assertFalse(Addresses.isDeprecated((Inet6Address) Inet6Address.getByAddress(host, address)));
}
@Test
public void testToString() {
byte[] address = {127, 0, 0, 1};
assertEquals("127.0.0.1", Addresses.toString(address));
}
@Test
public void testToString__ipv4withPort() {
byte[] address = {127, 0, 0, 1};
assertEquals("127.0.0.1:80", Addresses.toString(address, 80));
}
@Test
public void testToString__ipv6withPort() {
byte[] address = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1,
};
assertEquals("[0:0:0:0:0:0:0:1]:80", Addresses.toString(address, 80));
}
@Test
public void testToString__null() {
assertEquals("null", Addresses.toString(null));
}
@Test
public void testToString__nullWithPort() {
assertEquals("null:80", Addresses.toString(null, 80));
}
@Test
public void testToString__badLength() {
byte[] address = {1};
assertTrue(Addresses.toString(address).startsWith("bad IP length"));
}
@Test
public void testToString__badLengthWithPort() {
byte[] address = {1};
String string = Addresses.toString(address, 80);
String expectedStartString = "(bad IP length";
assertTrue(
String.format("%s doesn't start with: %s", string, expectedStartString),
string.startsWith(expectedStartString)
);
String expectedEndString = "80";
assertTrue(
String.format("%s doesn't end with: %s", string, expectedEndString),
string.endsWith(expectedEndString)
);
}
}