text
stringlengths
12
786k
module type TEST = sig val name : string val run : unit -> unit tzresult Lwt . t end
let canceler = Lwt_canceler . create ( )
module Authentication = struct open P2p_test_utils let name = " authentication " let expected_section = Internal_event . Section . make_sanitized [ " p2p " ; " connection " ] let auth_event = Mock_sink . Pattern . { level = Some Internal_event . Debug ; section = Some ( Some ( ...
module Nack = struct open P2p_test_utils let name = " nack " let expected_section = Internal_event . Section . make_sanitized [ " p2p " ; " connection " ] let nack_event = Mock_sink . Pattern . { level = Some Internal_event . Debug ; section = Some ( Some ( Internal_event . S...
module Read_and_write = struct open P2p_test_utils let name = " read_and_write " let expected_section = Internal_event . Section . make_sanitized [ " p2p " ; " socket " ] let read_event = Mock_sink . Pattern . { name = " socket_read " ; level = Some Debug ; section = Some ( ...
module P2p_net = struct let name = " activate_network " let conf identity = P2p . { listening_port = None ; listening_addr = None ; advertised_port = None ; discovery_port = None ; discovery_addr = None ; trusted_points = [ ] ; peers_file = Filename . temp_file " test_p2p_logging "...
let lwt_log_sink = Lwt_log_sink_unix . create_cfg ~ rules " :* -> debug " ( )
let testcase ( module T : TEST ) = Alcotest_lwt . test_case T . name ` Quick ( fun _switch ( ) -> let open Lwt_syntax in let * ( ) = Internal_event_unix . init ~ lwt_log_sink ( ) in Tztest . with_empty_mock_sink ( fun ( ) -> let * r = T . run ( ) in match r with | Ok (...
let ( ) = Lwt_main . run @@ Alcotest_lwt . run ~ argv [ " " ] :|| " tezos - p2p " [ ( " p2p - logging " , [ testcase ( module Authentication ) ; testcase ( module Nack ) ; testcase ( module Read_and_write ) ; testcase ( module P2p_net ) ; ] ) ; ]
module Event = struct include Internal_event . Simple let section = [ " test " ; " p2p " ; " node " ] let port_conflicts = declare_0 ~ section ~ name " : port_conflicts " ~ msg " : conflict on ports , retry the test " ~ level : Warning ( ) end
type error += Some_error | Invalid_test_result
let ( ) = register_error_kind ` Permanent ~ id " : test_p2p_node . some_error " ~ title " : An error use to simulate a failing test . " ~ description " : This error is used to simulate a failing test . " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " This error is used to sim...
let propagation_tzresult points = let open Lwt_tzresult_syntax in let x = Random . int ( List . length points ) in let *! r = Node . detach_nodes ( fun i _ -> if x = i then Error_monad . fail Some_error else return_unit ) points in match r with Ok ( ) -> fail Invalid_test_result | Error _ ...
let points = ref [ ]
let gen_points ( ) = let clients = 4 in let addr = Ipaddr . V6 . localhost in let port = 49152 + Random . int 16384 in let ports = port -- ( port + clients - 1 ) in points := List . map ( fun port -> ( addr , port ) ) ports
let wrap n f = Alcotest_lwt . test_case n ` Quick ( fun _ ( ) -> let rec aux n f = let open Lwt_syntax in let * r = f ( ) in match r with | Ok ( ) -> Lwt . return_unit | Error ( Exn ( Unix . Unix_error ( ( EADDRINUSE | EADDRNOTAVAIL ) , _ , _ ) ) :: _ ) -> let *...
let main ( ) = let ( ) = let lwt_log_sink = Lwt_log_sink_unix . create_cfg ~ rules " : test . p2p . node -> debug ; " ( ) in Lwt_main . run ( Tezos_base_unix . Internal_event_unix . init ~ lwt_log_sink ( ) ) in gen_points ( ) ; Lwt_main . run @@ Alcotest_lwt . run...
let ( ) = Sys . catch_break true ; try main ( ) with _ -> ( )
let name = " test - p2p - banned_peers " end )
let assert_equal_bool ~ msg a b = if a <> b then Alcotest . fail msg
let a s = P2p_peer . Id . hash_string [ s ]
let test_empty _ = let peers = List . map a [ " foo " ; " bar " ; " baz " ] in let empty = P2p_acl . PeerFIFOCache . create 10 in List . iter ( fun peer -> assert_equal_bool ~ msg : __LOC__ false ( P2p_acl . PeerFIFOCache . mem empty peer ) ) peers
let test_add _ = let peers = List . map a [ " foo " ; " bar " ; " baz " ] in let set = P2p_acl . PeerFIFOCache . create 10 in List . iter ( fun peer -> P2p_acl . PeerFIFOCache . add set peer ) peers ; List . iter ( fun peer -> assert_equal_bool ~ msg : __LOC__ true ( ...
let test_remove _ = let peers = List . map a [ " foo " ; " bar " ; " baz " ] in let set = P2p_acl . PeerFIFOCache . create 10 in List . iter ( fun peer -> P2p_acl . PeerFIFOCache . add set peer ) peers ; assert_equal_bool ~ msg : __LOC__ true ( P2p_acl . PeerFIFOCache ...
let test_LRU_overflow _ = let peers = List . map a [ " foo " ; " bar " ; " baz " ] in let set = P2p_acl . PeerFIFOCache . create 3 in List . iter ( fun peer -> P2p_acl . PeerFIFOCache . add set peer ) peers ; P2p_acl . PeerFIFOCache . add set ( a " foo " ) ; P2p...
let ( ) = Alcotest . run ~ argv [ " " ] :|| " tezos - p2p " [ ( " p2p . peerset " , [ ( " empty " , ` Quick , test_empty ) ; ( " add " , ` Quick , test_add ) ; ( " LRU overflow " , ` Quick , test_LRU_overflow ) ; ( " remove " , ...
let name = " test . p2p . connection - pool " end )
type error += Connect | Write | Read
module Simple = struct let rec connect ~ timeout connect_handler pool point = let open Lwt_syntax in let * ( ) = lwt_log_info " Connect to % a " P2p_point . Id . pp point in let * r = P2p_connect_handler . connect connect_handler point ~ timeout in match r with | Error ( Tezos_p2p_services . ...
module Random_connections = struct let rec connect_random connect_handler pool total rem point n = let open Lwt_result_syntax in let *! ( ) = Lwt_unix . sleep ( 0 . 2 . + Random . float 1 . 0 ) in let * conn = trace Connect @@ Simple . connect ~ timeout ( : Time . System . Span ....
module Garbled = struct let is_connection_closed = function | Error ( ( Write | Read ) :: Tezos_p2p_services . P2p_errors . Connection_closed :: _ ) -> true | Ok _ -> false | Error err -> log_info " Unexpected error : % a . " @ pp_print_trace err ; false let write_bad_all conns = le...
module Overcrowded = struct type error += Advertisement_failure of P2p_point . Id . t list let ( ) = register_error_kind ` Permanent ~ id " : test_p2p_pool . Overcrowded . advertisement_failure " ~ title " : Advertisement Failure " ~ description " : The given list of points should be kn...
module No_common_network = struct let rec connect ? iter_count ~ timeout connect_handler pool point = let open Lwt_syntax in let * ( ) = lwt_log_info " Connect % a to [ @% a ] . " @@ ( fun ppf iter_count -> Option . iter ( Format . fprintf ppf " to peer % d " ) iter_count ) iter...
let ( ) = Random . self_init ( )
let addr = ref Ipaddr . V6 . localhost
let port = ref None
let clients = ref 10
let repeat_connections = ref 5
let log_config = ref None
let spec = Arg . [ ( " -- port " , Int ( fun p -> port := Some p ) , " Listening port of the first peer . " ) ; ( " -- addr " , String ( fun p -> addr := Ipaddr . V6 . of_string_exn p ) , " Listening addr " ) ; ( " -- clients " , Set_int clients , ...
let init_logs = lazy ( Tezos_base_unix . Internal_event_unix . init ? lwt_log_sink :! log_config ( ) )
let points = ref [ ]
let gen_points ( ) = let clients = ! clients in let addr = ! addr in let port = Option . value_f ~ default ( : fun ( ) -> 49152 + Random . int 16384 ) ! port in let ports = port -- ( port + clients - 1 ) in points := List . map ( fun port -> ( addr , port ) ) ports
let wrap n f = Alcotest_lwt . test_case n ` Quick ( fun _ ( ) -> let open Lwt_syntax in let * ( ) = Lazy . force init_logs in let rec aux n f = let * r = f ( ) in match r with | Ok ( ) -> Lwt . return_unit | Error ( Exn ( Unix . Unix_error ( ( EADDRINUSE | EADDRNOTAVAIL )...
let main ( ) = let anon_fun _num_peers = raise ( Arg . Bad " No anonymous argument . " ) in let usage_msg = " Usage : % s < num_peers . >\ nArguments are " : in Arg . parse spec anon_fun usage_msg ; gen_points ( ) ; Lwt_main . run @@ Alcotest_lwt . run ~ argv [ " " ]...
let ( ) = Sys . catch_break true ; try main ( ) with _ -> ( )
let name = " test . p2p . connection " end )
let tzassert b pos = let p ( file , lnum , cnum , _ ) = ( file , lnum , cnum ) in if b then return_unit else fail_with_exn ( Assert_failure ( p pos ) )
let high_pow_target = Crypto_box . make_pow_target 100 .
let sync ch = let open Lwt_result_syntax in let * ( ) = Process . Channel . push ch ( ) in let * ( ) = Process . Channel . pop ch in return_unit
let connect ? proof_of_work_target ( ? target_id = id1 ) sched addr port id = let open Lwt_result_syntax in let * ( info , auth_fd ) = P2p_test_utils . connect ? proof_of_work_target sched addr port id in let *! id1 = target_id in let * ( ) = tzassert ( not info . incoming ) __POS__ in...
let is_connection_closed = function | Error ( Tezos_p2p_services . P2p_errors . Connection_closed :: _ ) -> true | Ok _ -> false | Error err -> log_notice " Error : % a " pp_print_trace err ; false
let is_decoding_error = function | Error ( Tezos_p2p_services . P2p_errors . Decoding_error _ :: _ ) -> true | Ok _ -> false | Error err -> log_notice " Error : % a " pp_print_trace err ; false
module Crypto_test = struct let bufsize = ( 1 lsl 16 ) - 1 let header_length = 2 let tag_length = Crypto_box . tag_length let extrabytes = header_length + tag_length let max_content_length = bufsize - extrabytes type data = { channel_key : Crypto_box . channel_key ; mutable local_nonce : Cry...
module Pow_check = struct let is_failing = function | Error ( P2p_errors . Not_enough_proof_of_work _ :: _ ) -> true | _ -> false let server _ch sched socket = let open Lwt_syntax in let * res = accept ~ proof_of_work_target : high_pow_target sched socket in tzassert ( is_failing res ) __POS__...
module Low_level = struct let simple_msg = Rand . generate ( 1 lsl 4 ) let client ch sched addr port = let open Lwt_result_syntax in let msg = Bytes . create ( Bytes . length simple_msg ) in let *! fd = raw_connect sched addr port in let * ( ) = P2p_buffer_reader . ( read_full ( P2p_io...
module Nack = struct let encoding = Data_encoding . bytes let is_rejected = function | Error ( Tezos_p2p_services . P2p_errors . Rejected_by_nack _ :: _ ) -> true | Ok _ -> false | Error err -> log_notice " Error : % a " pp_print_trace err ; false let server ch sched socket = let open Lw...
module Nacked = struct let encoding = Data_encoding . bytes let server ch sched socket = let open Lwt_result_syntax in let * ( _info , auth_fd ) = accept sched socket in let *! conn = P2p_socket . accept ~ canceler auth_fd encoding in let * ( ) = tzassert ( Nack . is_rejected conn ) __POS...
module Simple_message = struct let encoding = Data_encoding . bytes let simple_msg = Rand . generate ( 1 lsl 4 ) let simple_msg2 = Rand . generate ( 1 lsl 4 ) let server ch sched socket = let open Lwt_result_syntax in let * ( _info , auth_fd ) = accept sched socket in let * conn = P2p_...
module Chunked_message = struct let encoding = Data_encoding . bytes let simple_msg = Rand . generate ( 1 lsl 8 ) let simple_msg2 = Rand . generate ( 1 lsl 8 ) let server ch sched socket = let open Lwt_result_syntax in let * ( _info , auth_fd ) = accept sched socket in let * conn = P2p...
module Oversized_message = struct let encoding = Data_encoding . bytes let rec rand_gen ( ) = try Rand . generate ( 1 lsl 17 ) with _ -> log_error " Not enough entropy , retrying to generate random data " ; rand_gen ( ) let simple_msg = rand_gen ( ) let simple_msg2 = rand_gen ( ) ...
module Close_on_read = struct let encoding = Data_encoding . bytes let server ch sched socket = let open Lwt_result_syntax in let * ( _info , auth_fd ) = accept sched socket in let * conn = P2p_socket . accept ~ canceler auth_fd encoding in let * ( ) = sync ch in let *! _stat = P2p_socket . ...
module Close_on_write = struct let encoding = Data_encoding . bytes let simple_msg = Rand . generate ( 1 lsl 4 ) let server ch sched socket = let open Lwt_result_syntax in let * ( _info , auth_fd ) = accept sched socket in let * conn = P2p_socket . accept ~ canceler auth_fd encoding in let ...
module Garbled_data = struct let encoding = let open Data_encoding in dynamic_size @@ option @@ string let garbled_msg = let buf = Bytes . create ( 1 lsl 4 ) in TzEndian . set_int32 buf 0 ( Int32 . of_int 4 ) ; TzEndian . set_int32 buf 4 ( Int32 . of_int ( - 1 ) ) ; TzEndian ....
let log_config = ref None
let spec = Arg . [ ( " -- addr " , String ( fun p -> addr := Ipaddr . V6 . of_string_exn p ) , " Listening addr " ) ; ( " - v " , Unit ( fun ( ) -> log_config := Some ( Lwt_log_sink_unix . create_cfg ~ rules " : test . p2p . connection -> info ; p2p . ...
let init_logs = lazy ( Tezos_base_unix . Internal_event_unix . init ? lwt_log_sink :! log_config ( ) )
let wrap n f = Alcotest_lwt . test_case n ` Quick ( fun _ ( ) -> let open Lwt_syntax in let * ( ) = Lazy . force init_logs in let * r = f ( ) in match r with | Ok ( ) -> Lwt . return_unit | Error error -> Format . kasprintf Stdlib . failwith " % a " pp_print_trace error )
let main ( ) = let anon_fun _num_peers = raise ( Arg . Bad " No anonymous argument . " ) in let usage_msg = " Usage : % s . \ nArguments are " : in Arg . parse spec anon_fun usage_msg ; Lwt_main . run @@ Alcotest_lwt . run ~ argv [ " " ] :|| " tezos - p2p " [ ( ...
let ( ) = Sys . catch_break true ; try main ( ) with _ -> ( )
module Heap = Pairing_heap let test list = let heap = Heap . of_list list ~ cmp : Int . compare in print_s [ % sexp ( heap : int Heap . t ) ] ; while not ( Heap . is_empty heap ) do ignore ( Heap . pop_exn heap : int ) ; print_s [ % sexp ( heap : int Heap . t ) ] done i...
let test_copy ~ add_removable ~ remove = let sum t = fold t ~ init : 0 ~ f ( : fun acc i -> acc + i ) in let t = create ~ cmp : Int . compare ( ) in for i = 1 to 99 do add t i ; if i % 10 = 0 then ( ignore ( pop t ) ; add t i ) done ; let token = add_removable t 100 in inv...
let test_removal ~ add_removable ~ remove ~ elt_value_exn = let t = create ~ cmp : Int . compare ( ) in let tokens = ref [ ] in for i = 1 to 10_000 do tokens := add_removable t i :: ! tokens done ; invariant Fn . ignore t ; List . iter ! tokens ~ f ( : fun token -> if elt_value_exn...
let test_ordering ( ) = let t = create ~ cmp : Int . compare ( ) in for _ = 1 to 10_000 do add t ( Random . int 100_000 ) done ; let rec loop last count = if count % 1_000 = 0 then invariant Fn . ignore t ; match pop t with | None -> ( ) | Some v -> assert ( v >= last ) ...
module Q = struct open Caqti_request . Infix open Caqti_type . Std let create = unit . --> unit @:- " CREATE TABLE test_parallel ( x int NOT NULL , y int NOT NULL ) " let drop = unit . --> unit @:- " DROP TABLE IF EXISTS test_parallel " let insert = tup2 int int . --> unit @:- " INSE...
let random_int ( ) = Random . int ( 1 + Random . int 16 )
module Make ( Ground : Testlib . Sig . Ground ) = struct open Ground let do_query pool = pool |> Caqti_sys . Pool . use @@ fun ( module C : Caqti_sys . CONNECTION ) -> ( match Random . int 4 with | 0 -> C . exec Q . insert ( random_int ( ) , random_int ( ) ) >>=? fun ...
module Q = struct open Caqti_request . Infix open Caqti_type . Std let nonlin1 = tup3 int int int -->! int @@ fun _ -> S [ L " SELECT 2 * " ; P 2 ; L " + " ; P 2 ; L " - 3 * " ; P 0 ; L " + 5 * " ; P 1 ] let nonlin2 = tup3 int int int -->! int @:- " SELECT 2...
module Make ( Ground : Testlib . Sig . Ground ) = struct open Ground let nonlin ( p0 , p1 , p2 ) = 2 * p2 + p2 - 3 * p0 + 5 * p1 let test_nonlin ( module Db : Caqti_sys . CONNECTION ) = let rec loop n = if n = 0 then return ( ) else let p = ( Random . int 1000 , Random...
let compare_ids ( ID expected_id ) ( ID actual_id ) = String . equal expected_id actual_id
let compare_params ( Param ( t1 , id1 ) ) ( Param ( t2 , id2 ) ) = ( t1 == t2 ) && ( compare_ids id1 id2 )
let compare_consts expected actual = match expected , actual with | Int i1 , Int i2 -> i1 == i2 | Char c1 , Char c2 -> c1 == c2 | _ -> false
let rec compare_exps expected actual = match expected , actual with | Const expected_const , Const actual_const -> compare_consts expected_const actual_const | Var id1 , Var id2 -> compare_ids id1 id2 | BinOp ( expected_op , expected_e1 , expected_e2 ) , BinOp ( actual_op , actual_e1 , actua...
let compare_optional_exps expected actual = match expected , actual with | None , None -> true | Some e1 , Some e2 -> compare_exps e1 e2 | _ -> false
let compare_declarations { var_type = t1 ; var_name = id1 ; init = init1 ; } { var_type = t2 ; var_name = id2 ; init = init2 ; } = t1 == t2 && compare_ids id1 id2 && match init1 , init2 with | None , None -> true | Some e1 , Some e2 -> compare_exps e1 e2 | _ -> false
let rec compare_statements expected actual = match expected , actual with | ReturnVal v1 , ReturnVal v2 -> compare_exps v1 v2 | Block stmts1 , Block stmts2 -> List . for_all2 compare_block_items stmts1 stmts2 | If { cond = cond1 ; if_body = then1 ; else_body = else1 } , If { cond = cond2 ;...
let compare_funs expected actual = match expected , actual with | FunDecl { fun_type = t1 ; name = name1 ; params = params1 ; body = Some body1 } , FunDecl { fun_type = t2 ; name = name2 ; params = params2 ; body = Some body2 } -> t1 == t2 && compare_ids name1 name2 && List . for...
let compare_asts ( Prog fun_list1 ) ( Prog fun_list2 ) = List . for_all2 compare_funs fun_list1 fun_list2
let make_int i = Const ( Int i )
let make_exp_statement e = Statement ( Exp ( Some e ) )
let print_test_failure expected actual = let _ = print_string " Expected AST :\ n " in let _ = Pprint . pprint expected in let _ = print_string " Actual AST :\ n " in let _ = Pprint . pprint actual in ( )
let test_expect_failure input fail_msg test_ctxt = let f = fun ( ) -> Parse . parse input in assert_raises ( Failure fail_msg ) f
let test_compare_asts tokens expected_ast test_ctxt = let actual_ast = Parse . parse tokens in let same = compare_asts expected_ast actual_ast in let _ = if ( not same ) then ( print_test_failure expected_ast actual_ast ) else ( ) in assert_bool " Mismatched ASTS " same
let make_ast params block_items = let body = block_items in let name = ID " main " in let f = FunDecl { fun_type = IntType ; name ; params ; body = Some body } in Prog ( [ f ] )
let make_simple_ast params exp = let ret = ReturnVal exp in make_ast params [ Statement ret ]
let simple_token_list = Lex . lex " int main ( ) { return 2 ; } "
let simple_ast = make_simple_ast [ ] ( make_int 2 )
let fun_arg_token_list = Lex . lex " int main ( int argc ) { return 2 ; } "
let fun_arg_ast = let params = [ Param ( IntType , ID " argc " ) ] in let return_exp = make_int 2 in make_simple_ast params return_exp
let return_char_tokens = Lex . lex " int main ( int argc ) { return ' a ' ; } "