···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2025 Thomas Gazagnaire. All rights reserved.
33+ SPDX-License-Identifier: MIT
44+ ---------------------------------------------------------------------------*)
55+66+open Crowbar
77+88+(* TLV roundtrip: encode(decode(x)) should produce valid TLV *)
99+let test_tlv_decode_encode input =
1010+ (* Decoding arbitrary bytes should not crash *)
1111+ let decoded = Hap.Tlv.decode input in
1212+ (* Re-encoding should produce valid TLV *)
1313+ let _ = Hap.Tlv.encode decoded in
1414+ check true
1515+1616+(* TLV encode/decode roundtrip for valid TLV *)
1717+let test_tlv_roundtrip typ value =
1818+ let tlv = Hap.Tlv.(add typ value empty) in
1919+ let encoded = Hap.Tlv.encode tlv in
2020+ let decoded = Hap.Tlv.decode encoded in
2121+ let retrieved = Hap.Tlv.get typ decoded in
2222+ check_eq ~pp:Format.pp_print_bool (retrieved = Some value) true
2323+2424+(* Multiple TLV entries roundtrip *)
2525+let test_tlv_multi_roundtrip entries =
2626+ let tlv =
2727+ List.fold_left
2828+ (fun acc (typ, value) -> Hap.Tlv.add typ value acc)
2929+ Hap.Tlv.empty entries
3030+ in
3131+ let encoded = Hap.Tlv.encode tlv in
3232+ let decoded = Hap.Tlv.decode encoded in
3333+ (* Check that all entries can be retrieved *)
3434+ List.iter
3535+ (fun (typ, value) ->
3636+ let retrieved = Hap.Tlv.get typ decoded in
3737+ check_eq ~pp:Format.pp_print_bool (retrieved = Some value) true)
3838+ entries
3939+4040+(* Category name should never crash *)
4141+let test_category_name code =
4242+ let _ = Hap.category_name code in
4343+ check true
4444+4545+let () =
4646+ add_test ~name:"hap: TLV decode/encode no crash" [ bytes ]
4747+ test_tlv_decode_encode;
4848+ add_test ~name:"hap: TLV roundtrip" [ range 256; bytes ] test_tlv_roundtrip;
4949+ add_test ~name:"hap: TLV multi roundtrip"
5050+ [ list (pair (range 256) bytes) ]
5151+ test_tlv_multi_roundtrip;
5252+ add_test ~name:"hap: category_name no crash" [ int ] test_category_name