text
stringlengths
12
786k
let suite = " suite " >::: [ " gossip " >:: test_gossip ; ]
let ( ) = Nocrypto_entropy_unix . initialize ( ) ; run_test_tt_main suite
let sent_msgs = Hashtbl . create 3
module Node = P2p . Node . Make ( Node_id )
module View = P2p . View . Make ( Node_id ) ( Node )
module Ringcast = P2p_ringcast . Make ( Node_id ) ( Node ) ( View ) ( Msg_id )
module Io = struct type t = { node_id : Node_id . t ; in_chan : Lwt_io . input_channel ; out_chan : Lwt_io . output_channel ; } let init node_id in_chan out_chan = { node_id ; in_chan ; out_chan } let initiate_gossip t dst xchg = pf out " % a # INITIATE_GOSSIP to node % a \ n " No...
module Ringcast_lwt = P2p_ringcast_lwt . Make ( Node_id ) ( Node ) ( View ) ( Msg_id ) ( Ringcast ) ( Io )
let rec read_chan ch rc node rnode = let % lwt recvd = Lwt_io . read_value ch in pf out " % a # READ_CHAN \ n " Node_id . pp ( Node . id node ) ; pf out " recvd :\ n % a \ n " View . pp recvd ; flush stdout ; let % lwt view = Ringcast_lwt . respond rc rnode recvd in pf out " rec...
let forward delay rc src msgid msg = let % lwt _ = Lwt_unix . sleep delay in Ringcast_lwt . forward rc src msgid ( Cstruct . of_string msg )
let check_sent_msgs delay = let % lwt _ = Lwt_unix . sleep delay in let s1 , s2 , s3 = Hashtbl . find sent_msgs ( Uint64 . of_int 1 ) , Hashtbl . find sent_msgs ( Uint64 . of_int 2 ) , Hashtbl . find sent_msgs ( Uint64 . of_int 3 ) in let % lwt _ = Lwt_io . printf " chec...
let _ = Nocrypto_entropy_lwt . initialize ( )
let ( ) = let view_len = 8 in let xchg_len = 4 in let period = 1 . 0 in let fanout = 5 in let seen_len = 10 in let ( in_ch1 , out_ch2 ) = Lwt_io . pipe ( ) in let ( in_ch2 , out_ch1 ) = Lwt_io . pipe ( ) in let node1 = Node . init ( u64 100L ) in let io1 = Io . ini...
let account_pair = function [ a1 ; a2 ] -> ( a1 , a2 ) | _ -> assert false
let wrap e = Lwt . return ( Environment . wrap_tzresult e )
let check_rolls ( b : Block . t ) ( account : Account . t ) = Context . get_constants ( B b ) >>=? fun constants -> Context . Delegate . info ( B b ) account . pkh >>=? fun { staking_balance ; _ } -> let token_per_roll = constants . parametric . tokens_per_roll in let expe...
let check_no_rolls ( b : Block . t ) ( account : Account . t ) = Raw_context . prepare b . context ~ level : b . header . shell . level ~ predecessor_timestamp : b . header . shell . timestamp ~ timestamp : b . header . shell . timestamp ~ fitness : b . header . shell . ...
let test_simple_staking_rights ( ) = Context . init 2 >>=? fun ( b , accounts ) -> let ( a1 , _a2 ) = account_pair accounts in Context . Contract . balance ( B b ) a1 >>=? fun balance -> Context . Contract . manager ( B b ) a1 >>=? fun m1 -> Context . Delegate . info ( ...
let test_simple_staking_rights_after_baking ( ) = Context . init 2 >>=? fun ( b , accounts ) -> let ( a1 , a2 ) = account_pair accounts in Context . Contract . balance ( B b ) a1 >>=? fun balance -> Context . Contract . manager ( B b ) a1 >>=? fun m1 -> Context . Contract . ...
let frozen_deposit ( info : Context . Delegate . info ) = Cycle . Map . fold ( fun _ { Delegate . deposit ; _ } acc -> Test_tez . Tez . ( deposit + acc ) ) info . frozen_balance_by_cycle Tez . zero
let check_activate_staking_balance ~ loc ~ deactivated b ( a , ( m : Account . t ) ) = Context . Delegate . info ( B b ) m . pkh >>=? fun info -> Assert . equal_bool ~ loc info . deactivated deactivated >>=? fun ( ) -> Context . Contract . balance ( B b ) a >>=? fun balan...
let run_until_deactivation ( ) = Context . init 2 >>=? fun ( b , accounts ) -> let ( a1 , a2 ) = account_pair accounts in Context . Contract . balance ( B b ) a1 >>=? fun balance_start -> Context . Contract . manager ( B b ) a1 >>=? fun m1 -> Context . Contract . manager (...
let test_deactivation_then_bake ( ) = run_until_deactivation ( ) >>=? fun ( b , ( ( ( _deactivated_contract , deactivated_account ) as deactivated ) , _start_balance ) , ( _a2 , _m2 ) ) -> Block . bake ~ policy ( : By_account deactivated_account . pkh ) b >>=? fun...
let test_deactivation_then_self_delegation ( ) = run_until_deactivation ( ) >>=? fun ( b , ( ( ( deactivated_contract , deactivated_account ) as deactivated ) , start_balance ) , ( _a2 , m2 ) ) -> Op . delegation ( B b ) deactivated_contract ( Some deactivated_account ....
let test_deactivation_then_empty_then_self_delegation ( ) = run_until_deactivation ( ) >>=? fun ( b , ( ( ( deactivated_contract , deactivated_account ) as deactivated ) , _start_balance ) , ( _a2 , m2 ) ) -> Context . Contract . balance ( B b ) deactivated_contract >...
let test_deactivation_then_empty_then_self_delegation_then_recredit ( ) = run_until_deactivation ( ) >>=? fun ( b , ( ( ( deactivated_contract , deactivated_account ) as deactivated ) , balance ) , ( _a2 , m2 ) ) -> let sink_account = Account . new_account ( ) in let sin...
let test_delegation ( ) = Context . init 2 >>=? fun ( b , accounts ) -> let ( a1 , a2 ) = account_pair accounts in let m3 = Account . new_account ( ) in Account . add_account m3 ; Context . Contract . manager ( B b ) a1 >>=? fun m1 -> Context . Contract . manager ( B b ...
let tests = [ Tztest . tztest " simple staking rights " ` Quick test_simple_staking_rights ; Tztest . tztest " simple staking rights after baking " ` Quick test_simple_staking_rights_after_baking ; Tztest . tztest " deactivation then bake " ` Quick test_deactivation_then_bake ; Tztest ....
let testlib = Dl . ( dlopen ~ filename " : clib / libtest_functions . so " ~ flags [ : RTLD_NOW ] )
let test_root_lifetime _ = let alive = ref true in let ( ) = let v = [ | 1 ; 2 ; 3 ] | in Gc . finalise ( fun _ -> alive := false ) v ; in Gc . compact ( ) ; assert_equal false ! alive ~ msg " : values not registered as roots are collected " ; let alive = ref true in l...
let test_passing_roots _ = let save = foreign ~ from : testlib " save_ocaml_value " ( ptr void @-> returning void ) and retrieve = foreign ~ from : testlib " retrieve_ocaml_value " ( void @-> returning ( ptr void ) ) in let r = Root . create [ | ( + ) 1 ; ( * ) 2 ] |...
let suite = " Root tests " >::: [ " root lifetime " >:: test_root_lifetime ; " passing roots " >:: test_passing_roots ; ]
let _ = run_test_tt_main suite
let x = of_string " x " in Exn . does_raise ( fun ( ) -> Fn . apply_n_times ~ n : Int . num_bits ( fun x -> x ^ x ) x ) ; ; [ % test_result : string ] ~ expect " " : ( concat ~ sep ( : of_string " , " ) [ ] |> to_string ) ; [ % test_result : string ...
let a2 = of_string " aa "
let a3 = of_string " aaa "
let aba = of_string " aba "
let ab = of_string " ab "
let ba = of_string " ba " let test a b = require_equal [ % here ] ( module Bool ) ( String . is_prefix ( to_string a ) ~ prefix ( : to_string b ) ) ( is_prefix a ~ prefix : b ) in test a2 a3 ; test a3 a2 ; test ( a2 ^ a2 ) a2 ; test ( a2 ^ a2 ) a3 ; test ( a3 ^ ...
let ( >>>=? ) v f = v >|= Environment . wrap_tzresult >>=? f
let ( >>>?= ) v f = v |> Environment . wrap_tzresult >>?= f
let ( >>>? ) v f = v |> Environment . wrap_tzresult >>? f
type round_test = { round_duration : ( int * int ) list ; round_and_offset : ( int * ( int * int ) ) list ; timestamp_of_round : ( ( int * int * int ) * int ) list ; round_of_timestamp : ( ( int * int * int ) * int ) list ; }
let case_3_4 = { round_duration = [ ( 0 , 3 ) ; ( 1 , 4 ) ; ( 2 , 5 ) ; ( 3 , 6 ) ] ; round_and_offset = [ ( 0 , ( 0 , 0 ) ) ; ( 1 , ( 0 , 1 ) ) ; ( 2 , ( 0 , 2 ) ) ; ( 3 , ( 1 , 0 ) ) ; ( 4 , ( ...
let case_3_6 = { round_duration = [ ( 0 , 3 ) ; ( 1 , 6 ) ; ( 2 , 9 ) ; ( 3 , 12 ) ; ( 4 , 15 ) ; ( 5 , 18 ) ; ( 6 , 21 ) ; ( 7 , 24 ) ; ( 8 , 27 ) ; ] ; round_and_offset = [ ( 0 , ( 0 , 0 ) ) ; ( 1...
let test_cases = [ ( ( 3 , 1 ) , case_3_4 , " case_3_4 " ) ; ( ( 3 , 3 ) , case_3_6 , " case_3_6 " ) ; ]
let round_of_int i = Round_repr . of_int i |> Environment . wrap_tzresult
let mk_round_durations first_round_duration delay_increment_per_round = let first_round_duration = Period_repr . of_seconds_exn @@ Int64 . of_int first_round_duration in let delay_increment_per_round = Period_repr . of_seconds_exn @@ Int64 . of_int delay_increment_per_round in Stdlib . Option . get @@...
let process_test_case ( round_durations , ios , _ ) = let open Round_repr in List . iter_es ( fun ( i , o ) -> round_of_int i >>?= fun round -> let dur = Durations . round_duration round_durations round in Assert . equal_int64 ~ loc : __LOC__ ( Int64 . of_int o ) ( Period_repr . ...
let test_round ( ) = let final_test_cases = List . map ( fun ( ( first_round_duration , delay_increment_per_round ) , ios , name ) -> ( mk_round_durations first_round_duration delay_increment_per_round , ios , name ) ) test_cases in List . iter_es process_test_case final_test_cases
let ts_add ts period = match Timestamp . ( ts +? period ) with | Ok ts ' -> ts ' | Error _ -> Environment . Pervasives . failwith " timestamp add "
let test_round_of_timestamp ( ) = let duration0 = Period . of_seconds_exn 1L in Environment . wrap_tzresult @@ Round . Durations . create ~ first_round_duration : duration0 ~ delay_increment_per_round : Period . one_second >>?= fun round_durations -> let predecessor_timestamp = Time . Protoco...
let round_of_timestamp_perf ( duration0_int64 , dipr ) = let duration0 = Period . of_seconds_exn duration0_int64 in let delay_increment_per_round = Period . of_seconds_exn dipr in let round_durations = Stdlib . Option . get @@ Round . Durations . create_opt ~ first_round_duration : duration0 ~...
let default_round_durations_list = [ ( 1L , 1L ) ; ( 1L , 2L ) ; ( 1L , 3L ) ; ( 2L , 3L ) ; ( 2L , 4L ) ]
let test_round_of_timestamp_perf ( ) = List . iter_es round_of_timestamp_perf default_round_durations_list
let timestamp_of_round_perf ( duration0_int64 , dipr ) = let duration0 = Period . of_seconds_exn duration0_int64 in let delay_increment_per_round = Period . of_seconds_exn dipr in let round_durations = Stdlib . Option . get @@ Round . Durations . create_opt ~ first_round_duration : duration0 ~...
let test_timestamp_of_round_perf ( ) = List . iter_es timestamp_of_round_perf default_round_durations_list
let test_error_is_triggered_for_too_high_timestamp ( ) = let round_durations = Stdlib . Option . get @@ Round . Durations . create_opt ~ first_round_duration : Period . one_second ~ delay_increment_per_round : Period . one_second in let predecessor_timestamp = Time . Protocol . epoch in let ...
let rec ( --> ) i j = if Compare . Int . ( i > j ) then [ ] else i :: ( succ i --> j )
let ts_of_round_inverse ( duration0_int64 , dipr ) round_int = let first_round_duration = Period . of_seconds_exn duration0_int64 in let delay_increment_per_round = Period . of_seconds_exn dipr in let round_durations = Stdlib . Option . get @@ Round . Durations . create_opt ~ first_round_duratio...
let test_ts_of_round_inverse ( ) = List . iter_es ( fun durations -> List . iter_es ( ts_of_round_inverse durations ) ( ( 0 --> 20 ) @ ( 60000 --> 60010 ) ) ) default_round_durations_list >>=? fun ( ) -> List . iter_es ( ts_of_round_inverse ( 1L , 1L ) ) ( List...
let round_of_ts_inverse ~ first_round_duration ~ delay_increment_per_round ts = Format . printf " ts = % Ld . " @ ts ; let first_round_duration = Period . of_seconds_exn first_round_duration in let delay_increment_per_round = Period . of_seconds_exn delay_increment_per_round in Round . Durations...
let test_round_of_ts_inverse ( ) = List . iter_es ( fun ( first_round_duration , delay_increment_per_round ) -> List . iter_es ( fun ts -> round_of_ts_inverse ~ first_round_duration ~ delay_increment_per_round ( Int64 . of_int ts ) ) ( ( 0 --> 20 ) @ ( 60000 --> 60010 ) )...
let test_level_offset_of_round ( ) = let rd1 = let first_round_duration = 3 in let delay_increment_per_round = 1 in mk_round_durations first_round_duration delay_increment_per_round in List . iter_es ( fun ( round_durations , tests ) -> List . iter_es ( fun ( round , expected_offset ) ->...
let round_and_offset_oracle ( round_durations : Round_repr . Durations . t ) ~ level_offset = let level_offset_in_seconds = Period_repr . to_seconds level_offset in let right_bound = if Compare . Int64 . ( level_offset_in_seconds < Int64 . of_int32 Int32 . max_int ) then Int32 . of_int ( ...
let test_round_and_offset_correction = Tztest . tztest_qcheck ~ name " : round_and_offset is correct " QCheck . ( pair Lib_test . Qcheck_helpers . ( pair uint16 uint16 ) ( Lib_test . Qcheck_helpers . int64_range 0L 100000L ) ) ( fun ( ( first_round_duration , delay_increment_per...
let tests = Tztest . [ tztest " test_level_offset_of_round " ` Quick test_level_offset_of_round ; tztest " test Round_duration " ` Quick test_round ; tztest " round_of_timestamp " ` Quick test_round_of_timestamp ; tztest " round_of_timestamp_perf " ` Quick test_round_of_timestamp_perf...
let test ~ make_transport ~ imp1 ~ imp2 ~ state1 ~ state2 ~ f ( ) = Unix . pipe ( Info . of_string " rpc_test 1 " ) >>= fun ( ` Reader r1 , ` Writer w2 ) -> Unix . pipe ( Info . of_string " rpc_test 2 " ) >>= fun ( ` Reader r2 , ` Writer w1 ) -> let t1 = mak...
let test1 ~ make_transport ~ imp ~ state ~ f = test ~ make_transport ~ imp1 : imp ~ state1 : state ~ imp2 [ ] : ~ state2 ( ) : ~ f ; ;
module Pipe_count_error = struct type t = [ ` Argument_must_be_positive ] [ @@ deriving bin_io ] end
let pipe_count_rpc = Rpc . Pipe_rpc . create ~ name " : pipe_count " ~ version : 0 ~ bin_query : Int . bin_t ~ bin_response : Int . bin_t ~ bin_error : Pipe_count_error . bin_t ( ) ; ;
let pipe_wait_rpc = Rpc . Pipe_rpc . create ~ name " : pipe_wait " ~ version : 0 ~ bin_query : Unit . bin_t ~ bin_response : Unit . bin_t ~ bin_error : Unit . bin_t ( ) ; ;
let pipe_count_imp = Rpc . Pipe_rpc . implement pipe_count_rpc ( fun ( ) n -> if n < 0 then return ( Error ` Argument_must_be_positive ) else ( let pipe_r , pipe_w = Pipe . create ( ) in upon ( Deferred . List . iter ( List . init n ~ f : Fn . id ) ~ how ` : Sequential...
let pipe_wait_imp ivar = Rpc . Pipe_rpc . implement pipe_wait_rpc ( fun ( ) ( ) -> let pipe_r , pipe_w = Pipe . create ( ) in ( Pipe . write pipe_w ( ) >>> fun ( ) -> Ivar . read ivar >>> fun ( ) -> Pipe . write pipe_w ( ) >>> fun ( ) -> Pipe . close pipe_w )...
let make_tests ~ make_transport ~ transport_name = List . mapi ~ f ( : fun i f -> sprintf " rpc -% s -% d " transport_name i , f ) [ test1 ~ make_transport ~ imp [ : pipe_count_imp ] ~ state ( ) : ~ f ( : fun _ conn -> let n = 3 in Rpc . Pipe_rpc . dispatch_exn pipe_count_...
module Arbitrary = struct open QCheck open RPC_server . Acl open Tz_arbitrary let meth_matcher : meth_matcher arbitrary = oneofl [ Any ; Exact ` GET ; Exact ` PUT ; Exact ` POST ; Exact ` PATCH ; Exact ` DELETE ] let chunk_matcher : chunk_matcher arbitrary = let of_string s = Literal s i...
let resolve_domain_name = let resolver = TzString . Map . of_seq @@ List . to_seq [ ( " localhost " , List . map Ipaddr . V6 . of_int64 [ ( 0L , 1L ) ; ( 0L , 281472812449793L ) ] ) ; ( " 127 . 0 . 0 . 1 " , List . map Ipaddr . V6 . of_int64 [ ...
let resolve_domain_names_in_policy = RPC_server . Acl . Internal_for_test . resolve_domain_names ( fun { addr ; port ; _ } -> resolve_domain_name addr |> List . map ( fun addr -> ( addr , port ) ) |> Lwt . return )
let example_policy = ` A [ ` O [ ( " address " , ` String " localhost : 22 " ) ; ( " blacklist " , ` A [ ] ) ] ; ` O [ ( " address " , ` String " localhost " ) ; ( " whitelist " , ` A [ ` String " / chains " ] ) ; /** ] ;...
let acl_testable = let pp_matchers fmt matchers = let open Format in pp_print_list ~ pp_sep ( : fun fmt ( ) -> pp_print_string fmt " ; @ " ) ( fun fmt m -> pp_print_string fmt ( RPC_server . Acl . matcher_to_string m ) ) fmt matchers ; pp_print_string fmt " ] " in let pp fmt = ...
let pp_policy ppf policy = Format . fprintf ppf " % s " ( RPC_server . Acl . policy_to_string policy )
let test_codec_identity = let open QCheck in Test . make ~ name " : Encoding and decoding an ACL is an identity function . " Arbitrary . policy ( fun policy -> let json = Data_encoding . Json . construct RPC_server . Acl . policy_encoding policy in let decoded = Data_encoding . Json . dest...
let check_find_policy = let open QCheck in let assert_results_satisfactory before_put after_put = match ( before_put , after_put ) with | ( Some _ , None ) -> false | ( _ , _ ) -> true in Test . make ~ name " : put_policy preserves existing entries . " Arbitrary . find_policy_setu...
let mk_acl ( ( tag , matchers ) : [ ` Whitelist | ` Blacklist ] * string list ) = let open RPC_server . Acl in let except = List . map parse matchers in match tag with | ` Whitelist -> Deny_all { except } | ` Blacklist -> Allow_all { except }
let check_acl_search ( description : string ) ( policy : RPC_server . Acl . policy ) ( expected : ( [ ` Whitelist | ` Blacklist ] * string list ) option ) ( addr : string * int option ) = Alcotest . check ( Alcotest . option acl_testable ) description ( Option . map m...
let test_finding_policy = Alcotest . test_case " policy matching rules " ` Quick ( fun ( ) -> check_acl_search " An exact match is when address and port match exactly . " example_policy ( Some ( ` Whitelist , [ " GET " ; /** " DELETE / chains /*/ invalid_blocks " ] ) ) ...
let ensure_default_policy_parses = let open QCheck in Test . make ~ name " : default policy parses and is of correct type " Tz_arbitrary . ipv6 ( fun ip_addr -> let expected = let open Ipaddr . V6 in if scope ip_addr = Interface then ` Blacklist else ` Whitelist in RPC_server . Acl . ( acl_t...
let ensure_unsafe_rpcs_blocked = let known_unsafe_rpcs = [ ( ` DELETE , [ " chains " ; " main " ; " invalid_blocks " ; " hash " ] ) ; ( ` GET , [ " fetch_protocol " ; " ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK " ; ] ) ; ( ` GET , [ " ...
let test_matching_with_name_resolving = let to_test = [ ( " :: 1 " , Some 22 , Some ( ` Blacklist , [ ] ) ) ; ( " :: 1 " , Some 8732 , Some ( ` Whitelist , [ " / chains " ] ) ) ; /** ] in Alcotest . test_case " make sure addresses match well with...
let ( ) = let open Qcheck_helpers in Alcotest . run " tezos - rpc - http " [ ( " qcheck " , qcheck_wrap [ test_codec_identity ; check_find_policy ; ensure_default_policy_parses ] ) ; ( " find_policy_matching_rules " , [ test_finding_policy ] ) ; ( " ensure_unsafe_r...
let vz = Z . of_string_base 16
module Null = struct type g = Cstruct . t ref let block = 1 let create ? time : _ ( ) = ref Cstruct . empty let generate ~ g n = try let ( a , b ) = Cstruct . split ! g n in ( g := b ; a ) with Invalid_argument _ -> raise Mirage_crypto_rng . Unseeded_generator let reseed ~ g cs ...
let random_is seed = Mirage_crypto_rng . create ~ seed ( module Null )
let gen_rsa ~ bits = let e = Z . ( if bits < 24 then ~$ 3 else ~$ 0x10001 ) in let key = Rsa . ( generate ~ e ~ bits ( ) ) in let key_s = Sexplib0 . Sexp . to_string_hum Rsa . ( sexp_of_priv key ) in assert_equal ~ msg : Printf . ( sprintf " key size not % d bits :\ n %...
let rsa_priv_of_primes_regression _ = let e = Z . of_string " 65537 " and p = Z . of_string " 63541376186162969 " and q = Z . of_string " 31114890003960709 " in match Rsa . priv_of_primes ~ e ~ p ~ q with | exception _ -> assert_failure " expected an error " | Error _ -> ( ) ...
let rsa_priv_of_primes_regression_62 _ = let e = Z . of_string " 65537 " and d = Z . of_string " 3108431922676000487023821479912741349223115124336455693119108686758268939583975029271734799300422643496417197940166373626629291080744953934921341465364968117931378406446980227029856589807773725566867068344285...
let rsa_priv_of_primes_regression_openssl _ = let e = Z . of_string " 65537 " and d = Z . of_string " 21364966876797335224937981624977347791305770821352826744474497613118281825259093305200082888709328664041494911511266059341542974088052755771514853303591832823929488189866359158215383109671205375680439686...
let rsa_selftest ~ bits n = " selftest " >:: times ~ n @@ fun _ -> let msg = let size = bits // 8 in let cs = Mirage_crypto_rng . generate size and i = 1 + Randomconv . int ~ bound ( : pred size ) Mirage_crypto_rng . generate in Cstruct . set_uint8 cs 0 0 ; Cstruct . ( set_uint8...
let show_key_size key = Printf . sprintf " ( % d bits ) " ( Rsa . priv_bits key )
let pkcs_message_for_bits bits = let padding = 12 in let size = bits // 8 - padding in assert ( size >= 0 ) ; Mirage_crypto_rng . generate size