Skip to content

Commit

Permalink
Add tests for --dhcp-host
Browse files Browse the repository at this point in the history
The examples are from the example configs in #17 and #19 as well as the
dnsmasq.conf.example distributed with Dnsmasq version 2.89
  • Loading branch information
reynir committed Dec 11, 2024
1 parent c003639 commit 658d1ad
Showing 1 changed file with 155 additions and 6 deletions.
161 changes: 155 additions & 6 deletions test/config_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ let opt_eq f a b =
| Some a, Some b -> f a b
| None, Some _ | Some _, None -> false

let ip_eq a b = Ipaddr.V4.compare a b = 0
let ipv4_eq a b = Ipaddr.V4.compare a b = 0

let ipv6_eq a b = Ipaddr.V6.compare a b = 0

let mac_eq a b = Macaddr.compare a b = 0

let mode_eq a b =
match (a, b) with
Expand All @@ -19,15 +23,34 @@ let mode_eq a b =

let dhcp_range_t =
let equal a b =
ip_eq a.start_addr b.start_addr
&& opt_eq ip_eq a.end_addr b.end_addr
ipv4_eq a.start_addr b.start_addr
&& opt_eq ipv4_eq a.end_addr b.end_addr
&& opt_eq mode_eq a.mode b.mode
&& opt_eq ip_eq a.netmask b.netmask
&& opt_eq ip_eq a.broadcast b.broadcast
&& opt_eq ipv4_eq a.netmask b.netmask
&& opt_eq ipv4_eq a.broadcast b.broadcast
&& opt_eq Int.equal a.lease_time b.lease_time
in
Alcotest.testable pp_dhcp_range equal

let dhcp_host_t =
let equal { id; nets; tags; macs; ipv4; ipv6; lease_time; ignore; domain_name } b =
Option.equal (fun id id' ->
match id, id' with
| `Any_client_id, `Any_client_id -> true
| `Client_id id, `Client_id id' -> String.equal id id'
| `Any_client_id, `Client_id _ | `Client_id _, `Any_client_id -> false)
id b.id &&
List.equal String.equal nets b.nets &&
List.equal String.equal tags b.tags &&
List.equal mac_eq macs b.macs &&
Option.equal ipv4_eq ipv4 b.ipv4 &&
Option.equal ipv6_eq ipv6 b.ipv6 &&
Option.equal Int.equal lease_time b.lease_time &&
Bool.equal ignore b.ignore &&
Option.equal Domain_name.equal domain_name b.domain_name
in
Alcotest.testable pp_dhcp_config equal

let parse_one_arg rule input = parse_one (rule arg_end_of_directive) input

let ok_dhcp_range () =
Expand Down Expand Up @@ -86,12 +109,138 @@ let ok_dhcp_range_static () =
"DHCP range with static is good" (Ok expected)
(parse_one_arg dhcp_range input))

let make_dhcp_config ?id ?(nets=[]) ?(tags=[]) ?(macs=[]) ?ipv4 ?ipv6 ?lease_time ?(ignore=false) ?domain_name () =
{ id; nets; tags; macs; ipv4; ipv6; lease_time; ignore; domain_name }

let ok_dhcp_host_thedoctor () =
let input = "00:00:5e:00:53:42,thedoctor,192.168.0.10" in
let expected =
make_dhcp_config
~macs:[Macaddr.of_string_exn "00:00:5e:00:53:42"]
~domain_name:(Domain_name.of_string_exn "thedoctor")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.10")
()
in
Alcotest.(check (result dhcp_host_t msg_t)) "DHCP host thedoctor is good"
(Ok expected) (parse_one_arg dhcp_host input)

let ok_dhcp_host_tardis () =
let input = "00:00:5e:00:53:01,00:00:5e:00:53:02,tardis,192.168.0.22" in
let expected =
make_dhcp_config
~macs:Macaddr.[of_string_exn "00:00:5e:00:53:01";
of_string_exn "00:00:5e:00:53:02"]
~domain_name:(Domain_name.of_string_exn "tardis")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.22")
()
in
Alcotest.(check (result dhcp_host_t msg_t)) "DHCP host tardis is good"
(Ok expected) (parse_one_arg dhcp_host input)

