text
stringlengths
12
786k
let ( ) = match Bool . get ( ) with | false -> ( ) ; ; [ %% expect { | ^^^^^ ' a -> Bool . t } ] |
let ( ) = match List . get ( ) with | [ ] -> ( ) ; ; [ %% expect { | ^^ ' a -> ' b List . t } ] |
let ( ) = match List . get ( ) with | _ :: _ -> ( ) ; ; [ %% expect { | ^^^^^^ ' a -> ' b List . t } ] |
let ( ) = match Unit . get ( ) with | ( ) -> ( ) ; ; [ %% expect { | ^^ ' a -> Unit . t } ] |
let ( ) = Constr . put A ; ; [ %% expect { | ^ unit -> Constr . t } ] |
let ( ) = Record . put { a = 0 ; b = 0 ; c = 0 } ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^ unit -> Record . t } ] |
let ( ) = Bool . put true ; ; [ %% expect { | ^^^^ unit -> Bool . t } ] |
let ( ) = Bool . put false ; ; [ %% expect { | ^^^^^ unit -> Bool . t } ] |
let ( ) = List . put [ ] ; ; [ %% expect { | ^^ unit -> int List . t } ] |
let ( ) = List . put ( 1 :: 2 ) ; ; [ %% expect { | ^^^^^^^^ unit -> int List . t } ] |
let ( ) = Unit . put ( ) ; ; [ %% expect { | ^^ unit -> Unit . t } ] |
let ( ) = ignore ( ( Record . get ( ) ) . a ) ; ; [ %% expect { | ^^^^^^^^^^^^^^^ } ] |
let ( ) = ( Record . get ( ) ) . a <- 5 ; ; [ %% expect { | ^^^^^^^^^^^^^^^ } ] |
let ( ) = ignore { ( Record . get ( ) ) with a = 5 } ; ; [ %% expect { | ^^^^^^^^^^^^^^^ } ] |
let foo x = Record . put { x with a = 5 } ; ; [ %% expect { | ^^^^^^^^^^^^^^^^ unit -> Record . t } ] |
let section = Lwt_log . Section . make " wscat "
let client uri = let open Websocket in Resolver_lwt . resolve_uri ~ uri Resolver_lwt_unix . system >>= fun endp -> let ctx = Lazy . force Conduit_lwt_unix . default_ctx in Conduit_lwt_unix . endp_to_client ~ ctx endp >>= fun client -> connect ~ ctx client uri >>= fun conn -> let close_sent = ref f...
let rec react client client_id = let open Websocket in Connected_client . recv client >>= fun fr -> Lwt_log . debug_f ~ section " Client % d : % S " client_id Frame . ( show fr ) >>= fun ( ) -> match fr . opcode with | Frame . Opcode . Ping -> Connected_client . send client Frame . ...
let server uri = let id = ref ( - 1 ) in let echo_fun client = incr id ; let id = ! id in Lwt_log . info_f ~ section " Connection from client id % d " id >>= fun ( ) -> Lwt . catch ( fun ( ) -> react client id ) ( fun exn -> Lwt_log . error_f ~ section ~ exn " Client % d ...
let main is_server uri = if ! is_server then ( ignore @@ server uri ; fst @@ Lwt . wait ( ) ) else client uri
let apply_loglevel = function | 2 -> Lwt_log . ( add_rule " " * Info ) | 3 -> Lwt_log . ( add_rule " " * Debug ) | _ -> ( )
let ( ) = let uri = ref " " in let server = ref false in let speclist = Arg . align [ ( " - s " , Arg . Set server , " Run as server " ) ; ( " - loglevel " , Arg . Int apply_loglevel , " 1 - 3 Set loglevel " ) ] in let anon_fun s = uri := s in let usage_msg...
module LwtWsIteratee = Wslib . Websockets . Wsprotocol ( Lwt )
let start handler = Logs_lwt . info ( fun m -> m " Starting wsproxy " ) >>= fun ( ) -> let fd_sock = Lwt_unix . stdin in let ( ) = Lwt_unix . listen fd_sock 5 in let rec loop ( ) = let ensure_close = function | [ ] -> Lwt . return_unit | fds -> Logs_lwt . warn ( fun m -...
let proxy ( fd : Lwt_unix . file_descr ) addr protocol = let open LwtWsIteratee in let open Lwt_support in ( match protocol with | " hixie76 " -> Logs_lwt . debug ( fun m -> m " Old - style ( hixie76 ) protocol " ) >>= fun ( ) -> Lwt . return ( wsframe_old , wsunframe_old ) ...
module RX = struct let socket = Re . Str . regexp " ^/ var / run / xen / vnc [ - 0 - 9 ] " +$ let port = Re . Str . regexp " [ ^ 0 - 9 ] " +$ end
let handler sock msg = Logs_lwt . debug ( fun m -> m " Got msg : ' % s ' " msg ) >>= fun ( ) -> match Re . Str . ( split @@ regexp " [ ] " ) : msg with | ( [ protocol ; _ ; path ] | [ protocol ; path ] ) when Re . Str . string_match RX . socket path 0...
let lwt_reporter ( ) = let buf_fmt ~ like = let b = Buffer . create 512 in ( Fmt . with_buffer ~ like b , fun ( ) -> let m = Buffer . contents b in Buffer . reset b ; m ) in let app , app_flush = buf_fmt ~ like : Fmt . stdout in let dst , dst_flush = buf_fmt ~ like : Fmt . ...
let _ = Logs . set_reporter ( lwt_reporter ( ) ) ; Logs . set_level ~ all : true ( Some Logs . Info ) ; Lwt_main . run ( start handler )
let src = Logs . Src . create " fastws . async . wstest "
module Log = ( val Logs . src_log src : Logs . LOG )
module Log_async = ( val Logs_async . src_log src : Logs_async . LOG )
let url_of_test url prefix i = let open Uri in with_query ' ( with_path url " runCase " ) [ ( " casetuple " , prefix ^ " . " ^ string_of_int i ) ; ( " agent " , " fastws " ) ]
let run_test url section j = Async_uri . with_connection url ( fun { r ; w ; _ } -> Fastws_async . with_connection ( url_of_test url section j ) r w Fn . id Fn . id ( fun r w -> Pipe . iter r ~ f ( : fun ( fr : Frame . t ) -> Pipe . write w fr ) ) )
let main url section tests = Random . self_init ( ) ; Deferred . Or_error . List . iter ( List . concat tests ) ~ f ( : fun x -> Fastws_async . Raw . to_or_error ( run_test url section x ) )
let url_cmd = Command . Arg_type . create Uri . of_string
let pp_header ppf ( l , _ ) = let now = Time_ns . now ( ) in Format . fprintf ppf " % a [ % a ] " Time_ns . pp now Logs . pp_level l
let range_of_string s = match String . rsplit2 s ~ on ' ' :- with | None -> [ Int . of_string s ] | Some ( a , b ) -> let a = Int . of_string a in let b = Int . of_string b in List . range ~ start ` : inclusive ~ stop ` : inclusive a b
let range = let open Command . Arg_type in comma_separated ( map Export . string ~ f : range_of_string )
let ( ) = Command . async_or_error ~ summary " : Autobahn test client " ( let open Command . Let_syntax in [ % map_open let ( ) = Logs_async_reporter . set_level_via_param [ ] and url = anon ( " url " %: url_cmd ) and section = anon ( " section " %: string ) and tests ...
module type S = sig type ' a t val create : unit -> ' a t val push : ' a t -> ' a -> unit val pop : ' a t -> ' a val steal : ' a t -> ' a end
module CArray = struct type ' a t = ' a array let rec log2 n = if n <= 1 then 0 else 1 + ( log2 ( n asr 1 ) ) let create sz v = assert ( 0 < sz && sz = Int . shift_left 1 ( log2 sz ) ) ; assert ( Int . logand sz ( sz - 1 ) == 0 ) ; Array . make sz v let size t =...
module M : S = struct let min_size = 32 let shrink_const = 3 type ' a t = { top : int Atomic . t ; bottom : int Atomic . t ; tab : ' a ref CArray . t Atomic . t ; mutable next_shrink : int ; } let create ( ) = { top = Atomic . make 1 ; bottom = Atomic . make 1 ; tab...
type codepoint = | Point of int | Malformed
type ' a folder = ' a -> int -> codepoint -> ' a
let needed_bytes c = if 0x00 <= c && c <= 0x7F then 1 else if 0xC2 <= c && c <= 0xDF then 2 else if 0xE0 <= c && c <= 0xEF then 3 else if 0xF0 <= c && c <= 0xF4 then 4 else 0
let unsafe_char s i = Char . code ( Bytes . unsafe_get s i )
let codepoint s i = function | 1 -> unsafe_char s i | 2 -> let b0 = unsafe_char s i in let b1 = unsafe_char s ( i + 1 ) in ( ( b0 land 0x1F ) lsl 6 ) lor ( b1 land 0x3F ) | 3 -> let b0 = unsafe_char s ( i ) in let b1 = unsafe_char s ( i + 1 ) in let b2 = unsafe_char s ( i...
let fold_wtf_8 ( ? pos = 0 ) ? len f acc s = let rec loop acc f s i l = if i = l then acc else let need = needed_bytes ( unsafe_char s i ) in if need = 0 then ( loop [ @ tailcall ] ) ( f acc i Malformed ) f s ( i + 1 ) l else let rem = l - i in if rem < need then f acc i Malforme...
let add_wtf_8 buf code = let [ @ inline ] w byte = Buffer . add_char buf ( Char . unsafe_chr byte ) in if code >= 0x10000 then begin w ( 0xf0 lor ( code lsr 18 ) ) ; w ( 0x80 lor ( ( code lsr 12 ) land 0x3F ) ) ; w ( 0x80 lor ( ( code lsr 6 ) land 0x3F ) ) ; ...
type json = Yojson . Safe . t [ @@ deriving of_yojson ]
type hex = string [ @@ deriving eq ]
let pp_hex fmt s = let ( ` Hex h ) = Hex . of_string s in Format . pp_print_string fmt h
let hex_of_yojson json = let padded s = if String . length s mod 2 = 0 then s else " 0 " ^ s in match [ % of_yojson : string ] json with | Ok s -> Ok ( Hex . to_string ( ` Hex ( padded s ) ) ) | Error _ as e -> e
type test_result = Valid | Acceptable | Invalid [ @@ deriving show ]
let test_result_of_yojson = function | ` String " valid " -> Ok Valid | ` String " acceptable " -> Ok Acceptable | ` String " invalid " -> Ok Invalid | _ -> Error " test_result "
type ecdh_test = { tcId : int ; comment : string ; curve : json option ; [ @ yojson . default None ] public : hex ; private_ : hex ; [ @ yojson . key " private " ] shared : hex ; result : test_result ; flags : string list ; }
let has_ignored_flag test ~ ignored_flags = List . exists ( fun ignored_flag -> List . mem ignored_flag test . flags ) ignored_flags
type ecdh_test_group = { curve : string ; tests : ecdh_test list ; encoding : json option ; [ @ yojson . default None ] type_ : json option ; [ @ yojson . default None ] [ @ yojson . key " type " ] }
type ecdsa_key = { curve : string ; keySize : int ; type_ : json ; [ @ yojson . key " type " ] uncompressed : hex ; wx : hex ; wy : hex ; }
type dsa_test = { tcId : int ; comment : string ; msg : hex ; sig_ : hex ; [ @ yojson . key " sig " ] result : test_result ; flags : string list ; }
type ecdsa_test_group = { key : ecdsa_key ; keyDer : string ; keyPem : string ; sha : string ; tests : dsa_test list ; type_ : json option ; [ @ yojson . default None ] [ @ yojson . key " type " ] }
type eddsa_key = { curve : string ; keySize : int ; pk : hex ; sk : hex ; type_ : json ; [ @ yojson . key " type " ] }
type eddsa_test_group = { jwk : json ; key : eddsa_key ; keyDer : string ; keyPem : string ; type_ : json ; [ @ yojson . key " type " ] tests : dsa_test list ; }
type test_file = { algorithm : json ; generatorVersion : json ; header : json ; notes : json ; numberOfTests : json ; schema : json ; testGroups : json list ; }
let get_json = function Ok x -> x | Error s -> failwith s
let load_file_exn path = Yojson . Safe . from_file path |> [ % of_yojson : test_file ] |> get_json
let ecdh_test_group_exn json = [ % of_yojson : ecdh_test_group ] json |> get_json
let ecdsa_test_group_exn json = [ % of_yojson : ecdsa_test_group ] json |> get_json
let eddsa_test_group_exn json = [ % of_yojson : eddsa_test_group ] json |> get_json
type tBSCertificate = { version : [ ` V1 | ` V2 | ` V3 ] ; serial : Z . t ; signature : OID . t ; issuer : ( OID . t * string ) list list ; validity : Ptime . t * Ptime . t ; subject : ( OID . t * string ) list list ; pk_info : OID . t * Cstruct . t ; issu...
type certificate = { tbs_cert : tBSCertificate ; signature_algo : OID . t ; signature : Cstruct . t }
let def x = function None -> x | Some y -> y
let def ' x = fun y -> if y = x then None else Some y
let extensions = let extension = map ( fun ( oid , b , v ) -> ( oid , def false b , v ) ) ( fun ( oid , b , v ) -> ( oid , def ' false b , v ) ) @@ sequence3 ( required ~ label " : id " oid ) ( optional ~ label " : critical " bool ) ( required ~ labe...
let directory_name = map ( function | ` C1 s -> s | ` C2 s -> s | ` C3 s -> s | ` C4 s -> s | ` C5 s -> s | ` C6 s -> s ) ( function s -> ` C1 s ) @@ choice6 printable_string utf8_string teletex_string universal_string bmp_string ia5_string
let name = let attribute_tv = sequence2 ( required ~ label " : attr type " oid ) ( required ~ label " : attr value " directory_name ) in let rd_name = set_of attribute_tv in let rdn_sequence = sequence_of rd_name in rdn_sequence
let algorithmIdentifier = map ( fun ( oid , _ ) -> oid ) ( fun oid -> ( oid , None ) ) @@ sequence2 ( required ~ label " : algorithm " oid ) ( optional ~ label " : params " null )
let version = map ( function 2 -> ` V2 | 3 -> ` V3 | _ -> ` V1 ) ( function ` V2 -> 2 | ` V3 -> 3 | _ -> 1 ) int
let time = map ( function ` C1 t -> t | ` C2 t -> t ) ( fun t -> ` C2 t ) ( choice2 utc_time generalized_time )
let validity = sequence2 ( required ~ label " : not before " time ) ( required ~ label " : not after " time )
let subjectPublicKeyInfo = sequence2 ( required ~ label " : algorithm " algorithmIdentifier ) ( required ~ label " : subjectPK " bit_string_cs )
let tBSCertificate = let f = fun ( a , ( b , ( c , ( d , ( e , ( f , ( g , ( h , ( i , j ) ) ) ) ) ) ) ) ) -> { version = def ` V1 a ; serial = b ; signature = c ; issuer = d ; validity = e ; subject = f ; pk_info = g ; issuer_id = h ; ...
let certificate = let f ( a , b , c ) = { tbs_cert = a ; signature_algo = b ; signature = c } and g { tbs_cert = a ; signature_algo = b ; signature = c } = ( a , b , c ) in map f g @@ sequence3 ( required ~ label " : tbsCertificate " tBSCertificate ) ( required ~ l...
let cert_ber , cert_der = ( codec ber certificate , codec der certificate )
let examples = [ " 3082062030820408a003020102 0203020acd300d06092a864886 f70d0101050500305431143012 060355040a130b434163657274 20496e632e311e301c06035504 0b1315687474703a2f2f777777 2e4341636572742e6f7267311c 301a0603550403131343416365 727420436c617373203320526f 6f74301e170d31333039303431 35343333395a170d3...
let time ( ) = None
let with_loaded_file file ~ f = let fullpath = " . / testcertificates " / ^ file ^ " . pem " in let fd = Unix . ( openfile fullpath [ O_RDONLY ] 0 ) in let buf = Unix_cstruct . of_fd fd in try let r = f buf in Unix . close fd ; match r with | Ok data -> data | Error ( ` Msg...
let priv = match with_loaded_file " private / cakey " ~ f : Private_key . decode_pem with | ` RSA x -> x | _ -> assert false
let cert name = with_loaded_file name ~ f : Certificate . decode_pem
let host name = Domain_name . host_exn ( Domain_name . of_string_exn name )
let invalid_cas = [ " cacert - basicconstraint - ca - false " ; " cacert - unknown - critical - extension " ; " cacert - keyusage - crlsign " ; " cacert - ext - usage - timestamping " ]
let cert_public_is_pub cert = let pub = Mirage_crypto_pk . Rsa . pub_of_priv priv in ( match Certificate . public_key cert with | ` RSA pub ' when pub = pub ' -> ( ) | _ -> Alcotest . fail " public / private key doesn ' t match " )
let test_invalid_ca name ( ) = let c = cert name in cert_public_is_pub c ; Alcotest . ( check int " CA list is empty " 0 ( List . length ( Validation . valid_cas [ c ] ) ) )
let invalid_ca_tests = List . mapi ( fun i args -> " invalid CA " ^ string_of_int i , ` Quick , test_invalid_ca args ) invalid_cas
let cacert = cert " cacert "
let cacert_pathlen0 = cert " cacert - pathlen - 0 "
let cacert_ext = cert " cacert - unknown - extension "
let cacert_ext_ku = cert " cacert - ext - usage "
let cacert_v1 = cert " cacert - v1 "