// Tests a 2-node system func TestBroadcast(t *testing.T) { for _, nbrNodes := range []int{3, 10, 14} { local := onet.NewLocalTest() _, _, tree := local.GenTree(nbrNodes, false) pi, err := local.CreateProtocol("Broadcast", tree) if err != nil { t.Fatal("Couldn't start protocol:", err) } protocol := pi.(*manage.Broadcast) done := make(chan bool) protocol.RegisterOnDone(func() { done <- true }) protocol.Start() timeout := network.WaitRetry * time.Duration(network.MaxRetryConnect*nbrNodes*2) * time.Millisecond select { case <-done: log.Lvl2("Done with connecting everybody") case <-time.After(timeout): t.Fatal("Didn't finish in time") } local.CloseAll() log.AfterTest(t) } }
func TestService_Verification(t *testing.T) { local := onet.NewLocalTest() defer local.CloseAll() sbLength := 4 _, el, genService := local.MakeHELS(sbLength, skipchainSID) service := genService.(*Service) elRoot := onet.NewRoster(el.List[0:3]) sbRoot, err := makeGenesisRoster(service, elRoot) log.ErrFatal(err) log.Lvl1("Creating non-conforming skipBlock") sb := NewSkipBlock() sb.Roster = el sb.MaximumHeight = 1 sb.BaseHeight = 1 sb.ParentBlockID = sbRoot.Hash sb.VerifierID = VerifyShard _, err = service.ProposeSkipBlock(&ProposeSkipBlock{nil, sb}) require.NotNil(t, err, "Shouldn't accept a non-confoirming skipblock") log.Lvl1("Creating skipblock with same Roster as root") sbInter, err := makeGenesisRosterArgs(service, elRoot, sbRoot.Hash, VerifyShard, 1, 1) log.ErrFatal(err) log.Lvl1("Creating skipblock with sub-Roster from root") elSub := onet.NewRoster(el.List[0:2]) sbInter, err = makeGenesisRosterArgs(service, elSub, sbRoot.Hash, VerifyShard, 1, 1) log.ErrFatal(err) scsb := &SetChildrenSkipBlock{sbRoot.Hash, sbInter.Hash} service.SetChildrenSkipBlock(scsb) }
func TestService_MultiLevel(t *testing.T) { local := onet.NewLocalTest() defer local.CloseAll() _, el, genService := local.MakeHELS(3, skipchainSID) service := genService.(*Service) for base := 1; base <= 3; base++ { for height := 1; height <= 3; height++ { if base == 1 && height > 1 { break } sbRoot, err := makeGenesisRosterArgs(service, el, nil, VerifyNone, base, height) log.ErrFatal(err) latest := sbRoot log.Lvl1("Adding blocks for", base, height) for sbi := 1; sbi < 10; sbi++ { sb := NewSkipBlock() sb.Roster = el psbr, err := service.ProposeSkipBlock(&ProposeSkipBlock{latest.Hash, sb}) log.ErrFatal(err) latest = psbr.(*ProposedSkipBlockReply).Latest } log.ErrFatal(checkMLForwardBackward(service, sbRoot, base, height)) log.ErrFatal(checkMLUpdate(service, sbRoot, latest, base, height)) } } // Setting up two chains and linking one to the other }
func TestService_RegisterVerification(t *testing.T) { // Testing whether we sign correctly the SkipBlocks onet.RegisterNewService("ServiceVerify", newServiceVerify) local := onet.NewLocalTest() defer local.CloseAll() hosts, el, s1 := makeHELS(local, 3) VerifyTest := VerifierID(uuid.NewV5(uuid.NamespaceURL, "Test1")) ver := make(chan bool, 3) verifier := func(msg []byte, s *SkipBlock) bool { ver <- true return true } for _, h := range hosts { s := h.GetService(ServiceName).(*Service) log.ErrFatal(s.RegisterVerification(VerifyTest, verifier)) } sb, err := makeGenesisRosterArgs(s1, el, nil, VerifyTest, 1, 1) log.ErrFatal(err) require.NotNil(t, sb.Data) require.Equal(t, 3, len(ver)) sb, err = makeGenesisRosterArgs(s1, el, nil, ServiceVerifier, 1, 1) log.ErrFatal(err) require.NotNil(t, sb.Data) require.Equal(t, 3, len(ServiceVerifierChan)) }
func TestThreshold(t *testing.T) { const TestProtocolName = "DummyBFTCoSiThr" // Register test protocol using BFTCoSi onet.GlobalProtocolRegister(TestProtocolName, func(n *onet.TreeNodeInstance) (onet.ProtocolInstance, error) { return NewBFTCoSiProtocol(n, verify) }) local := onet.NewLocalTest() defer local.CloseAll() tests := []struct{ h, t int }{ {1, 1}, {2, 2}, {3, 2}, {4, 3}, {5, 4}, {6, 4}, } for _, s := range tests { hosts, thr := s.h, s.t log.Lvl3("Hosts is", hosts) _, _, tree := local.GenBigTree(hosts, hosts, 2, true) log.Lvl3("Tree is:", tree.Dump()) // Start the protocol node, err := local.CreateProtocol(TestProtocolName, tree) log.ErrFatal(err) bc := node.(*ProtocolBFTCoSi) assert.Equal(t, thr, bc.threshold, "hosts was %d", hosts) local.CloseAll() } }
func runProtocolOnceGo(nbrHosts int, name string, refuseCount int, succeed bool) error { log.Lvl2("Running BFTCoSi with", nbrHosts, "hosts") local := onet.NewLocalTest() defer local.CloseAll() _, _, tree := local.GenBigTree(nbrHosts, nbrHosts, 2, true) log.Lvl3("Tree is:", tree.Dump()) done := make(chan bool) // create the message we want to sign for this round msg := []byte("Hello BFTCoSi") // Start the protocol node, err := local.CreateProtocol(name, tree) if err != nil { return errors.New("Couldn't create new node: " + err.Error()) } // Register the function generating the protocol instance var root *ProtocolBFTCoSi root = node.(*ProtocolBFTCoSi) root.Msg = msg cMux.Lock() counter := &Counter{refuseCount: refuseCount} counters.add(counter) root.Data = []byte(strconv.Itoa(counters.size() - 1)) log.Lvl3("Added counter", counters.size()-1, refuseCount) cMux.Unlock() log.ErrFatal(err) // function that will be called when protocol is finished by the root root.RegisterOnDone(func() { done <- true }) go node.Start() log.Lvl1("Launched protocol") // are we done yet? wait := time.Second * 60 select { case <-done: counter.Lock() if counter.veriCount != nbrHosts { return errors.New("Each host should have called verification.") } // if assert refuses we don't care for unlocking (t.Refuse) counter.Unlock() sig := root.Signature() err := sig.Verify(root.Suite(), root.Roster().Publics()) if succeed && err != nil { return fmt.Errorf("%s Verification of the signature refused: %s - %+v", root.Name(), err.Error(), sig.Sig) } if !succeed && err == nil { return fmt.Errorf("%s: Shouldn't have succeeded for %d hosts, but signed for count: %d", root.Name(), nbrHosts, refuseCount) } case <-time.After(wait): log.Lvl1("Going to break because of timeout") return errors.New("Waited " + wait.String() + " for BFTCoSi to finish ...") } return nil }
func TestService_SetChildrenSkipBlock(t *testing.T) { // How many nodes in Root nodesRoot := 3 local := onet.NewLocalTest() defer local.CloseAll() hosts, el, genService := local.MakeHELS(nodesRoot, skipchainSID) service := genService.(*Service) // Setting up two chains and linking one to the other sbRoot, err := makeGenesisRoster(service, el) log.ErrFatal(err) sbInter, err := makeGenesisRosterArgs(service, el, sbRoot.Hash, VerifyNone, 1, 1) log.ErrFatal(err) scsb := &SetChildrenSkipBlock{sbRoot.Hash, sbInter.Hash} service.SetChildrenSkipBlock(scsb) // Verifying other nodes also got the updated chains // Check for the root-chain for i, h := range hosts { log.Lvlf2("%x", skipchainSID) s := local.Services[h.ServerIdentity.ID][skipchainSID].(*Service) m, err := s.GetUpdateChain(&GetUpdateChain{sbRoot.Hash}) log.ErrFatal(err, "Failed in iteration="+strconv.Itoa(i)+":") sb := m.(*GetUpdateChainReply) log.Lvl2(s.Context) if len(sb.Update) != 1 { // we expect only the first block t.Fatal("There should be only 1 SkipBlock in the update") } link := sb.Update[0].ChildSL if !bytes.Equal(link.Hash, sbInter.Hash) { t.Fatal("The child-link doesn't point to our intermediate SkipBlock", i) } // We need to verify the signature on the child-link, too. This // has to be signed by the collective signature of sbRoot. if cerr := sbRoot.VerifySignatures(); cerr != nil { t.Fatal("Signature on child-link is not valid") } } // And check for the intermediate-chain to be updated for _, h := range hosts { s := local.Services[h.ServerIdentity.ID][skipchainSID].(*Service) m, cerr := s.GetUpdateChain(&GetUpdateChain{sbInter.Hash}) sb := m.(*GetUpdateChainReply) log.ErrFatal(cerr) if len(sb.Update) != 1 { t.Fatal("There should be only 1 SkipBlock in the update") } if !bytes.Equal(sb.Update[0].ParentBlockID, sbRoot.Hash) { t.Fatal("The intermediate SkipBlock doesn't point to the root") } if err := sb.Update[0].VerifySignatures(); err != nil { t.Fatal("Signature of that SkipBlock doesn't fit") } } }
func TestService_GetUpdateChain(t *testing.T) { // Create a small chain and test whether we can get from one element // of the chain to the last element with a valid slice of SkipBlocks local := onet.NewLocalTest() defer local.CloseAll() sbLength := 3 _, el, gs := local.MakeHELS(sbLength, skipchainSID) s := gs.(*Service) sbs := make([]*SkipBlock, sbLength) var err error sbs[0], err = makeGenesisRoster(s, el) log.ErrFatal(err) log.Lvl1("Initialize skipchain.") // init skipchain for i := 1; i < sbLength; i++ { log.Lvl2("Doing skipblock", i) newSB := NewSkipBlock() newSB.Roster = el psbrMsg, err := s.ProposeSkipBlock(&ProposeSkipBlock{sbs[i-1].Hash, newSB}) assert.Nil(t, err) reply := psbrMsg.(*ProposedSkipBlockReply) sbs[i] = reply.Latest } for i := 0; i < sbLength; i++ { m, err := s.GetUpdateChain(&GetUpdateChain{sbs[i].Hash}) sbc := m.(*GetUpdateChainReply) log.ErrFatal(err) if !sbc.Update[0].Equal(sbs[i]) { t.Fatal("First hash is not from our SkipBlock") } if !sbc.Update[len(sbc.Update)-1].Equal(sbs[sbLength-1]) { log.Lvl2(sbc.Update[len(sbc.Update)-1].Hash) log.Lvl2(sbs[sbLength-1].Hash) t.Fatal("Last Hash is not equal to last SkipBlock for", i) } for up, sb1 := range sbc.Update { log.ErrFatal(sb1.VerifySignatures()) if up < len(sbc.Update)-1 { sb2 := sbc.Update[up+1] h1 := sb1.Height h2 := sb2.Height log.Lvl2("sbc1.Height=", sb1.Height) log.Lvl2("sbc2.Height=", sb2.Height) // height := min(len(sb1.ForwardLink), h2) height := h1 if h2 < height { height = h2 } if !bytes.Equal(sb1.ForwardLink[height-1].Hash, sb2.Hash) { t.Fatal("Forward-pointer of", up, "is different of hash in", up+1) } } } } }
func TestService_ProposeSkipBlock(t *testing.T) { // First create a roster to attach the data to it local := onet.NewLocalTest() defer local.CloseAll() _, el, genService := local.MakeHELS(5, skipchainSID) service := genService.(*Service) service.SkipBlocks = make(map[string]*SkipBlock) // Setting up root roster sbRoot, err := makeGenesisRoster(service, el) log.ErrFatal(err) // send a ProposeBlock genesis := NewSkipBlock() genesis.Data = []byte("In the beginning God created the heaven and the earth.") genesis.MaximumHeight = 2 genesis.BaseHeight = 2 genesis.ParentBlockID = sbRoot.Hash genesis.Roster = sbRoot.Roster blockCount := 0 psbrMsg, err := service.ProposeSkipBlock(&ProposeSkipBlock{nil, genesis}) assert.Nil(t, err) psbr := psbrMsg.(*ProposedSkipBlockReply) latest := psbr.Latest // verify creation of GenesisBlock: assert.Equal(t, blockCount, latest.Index) // the genesis block has a random back-link: assert.Equal(t, 1, len(latest.BackLinkIds)) assert.NotEqual(t, 0, latest.BackLinkIds) next := NewSkipBlock() next.Data = []byte("And the earth was without form, and void; " + "and darkness was upon the face of the deep. " + "And the Spirit of God moved upon the face of the waters.") next.MaximumHeight = 2 next.ParentBlockID = sbRoot.Hash next.Roster = sbRoot.Roster id := psbr.Latest.Hash psbrMsg, err = service.ProposeSkipBlock(&ProposeSkipBlock{id, next}) assert.Nil(t, err) psbr2 := psbrMsg.(*ProposedSkipBlockReply) log.Lvl2(psbr2) if psbr2 == nil { t.Fatal("Didn't get anything in return") } assert.NotNil(t, psbr2) assert.NotNil(t, psbr2.Latest) latest2 := psbr2.Latest // verify creation of GenesisBlock: blockCount++ assert.Equal(t, blockCount, latest2.Index) assert.Equal(t, 1, len(latest2.BackLinkIds)) assert.NotEqual(t, 0, latest2.BackLinkIds) // We've added 2 blocks, + root block = 3 assert.Equal(t, 3, service.lenSkipBlocks()) }
func TestCosi(t *testing.T) { //defer log.AfterTest(t) log.TestOutput(testing.Verbose(), 4) for _, nbrHosts := range []int{1, 3, 13} { log.Lvl2("Running cosi with", nbrHosts, "hosts") local := onet.NewLocalTest() hosts, el, tree := local.GenBigTree(nbrHosts, nbrHosts, 3, true) aggPublic := network.Suite.Point().Null() for _, e := range el.List { aggPublic = aggPublic.Add(aggPublic, e.Public) } done := make(chan bool) // create the message we want to sign for this round msg := []byte("Hello World Cosi") // Register the function generating the protocol instance var root *CoSi // function that will be called when protocol is finished by the root doneFunc := func(sig []byte) { suite := hosts[0].Suite() publics := el.Publics() if err := root.VerifyResponses(aggPublic); err != nil { t.Fatal("Error verifying responses", err) } if err := VerifySignature(suite, publics, msg, sig); err != nil { t.Fatal("Error verifying signature:", err) } done <- true } // Start the protocol p, err := local.CreateProtocol("CoSi", tree) if err != nil { t.Fatal("Couldn't create new node:", err) } root = p.(*CoSi) root.Message = msg responseFunc := func(in []abstract.Scalar) { log.Lvl1("Got response") if len(root.Children()) != len(in) { t.Fatal("Didn't get same number of responses") } } root.RegisterResponseHook(responseFunc) root.RegisterSignatureHook(doneFunc) go root.StartProtocol() select { case <-done: case <-time.After(time.Second * 2): t.Fatal("Could not get signature verification done in time") } local.CloseAll() } }
func TestRandHound(t *testing.T) { var name = "RandHound" var nodes int = 28 var faulty int = 2 var groups int = 4 var purpose string = "RandHound test run" local := onet.NewLocalTest() _, _, tree := local.GenTree(int(nodes), true) defer local.CloseAll() // Setup and start RandHound log.Lvlf1("RandHound - starting") protocol, err := local.CreateProtocol(name, tree) if err != nil { t.Fatal("Couldn't initialise RandHound protocol:", err) } rh := protocol.(*randhound.RandHound) err = rh.Setup(nodes, faulty, groups, purpose) if err != nil { t.Fatal("Couldn't initialise RandHound protocol:", err) } if err := protocol.Start(); err != nil { t.Fatal(err) } select { case <-rh.Done: log.Lvlf1("RandHound - done") random, transcript, err := rh.Random() if err != nil { t.Fatal(err) } log.Lvlf1("RandHound - collective randomness: ok") //log.Lvlf1("RandHound - collective randomness: %v", random) err = rh.Verify(rh.Suite(), random, transcript) if err != nil { t.Fatal(err) } log.Lvlf1("RandHound - verification: ok") case <-time.After(time.Second * time.Duration(nodes) * 2): t.Fatal("RandHound – time out") } }
func TestService_ProtocolVerification(t *testing.T) { // Testing whether we sign correctly the SkipBlocks local := onet.NewLocalTest() defer local.CloseAll() hosts, el, s := local.MakeHELS(3, skipchainSID) s1 := s.(*Service) s2 := local.Services[hosts[1].ServerIdentity.ID][skipchainSID].(*Service) s3 := local.Services[hosts[2].ServerIdentity.ID][skipchainSID].(*Service) services := []*Service{s1, s2, s3} sb, err := makeGenesisRosterArgs(s1, el, nil, VerifyNone, 1, 1) log.ErrFatal(err) for i := 0; i < 3; i++ { sb = launchVerification(t, services, i, sb) } }
func TestSkipBlock_Hash2(t *testing.T) { local := onet.NewLocalTest() hosts, el, _ := local.GenTree(2, false) defer local.CloseAll() sbd1 := NewSkipBlock() sbd1.Roster = el sbd1.Height = 1 h1 := sbd1.updateHash() assert.Equal(t, h1, sbd1.Hash) sbd2 := NewSkipBlock() sbd2.Roster = local.GenRosterFromHost(hosts[0]) sbd2.Height = 1 h2 := sbd2.updateHash() assert.NotEqual(t, h1, h2) }
func TestCoSimul(t *testing.T) { for VerifyResponse = 0; VerifyResponse < 3; VerifyResponse++ { for _, nbrHosts := range []int{1, 3, 13} { log.Lvl2("Running cosi with", nbrHosts, "hosts") local := onet.NewLocalTest() hosts, _, tree := local.GenBigTree(nbrHosts, nbrHosts, 3, true) log.Lvl2(tree.Dump()) done := make(chan bool) // create the message we want to sign for this round msg := []byte("Hello World Cosi") // Register the function generating the protocol instance var root *CoSimul // function that will be called when protocol is finished by the root doneFunc := func(sig []byte) { suite := hosts[0].Suite() if err := cosi.VerifySignature(suite, root.Publics(), msg, sig); err != nil { t.Fatal("error verifying signature:", err) } else { log.Lvl1("Verification OK") } done <- true } // Start the protocol p, err := local.CreateProtocol(Name, tree) if err != nil { t.Fatal("Couldn't create new node:", err) } root = p.(*CoSimul) root.Message = msg root.RegisterSignatureHook(doneFunc) go root.StartProtocol() select { case <-done: case <-time.After(time.Second * 2): t.Fatal("Could not get signature verification done in time") } local.CloseAll() } } }
func TestNtree(t *testing.T) { for _, nbrHosts := range []int{1, 3, 13} { log.Lvl2("Running ntree with", nbrHosts, "hosts") local := onet.NewLocalTest() _, _, tree := local.GenBigTree(nbrHosts, nbrHosts, 3, true) done := make(chan bool) // create the message we want to sign for this round msg := []byte("Ntree rocks slowly") // Register the function generating the protocol instance var root *ntree.Protocol // function that will be called when protocol is finished by the root doneFunc := func() bool { done <- true return true } // Start the protocol pi, err := local.CreateProtocol("NaiveTree", tree) if err != nil { t.Fatal("Couldn't create new node:", err) } root = pi.(*ntree.Protocol) root.Message = msg root.OnDoneCallback(doneFunc) err = pi.Start() if nbrHosts == 1 { if err == nil { t.Fatal("Shouldn't be able to start NTree with 1 node") } } else if err != nil { t.Fatal("Couldn't start protocol:", err) } else { select { case <-done: case <-time.After(time.Second * 2): t.Fatal("Protocol didn't finish in time") } } local.CloseAll() } }
func TestService_SignBlock(t *testing.T) { // Testing whether we sign correctly the SkipBlocks local := onet.NewLocalTest() defer local.CloseAll() _, el, genService := local.MakeHELS(3, skipchainSID) service := genService.(*Service) sbRoot, err := makeGenesisRosterArgs(service, el, nil, VerifyNone, 1, 1) log.ErrFatal(err) el2 := onet.NewRoster(el.List[0:2]) sb := NewSkipBlock() sb.Roster = el2 psbr, err := service.ProposeSkipBlock(&ProposeSkipBlock{sbRoot.Hash, sb}) log.ErrFatal(err) reply := psbr.(*ProposedSkipBlockReply) sbRoot = reply.Previous sbSecond := reply.Latest log.Lvl1("Verifying signatures") log.ErrFatal(sbRoot.VerifySignatures()) log.ErrFatal(sbSecond.VerifySignatures()) }
// Tests a 2-node system func TestCloseAll(t *testing.T) { local := onet.NewLocalTest() defer log.AfterTest(t) nbrNodes := 2 _, _, tree := local.GenTree(nbrNodes, true) defer local.CloseAll() pi, err := local.CreateProtocol("CloseAll", tree) if err != nil { t.Fatal("Couldn't start protocol:", err) } done := make(chan bool) go func() { pi.Start() done <- true }() select { case <-done: case <-time.After(10 * time.Second): t.Fatal("Didn't finish in 10 seconds") } }
// Tests a 2-node system func TestCount(t *testing.T) { local := onet.NewLocalTest() nbrNodes := 2 _, _, tree := local.GenTree(nbrNodes, true) defer local.CloseAll() pi, err := local.StartProtocol("Count", tree) if err != nil { t.Fatal("Couldn't start protocol:", err) } protocol := pi.(*manage.ProtocolCount) timeout := network.WaitRetry * time.Duration(network.MaxRetryConnect*nbrNodes*2) * time.Millisecond select { case children := <-protocol.Count: log.Lvl2("Instance 1 is done") if children != nbrNodes { t.Fatal("Didn't get a child-cound of", nbrNodes) } case <-time.After(timeout): t.Fatal("Didn't finish in time") } }
// Tests an n-node system func TestPropagate(t *testing.T) { for _, nbrNodes := range []int{3, 10, 14} { local := onet.NewLocalTest() conodes, el, _ := local.GenTree(nbrNodes, true) var i int var iMut sync.Mutex msg := &PropagateMsg{[]byte("propagate")} propFuncs := make([]PropagationFunc, nbrNodes) var err error for n, conode := range conodes { pc := &PC{conode, local.Overlays[conode.ServerIdentity.ID]} propFuncs[n], err = NewPropagationFunc(pc, "Propagate", func(m network.Body) { if bytes.Equal(msg.Data, m.(*PropagateMsg).Data) { iMut.Lock() i++ iMut.Unlock() } else { t.Error("Didn't receive correct data") } }) log.ErrFatal(err) } log.Lvl2("Starting to propagate", reflect.TypeOf(msg)) children, err := propFuncs[0](el, msg, 1000) log.ErrFatal(err) if i != nbrNodes { t.Fatal("Didn't get data-request") } if children != nbrNodes { t.Fatal("Not all nodes replied") } local.CloseAll() log.AfterTest(t) } }
func TestJVSS(t *testing.T) { // Setup parameters var name string = "JVSS" // Protocol name var nodes uint32 = 16 // Number of nodes var rounds int = 1 // Number of rounds msg := []byte("Hello World!") // Message to-be-signed local := onet.NewLocalTest() _, _, tree := local.GenTree(int(nodes), true) defer local.CloseAll() log.Lvl1("JVSS - starting") leader, err := local.CreateProtocol(name, tree) if err != nil { t.Fatal("Couldn't initialise protocol tree:", err) } jv := leader.(*JVSS) leader.Start() log.Lvl1("JVSS - setup done") for i := 0; i < rounds; i++ { log.Lvl1("JVSS - starting round", i) log.Lvl1("JVSS - requesting signature") sig, err := jv.Sign(msg) if err != nil { t.Fatal("Error signature failed", err) } log.Lvl1("JVSS - signature received") err = jv.Verify(msg, sig) if err != nil { t.Fatal("Error signature verification failed", err) } log.Lvl1("JVSS - signature verification succeded") } }