From 5a0e87be541c2e5c3a6ba9083082022578cd6c0b Mon Sep 17 00:00:00 2001 From: Ryan Harg Date: Tue, 3 Dec 2024 09:43:59 +0100 Subject: [PATCH] Error handling if public ips are missing --- src/main/java/de/rpr/ddnsclient/Updater.java | 4 +++ .../java/de/rpr/ddnsclient/UpdaterTest.java | 30 ++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/rpr/ddnsclient/Updater.java b/src/main/java/de/rpr/ddnsclient/Updater.java index 458fdbd..baa3d17 100644 --- a/src/main/java/de/rpr/ddnsclient/Updater.java +++ b/src/main/java/de/rpr/ddnsclient/Updater.java @@ -53,6 +53,10 @@ public class Updater { Addresses publicIps = publicIpLookup.get(); log.debugf("Public ips - v4: %s, v6: %s", publicIps.v4(), publicIps.v6()); + if (publicIps.v4().isEmpty() && publicIps.v6().isEmpty()) { + log.error("No public ips present, stopping"); + return; + } config.forEach(cfg -> { diff --git a/src/test/java/de/rpr/ddnsclient/UpdaterTest.java b/src/test/java/de/rpr/ddnsclient/UpdaterTest.java index 6f730ec..e3f91bf 100644 --- a/src/test/java/de/rpr/ddnsclient/UpdaterTest.java +++ b/src/test/java/de/rpr/ddnsclient/UpdaterTest.java @@ -17,6 +17,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.time.Duration; import java.util.List; +import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; @@ -40,6 +41,7 @@ class UpdaterTest { @Test void should_throw_exception_if_config_is_empty() { Config config = new Config(); + PublicIpLookup publicIpLookup = mock(PublicIpLookup.class); Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, config, backoff); Assertions.assertThrows(IllegalStateException.class, updater::run); } @@ -47,7 +49,6 @@ class UpdaterTest { @Test void should_process_all_config_entries() { Addresses ips = new Addresses("ipv4", "ipv6"); - when(publicIpLookup.get()).thenReturn(ips); when(dnsResolver.resolve(any())).thenReturn(ips); @@ -89,17 +90,27 @@ class UpdaterTest { when(dyndnsProviderRegistry.get("ddnss")).thenReturn(ddnss); Addresses publicIps = new Addresses("ipv4", "ipv6"); + when(publicIpLookup.get()).thenReturn(publicIps); when(dnsResolver.resolve("example.org")).thenReturn(new Addresses("registered_ipv4", "registered_ipv6")); Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, ddnssConfig, backoff); updater.run(); - verify(ddnss).update( - "example.org", - publicIps, - new DyndnsAuth(null, null, "token") - ); + verify(ddnss).update("example.org", publicIps, new DyndnsAuth(null, null, "token")); + } + + @Test + void should_not_trigger_dyndns_update_if_public_ips_are_empty() { + + Addresses publicIps = new Addresses(Optional.empty(), Optional.empty()); + + when(publicIpLookup.get()).thenReturn(publicIps); + + Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, ddnssConfig, backoff); + updater.run(); + + verify(dyndnsProviderRegistry, never()).get(any()); } @Test @@ -110,6 +121,7 @@ class UpdaterTest { when(dyndnsProviderRegistry.get("ddnss")).thenReturn(ddnss); Addresses publicIps = new Addresses("ipv4", "ipv6"); + when(publicIpLookup.get()).thenReturn(publicIps); when(dnsResolver.resolve("example.org")).thenReturn(new Addresses("registered_ipv4", "registered_ipv6")); @@ -118,11 +130,7 @@ class UpdaterTest { updater.run(); - verify(ddnss, times(1)).update( - "example.org", - publicIps, - new DyndnsAuth(null, null, "token") - ); + verify(ddnss, times(1)).update("example.org", publicIps, new DyndnsAuth(null, null, "token")); } } \ No newline at end of file