let ok_dhcp_host_sonicscrewdriver () =
let input = "00:00:5e:00:53:08,sonicscrewdriver,192.168.0.23" in
let expected =
make_dhcp_config
~macs:[Macaddr.of_string_exn "00:00:5e:00:53:08"]
~domain_name:(Domain_name.of_string_exn "sonicscrewdriver")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.23")
()
in
Alcotest.(check (result dhcp_host_t msg_t)) "DHCP host sonicscrewdriver is good"
(Ok expected) (parse_one_arg dhcp_host input)

let ok_dhcp_host_apollon () =
let input = "52:54:00:42:6a:43,apollon,10.10.10.51,infinite" in
let expected =
make_dhcp_config
~macs:[Macaddr.of_string_exn "52:54:00:42:6a:43"]
~domain_name:(Domain_name.of_string_exn "apollon")
~ipv4:(Ipaddr.V4.of_string_exn "10.10.10.51")
~lease_time:(1 lsl 32) (* infinite *)
()
in
Alcotest.(check (result dhcp_host_t msg_t)) "DHCP host apollon is good"
(Ok expected) (parse_one_arg dhcp_host input)

let ok_dhcp_host_dnsmasq_conf_example =
let to_test (name, input, expected) =
(name, `Quick, fun () ->
Alcotest.(check (result dhcp_host_t msg_t))
(name ^ " is good")
(Ok expected)
(parse_one_arg dhcp_host input))
in
List.map to_test [
("First dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,192.168.0.60",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60") ());
("Second dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,fred",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~domain_name:(Domain_name.of_string_exn "fred") ());
("Third dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,fred,192.168.0.60,45m",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~domain_name:(Domain_name.of_string_exn "fred")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60")
~lease_time:(45*60) ());
("Fourth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,12:34:56:78:90:12,192.168.0.60",
make_dhcp_config ~macs:Macaddr.[of_string_exn "11:22:33:44:55:66";
of_string_exn "12:34:56:78:90:12"]
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60") ());
("Fifth dhcp-host from dnsmasq.conf.example",
"bert,192.168.0.70,infinite",
make_dhcp_config ~domain_name:(Domain_name.of_string_exn "bert")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.70")
~lease_time:(1 lsl 32) ());
("Sixth dhcp-host from dnsmasq.conf.example",
"id:01:02:02:04,192.168.0.60",
make_dhcp_config ~id:(`Client_id "\x01\x02\x02\x04")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60") ());
(* skipping seventh as it's similar to above, but is infiniband in a manner we wouldn't care about *)
("Eigth dhcp-host from dnsmasq.conf.example",
"id:marjorie,192.168.0.60",
make_dhcp_config ~id:(`Client_id "marjorie")
~ipv4:(Ipaddr.V4.of_string_exn "192.168.0.60") ());
("Ninth dhcp-host from dnsmasq.conf.example",
"judge",
make_dhcp_config ~domain_name:(Domain_name.of_string_exn "judge")
());
("Tenth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,ignore",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~ignore:true ());
("Eleventh dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,id:*",
make_dhcp_config ~id:`Any_client_id
~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"] ());
("Twelvth dhcp-host from dnsmasq.conf.example",
"11:22:33:44:55:66,set:red",
make_dhcp_config ~macs:[Macaddr.of_string_exn "11:22:33:44:55:66"]
~nets:["red"] ());
(* TODO:
("Thirteenth dhcp-host from dnsmasq.conf.example",
"11:22:33:*:*:*,set:red",
_);
("Fourteenth dhcp-host from dnsmasq.conf.example",
"id:00:01:00:01:16:d2:83:fc:92:d4:19:e2:d8:b2, fred, [1234::5]",
_);
*)
]

let tests =
[
("DHCP range", `Quick, ok_dhcp_range);
("DHCP range with netmask", `Quick, ok_dhcp_range_with_netmask);
("DHCP range static", `Quick, ok_dhcp_range_static);
]
("DHCP host thedoctor", `Quick, ok_dhcp_host_thedoctor);
("DHCP host tardis", `Quick, ok_dhcp_host_tardis);
("DHCP host sonicscrewdriver", `Quick, ok_dhcp_host_sonicscrewdriver);
("DHCP host apollon", `Quick, ok_dhcp_host_apollon);
] @
ok_dhcp_host_dnsmasq_conf_example

let string_of_file filename =
let config_dir = "sample-configuration-files" in
Expand Down

0 comments on commit 658d1ad

Please sign in to comment.