summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2021-10-27 18:38:03 +0200
committerJordan Bracco <href@random.sh>2021-10-27 18:38:03 +0200
commit27eec8413540bfd11454cba6e35b0cc4daa5c5ad (patch)
tree405a35c02ccbc3adfb7cddd7f71bea8b2714b289 /test
🤷
Diffstat (limited to 'test')
-rw-r--r--test/fib_macos_SUITE.erl157
-rw-r--r--test/mock/netstat_rn_freebsd.txt59
-rw-r--r--test/mock/netstat_rn_linux.txt4
-rw-r--r--test/mock/netstat_rn_macos.txt81
4 files changed, 301 insertions, 0 deletions
diff --git a/test/fib_macos_SUITE.erl b/test/fib_macos_SUITE.erl
new file mode 100644
index 0000000..fd42f8d
--- /dev/null
+++ b/test/fib_macos_SUITE.erl
@@ -0,0 +1,157 @@
+-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).
diff --git a/test/mock/netstat_rn_freebsd.txt b/test/mock/netstat_rn_freebsd.txt
new file mode 100644
index 0000000..170f05d
--- /dev/null
+++ b/test/mock/netstat_rn_freebsd.txt
@@ -0,0 +1,59 @@
+Routing tables
+
+Internet:
+Destination Gateway Flags Netif Expire
+default 51.15.183.1 UGS pub
+10.1.125.2 link#10 UHS greI4VUL
+10.1.127.1 link#9 UHS lo0
+10.1.127.2 link#9 UH greI4NL1
+10.1.130.2 link#12 UHS greI4NY1
+10.69.0.0/16 link#4 U tinc
+10.69.9.1 link#4 UHS lo0
+45.150.148.0/24 link#7 U as43069
+45.150.148.1 link#7 UHS lo0
+45.150.148.225 45.150.148.224 UGHS as43069
+45.150.150.5 10.1.125.2 UGHS greI4VUL
+51.15.183.0/24 link#1 U pub
+51.15.183.74 link#1 UHS lo0
+127.0.0.1 link#3 UH lo0
+192.168.1.0/24 link#6 U local
+192.168.1.1 link#6 UHS lo0
+194.28.98.82 51.15.183.1 UGHS pub
+217.69.11.86 51.15.183.1 UGHS pub
+
+Internet6:
+Destination Gateway Flags Netif Expire
+::/96 ::1 UGRS lo0
+::1 link#3 UHS lo0
+::ffff:0.0.0.0/96 ::1 UGRS lo0
+2001:bc8:360b:101::/64 link#1 U pub
+2001:bc8:360b:101::1 link#1 UHS lo0
+2a0f:d200:45:150:150::5 fd00:d200::1111:2 UGHS gifI6NL1
+2a0f:d200:2000::/35 link#7 US as43069
+2a0f:d200:2000::1 link#3 UHS lo0
+2a0f:d200:3000::/36 link#7 US as43069
+2a0f:d207::/32 link#4 U tinc
+2a0f:d207:1:: link#4 UHS lo0
+2a0f:d207:1::/48 link#4 U tinc
+2a0f:d207:1::1 link#4 UHS lo0
+fd00:d200::1111:1 link#3 UHS lo0
+fd00:d200::1111:2 link#8 UHS gifI6NL1
+fd00:d200::1112:1 link#3 UHS lo0
+fd00:d200::1112:2 link#11 UHS gifI6VUL
+fd00:d200::1115:2 link#13 UHS gifI6NY1
+fe80::/10 ::1 UGRS lo0
+fe80::%pub/64 link#1 U pub
+fe80::ec4:7aff:fe86:8422%pub link#1 UHS lo0
+fe80::%igb1/64 link#2 U igb1
+fe80::ec4:7aff:fe86:8423%igb1 link#2 UHS lo0
+fe80::%lo0/64 link#3 U lo0
+fe80::1%lo0 link#3 UHS lo0
+fe80::%tinc/64 link#4 U tinc
+fe80::5a9c:fcff:fe10:ff9f%tinc link#4 UHS lo0
+fe80::%local/64 link#6 U local
+fe80::ff:fe00:0%local link#6 UHS lo0
+fe80::%as43069/64 link#7 U as43069
+fe80::5a9c:fcff:fe10:152a%as43069 link#7 UHS lo0
+fe80::%greI4NL1/64 link#9 U greI4NL1
+fe80::ec4:7aff:fe86:8422%greI4NL1 link#9 UHS lo0
+ff02::/16 ::1 UGRS lo0
diff --git a/test/mock/netstat_rn_linux.txt b/test/mock/netstat_rn_linux.txt
new file mode 100644
index 0000000..e23af92
--- /dev/null
+++ b/test/mock/netstat_rn_linux.txt
@@ -0,0 +1,4 @@
+Kernel IP routing table
+Destination Gateway Genmask Flags MSS Window irtt Iface
+0.0.0.0 10.73.94.80 0.0.0.0 UG 0 0 0 ens2
+10.73.94.80 0.0.0.0 255.255.255.254 U 0 0 0 ens2
diff --git a/test/mock/netstat_rn_macos.txt b/test/mock/netstat_rn_macos.txt
new file mode 100644
index 0000000..e180b75
--- /dev/null
+++ b/test/mock/netstat_rn_macos.txt
@@ -0,0 +1,81 @@
+Routing tables
+
+Internet:
+Destination Gateway Flags Netif Expire
+default 172.20.10.1 UGScg en0
+127 127.0.0.1 UCS lo0
+127.0.0.1 127.0.0.1 UH lo0
+169.254 link#12 UCS en0 !
+169.254.169.254 link#12 UHLSW en0 !
+172.20.10/28 link#12 UCS en0 !
+172.20.10.1/32 link#12 UCS en0 !
+172.20.10.1 f2:c3:71:fc:c5:64 UHLWIir en0 1192
+172.20.10.13/32 link#12 UCS en0 !
+224.0.0/4 link#12 UmCS en0 !
+224.0.0.251 1:0:5e:0:0:fb UHmLWI en0
+255.255.255.255/32 link#12 UCS en0 !
+
+Internet6:
+Destination Gateway Flags Netif Expire
+default fe80::%utun0 UGcIg utun0
+default fe80::%utun1 UGcIg utun1
+default fe80::%utun2 UGcIg utun2
+default fe80::%utun3 UGcIg utun3
+default fe80::%utun4 UGcIg utun4
+::1 ::1 UHL lo0
+fe80::%lo0/64 fe80::1%lo0 UcI lo0
+fe80::1%lo0 link#1 UHLI lo0
+fe80::%anpi1/64 link#4 UCI anpi1
+fe80::20e8:c4ff:fe50:86e8%anpi1 22:e8:c4:50:86:e8 UHLI lo0
+fe80::%anpi0/64 link#5 UCI anpi0
+fe80::20e8:c4ff:fe50:86e7%anpi0 22:e8:c4:50:86:e7 UHLI lo0
+fe80::%en0/64 link#12 UCI en0
+fe80::858:1f26:74bc:900f%en0 a0:78:17:af:bb:68 UHLI lo0
+fe80::f0c3:71ff:fefc:c564%en0 f2:c3:71:fc:c5:64 UHLWI en0
+fe80::%awdl0/64 link#14 UCI awdl0
+fe80::64fd:6aff:fe02:5fa8%awdl0 66:fd:6a:2:5f:a8 UHLI lo0
+fe80::%llw0/64 link#15 UCI llw0
+fe80::64fd:6aff:fe02:5fa8%llw0 66:fd:6a:2:5f:a8 UHLI lo0
+fe80::%utun0/64 fe80::4703:e65b:838e:fde7%utun0 UcI utun0
+fe80::4703:e65b:838e:fde7%utun0 link#16 UHLI lo0
+fe80::%utun1/64 fe80::718f:dcb8:da62:e0ba%utun1 UcI utun1
+fe80::718f:dcb8:da62:e0ba%utun1 link#17 UHLI lo0
+fe80::%utun2/64 fe80::ce81:b1c:bd2c:69e%utun2 UcI utun2
+fe80::ce81:b1c:bd2c:69e%utun2 link#18 UHLI lo0
+fe80::%utun3/64 fe80::b3c4:f14a:56b2:44d5%utun3 UcI utun3
+fe80::b3c4:f14a:56b2:44d5%utun3 link#19 UHLI lo0
+fe80::%utun4/64 fe80::e454:d779:6d6d:89d0%utun4 UcI utun4
+fe80::e454:d779:6d6d:89d0%utun4 link#20 UHLI lo0
+ff00::/8 ::1 UmCI lo0
+ff00::/8 link#4 UmCI anpi1
+ff00::/8 link#5 UmCI anpi0
+ff00::/8 link#12 UmCI en0
+ff00::/8 link#14 UmCI awdl0
+ff00::/8 link#15 UmCI llw0
+ff00::/8 fe80::4703:e65b:838e:fde7%utun0 UmCI utun0
+ff00::/8 fe80::718f:dcb8:da62:e0ba%utun1 UmCI utun1
+ff00::/8 fe80::ce81:b1c:bd2c:69e%utun2 UmCI utun2
+ff00::/8 fe80::b3c4:f14a:56b2:44d5%utun3 UmCI utun3
+ff00::/8 fe80::e454:d779:6d6d:89d0%utun4 UmCI utun4
+ff01::%lo0/32 ::1 UmCI lo0
+ff01::%anpi1/32 link#4 UmCI anpi1
+ff01::%anpi0/32 link#5 UmCI anpi0
+ff01::%en0/32 link#12 UmCI en0
+ff01::%awdl0/32 link#14 UmCI awdl0
+ff01::%llw0/32 link#15 UmCI llw0
+ff01::%utun0/32 fe80::4703:e65b:838e:fde7%utun0 UmCI utun0
+ff01::%utun1/32 fe80::718f:dcb8:da62:e0ba%utun1 UmCI utun1
+ff01::%utun2/32 fe80::ce81:b1c:bd2c:69e%utun2 UmCI utun2
+ff01::%utun3/32 fe80::b3c4:f14a:56b2:44d5%utun3 UmCI utun3
+ff01::%utun4/32 fe80::e454:d779:6d6d:89d0%utun4 UmCI utun4
+ff02::%lo0/32 ::1 UmCI lo0
+ff02::%anpi1/32 link#4 UmCI anpi1
+ff02::%anpi0/32 link#5 UmCI anpi0
+ff02::%en0/32 link#12 UmCI en0
+ff02::%awdl0/32 link#14 UmCI awdl0
+ff02::%llw0/32 link#15 UmCI llw0
+ff02::%utun0/32 fe80::4703:e65b:838e:fde7%utun0 UmCI utun0
+ff02::%utun1/32 fe80::718f:dcb8:da62:e0ba%utun1 UmCI utun1
+ff02::%utun2/32 fe80::ce81:b1c:bd2c:69e%utun2 UmCI utun2
+ff02::%utun3/32 fe80::b3c4:f14a:56b2:44d5%utun3 UmCI utun3
+ff02::%utun4/32 fe80::e454:d779:6d6d:89d0%utun4 UmCI utun4