···7788(** {1 Decode crash safety} *)
991010-let test_decode_crash buf =
1111- ignore (Aos.decode buf)
1010+let test_decode_crash buf = ignore (Aos.decode buf)
12111312(** {1 Roundtrip: encode then decode recovers the same frame} *)
1413···3231 match Aos.decode encoded with
3332 | Error e -> fail (Fmt.str "decode failed: %a" Aos.pp_error e)
3433 | Ok frame' ->
3535- check_eq ~pp:Fmt.int (Aos.scid_to_int frame.header.scid)
3434+ check_eq ~pp:Fmt.int
3535+ (Aos.scid_to_int frame.header.scid)
3636 (Aos.scid_to_int frame'.header.scid);
3737 check_eq ~pp:Fmt.(option int) frame.ocf frame'.ocf)
3838 | _ -> ()
···5757 let encoded = Aos.encode ~with_fecf:true frame in
5858 let len = String.length encoded in
5959 if len > 2 then begin
6060- let byte_pos = (bit_pos mod (len - 2)) in
6060+ let byte_pos = bit_pos mod (len - 2) in
6161 let buf = Bytes.of_string encoded in
6262 let old = Bytes.get_uint8 buf byte_pos in
6363 Bytes.set_uint8 buf byte_pos (old lxor 1);
···6767 | Ok _ ->
6868 (* Allowed: if the flip is in FECF itself, the CRC may
6969 accidentally match. Otherwise this is a real miss. *)
7070- if byte_pos < len - 2 then
7171- fail "FECF did not detect corruption"
7070+ if byte_pos < len - 2 then fail "FECF did not detect corruption"
7271 end
7372 | _ -> ()
7473···85848685let test_clcw_roundtrip scid_val vcid_val vcfc clcw_vcid report_value data =
8786 match (Aos.scid scid_val, Aos.vcid vcid_val) with
8888- | Some scid, Some vcid ->
8787+ | Some scid, Some vcid -> (
8988 let clcw_vcid = clcw_vcid mod 64 in
9089 let report_value = report_value mod 256 in
9190 let clcw = Clcw.v ~vcid:clcw_vcid ~report_value () in
9291 let frame = Aos.with_clcw ~scid ~vcid ~vcfc ~clcw data in
9392 let encoded = Aos.encode frame in
9494- (match Aos.decode encoded with
9393+ match Aos.decode encoded with
9594 | Error e -> fail (Fmt.str "decode failed: %a" Aos.pp_error e)
9695 | Ok frame' -> (
9796 match Aos.clcw frame' with
···114113115114let test_wire_header_roundtrip buf =
116115 let buf =
117117- if String.length buf < 6 then buf ^ String.make (6 - String.length buf) '\x00'
116116+ if String.length buf < 6 then
117117+ buf ^ String.make (6 - String.length buf) '\x00'
118118 else String.sub buf 0 6
119119 in
120120 let bytes_buf = Bytes.of_string buf in
121121 match Aos.decode_bytes bytes_buf with
122122 | Error _ -> ()
123123- | Ok hdr ->
123123+ | Ok hdr -> (
124124 let encoded = Aos.encode_bytes hdr in
125125- (match Aos.decode_bytes encoded with
125125+ match Aos.decode_bytes encoded with
126126 | Error _ -> fail "re-decode of encoded header failed"
127127 | Ok hdr' -> check_eq ~pp:pp_int hdr.version hdr'.version)
128128
+43-43
lib/aos.ml
···410410 let min_size =
411411 header_len + insert_zone_len
412412 + (if expect_ocf then ocf_len else 0)
413413- + (if expect_fecf then fecf_len else 0)
413413+ + if expect_fecf then fecf_len else 0
414414 in
415415 if frame_len < min_size then
416416 Error (Truncated { need = min_size; have = frame_len })
417417 else
418418- match Wire.Codec.decode_with frame_codec env frame_buf 0 with
419419- | Error _ -> Error (Truncated { need = frame_len; have = buf_len })
420420- | Ok pf -> (
421421- if pf.pf_version > 1 then Error (Invalid_version pf.pf_version)
422422- else
423423- match
424424- of_packed_header
425425- {
426426- version = pf.pf_version;
427427- scid = pf.pf_scid;
428428- vcid = pf.pf_vcid;
429429- vcfc = pf.pf_vcfc;
430430- replay_flag = pf.pf_replay_flag;
431431- vc_count_flag = pf.pf_vc_count_flag;
432432- spare = pf.pf_spare;
433433- vc_count_cycle = pf.pf_vc_count_cycle;
434434- }
435435- with
436436- | Error `Invalid_scid -> Error (Invalid_scid pf.pf_scid)
437437- | Error `Invalid_vcid -> Error (Invalid_vcid pf.pf_vcid)
438438- | Ok header ->
439439- let insert_zone =
440440- if insert_zone_len > 0 then Some pf.pf_insert_zone else None
441441- in
442442- let frame =
418418+ match Wire.Codec.decode_with frame_codec env frame_buf 0 with
419419+ | Error _ -> Error (Truncated { need = frame_len; have = buf_len })
420420+ | Ok pf -> (
421421+ if pf.pf_version > 1 then Error (Invalid_version pf.pf_version)
422422+ else
423423+ match
424424+ of_packed_header
443425 {
444444- header;
445445- insert_zone;
446446- data = pf.pf_data;
447447- ocf = pf.pf_ocf;
448448- fecf = pf.pf_fecf;
426426+ version = pf.pf_version;
427427+ scid = pf.pf_scid;
428428+ vcid = pf.pf_vcid;
429429+ vcfc = pf.pf_vcfc;
430430+ replay_flag = pf.pf_replay_flag;
431431+ vc_count_flag = pf.pf_vc_count_flag;
432432+ spare = pf.pf_spare;
433433+ vc_count_cycle = pf.pf_vc_count_cycle;
449434 }
450450- in
451451- if expect_fecf && check_fecf then
452452- match pf.pf_fecf with
453453- | Some actual ->
454454- let computed =
455455- compute_fecf (String.sub buf 0 (frame_len - 2))
456456- in
457457- if computed <> actual then
458458- Error (Fecf_mismatch { expected = computed; actual })
459459- else Ok frame
460460- | None -> Ok frame
461461- else Ok frame)
435435+ with
436436+ | Error `Invalid_scid -> Error (Invalid_scid pf.pf_scid)
437437+ | Error `Invalid_vcid -> Error (Invalid_vcid pf.pf_vcid)
438438+ | Ok header ->
439439+ let insert_zone =
440440+ if insert_zone_len > 0 then Some pf.pf_insert_zone else None
441441+ in
442442+ let frame =
443443+ {
444444+ header;
445445+ insert_zone;
446446+ data = pf.pf_data;
447447+ ocf = pf.pf_ocf;
448448+ fecf = pf.pf_fecf;
449449+ }
450450+ in
451451+ if expect_fecf && check_fecf then
452452+ match pf.pf_fecf with
453453+ | Some actual ->
454454+ let computed =
455455+ compute_fecf (String.sub buf 0 (frame_len - 2))
456456+ in
457457+ if computed <> actual then
458458+ Error (Fecf_mismatch { expected = computed; actual })
459459+ else Ok frame
460460+ | None -> Ok frame
461461+ else Ok frame)
462462463463let encode_header buf off hdr =
464464 let packed = to_packed_header hdr in