From 75eb691517cac3b67912ef1d347950a9866165c7 Mon Sep 17 00:00:00 2001 From: Ben Hutchison Date: Tue, 25 Jun 2024 02:49:19 -0700 Subject: [PATCH] Updated tests --- Readme.md | 15 ++++++++------- Tests/DynamicDnsServiceTest.cs | 16 ++++++++++++---- Tests/Net/Stun/MultiServerStunClientTest.cs | 17 ++++++++++++----- Tests/Tests.runsettings | 2 +- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/Readme.md b/Readme.md index c995439..f25863c 100644 --- a/Readme.md +++ b/Readme.md @@ -39,16 +39,17 @@ This is an alternative to filling out monthly CAPTCHAs for [No-IP](https://www.n ``` ## Configuration -Open `appsettings.json` in a text editor. +Open `appsettings.json` in a text editor and fill in the following values. Keys marked with ✴ are the most important ones to configure. |Key|Type|Examples|Description| |-|-|-|-| -|`gandiApiKey`|`string`|`abcdefg`|Generate an API key under [Developer access](https://account.gandi.net/en/users/_/security) in your [Gandi Account](https://account.gandi.net/en). Personal Access Tokens are unfortunately not supported by [G6.GandiLiveDns](https://github.com/gaylord-roger/G6.GandiLiveDns).| -|`domain`|`string`|`example.com`
`example.co.uk`|The second-level domain name that you registered, including the TLD.| -|`subdomain`|`string`|`www`
`@`
`api.stage`|The subdomain whose DNS record you want to update, not including `domain` or a trailing period. To update `domain` itself, set this to `@`. Can also be a multi-level subdomain.| -|`updateInterval`|`TimeSpan`|`0.00:05:00`|How frequently this program will check if your public IP address has changed and update DNS. Format is `d.hh:mm:ss`.
**One-shot mode:** if set to `0:0:0` or negative, this program will exit after the first update attempt, instead of remaining running and updating periodically; useful if you want to trigger it yourself, like with `cron`.| -|`dnsRecordTimeToLive`|`TimeSpan`|`0.00:05:00`|How long DNS resolvers can cache your record before they must look it up again. Gandi requires this to be between 5 minutes and 30 days, inclusive.| -|`dryRun`|`bool`|`false`
`true`|Set to `false` to run normally, or `true` to avoid changing any DNS records.| +|`gandiApiKey` ✴|`string`|`abcdefg`|Generate an API key under [Developer access](https://account.gandi.net/en/users/_/security) in your [Gandi Account](https://account.gandi.net/en). Personal Access Tokens are unfortunately not supported by [G6.GandiLiveDns](https://github.com/gaylord-roger/G6.GandiLiveDns).| +|`domain` ✴|`string`|`example.com`
`example.co.uk`|The second-level domain name that you registered, including the TLD.| +|`subdomain` ✴|`string`|`www`
`@`
`api.stage`|The subdomain whose DNS record you want to update, not including `domain` or a trailing period. To update `domain` itself, set this to `@` (default). Can also be a multi-level subdomain.| +|`updateInterval`|`TimeSpan`|`0.00:05:00`|How frequently this program will check if your public IP address has changed and update DNS. Format is `d.hh:mm:ss`. Defaults to 5 minutes.
**One-shot mode:** if set to `0:0:0` or negative, this program will exit after the first update attempt, instead of remaining running and updating periodically; useful if you want to trigger it yourself, like with `cron`.| +|`dnsRecordTimeToLive`|`TimeSpan`|`0.00:05:00`|How long DNS resolvers can cache your record before they must look it up again. Gandi requires this to be between 5 minutes and 30 days, inclusive. Defaults to 5 minutes.| +|`dryRun`|`bool`|`false`
`true`|Set to `false` (default) to run normally, or `true` to avoid changing any DNS records.| +|`stunServerBlacklist`|`string[]`|`["stun.bergophor.de", "stun.usfamily.net"]`|List of STUN server hostnames to not use when determining your computer's public IP address. Defaults to blocking `stun.bergophor.de` and `stun.usfamily.net` due to incorrect responses.| ## Execution - **Manually**: `./GandiDynamicDns` diff --git a/Tests/DynamicDnsServiceTest.cs b/Tests/DynamicDnsServiceTest.cs index 47cd109..0c4148d 100644 --- a/Tests/DynamicDnsServiceTest.cs +++ b/Tests/DynamicDnsServiceTest.cs @@ -30,7 +30,8 @@ public DynamicDnsServiceTest() { [Fact] public async Task updateRecord() { A.CallTo(() => dns.fetchDnsRecords(A._, A._, A._, A._)).Returns(["192.0.2.1"]); - A.CallTo(() => stun.getSelfWanAddress(A._)).Returns(new SelfWanAddressResponse(IPAddress.Parse("192.0.2.2"), IPEndPoint.Parse("192.0.2.3"))); + A.CallTo(() => stun.getSelfWanAddress(A._)) + .Returns(new SelfWanAddressResponse(IPAddress.Parse("192.0.2.2"), new DnsEndPoint("example.com", 3478), IPEndPoint.Parse("192.0.2.3"))); await service.StartAsync(default); await service.ExecuteTask!; @@ -45,7 +46,8 @@ public async Task updateRecord() { [Fact] public async Task invalidExistingRecord() { A.CallTo(() => dns.fetchDnsRecords(A._, A._, A._, A._)).Returns(["hargle"]); - A.CallTo(() => stun.getSelfWanAddress(A._)).Returns(new SelfWanAddressResponse(IPAddress.Parse("192.0.2.2"), IPEndPoint.Parse("192.0.2.3"))); + A.CallTo(() => stun.getSelfWanAddress(A._)) + .Returns(new SelfWanAddressResponse(IPAddress.Parse("192.0.2.2"), new DnsEndPoint("example.com", 3478), IPEndPoint.Parse("192.0.2.3"))); await service.StartAsync(default); await service.ExecuteTask!; @@ -60,7 +62,8 @@ public async Task invalidExistingRecord() { [Fact] public async Task unchanged() { A.CallTo(() => dns.fetchDnsRecords(A._, A._, A._, A._)).Returns(["192.0.2.1"]); - A.CallTo(() => stun.getSelfWanAddress(A._)).Returns(new SelfWanAddressResponse(IPAddress.Parse("192.0.2.1"), IPEndPoint.Parse("192.0.2.3"))); + A.CallTo(() => stun.getSelfWanAddress(A._)) + .Returns(new SelfWanAddressResponse(IPAddress.Parse("192.0.2.1"), new DnsEndPoint("example.com", 3478), IPEndPoint.Parse("192.0.2.3"))); await service.StartAsync(default); await service.ExecuteTask!; @@ -93,7 +96,7 @@ public async Task loop() { latch.Signal(); } catch (InvalidOperationException) { } }) - .Returns(new SelfWanAddressResponse(IPAddress.Parse("192.0.2.2"), IPEndPoint.Parse("192.0.2.3"))); + .Returns(new SelfWanAddressResponse(IPAddress.Parse("192.0.2.2"), new DnsEndPoint("example.com", 3478), IPEndPoint.Parse("192.0.2.3"))); await service.StartAsync(cts.Token); latch.Wait(10_000); @@ -109,4 +112,9 @@ public async Task loop() { A.CallTo(() => lifetime.StopApplication()).MustNotHaveHappened(); } + [Fact] + public void disposal() { + service.Dispose(); + } + } \ No newline at end of file diff --git a/Tests/Net/Stun/MultiServerStunClientTest.cs b/Tests/Net/Stun/MultiServerStunClientTest.cs index e88a5f2..5657a9e 100644 --- a/Tests/Net/Stun/MultiServerStunClientTest.cs +++ b/Tests/Net/Stun/MultiServerStunClientTest.cs @@ -1,8 +1,9 @@ -using GandiDynamicDns.Net.Stun; +using GandiDynamicDns; +using GandiDynamicDns.Net.Stun; using GandiDynamicDns.Unfucked.Stun; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using STUN.Enums; -using STUN.Proxy; using STUN.StunResult; using System.Net; using Tests.Mocks; @@ -18,9 +19,15 @@ public class MultiServerStunClientTest: IDisposable { public MultiServerStunClientTest() { MultiServerStunClient.SERVERS_CACHE.Clear(); - multiServerStunClient = new MultiServerStunClient(new HttpClient(httpMessageHandler), stunClientFactory, new NullLogger()); - - A.CallTo(() => stunClientFactory.createStunClient(A._, A._, A._)).Returns(singleServerStunClient); + multiServerStunClient = new MultiServerStunClient(new HttpClient(httpMessageHandler), stunClientFactory, new NullLogger(), new OptionsWrapper( + new Configuration { + gandiApiKey = "", + domain = "example.com" + })); + + A.CallTo(() => stunClientFactory.createStunClient(A._)).Returns(singleServerStunClient); + A.CallTo(() => singleServerStunClient.Server).Returns(new DnsEndPoint("example.com", 3478)); + A.CallTo(() => singleServerStunClient.ServerAddress).Returns(IPEndPoint.Parse("192.0.2.1:12345")); A.CallTo(() => httpMessageHandler.fakeSendAsync(A._, A._)) .Returns(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("192.0.2.1:12345\n192.0.2.2:12345\n192.0.2.3:hargle\n") }); diff --git a/Tests/Tests.runsettings b/Tests/Tests.runsettings index b657380..6c3092c 100644 --- a/Tests/Tests.runsettings +++ b/Tests/Tests.runsettings @@ -9,7 +9,7 @@ - [Test*]*,[GandiDynamicDns]GandiDynamicDns.Unfucked.* + [Test*]*,[StunDnsResolver]*,[GandiDynamicDns]GandiDynamicDns.Unfucked.*