From d497422bf9bc075d7e46a7ff97aea42afc978d8f Mon Sep 17 00:00:00 2001 From: Jeff Rego Date: Tue, 6 Aug 2024 20:59:14 -0500 Subject: [PATCH] Add T_URL type for config properties --- Makefile.am | 1 + ddclient.in | 4 ++++ t/check_value.pl | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 t/check_value.pl diff --git a/Makefile.am b/Makefile.am index 2ecf620..1d2e37e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -63,6 +63,7 @@ AM_PL_LOG_FLAGS = -Mstrict -w \ -MDevel::Autoflush handwritten_tests = \ t/builtinfw_query.pl \ + t/check_value.pl \ t/get_ip_from_if.pl \ t/geturl_connectivity.pl \ t/geturl_response.pl \ diff --git a/ddclient.in b/ddclient.in index 8d043d6..9e3bd5a 100755 --- a/ddclient.in +++ b/ddclient.in @@ -181,6 +181,7 @@ sub T_OFQDN { 'optional fully qualified host name' } sub T_FILE { 'file name' } sub T_FQDNP { 'fully qualified host name and optional port number' } sub T_PROTO { 'protocol' } +sub T_URL { 'url including fully qualified host name, optional port number, and path' } sub T_USE { 'ip strategy' } sub T_USEV4 { 'ipv4 strategy' } sub T_USEV6 { 'ipv6 strategy' } @@ -2524,6 +2525,9 @@ sub check_value { $value = lc $value; return undef if !exists $protocols{$value}; + } elsif ($type eq T_URL) { + return undef if $value !~ qr{^(?i:https?://)?[^./]+(\.[^./]+)+(:\d+)?(/[^/]+)*/?$}; + } elsif ($type eq T_USE) { $value = lc $value; return undef if !exists $ip_strategies{$value}; diff --git a/t/check_value.pl b/t/check_value.pl new file mode 100644 index 0000000..02a0cde --- /dev/null +++ b/t/check_value.pl @@ -0,0 +1,47 @@ +use Test::More; +use strict; +SKIP: { eval { require Test::Warnings; } or skip($@, 1); } +eval { require 'ddclient'; } or BAIL_OUT($@); + +my @test_cases = ( + { + type => ddclient::T_FQDN(), + input => 'example.com', + want => 'example.com', + }, + { + type => ddclient::T_FQDN(), + input => 'example', + want => undef, + }, + { + type => ddclient::T_URL(), + input => 'https://www.example.com', + want => 'https://www.example.com', + }, + { + type => ddclient::T_URL(), + input => 'https://directnic.com/dns/gateway/ad133/', + want => 'https://directnic.com/dns/gateway/ad133/', + }, + { + type => ddclient::T_URL(), + input => 'HTTPS://MixedCase.com/', + want => 'HTTPS://MixedCase.com/', + }, + { + type => ddclient::T_URL(), + input => 'ftp://bad.protocol/', + want => undef, + }, + { + type => ddclient::T_URL(), + input => 'bad-url', + want => undef, + }, +); +for my $tc (@test_cases) { + my $got = ddclient::check_value($tc->{input}, ddclient::setv($tc->{type}, 0, 0, undef, undef)); + is($got, $tc->{want}, "$tc->{type}: $tc->{input}"); +} +done_testing();