text
stringlengths
12
786k
let udp_send_recv = " udp_send_recv " >::: ( List . map ( fun ( name , packet ) -> name >:: ( fun ( ) -> test_udp_send_recv packet ) ) Test_common . test_packets_internal )
let suite = " unix_suite " >::: [ udp_send_recv ; ]
let ( ) = print_endline " -------- Unix tests " ; -------- Test_common . run suite
let run args = let out , inp = pipe ( ) in let in_chan = in_channel_of_descr out in set_binary_mode_in in_chan false ; let pid = create_process ( " . " / ^ prog_name ) ( Array . of_list ( prog_name :: args ) ) Unix . stdin inp Unix . stderr in List . iter ( fun arg -> let s ...
let ( ) = List . iter run [ [ " " ; " " ; " \ t \ 011 " ] ; [ " a " ; " b " ; " c . txt " ] ; @! [ " " " ] ; \ [ " " ; " a " ; " " \ " \\\ " ] ; [ " \\ \\ " ] ; \\\\\\ [ " " \ hola " " ] ; \ ...
let errors = [ E2BIG ; EACCES ; EAGAIN ; EBADF ; EBUSY ; ECHILD ; EDEADLK ; EDOM ; EEXIST ; EFAULT ; EFBIG ; EINTR ; EINVAL ; EIO ; EISDIR ; EMFILE ; EMLINK ; ENAMETOOLONG ; ENFILE ; ENODEV ; ENOENT ; ENOEXEC ; ENOLCK ; ENOMEM ; ENOSPC ; ENOSYS ; ENOTDIR ; ENOTEM...
let unix_error_testable = Alcotest . testable ( fun ppf error -> let error_name = Unix_error . Internal_for_tests . get_constructor_name error in match error with | EUNKNOWNERR code -> Format . fprintf ppf " % s % d " error_name code | _ -> Format . fprintf ppf " % s " error_name ) ( ...
let json_encode_and_decode error = let open Data_encoding . Json in let encoding = Unix_error . encoding in destruct encoding @@ construct encoding error
let binary_encode_and_decode error = let open Data_encoding . Binary in let encoding = Unix_error . encoding in of_bytes_exn encoding @@ to_bytes_exn encoding error
let test_json ( ) = List . iter ( fun error -> let result = json_encode_and_decode error in Alcotest . ( check unix_error_testable ) " unix error equality " error result ) errors
let test_binary ( ) = List . iter ( fun error -> let result = binary_encode_and_decode error in Alcotest . ( check unix_error_testable ) " unix error equality " error result ) errors
let ( ) = let open Alcotest in run " Base . unix . unix_error " [ ( " encoding " , [ test_case " json encoding roundtrip " ` Quick test_json ; test_case " binary encoding roundtrip " ` Quick test_binary ; ] ) ; ]
let is_dead t = Result . is_error ( is_empty t )
let is_empty_exn t = ok_exn ( is_empty t )
let unpack t = let q = Queue . create ( ) in match unpack_into t q with | Ok ( ) -> Ok q | Error _ as err -> assert ( is_dead t ) ; if Queue . is_empty q then err else Ok q ; ; let buggy_unpack ~ state ( ) : ~ buf : _ ~ pos : _ ~ len : _ = ` Ok ( ( ) , 0 ) in le...
module type Value = sig type t [ @@ deriving sexp_of ] include Equal . S with type t := t val pack : t list -> string val unpack_one : t Unpack_one . t end
let test ( type value ) ( module V : Value with type t = value ) values = let input = Bigstring . of_string ( V . pack values ) in let input_size = Bigstring . length input in let chunk_sizes = List . filter ~ f ( : fun n -> 1 <= n && n <= input_size ) [ 1 ; 2 ; 13 ; inpu...
module Example = struct type t = { x : int ; y : int ; } [ @@ deriving bin_io ] let t = { x = 0 ; y = 0 ; } let pack ts = List . map ts ~ f ( : fun t -> Bin_prot . Utils . bin_dump ~ header : true bin_writer_t t ) |> Bigstring . concat ; ; let object_size = 3 let ...
let test_unzip_bug1 ( ) = let test data = let input = IO . input_string data in let unzipped = Unzip . inflate input in try let str = IO . read_all unzipped in assert ( str = " XY ) " with Unzip . Error Unzip . Invalid_data -> assert false in test " \ x78 \ xda \ x8b \ x88 \ x04 \ x00 \ x...
let inputs = [ ( " a " , " \ x78 \ x01 \ x4b \ x04 \ x00 \ x00 \ x62 \ x00 \ x62 ) " ; ( " a " , " \ x78 \ x5e \ x4b \ x04 \ x00 \ x00 \ x62 \ x00 \ x62 ) " ; ( " a " , " \ x78 \ x9c \ x4b \ x04 \ x00 \ x00 \ x62 \ x00 \ x62 ) " ; ( " a " , " \ x78 \ x...
let test_unzip_gen_inputs ( ) = let test orig data = let input = IO . input_string data in let unzipped = Unzip . inflate input in try let str = IO . read_all unzipped in assert ( str = orig ) orig with Unzip . Error Unzip . Invalid_data -> assert false in List . iter ( fun ( orig , compresse...
let ( ) = Util . register " Unzip " [ " bug1 " , test_unzip_bug1 ; " gen_inputs " , test_unzip_gen_inputs ; ]
print_s [ % sexp ( is_none none : bool ) ] ; [ % expect { | true } ] | ; ; print_s [ % sexp ( is_some none : bool ) ] ; [ % expect { | false } ] | ; ; print_s [ % sexp ( is_none ( some 13 ) : bool ) ] ; [ % expect { | false } ] | ; ; ...
let url_string_url u = Url . url_of_string ( Url . string_of_url u )
let url = " http :// ocsigen . org / js_of_ocaml " / ( match Url . url_of_string url with | None -> print_endline " can ' t parse current url2 " | Some u -> ( match url_string_url u with | None -> print_endline " can ' t parse pretty - printed url " | Some v -> if u = v then ( ) ...
let mkdir ( ? perm = 0o750 ) dir = if not ( Sys . file_exists dir ) then Unix . mkdir dir perm
let mount_inside ~ dir ~ src ~ tgt ( ? fstype " " ) = ~ flags ( ? data " " ) = ( ) = let tgt = Filename . concat dir tgt in mkdir tgt ; mount ~ source : src ~ target : tgt ~ fstype flags ~ data
let mount_base dir = mount_inside ~ dir ~ src " : proc " ~ tgt " : proc " ~ fstype " : proc " ~ flags [ : MS_NOSUID ; MS_NOEXEC ; MS_NODEV ] ( ) ; mount_inside ~ dir ~ src " :/ sys " ~ tgt " : sys " ~ flags [ : MS_BIND ; MS_REC ] ( ) ; mount_inside ~ dir ...
let do_chroot dest = Sys . chdir dest ; chroot " . " ; Sys . chdir " " /
let read_in_file fmt = Printf . ksprintf ( fun file -> let c = open_in file in let v = input_line c in close_in c ; v ) fmt
let test_userns_availability ( ) = let unpriviledge_userns_clone = " / proc / sys / kernel / unprivileged_userns_clone " in if Sys . file_exists unpriviledge_userns_clone then begin let v = read_in_file " % s " unpriviledge_userns_clone in if v <> " 1 " then begin Printf . eprintf " This...
let write_in_file fmt = Printf . ksprintf ( fun file -> Printf . ksprintf ( fun towrite -> try let cout = open_out file in output_string cout towrite ; close_out cout with _ -> Printf . eprintf " Error during write of % s in % s \ n " towrite file ; exit 1 ) ) fmt
let command fmt = Printf . ksprintf ( fun cmd -> Sys . command cmd = 0 ) fmt
let command_no_fail ( ? error ( = fun ( ) -> ( ) ) ) fmt = Printf . ksprintf ( fun cmd -> let c = Sys . command cmd in if c <> 0 then begin Printf . eprintf " Error during : % s \ n " %! cmd ; error ( ) ; exit 1 ; end ) fmt
type gpg_t = | GPGNotAvailable | GPGAvailable of string
let gpg_setup ~ dir = let command_raise fmt = Printf . ksprintf ( fun cmd -> Printf . ksprintf ( fun msg -> let c = Sys . command cmd in if c <> 0 then begin Printf . eprintf " % s \ n " %! msg ; raise Exit end ) ) fmt in try command_raise " which gpg >/ dev / null 2 >& 1 " " ...
let gpg_check file = function | GPGNotAvailable -> ( ) | GPGAvailable gpg_dir -> command_no_fail ~ error ( : fun ( ) -> Printf . eprintf " Invalid signature for % s \ n " %! file ) " GNUPGHOME =% S gpg -- verify % S > / dev / null 2 >& 1 " gpg_dir ( file " . ^ asc " ) ...
let download ( ? quiet = true ) fmt = Printf . ksprintf ( fun src -> Printf . ksprintf ( fun dst -> command " wget - T 30 % s https ://% s /% s - O % S % s " ( if quiet then " - q " else " " ) download_server src dst ( if quiet then " >/ dev / null 2 >& 1 " else " ...
let download_index ~ dir ~ gpg = let index = Filename . concat dir " index " in let url_index = " meta / 1 . 0 / index - user " in Printf . printf " Download the index : " ; %! if not ( download " % s . % i " url_index download_compat_level " % s " index && download " ...
let semicomma = Str . regexp_string " ; "
let find_image ~ distr ~ release ~ arch index = let cin = open_in index in let rec aux ( ) = match Str . split semicomma ( input_line cin ) with | [ d ; r ; a ; _ ; db ; u ] when d = distr && r = release && a = arch -> close_in cin ; db , u | _ -> aux ( ) in try aux ( ...
let download_rootfs_meta ~ dir ~ gpg ( build_id , url ) = let build_id_file = Filename . concat dir " build_id " in let rootfs_tar = Filename . concat dir " rootfs . tar . xz " in let meta_tar = Filename . concat dir " meta . tar . xz " in if not ( Sys . file_exists build_id_fi...
type userns_idmap = | KeepUser | IdMap of int * int
let set_usermap idmap pid = let curr_uid = Unix . getuid ( ) in let curr_gid = Unix . getgid ( ) in match idmap with | KeepUser -> write_in_file " / proc /% i / uid_map " pid " 0 % i 1 " curr_uid ; write_in_file " / proc /% i / gid_map " pid " 0 % i 1 " curr_gid ; | IdM...
let goto_child ~ exec_in_parent = let fin , fout = Unix . pipe ( ) in match Unix . fork ( ) with | - 1 -> Printf . eprintf " Fork failed \ n " ; %! exit 1 | 0 -> Unix . close fout ; ignore ( Unix . read fin ( Bytes . create 1 ) 0 1 ) ; Unix . close fin | pid -...
let exec_in_child ( type a ) f = let fin , fout = Unix . pipe ( ) in match Unix . fork ( ) with | - 1 -> Printf . eprintf " Fork failed \ n " ; %! exit 1 | 0 -> Unix . close fout ; let cin = Unix . in_channel_of_descr fin in let arg = ( Marshal . from_channel cin : a )...
let exec_now_in_child f arg = match Unix . fork ( ) with | - 1 -> Printf . eprintf " Fork failed \ n " ; %! exit 1 | 0 -> f arg ; exit 0 | pid -> let _ , status = Unix . waitpid [ ] pid in match status with | Unix . WEXITED 0 -> ( ) | Unix . WEXITED s -> exit s | Un...
let just_goto_child ( ) = match Unix . fork ( ) with | - 1 -> Printf . eprintf " Fork failed \ n " ; %! exit 1 | 0 -> ( ) | pid -> let _ , status = Unix . waitpid [ ] pid in match status with | Unix . WEXITED s -> exit s | Unix . WSIGNALED s -> Unix . kill ( Unix...
let go_in_userns idmap = let call_set_usermap = exec_in_child ( set_usermap idmap ) in unshare [ CLONE_NEWNS ; CLONE_NEWIPC ; CLONE_NEWPID ; CLONE_NEWUTS ; CLONE_NEWUSER ; ] ; goto_child ~ exec_in_parent : call_set_usermap
let create_rootfs ~ distr ~ release ~ arch testdir = let rootfsdir = Filename . concat testdir " rootfs " in if not ( Sys . file_exists rootfsdir ) then begin let gpg = gpg_setup ~ dir : testdir in let index = download_index ~ dir : testdir ~ gpg in let url = find_image ~ distr ~ release ~ a...
let idmap , ( cmd , arg ) , testdir , setuid , setgid , arch , distr , release = let open Arg in let testdir = ref " userns_test " in let idmap_id = ref ( - 1 ) in let idmap_rangeid = ref ( - 1 ) in let command = ref [ ] in let setuid = ref 0 in let setgid = ref 0 in let...
let ( ) = if Unix . getuid ( ) = 0 then begin Printf . eprintf " This program shouldn ' t be run as root !\ n " ; %! exit 1 end ; Unix . handle_unix_error begin fun ( ) -> test_userns_availability ( ) ; mkdir ~ perm : 0o750 testdir ; go_in_userns idmap ; let rootfsdir = ...
let prettify s = try " JSON \ n " ^ Yojson . Basic . prettify s ^ " \ n " with _ -> s
let print_req meth uri_s body = printf " Request % s % s \ n " ( Cohttp . Code . string_of_method meth ) uri_s ; ( match body with None -> ( ) | Some s -> printf " Request body :\ n % s \ n " ( prettify s ) ; ) ; flush stdout
let print_resp status body = printf " Response status : % s \ n " ( Cohttp . Code . string_of_status status ) ; printf " Response body :\ n % s \ n " ( prettify body ) ; flush stdout
let wrap ( ? headers = [ ] ) uri_s meth body = let uri = Uri . of_string uri_s in let headers = Cohttp . Header . of_list headers in let headers = match Cohttp . Header . get headers " content - length " , body with None , Some s -> Cohttp . Header . add headers " content - leng...
module Lwt_http_client = struct type ' a computation = ' a Lwt . t let bind = Lwt . bind let return = Lwt . return let head ? headers url = wrap ? headers url ` HEAD None let get ? headers url = wrap ? headers url ` GET None let post ? headers ? body url = wrap ? headers url ` POST body le...
let doctype = ` Doctype { Markup . doctype_name = Some " html " ; public_identifier = None ; system_identifier = None ; raw_text = None ; force_quirks = false }
let start_element name = ` Start_element ( ( Markup . Ns . html , name ) , [ ] )
let ok = wrong_k " failed "
type dom = | Text of string | Element of string * dom list
let tests = [ ( " utility . content " >:: fun _ -> " <? xml version ' = 1 . 0 ' ?><! DOCTYPE html ><!-- blah -->< p > foo </ p ><? bar baz " ?> |> string |> parse_xml |> signals |> content |> write_xml |> to_string |> assert_equal " < p > foo </ p " ) ; > ( " ut...
module Fixtures = struct let location = Location . { loc_start = { pos_fname = " test . ml " ; pos_lnum = 1 ; pos_bol = 0 ; pos_cnum = 5 } ; loc_end = { pos_fname = " test . ml " ; pos_lnum = 1 ; pos_bol = 0 ; pos_cnum = 10 } ; loc_ghost = false } end
module Factories = struct let ast_name_node ( ? loc = Fixtures . location ) name = { txt = name ; loc } let ast_type_node type_name = { ptyp_desc = Ptyp_constr ( ( ast_name_node ( Lident type_name ) ) , [ ] ) ; ptyp_loc = Fixtures . location ; ptyp_attributes = [ ] } ...
let test_constructor_is_bare = let open Parsetree in let test ~ constructor ~ expected ctxt = let actual = Ppx_enum_lib . Utils . constructor_is_bare constructor in assert_equal ~ ctxt ~ cmp [ :% eq : bool ] ~ printer [ :% show : bool ] expected actual in " constructor_is_bare " >::: [ " ...
let suite = " Factory " >::: [ test_constructor_is_bare ]
Quickcheck . test_distinct_values ~ sexp_of : Unstable . sexp_of_t quickcheck_generator ~ trials : 1_024 ~ distinct_values : 512 ~ compare ; [ % expect { } ] || ; ; Quickcheck . test ~ sexp_of : sexp_of_t quickcheck_generator ~ f : invariant ; [ % expect { } ] || ; ; l...
let uchar = map [ int32 ] ( fun n -> let n = ( Int32 . to_int n land 0xFFFFFFF ) mod 0x10FFFF in try Uchar . of_int n with Invalid_argument _ -> bad_test ( ) )
let unicode = list1 uchar
let norm form str = let n = Uunf . create form in let rec add acc v = match Uunf . add n v with | ` Uchar u -> add ( u :: acc ) ` Await | ` Await | ` End -> acc in let rec go acc = function | [ ] -> List . rev ( add acc ` End ) | ( v :: vs ) -> go ( add acc ( ` Uchar...
let unicode_to_string s = let b = Buffer . create 10 in List . iter ( Uutf . Buffer . add_utf_8 b ) s ; Buffer . contents b
let pp_unicode ppf s = Format . fprintf ppf " [ @< v 2 " ; > Format . fprintf ppf " [ " @\% s " ] \@@ " ( unicode_to_string s ) ; s |> List . iter ( fun u -> Format . fprintf ppf " [ @ U +% 04x % s ( % a ) ] @@ " ( Uchar . to_int u ) ( Uucp . Name ...
let unicode = with_printer pp_unicode unicode
let ( ) = add_test ~ name " : uunf " [ unicode ] @@ fun s -> let nfc = norm ` NFC s in let nfd = norm ` NFD s in let nfkc = norm ` NFKC s in let nfkd = norm ` NFKD s in let tests = [ nfc , [ norm ` NFC nfc ; norm ` NFC nfd ] ; nfd , [ norm ` NFD nfc ; norm ` NFD n...
let log_src = Logs . Src . create " distributed " ~ doc " : logs events related to the distributed library "
module Log = ( val Logs_lwt . src_log log_src : Logs_lwt . LOG )
module Test_io = struct type ' a t = ' a Lwt . t type ' a stream = ' a Lwt_stream . t type input_channel = Uwt_io . input_channel type output_channel = Uwt_io . output_channel type server = Uwt_io . server type level = Debug | Info | Warning | Error exception Timeout = Uwt_compat . Lwt_un...
let lwt_reporter log_it = let buf_fmt ( ) = let b = Buffer . create 512 in Format . formatter_of_buffer b , fun ( ) -> let m = Buffer . contents b in Buffer . reset b ; m in let app , app_flush = buf_fmt ( ) in let reporter = Logs . format_reporter ~ app ~ dst : app ( ) in let...
let log_it_quiet _ = Lwt . return ( )
let log_to_stdout = Uwt_io . write Uwt_io . stdout
let ( ) = let module Tests = Test_distributed . Make ( Test_io ) in let logger = log_it_quiet in Logs . Src . set_level log_src ( Some Logs . Debug ) ; Logs . set_reporter @@ lwt_reporter logger ; Tests . run_suite ( )
let print t = List . iter ( errors t ) ~ f : Caml . print_endline print ( all [ ( fun _ -> fail " a " ) ; ( fun _ -> pass ) ; ( fun _ -> fail " b " ) ; ( fun _ -> pass ) ; ( fun _ -> fail " c " ) ] ( ) ) ; [ % expect { | ( " " a ) ...
let two_errors = of_list [ fail " foo " ; fail " bar " ] print ( first_failure two_errors ( fail " snoo " ) ) ; [ % expect { | ( " " foo ) ( " " bar ) } ] | ; ; print ( first_failure ( fail " snoo " ) two_errors ) ; [ % expect { | ( " "...
type t = { x : bool } [ @@ deriving fields ] = let validate_x = Staged . unstage ( Validate . field_direct_folder Validate . pass_bool ) in let validate t = Fields . Direct . fold t ~ init [ ] : ~ x : validate_x |> Validate . of_list |> Validate . result in let t = { x = tr...
module Raw = struct type t = int [ @@ deriving bin_io , compare , hash , sexp , typerep ] let here = [ % here ] let validate _ = Validate . pass let validate_binio_deserialization = true let all = [ Int . min_value ; - 100 ; - 1 ; 0 ; 1 ; 100 ; Int . max_value ] end
module V = Make_bin_io_compare_hash_sexp ( Raw )
module type Output = sig type t [ @@ deriving sexp_of ] include Equal . S with type t := t end
let for_all ( type a ) raw_fn validated_fn ( module Output : Output with type t = a ) = List . iter Raw . all ~ f ( : fun raw -> let expected = raw_fn raw in let actual = validated_fn ( V . create_exn raw ) in require [ % here ] ( Output . equal expected actual ) ~ if_false_then_...
let for_all2 ( type a ) raw_fn validated_fn ( module Output : Output with type t = a ) = List . iter Raw . all ~ f ( : fun raw1 -> List . iter Raw . all ~ f ( : fun raw2 -> let expected = raw_fn raw1 raw2 in let actual = validated_fn ( V . create_exn raw1 ) ( V . create_exn raw2...
module V2 : sig type t = V . t [ @@ deriving typerep ] type t = V . t include Add_typerep ( Raw ) ( V ) end ( module struct module Positive_int = struct type t = int [ @@ deriving bin_io , sexp ] let validate t = if t > 0 then Validate . pass else Validate . fail " must be positi...
let good = [ OBus_string . validate " azerty " ; OBus_string . validate " Jérémie " ; OBus_path . validate " " ; / OBus_path . validate " / a " ; OBus_path . validate " / a / b " ; OBus_name . validate_bus " : 1 . 1 " ; OBus_name . validate_bus " : a . ...
let bad = [ OBus_string . validate " \ xe9 " ; OBus_path . validate " / dd // dd " ; OBus_path . validate " / dd " ; // OBus_path . validate " / dd " ; / OBus_path . validate " " ; OBus_name . validate_bus " : 1 . . 2 " ; OBus_name . validate_bus " a ...
let test ( ) = let % lwt ( ) = printl " Validation of all types of D - Bus strings " in let % lwt ( ) = Lwt_list . iter_s ( function | Some err -> printlf " valid string recognized as bad : % s " ( OBus_string . error_message err ) | None -> return ( ) ) good in let % lw...
let init_validator ( f : Validator . t -> Block_validator_process . t -> Store . chain_store -> ' a -> unit -> unit Lwt . t ) test_dir switch ( ) : unit Lwt . t = ( Shell_test_helpers . init_chain test_dir >>= fun store -> Shell_test_helpers . init_mock_p2p Distributed_db_version . ...
let wrap f _switch ( ) = Tztest . with_empty_mock_sink ( fun _ -> Lwt_utils_unix . with_tempdir " tezos_test_ " ( fun test_dir -> init_validator f test_dir _switch ( ) ) )
let validator_events validator block_validator chain _switch ( ) = Validator . activate ~ start_prevalidator : false validator ~ validator_process : block_validator chain >>= function | Error trace -> Format . printf " Error :\ n % a \ n " pp_print_trace trace ; Format . print_flush ( ) ...
let tests = [ Alcotest_lwt . test_case " validator_events " ` Quick ( wrap validator_events ) ]
let ( ) = Backtrace . elide := true
let show t = print_s [ % sexp ( t : t ) ]
let test_predicate f t = print_s [ % sexp ( f t : bool ) ] let test string = show ( string |> of_utf8_bytes ) in test " " ; [ % expect { | " " } ] ; | test " a " ; [ % expect { | a } ] ; | test " ( ) " ; [ % expect { | ( ) } ] ; | tes...