From 99a2e876ba4addbcae0e6652915596033e58e667 Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Sat, 18 Sep 2021 11:57:37 -0700 Subject: [PATCH] Test bls12_381 keys with peer and peerbook SUITEs - Update rebar.config and rebar.lock - Use test groups to test all key types --- rebar.config | 2 +- rebar.lock | 11 +++++--- test/peer_SUITE.erl | 59 ++++++++++++++++++++++++++++++----------- test/peerbook_SUITE.erl | 57 ++++++++++++++++++++++++++++----------- 4 files changed, 95 insertions(+), 34 deletions(-) diff --git a/rebar.config b/rebar.config index fe366d27..57235601 100644 --- a/rebar.config +++ b/rebar.config @@ -19,7 +19,7 @@ {deps, [ lager, {ranch, "1.5.0"}, - {libp2p_crypto, "1.4.1"}, + {libp2p_crypto, ".*", {git, "https://github.com/helium/libp2p-crypto", {branch, "rg/bls12-381"}}}, {nat, ".*", {git, "https://github.com/benoitc/erlang-nat", {branch, "master"}}}, {backoff, "1.1.6"}, {throttle, "0.2.0", {pkg, lambda_throttle}}, diff --git a/rebar.lock b/rebar.lock index 1b141b96..27e64cba 100644 --- a/rebar.lock +++ b/rebar.lock @@ -12,6 +12,10 @@ {ref,"8fc2a5aa2454bca3b57047c0b3f79f8d9d219483"}}, 0}, {<<"erl_base58">>,{pkg,<<"erl_base58">>,<<"0.0.1">>},1}, + {<<"erlang_tc">>, + {git,"https://github.com/helium/erlang-tc.git", + {ref,"588ca62467a1cbfe43d20e6f681656613dab0622"}}, + 1}, {<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},1}, {<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1}, {<<"inert">>,{pkg,<<"inert">>,<<"1.0.2">>},1}, @@ -22,7 +26,10 @@ 0}, {<<"intercept">>,{pkg,<<"intercept">>,<<"1.0.0">>},1}, {<<"lager">>,{pkg,<<"lager">>,<<"3.9.2">>},0}, - {<<"libp2p_crypto">>,{pkg,<<"libp2p_crypto">>,<<"1.4.1">>},0}, + {<<"libp2p_crypto">>, + {git,"https://github.com/helium/libp2p-crypto", + {ref,"111469308be3060c2c1c24b450f57046bd549a02"}}, + 0}, {<<"multiaddr">>,{pkg,<<"multiaddr">>,<<"1.1.3">>},1}, {<<"multihash">>,{pkg,<<"multihash">>,<<"2.1.0">>},1}, {<<"nat">>, @@ -56,7 +63,6 @@ {<<"inet_cidr">>, <<"4814A5B78B969A5E069B0CECBB102622AB0C459B690053ED94543CD529915A43">>}, {<<"intercept">>, <<"1F6C725E6FC070720643BD4D97EE53B1209365C80E520E1F5A1ACB36712A7EB5">>}, {<<"lager">>, <<"4CAB289120EB24964E3886BD22323CB5FEFE4510C076992A23AD18CF85413D8C">>}, - {<<"libp2p_crypto">>, <<"CB93FE834A53059671C547957C1235C9426E5B7EE91D156AD5D90A9710BF4AF0">>}, {<<"multiaddr">>, <<"978E58E28F6FACAF428C87AF933612B1E2F3F2775F1794EDA5E831A4EACD2984">>}, {<<"multihash">>, <<"F084F7C6BEC062F0C0E82AE18CFDC8DAEC8F4FAA4C8E1ACE0B9C676A9323162F">>}, {<<"procket">>, <<"1FC0D557ACC0145DD167C566C68F83B183E923016904DFE980534B3C9150E92B">>}, @@ -77,7 +83,6 @@ {<<"inet_cidr">>, <<"97046492E5C5BE0D8B92CD275980D667A8D28D9E79B2305828E358CC7D30A935">>}, {<<"intercept">>, <<"FC5F5E26A571637B0B80119FE2F1E83DDDEAC8078BD76279C015474AF432B32E">>}, {<<"lager">>, <<"7F904D9E87A8CB7E66156ED31768D1C8E26EBA1D54F4BC85B1AA4AC1F6340C28">>}, - {<<"libp2p_crypto">>, <<"A21C278896DA65E31A2C8FE767784F1493289CD956E172E7BA247EAB75207037">>}, {<<"multiaddr">>, <<"980D3EA5EB0EB2EFC51E3D10953F17447D417C49BD2FCD7FC6A2A42D1F66D5EE">>}, {<<"multihash">>, <<"E73AD5D0099DBFFB4EE429A78436B605AFE2530ED684AB36BB86733AB65707C8">>}, {<<"procket">>, <<"647C2D1F5006370E457522ECC69509F1020D7D558F3D6E196825E1F4F3660218">>}, diff --git a/test/peer_SUITE.erl b/test/peer_SUITE.erl index 58012ed3..2e9cbc37 100644 --- a/test/peer_SUITE.erl +++ b/test/peer_SUITE.erl @@ -3,10 +3,14 @@ -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). --export([all/0]). +-export([all/0, + init_per_group/2, + end_per_group/2, + groups/0]). + -export([coding_test/1, metadata_test/1, blacklist_test/1, association_test/1, signed_metadata_test/1]). -all() -> +test_cases() -> [coding_test, metadata_test, blacklist_test, @@ -14,13 +18,34 @@ all() -> signed_metadata_test ]. -coding_test(_) -> - #{public := PubKey1, secret := PrivKey1} = libp2p_crypto:generate_keys(ecc_compact), - #{public := PubKey2, secret := PrivKey2} = libp2p_crypto:generate_keys(ecc_compact), +all() -> + [{group, ecc_compact}, {group, ed25519}, {group, bls12_381}]. + +groups() -> + [ + {ecc_compact, [], test_cases()}, + {ed25519, [], test_cases()}, + {bls12_381, [], test_cases()} + ]. + +init_per_group(ecc_compact, Config) -> + [{key_type, ecc_compact} | Config]; +init_per_group(ed25519, Config) -> + [{key_type, ed25519} | Config]; +init_per_group(bls12_381, Config) -> + [{key_type, bls12_381} | Config]. + +end_per_group(_, _Config) -> + ok. + +coding_test(Config) -> + KeyType = ?config(key_type, Config), + #{public := PubKey1, secret := PrivKey1} = libp2p_crypto:generate_keys(KeyType), + #{public := PubKey2, secret := PrivKey2} = libp2p_crypto:generate_keys(KeyType), SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1), SigFun2 = libp2p_crypto:mk_sig_fun(PrivKey2), - #{public := AssocPubKey1, secret := AssocPrivKey1} = libp2p_crypto:generate_keys(ecc_compact), + #{public := AssocPubKey1, secret := AssocPrivKey1} = libp2p_crypto:generate_keys(KeyType), AssocSigFun1 = libp2p_crypto:mk_sig_fun(AssocPrivKey1), Associations = [libp2p_peer:mk_association(libp2p_crypto:pubkey_to_bin(AssocPubKey1), libp2p_crypto:pubkey_to_bin(PubKey1), @@ -62,8 +87,9 @@ coding_test(_) -> ok. -metadata_test(_) -> - #{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact), +metadata_test(Config) -> + KeyType = ?config(key_type, Config), + #{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(KeyType), SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1), PeerMap = #{pubkey => libp2p_crypto:pubkey_to_bin(PubKey1), @@ -91,8 +117,9 @@ metadata_test(_) -> ok. -blacklist_test(_) -> - #{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact), +blacklist_test(Config) -> + KeyType = ?config(key_type, Config), + #{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(KeyType), SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1), BadListenAddr = "/ip4/8.8.8.8/tcp/1234", @@ -111,8 +138,9 @@ blacklist_test(_) -> ok. -association_test(_) -> - #{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact), +association_test(Config) -> + KeyType = ?config(key_type, Config), + #{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(KeyType), SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1), PeerMap = #{pubkey => libp2p_crypto:pubkey_to_bin(PubKey1), @@ -120,7 +148,7 @@ association_test(_) -> connected => [], nat_type => static}, - #{secret := AssocPrivKey1, public := AssocPubKey1} = libp2p_crypto:generate_keys(ecc_compact), + #{secret := AssocPrivKey1, public := AssocPubKey1} = libp2p_crypto:generate_keys(KeyType), ValidAssoc = libp2p_peer:mk_association(libp2p_crypto:pubkey_to_bin(AssocPubKey1), libp2p_crypto:pubkey_to_bin(PubKey1), libp2p_crypto:mk_sig_fun(AssocPrivKey1)), @@ -158,8 +186,9 @@ association_test(_) -> ok. -signed_metadata_test(_) -> - #{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact), +signed_metadata_test(Config) -> + KeyType = ?config(key_type, Config), + #{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(KeyType), SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1), PeerMap = #{pubkey => libp2p_crypto:pubkey_to_bin(PubKey1), diff --git a/test/peerbook_SUITE.erl b/test/peerbook_SUITE.erl index 6eecddf3..570dca94 100644 --- a/test/peerbook_SUITE.erl +++ b/test/peerbook_SUITE.erl @@ -3,11 +3,20 @@ -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). --export([all/0, init_per_testcase/2, end_per_testcase/2]). +-export([all/0, + init_per_testcase/2, + end_per_testcase/2, + init_per_group/2, + end_per_group/2, + groups/0 + ]). -export([accessor_test/1, bad_peer_test/1, put_test/1, blacklist_test/1, association_test/1, gossip_test/1, network_id_gossip_test/1, stale_test/1]). all() -> + [{group, ecc_compact}, {group, ed25519}, {group, bls12_381}]. + +test_cases() -> [ accessor_test, bad_peer_test, @@ -19,10 +28,24 @@ all() -> association_test ]. +groups() -> + [ + {ecc_compact, [], test_cases()}, + {ed25519, [], test_cases()}, + {bls12_381, [], test_cases()} + ]. + +init_per_group(ecc_compact, Config) -> + [{key_type, ecc_compact} | Config]; +init_per_group(ed25519, Config) -> + [{key_type, ed25519} | Config]; +init_per_group(bls12_381, Config) -> + [{key_type, bls12_381} | Config]. +end_per_group(_, _Config) -> + ok. init_per_testcase(accessor_test = TestCase, Config) -> - Config0 = test_util:init_base_dir_config(?MODULE, TestCase, Config), setup_peerbook(Config0, []); init_per_testcase(bad_peer_test = TestCase, Config) -> @@ -98,8 +121,8 @@ accessor_test(Config) -> {_PeerBook, Address} = ?config(peerbook, Config), PeerBook = libp2p_swarm:peerbook(?config(tid, Config)), - Peer1 = mk_peer(), - Peer2 = mk_peer(), + Peer1 = mk_peer(Config), + Peer2 = mk_peer(Config), libp2p_peerbook:put(PeerBook, [Peer1]), libp2p_peerbook:put(PeerBook, [Peer2]), @@ -125,10 +148,11 @@ accessor_test(Config) -> ok. bad_peer_test(Config) -> + KeyType = ?config(key_type, Config), PeerBook = libp2p_swarm:peerbook(?config(tid, Config)), - #{public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact), - #{secret := PrivKey2, public := PubKey2} = libp2p_crypto:generate_keys(ecc_compact), + #{public := PubKey1} = libp2p_crypto:generate_keys(KeyType), + #{secret := PrivKey2, public := PubKey2} = libp2p_crypto:generate_keys(KeyType), SigFun2 = libp2p_crypto:mk_sig_fun(PrivKey2), @@ -149,7 +173,7 @@ blacklist_test(Config) -> {_PeerBook, _Address} = ?config(peerbook, Config), PeerBook = libp2p_swarm:peerbook(?config(tid, Config)), - Peer1 = mk_peer(), + Peer1 = mk_peer(Config), libp2p_peerbook:put(PeerBook, [Peer1]), @@ -165,10 +189,11 @@ blacklist_test(Config) -> association_test(Config) -> + KeyType = ?config(key_type, Config), {_PeerBook, Address} = ?config(peerbook, Config), PeerBook = libp2p_swarm:peerbook(?config(tid, Config)), - #{secret := AssocPrivKey, public := AssocPubKey} = libp2p_crypto:generate_keys(ecc_compact), + #{secret := AssocPrivKey, public := AssocPubKey} = libp2p_crypto:generate_keys(KeyType), AssocSigFun = libp2p_crypto:mk_sig_fun(AssocPrivKey), Assoc = libp2p_peer:mk_association(libp2p_crypto:pubkey_to_bin(AssocPubKey), Address, AssocSigFun), @@ -190,9 +215,9 @@ put_test(Config) -> {_PeerBook, Address} = ?config(peerbook, Config), PeerBook = libp2p_swarm:peerbook(?config(tid, Config)), - PeerList1 = [mk_peer() || _ <- lists:seq(1, 5)], + PeerList1 = [mk_peer(Config) || _ <- lists:seq(1, 5)], - ExtraPeers = [mk_peer() || _ <- lists:seq(1, 3)], + ExtraPeers = [mk_peer(Config) || _ <- lists:seq(1, 3)], PeerList2 = lists:sublist(PeerList1, 1, 3) ++ ExtraPeers, ok = libp2p_peerbook:put(PeerBook, PeerList1), @@ -351,7 +376,7 @@ stale_test(Config) -> S1Addr = libp2p_swarm:pubkey_bin(S1), {ok, S1First} = libp2p_peerbook:get(PeerBook, S1Addr), - Peer1 = mk_peer(), + Peer1 = mk_peer(Config), libp2p_peerbook:put(PeerBook, [Peer1]), %% This peer should remew itself after stale_time @@ -400,9 +425,10 @@ stale_test(Config) -> peer_keys(PeerList) -> [libp2p_crypto:bin_to_b58(libp2p_peer:pubkey_bin(P)) || P <- PeerList]. -mk_peer() -> - #{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(ecc_compact), - #{ public := PubKey2} = libp2p_crypto:generate_keys(ecc_compact), +mk_peer(Config) -> + KeyType = ?config(key_type, Config), + #{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(KeyType), + #{ public := PubKey2} = libp2p_crypto:generate_keys(KeyType), {ok, Peer} = libp2p_peer:from_map(#{pubkey => libp2p_crypto:pubkey_to_bin(PubKey), listen_addrs => ["/ip4/8.8.8.8/tcp/1234"], connected => [libp2p_crypto:pubkey_to_bin(PubKey2)], @@ -413,10 +439,11 @@ mk_peer() -> setup_peerbook(Config, Opts) -> test_util:setup(), + KeyType = ?config(key_type, Config), Name = list_to_atom("swarm" ++ integer_to_list(erlang:monotonic_time())), TID = ets:new(Name, [public, ordered_set, {read_concurrency, true}]), ets:insert(TID, {swarm_name, Name}), - #{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(ecc_compact), + #{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(KeyType), CompactKey = libp2p_crypto:pubkey_to_bin(PubKey), ets:insert(TID, {swarm_address, CompactKey}), PrivDir = ?config(priv_dir, Config),