-module(fib_macos_SUITE).
-compile(export_all).
-compile(nowarn_export_all).
-include_lib("fib/src/fib_macos.hrl").
-include_lib("stdlib/include/assert.hrl").
suite() ->
[{timetrap, 10000}].
all() ->
[{group, pf_route_parser}, {group, netstat_parser}].
groups() ->
[{pf_route_parser, [parallel], pf_route_parser_tests()}, {netstat_parser, [parallel], netstat_parser_tests()}].
init_per_group(_Group, Config) ->
Config.
end_per_group(_, _) ->
ok.
%% Netstat
netstat_parser_tests() ->
[netstat_list].
netstat_list(_) ->
{skip, todo}.
%% Packet
pf_route_parser_tests() ->
[pckt_rt_add, pckt_rt_add_error, pckt_rt_add_expire, pckt_rt_delete, pckt_rt_get, pckt_rt_get_default, pckt_rt_ifinfo].
%% Result of `route add 192.168.3.0/24 8.8.8.8`.
pckt_rt_add(_) ->
ct:comment("Simple RT_ADD"),
Binary = <<5,1,0,0,0,0,67,8,0,0,7,0,0,0,21,130,0,0,1,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,2,0, 0,192,168,3,0,0,0,0,0,0,0,0,0,16,2,0,0,8,8,8,8,0,0,0,0,0,0,0,0,7,2,0,0,255,255,255,0>>,
Rt = #macos_rt{} = fib_macos:parse_pf_route_packet(Binary),
?assertEqual(add, Rt#macos_rt.type),
?assertEqual([up, gateway, done, static], Rt#macos_rt.flags),
?assertEqual([destination, gateway, netmask], Rt#macos_rt.addrs),
?assertEqual(33301, Rt#macos_rt.pid),
?assertEqual(1, Rt#macos_rt.seq),
?assertEqual(0, Rt#macos_rt.errno),
?assertEqual(0, Rt#macos_rt.expire),
?assertEqual({0, 0, 0}, Rt#macos_rt.filler),
?assertEqual({192, 168, 3, 0}, Rt#macos_rt.destination),
?assertEqual({8, 8, 8, 8}, Rt#macos_rt.gateway),
?assertEqual({255, 255, 255, 0}, Rt#macos_rt.netmask),
?assertEqual(undefined, Rt#macos_rt.ifp),
?assertEqual(0, Rt#macos_rt.ifp_index),
?assertEqual(undefined, Rt#macos_rt.ifa),
?assertEqual(undefined, Rt#macos_rt.author),
?assertEqual(undefined, Rt#macos_rt.brd).
pckt_rt_add_error(_) ->
ct:comment("Simple RT_ADD with error (already existing)"),
Binary = <<5,1,0,0,0,0,3,8,0,0,7,0,0,0,99,228,0,0,1,0,0,0,17,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,2,0,
0,192,168,3,0,0,0,0,0,0,0,0,0,16,2,0,0,8,8,8,8,0,0,0,0,0,0,0,0,
7,2,0,0,255,255,255,0>>,
Rt = fib_macos:parse_pf_route_packet(Binary),
?assertEqual(17, Rt#macos_rt.errno).
pckt_rt_add_expire(_) ->
ct:comment("Simple RT_ADD with expiry"),
Binary = <<5,1,0,0,0,0,67,8,0,0,7,0,0,0,22,231,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,36,152,122,97,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
16,2,0,0,192,168,4,0,0,0,0,0,0,0,0,0,16,2,0,0,8,8,8,8,0,0,0,0,0,
0,0,0,7,2,0,0,255,255,255,0>>,
Rt = fib_macos:parse_pf_route_packet(Binary),
?assertEqual(1635424292, Rt#macos_rt.expire),
?assertEqual([expire], Rt#macos_rt.metrics_init).
pckt_rt_delete(_) ->
ct:comment("RT_DELETE with ifp/ifa specificities"),
%% RTM_DELETE: Delete Route: len 164, pid: 80, seq 166, errno 0, ifscope 12, flags:<HOST,DONE,WASCLONED,BROADCAST,IFSCOPE,CONDEMNED>
%% locks: inits:
%% sockaddrs: <DST,GATEWAY,IFP,IFA>
%% 172.20.10.15 ff.ff.ff.ff.ff.ff en0:a0.78.17.af.bb.68 172.20.10.13
Binary = <<5,2,12,0,0,0,68,0,66,3,51,0,0,0,80,0,0,0,166,0,0,0,0,0,0,0,8,0,
0,0,0,0,0,0,0,0,0,0,220,5,0,0,0,0,0,0,70,51,121,97,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,16,2,0,0,172,20,10,15,0,0,0,0,0,0,0,0,20,18,12,0,6,0,6,0,
255,255,255,255,255,255,0,0,0,0,0,0,20,18,12,0,6,3,6,0,101,110,
48,160,120,23,175,187,104,0,0,0,16,2,0,0,172,20,10,13,0,0,0,0,0,
0,0,0>>,
Rt = fib_macos:parse_pf_route_packet(Binary),
?assertEqual([host, done, wascloned, broadcast, ifscope, condemned], Rt#macos_rt.flags),
?assertEqual(12, Rt#macos_rt.ifp_index),
?assertEqual({172,20,10,15}, Rt#macos_rt.destination),
?assertEqual({18,<<12,0,6,0,6,0,255,255,255,255,255,255,0,0,0,0,0,0>>}, Rt#macos_rt.gateway),
?assertEqual({18,<<12,0,6,3,6,0,101,110,48,160,120,23,175,187,104,0,0,0>>}, Rt#macos_rt.ifp),
?assertEqual({172,20,10,13}, Rt#macos_rt.ifa).
pckt_rt_get(_) ->
ct:comment("RT_GET for a subnet"),
%RTM_GET: Report Metrics: len 168, pid: 69344, seq 1, errno 0, flags:<UP,GATEWAY,DONE,STATIC,PRCLONING>
% locks: inits:
% sockaddrs: <DST,GATEWAY,NETMASK,IFP,IFA>
% 192.168.3.0 dns.google (255) ffff ffff ff en0:a0.78.17.af.bb.68 172.20.10.13
Binary = <<5,4,12,0,0,0,67,8,1,0,55,0,0,0,224,14,1,0,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,220,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,
2,0,0,192,168,3,0,0,0,0,0,0,0,0,0,16,2,0,0,8,8,8,8,0,0,0,0,0,0,
0,0,7,255,255,255,255,255,255,0,20,18,12,0,6,3,6,0,101,110,48,
160,120,23,175,187,104,0,0,0,16,2,0,0,172,20,10,13,0,0,0,0,0,0,
0,0>>,
Rt = fib_macos:parse_pf_route_packet(Binary),
?assertEqual({192, 168, 3, 0}, Rt#macos_rt.destination),
?assertEqual({8, 8, 8, 8}, Rt#macos_rt.gateway),
?assertEqual({255, 255, 255, 0}, Rt#macos_rt.netmask).
pckt_rt_get_default(_) ->
ct:comment("Simple RT_GET (`route get default`)"),
%% RTM_GET: Report Metrics: len 164, pid: 56835, seq 1, errno 0, flags:<UP,GATEWAY,DONE,STATIC,PRCLONING,GLOBAL>
%% locks: inits:
%% sockaddrs: <DST,GATEWAY,NETMASK,IFP,IFA>
%% default 172.20.10.1 default en0:a0.78.17.af.bb.68 172.20.10.13
Binary = <<5,4,12,0,0,0,67,8,1,64,55,0,0,0,3,222,0,0,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,220,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,2,0,0,172,20,10,1,12,0,0,0,0,0,
0,0,0,0,0,0,20,18,12,0,6,3,6,0,101,110,48,160,120,23,175,187,
104,0,0,0,16,2,0,0,172,20,10,13,0,0,0,0,0,0,0,0>>,
Rt = #macos_rt{} = fib_macos:parse_pf_route_packet(Binary),
?assertEqual(get, Rt#macos_rt.type),
?assertEqual([up, gateway, done, static, prcloning, global], Rt#macos_rt.flags),
?assertEqual([destination, gateway, netmask, ifp, ifa], Rt#macos_rt.addrs),
?assertEqual(1500, Rt#macos_rt.mtu),
?assertEqual({0, 0, 0, 0}, Rt#macos_rt.destination),
?assertEqual({172, 20, 10, 1}, Rt#macos_rt.gateway),
?assertEqual(undefined, Rt#macos_rt.netmask),
?assertEqual({18,<<12,0,6,3,6,0,101,110,48,160,120,23,175,187,104,0,0,0>>}, Rt#macos_rt.ifp),
?assertEqual(12, Rt#macos_rt.ifp_index),
?assertEqual({172, 20, 10, 13}, Rt#macos_rt.ifa).
pckt_rt_ifinfo(_) ->
ct:comment("RT_IFINFO (new interface)"),
%% RTM_IFINFO: iface status change: len 112, if# 22, flags:<BROADCAST,b6,RUNNING,SIMPLEX,MULTICAST>
Binary = <<5,14,0,0,0,0,35,136,0,0,22,0,0,0,6,0,0,6,14,0,0,0,220,5,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,43,121,97,
0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0>>,
Rt = #macos_rt{} = fib_macos:parse_pf_route_packet(Binary),
?assertEqual(ifinfo, Rt#macos_rt.type),
{skip, todo}.
doc(String) ->
ct:comment(String),
ct:log(String).