Skip to content

Commit 138de77

Browse files
committed
checksum: test checksum with repeated roundtrip property
Arbitrary instances for TCP and UDP simply set it to zero. Since the marshal code recompute the checksum, the 0 value will not be preserved and the roundtrip property will fail. A simple way to get around this is to marshal and unmarshal a TCP or UDP packet once, which will set the chksum field to the proper value, and then check that the roundtrip property holds on that packet.
1 parent 7223d01 commit 138de77

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

lib/Packet.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,15 +1136,15 @@ let setNwTos pkt nwTos =
11361136

11371137
let tp_setTpSrc tp src = match tp with
11381138
| Ip.Tcp tcp ->
1139-
Ip.Tcp { tcp with Tcp.src = src } (* JNF: checksum? *)
1139+
Ip.Tcp { tcp with Tcp.src = src }
11401140
| Ip.Udp udp ->
11411141
Ip.Udp { udp with Udp.src = src }
11421142
| tp ->
11431143
tp
11441144

11451145
let tp_setTpDst tp dst = match tp with
11461146
| Ip.Tcp tcp ->
1147-
Ip.Tcp { tcp with Tcp.dst = dst } (* JNF: checksum? *)
1147+
Ip.Tcp { tcp with Tcp.dst = dst }
11481148
| Ip.Udp udp ->
11491149
Ip.Udp { udp with Udp.dst = dst }
11501150
| tp ->

test/Test.ml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ module RoundTrip = struct
1717
let unparsable_eq (l1, b1) (l2, b2) =
1818
l1 = l2 && compare (Cstruct.to_string b1) (Cstruct.to_string b2) = 0
1919

20-
let udp_eq e1 e2 =
20+
let udp_eq ?(chksum=false) e1 e2 =
2121
let open Udp in
2222
e1.src = e2.src &&
2323
e1.dst = e2.dst &&
24-
e1.chksum = e2.chksum &&
24+
((not chksum) || e1.chksum = e2.chksum) &&
2525
compare (Cstruct.to_string e1.payload) (Cstruct.to_string e2.payload) = 0
2626

27-
let tcp_eq e1 e2 =
27+
let tcp_eq ?(chksum=false) e1 e2 =
2828
let open Tcp in
2929
e1.src = e2.src &&
3030
e1.dst = e2.dst &&
@@ -34,9 +34,10 @@ module RoundTrip = struct
3434
e1.flags = e2.flags &&
3535
e1.window = e2.window &&
3636
e1.urgent = e2.urgent &&
37+
((not chksum) || e1.chksum = e2.chksum) &&
3738
compare (Cstruct.to_string e1.payload) (Cstruct.to_string e2.payload) = 0
3839

39-
let ip_eq e1 e2 =
40+
let ip_eq ?(chksum=false) e1 e2 =
4041
let open Ip in
4142
e1.tos = e2.tos &&
4243
e1.ident = e2.ident &&
@@ -49,13 +50,13 @@ module RoundTrip = struct
4950
| Unparsable u1, Unparsable u2 ->
5051
unparsable_eq u1 u2
5152
| Udp p1, Udp p2 ->
52-
udp_eq p1 p2
53+
udp_eq ~chksum p1 p2
5354
| Tcp p1, Tcp p2 ->
54-
tcp_eq p1 p2
55+
tcp_eq ~chksum p1 p2
5556
| _, _ ->
5657
e1 = e2
5758

58-
let dl_eq e1 e2 =
59+
let dl_eq ?(chksum=false) e1 e2 =
5960
e1.dlSrc = e2.dlSrc &&
6061
e1.dlDst = e2.dlDst &&
6162
e1.dlVlan = e2.dlVlan &&
@@ -64,12 +65,16 @@ module RoundTrip = struct
6465
| Unparsable u1, Unparsable u2 ->
6566
unparsable_eq u1 u2
6667
| Ip nw1, Ip nw2 ->
67-
ip_eq nw1 nw2
68+
ip_eq ~chksum nw1 nw2
6869
| _, _ ->
6970
e1 = e2
7071

71-
let prop_roundtrip parse marshal e =
72-
dl_eq (parse (marshal e)) e
72+
let prop_roundtrip ?(chksum=false) parse marshal e =
73+
dl_eq ~chksum (parse (marshal e)) e
74+
75+
let prop_roundtrip2 parse marshal e =
76+
let e' = parse (marshal e) in
77+
prop_roundtrip ~chksum:true parse marshal e'
7378

7479
TEST "Roundtrip property for unparsable Ethernet frames" =
7580
(packet_quickCheck (Arb.arbitrary_packet Arb.arbitrary_dl_unparsable)
@@ -91,13 +96,13 @@ module RoundTrip = struct
9196
let udp = Gen.map_gen (fun x -> Ip.Udp(x))
9297
(Arb.arbitrary_udp (Arb.arbitrary_payload 65507)) in
9398
(packet_quickCheck (mk_ip udp)
94-
(prop_roundtrip parse marshal))
99+
(prop_roundtrip2 parse marshal))
95100

96101
TEST "Roundtrip property for TCP packets" =
97102
let tcp = Gen.map_gen (fun x -> Ip.Tcp(x))
98103
(Arb.arbitrary_tcp (Arb.arbitrary_payload (65507 - 128))) in
99104
(packet_quickCheck (mk_ip tcp)
100-
(prop_roundtrip parse marshal))
105+
(prop_roundtrip2 parse marshal))
101106

102107
end
103108

0 commit comments

Comments
 (0)