From 6b9c2f93c54d569bc26f2c73acb49ec544c68148 Mon Sep 17 00:00:00 2001 From: Paolo Oliveira Date: Fri, 19 Jul 2024 13:17:42 -0300 Subject: [PATCH] #10178 SetNTP + tests --- lib/devices/set_ntp.ex | 58 +++++++++++++++ test/devices/fixtures/set_ntp_response.xml | 8 ++ test/devices/set_ntp.exs | 85 ++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 lib/devices/set_ntp.ex create mode 100644 test/devices/fixtures/set_ntp_response.xml create mode 100644 test/devices/set_ntp.exs diff --git a/lib/devices/set_ntp.ex b/lib/devices/set_ntp.ex new file mode 100644 index 0000000..42340e4 --- /dev/null +++ b/lib/devices/set_ntp.ex @@ -0,0 +1,58 @@ +defmodule Onvif.Devices.SetNTP do + import SweetXml + import XmlBuilder + + alias Onvif.Device + alias Onvif.Devices.NTP + + def soap_action, do: "http://www.onvif.org/ver10/device/wsdl/SetNTP" + + @spec request(Device.t(), list) :: {:ok, any} | {:error, map()} + def request(device, args) do + Onvif.Devices.request(device, args, __MODULE__) + end + + def request_body(config: %NTP{} = ntp) do + element(:"s:Body", [ + element(:"tds:SetNTP", [ + element(:"tds:FromDHCP", ntp.from_dhcp), + ntp_manual_element(ntp, ntp.from_dhcp) |> List.flatten() + ]) + ]) + end + + defp ntp_manual_element(%NTP{} = _ntp, true), do: [] + + defp ntp_manual_element(%NTP{} = ntp, false) do + [element(:"tds:NTPManual", ntp_add_manual_element(ntp.ntp_manual))] + end + + defp ntp_add_manual_element(ntp_manual) do + [ + element(:"tt:Type", ntp_manual.type), + ntp_manual_element_data(ntp_manual) + ] + end + + defp ntp_manual_element_data(ntp_manual) do + case ntp_manual.type do + "IPv4" -> element(:"tt:IPv4Address", ntp_manual.ipv4_address) + "IPv6" -> element(:"tt:IPv6Address", ntp_manual.ipv6_address) + "DNS" -> element(:"tt:DNSname", ntp_manual.dns_name) + end + end + + def response(xml_response_body) do + res = + xml_response_body + |> parse(namespace_conformant: true, quiet: true) + |> xpath( + ~x"//s:Envelope/s:Body/tds:SetNTPResponse/text()"s + |> add_namespace("s", "http://www.w3.org/2003/05/soap-envelope") + |> add_namespace("tds", "http://www.onvif.org/ver10/device/wsdl") + |> add_namespace("tt", "http://www.onvif.org/ver10/schema") + ) + + {:ok, res} + end +end diff --git a/test/devices/fixtures/set_ntp_response.xml b/test/devices/fixtures/set_ntp_response.xml new file mode 100644 index 0000000..285e735 --- /dev/null +++ b/test/devices/fixtures/set_ntp_response.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/test/devices/set_ntp.exs b/test/devices/set_ntp.exs new file mode 100644 index 0000000..08202b4 --- /dev/null +++ b/test/devices/set_ntp.exs @@ -0,0 +1,85 @@ +defmodule Onvif.Devices.SetNTPTest do + use ExUnit.Case, async: true + + @moduletag capture_log: true + + describe "SetNTP/2" do + test "check a dhcp-based ntp" do + xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml") + + device = Onvif.Factory.device() + + Mimic.expect(Tesla, :request, fn _client, _opts -> + {:ok, %{status: 200, body: xml_response}} + end) + + {:ok, service_capabilities} = + Onvif.Devices.SetNTP.request(device, config: %Onvif.Devices.NTP{from_dhcp: true}) + + assert service_capabilities == "" + end + + test "check a manual ntp with IPv4" do + xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml") + + device = Onvif.Factory.device() + + Mimic.expect(Tesla, :request, fn _client, _opts -> + {:ok, %{status: 200, body: xml_response}} + end) + + {:ok, service_capabilities} = + Onvif.Devices.SetNTP.request(device, + config: %Onvif.Devices.NTP{ + from_dhcp: false, + ntp_manual: %Onvif.Devices.NTP.NTPManual{type: "IPv4", ipv4_address: "6.6.6.0"} + } + ) + + assert service_capabilities == "" + end + + test "check a manual ntp with IPv6" do + xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml") + + device = Onvif.Factory.device() + + Mimic.expect(Tesla, :request, fn _client, _opts -> + {:ok, %{status: 200, body: xml_response}} + end) + + {:ok, service_capabilities} = + Onvif.Devices.SetNTP.request(device, + config: %Onvif.Devices.NTP{ + from_dhcp: false, + ntp_manual: %Onvif.Devices.NTP.NTPManual{ + type: "IPv6", + ipv6_address: "2001:0db8:85a3:0000:0000:8a2e:0370:7334" + } + } + ) + + assert service_capabilities == "" + end + + test "check a manual ntp with DNS" do + xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml") + + device = Onvif.Factory.device() + + Mimic.expect(Tesla, :request, fn _client, _opts -> + {:ok, %{status: 200, body: xml_response}} + end) + + {:ok, service_capabilities} = + Onvif.Devices.SetNTP.request(device, + config: %Onvif.Devices.NTP{ + from_dhcp: false, + ntp_manual: %Onvif.Devices.NTP.NTPManual{type: "DNS", dns_name: "ntp.example.com"} + } + ) + + assert service_capabilities == "" + end + end +end