123456789101112131415161718192021222324252627282930313233343536373839404142open!Core_kerneltypet=|Krb|Krb_test_mode|Rpc[@@derivingcompare,enumerate,sexp]letmagic_word=function|Krb->"KRB2"|Krb_test_mode->"KBT"|Rpc->"RPC";;letgen_magic_numberword=String.to_list_revword|>List.fold~init:0~f:(funaccc->(acc*256)+Char.to_intc);;letmagic_numbert=gen_magic_number(magic_wordt)letby_magic_number=Int.Map.of_alist_exn(List.mapall~f:(funp->magic_numberp,p))(* We once minted a new magic number for Krb in order to change the protocol
negotiation. Let's be careful that we don't reuse the old magic number *)letretired_krb_word="KRB"let%test_unit"validate magic words"=letmagic_words=retired_krb_word::List.mapall~f:magic_wordinletmagic_numbers=List.mapmagic_words~f:gen_magic_numberin(* Magic numbers must fit into Ocaml integers (31 bits on 32 bit builds). *)assert(List.for_allmagic_numbers~f:(funn->n<=Int.of_float((2.**30.)-.1.)));(* No duplicate magic numbers *)assert(not(List.contains_dupmagic_numbers~compare:Int.compare));;(* Ensure tests break if the magic numbers are changed *)let%test_unit"magic numbers"=assert(gen_magic_numberretired_krb_word=4_346_443);assert(magic_numberKrb=843_207_243);assert(magic_numberKrb_test_mode=5_521_995);assert(magic_numberRpc=4_411_474);;