// For testing the different round-types // Every round-type is in his own Test*-method, // so one can easily run just a given round-test func testRound(t *testing.T, roundType string) { dbg.TestOutput(testing.Verbose(), 4) dbg.Lvl2("Testing", roundType) peer1, peer2 := createPeers() round, err := sign.NewRoundFromType(roundType, peer1.Node) if err != nil { t.Fatal("Couldn't create", roundType, "round:", err) } peer1.StartAnnouncement(round) time.Sleep(time.Second) var cosi *sign.CosiStruct switch roundType { case sign.RoundCosiType: cosi = round.(*sign.RoundCosi).Cosi case sign.RoundExceptionType: cosi = round.(*sign.RoundException).Cosi case conode.RoundStamperType: cosi = round.(*conode.RoundStamper).Cosi case conode.RoundStamperListenerType: cosi = round.(*conode.RoundStamperListener).Cosi } if cosi.R_hat == nil { t.Fatal("Didn't finish round - R_hat empty") } err = cosi.VerifyResponses() if err != nil { t.Fatal("Couldn't verify responses") } peer1.Close() peer2.Close() }
// Runs two conodes and tests if the value returned is OK func TestStamp(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) peer1, peer2 := createPeers() go peer1.LoopRounds(conode.RoundStamperListenerType, 4) go peer2.LoopRounds(conode.RoundStamperListenerType, 4) time.Sleep(2 * time.Second) s, err := conode.NewStamp("testdata/config.toml") if err != nil { t.Fatal("Couldn't open config-file:", err) } for _, port := range []int{7000, 7010} { stamper := "localhost:" + strconv.Itoa(port) dbg.Lvl2("Contacting stamper", stamper) tsm, err := s.GetStamp([]byte("test"), stamper) dbg.Lvl3("Evaluating results of", stamper) if err != nil { t.Fatal("Couldn't get stamp from server:", err) } if !tsm.Srep.AggPublic.Equal(s.X0) { t.Fatal("Not correct aggregate public key") } } dbg.Lvl2("Closing peer1") peer1.Close() dbg.Lvl2("Closing peer2") peer2.Close() dbg.Lvl3("Done with test") }
func TestMonitor(t *testing.T) { dbg.TestOutput(testing.Verbose(), 2) m := make(map[string]string) m["machines"] = "1" m["ppm"] = "1" stat := NewStats(m) fresh := stat.String() // First set up monitor listening mon := NewMonitor(stat) go mon.Listen() time.Sleep(100 * time.Millisecond) // Then measure err := ConnectSink("localhost:" + SinkPort) if err != nil { t.Error(fmt.Sprintf("Error starting monitor: %s", err)) return } meas := NewMeasure("round") meas.Measure() time.Sleep(200 * time.Millisecond) meas.Measure() End() time.Sleep(100 * time.Millisecond) updated := stat.String() if updated == fresh { t.Error("Stats not updated ?") } StopSink() }
// Make sure closeall sends messages to everybody func TestCloseAll(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) peer1, peer2 := createPeers() // Launch peers in endless loop go peer1.LoopRounds(sign.RoundCosiType, -1) go peer2.LoopRounds(sign.RoundCosiType, -1) // Send CloseAll manually peer1.SendCloseAll() time.Sleep(time.Second) if !peer1.Closed { t.Fatal("Peer 1 should be closed now.") } if !peer2.Closed { t.Fatal("Peer 2 should be closed now.") } // Now let's just wait for two rounds peer1, peer2 = createPeers() go peer1.LoopRounds(sign.RoundCosiType, 2) go peer2.LoopRounds(sign.RoundCosiType, 2) time.Sleep(time.Second * 4) if !peer1.Closed { t.Fatal("Peer 1 should be closed now.") } if !peer2.Closed { t.Fatal("Peer 2 should be closed now.") } }
// Tests if the rounds are deleted when done func TestDeleteRounds(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) peer1, peer2 := createPeers() if len(peer1.Rounds) != 0 { t.Fatal("There should be 0 rounds to start with") } round, err := sign.NewRoundFromType(conode.RoundStamperListenerType, peer1.Node) if err != nil { t.Fatal("Couldn't create cosi-round") } peer1.StartAnnouncement(round) if len(peer1.Rounds) != 1 { t.Fatal("Created one round - should be there") } time.Sleep(time.Second) if len(peer1.Rounds) != 0 { t.Fatal("Doing one round shouldn't take more than 1 second") } peer1.Close() peer2.Close() }
func TestSigScanner(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) signame := "/tmp/sigs.txt" ioutil.WriteFile(signame, []byte(TestFileSignatures), 0660) blocks, err := SigScanner(signame) if err != nil { t.Fatal("Error while parsing blocks:", err) } dbg.Printf("%+v", blocks) }
func TestPolicyScanner(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) polname := "/tmp/policy.txt" ioutil.WriteFile(polname, []byte(TestFilePolicy), 0660) thres, devkeys, cothkey, err := PolicyScanner(polname) if err != nil { t.Fatal("Error while parsing blocks:", err) } dbg.Printf("%+v\n %+v\n %+v\n", thres, devkeys, cothkey) }
func TestReadyProxy(t *testing.T) { dbg.TestOutput(testing.Verbose(), 3) m := make(map[string]string) m["machines"] = "1" m["ppm"] = "1" stat := NewStats(m) // First set up monitor listening monitor := NewMonitor(stat) done := make(chan bool) go func() { monitor.Listen() done <- true }() time.Sleep(100 * time.Millisecond) // Then setup proxy // change port so the proxy does not listen to the same // than the original monitor oldSink := SinkPort SinkPort = "8000" // proxy listen to 0.0.0.0:8000 & redirect to // localhost:4000 go Proxy("localhost:" + oldSink) time.Sleep(100 * time.Millisecond) // Then measure proxyAddr := "localhost:" + SinkPort err := ConnectSink(proxyAddr) if err != nil { t.Error(fmt.Sprintf("Can not connect to proxy : %s", err)) return } s, err := GetReady(proxyAddr) if err != nil { t.Error("Couldn't get stats from proxy") } if s.Ready != 0 { t.Error("stats.Ready should be 0") } Ready(proxyAddr) s, err = GetReady(proxyAddr) if err != nil { t.Error("Couldn't get stats from proxy") } if s.Ready != 1 { t.Error("stats.Ready should be 1") } SinkPort = oldSink End() StopSink() }
// What happens if client closes before server does? func TestClientBeforeServer(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) peer1, peer2 := createPeers() peer2.Close() time.Sleep(time.Second) round, err := sign.NewRoundFromType(sign.RoundCosiType, peer1.Node) if err != nil { t.Fatal("Error while creating round:", err) } peer1.StartAnnouncement(round) time.Sleep(time.Second) peer1.Close() }
func TestVerification(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) var ( PolicyFile = "example/policy.txt" SignaturesFile = "example/signatures.txt" CommitIdFile = "example/commitid.txt" ) commitId, _ := CommitScanner(CommitIdFile) decision, err := ApprovalCheck(PolicyFile, SignaturesFile, commitId) if err != nil { dbg.Panic("Problem with verifying approval of developers", err) } // dbg.Printf("How many signatures have been read? %+v", len(co.Signatures)) // dbg.Printf("What is a threshold value? %+v", co.Policy.Threshold) dbg.Printf("Is commit approved? %+v", decision) }
func TestReadyNormal(t *testing.T) { dbg.TestOutput(testing.Verbose(), 3) m := make(map[string]string) m["machines"] = "1" m["ppm"] = "1" m["Ready"] = "0" stat := NewStats(m) if stat.Ready != 0 { t.Fatal("Stats should start with ready==0") } // First set up monitor listening mon := NewMonitor(stat) go mon.Listen() time.Sleep(100 * time.Millisecond) host := "localhost:" + SinkPort if stat.Ready != 0 { t.Fatal("Stats should have ready==0 after start of Monitor") } s, err := GetReady(host) if err != nil { t.Fatal("Couldn't get number of peers:", err) } if s.Ready != 0 { t.Fatal("Stats.Ready != 0") } err = Ready(host) if err != nil { t.Errorf("Error starting monitor: %s", err) return } s, err = GetReady(host) if err != nil { t.Fatal("Couldn't get number of peers:", err) } if s.Ready != 1 { t.Fatal("Stats.Ready != 1") } End() StopSink() }
// Can we build the Peer without a valid key? func TestEmptyKeys(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) conf1 := readConfig() emptyKeys(conf1.Tree) peer1 := createPeer(conf1, 1) dbg.Lvlf3("Peer 1 is %+v", peer1) conf2 := readConfig() emptyKeys(conf2.Tree) peer2 := createPeer(conf2, 1) dbg.Lvlf3("Peer 1 is %+v", peer2) go peer1.LoopRounds(sign.RoundCosiType, 2) go peer2.LoopRounds(sign.RoundCosiType, 2) time.Sleep(time.Second * 2) peer1.Close() peer2.Close() }
func TestStampListener(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) peer1, peer2 := createPeers() round1 := conode.NewRoundStamperListener(peer1.Node) round2, err := sign.NewRoundFromType(conode.RoundStamperListenerType, peer1.Node) if err != nil { dbg.Fatal("Error when creating round:", err) } dbg.Lvlf2("Round1: %+v", round1) dbg.Lvlf2("Round2: %+v", round2) name1, name2 := round1.Name, round2.(*conode.RoundStamperListener).Name if name1 != name2 { t.Fatal("Hostname of first round is", name1, "and should be equal to", name2) } peer1.Close() peer2.Close() }
func TestKeyOrder(t *testing.T) { dbg.TestOutput(testing.Verbose(), 3) m := make(map[string]string) m["machines"] = "1" m["ppm"] = "1" m["bf"] = "2" m["rounds"] = "3" for i := 0; i < 20; i++ { // First set up monitor listening stat := NewStats(m) NewMonitor(stat) time.Sleep(100 * time.Millisecond) b := bytes.NewBuffer(make([]byte, 1024)) stat.WriteHeader(b) dbg.Lvl2("Order:", strings.TrimSpace(b.String())) if strings.Contains(b.String(), "rounds, bf") { t.Fatal("Order of fields is not correct") } } }
func TestRoundSetup(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) roundType := "setup" dbg.Lvl2("Testing", roundType) peer1, peer2 := createPeers() round, err := sign.NewRoundFromType(roundType, peer1.Node) if err != nil { t.Fatal("Couldn't create", roundType, "round:", err) } peer1.StartAnnouncement(round) time.Sleep(time.Second) counted := <-round.(*sign.RoundSetup).Counted if counted != 2 { t.Fatal("Counted", counted, "nodes, but should be 2") } peer1.Close() peer2.Close() }
func TestMakeConfig(t *testing.T) { dbg.TestOutput(testing.Verbose(), 2) os.Chdir("/tmp") KeyGeneration("key1", "localhost:2000") KeyGeneration("key2", "localhost:2010") key1, _ := ioutil.ReadFile("key1.pub") key2, _ := ioutil.ReadFile("key2.pub") ioutil.WriteFile("hosts", []byte(fmt.Sprintf("%s%s", key1, key2)), 0666) Build("hosts", 2, "config.toml") wg := sync.WaitGroup{} wg.Add(2) maxRounds = 3 go (func() { Run("config.toml", "key1") wg.Done() })() go (func() { Run("config.toml", "key2") wg.Done() })() wg.Wait() }
func TestReadRunfile(t *testing.T) { dbg.TestOutput(testing.Verbose(), 2) tplat := &TPlat{} tmpfile := "/tmp/testrun.toml" err := ioutil.WriteFile(tmpfile, []byte(testfile), 0666) if err != nil { dbg.Fatal("Couldn't create file:", err) } tests := platform.ReadRunFile(tplat, tmpfile) dbg.Lvl2(tplat) dbg.Lvlf2("%+v\n", tests[0]) if tplat.App != "sign" { dbg.Fatal("App should be 'sign'") } if len(tests) != 2 { dbg.Fatal("There should be 2 tests") } if tests[0].Get("machines") != "8" { dbg.Fatal("Machines = 8 has not been copied into RunConfig") } }
func TestRoundException(t *testing.T) { dbg.TestOutput(testing.Verbose(), 4) peer1, peer2 := createPeers() sign.ExceptionForceFailure = peer2.Name() round, err := sign.NewRoundFromType(sign.RoundExceptionType, peer1.Node) if err != nil { t.Fatal("Couldn't create Exception round:", err) } peer1.StartAnnouncement(round) time.Sleep(time.Second) cosi := round.(*sign.RoundException).Cosi if cosi.R_hat == nil { t.Fatal("Didn't finish round - R_hat empty") } err = cosi.VerifyResponses() if err != nil { t.Fatal("Couldn't verify responses") } peer1.Close() peer2.Close() }
func TestProxy(t *testing.T) { dbg.TestOutput(testing.Verbose(), 3) m := make(map[string]string) m["machines"] = "1" m["ppm"] = "1" m["filter_round"] = "100" stat := NewStats(m) fresh := stat.String() // First set up monitor listening monitor := NewMonitor(stat) done := make(chan bool) go func() { monitor.Listen() done <- true }() time.Sleep(100 * time.Millisecond) // Then setup proxy // change port so the proxy does not listen to the same // than the original monitor oldSink := SinkPort SinkPort = "8000" // proxy listen to 0.0.0.0:8000 & redirect to // localhost:4000 go Proxy("localhost:" + oldSink) time.Sleep(100 * time.Millisecond) // Then measure proxyAddr := "localhost:" + SinkPort err := ConnectSink(proxyAddr) if err != nil { t.Error(fmt.Sprintf("Can not connect to proxy : %s", err)) return } meas := NewMeasure("setup") meas.Measure() time.Sleep(100 * time.Millisecond) meas.Measure() s, err := GetReady(proxyAddr) if err != nil { t.Error("Couldn't get stats from proxy") } if s.Ready != 0 { t.Error("stats.Ready should be 0") } Ready(proxyAddr) s, err = GetReady(proxyAddr) if err != nil { t.Error("Couldn't get stats from proxy") } if s.Ready != 1 { t.Error("stats.Ready should be 1") } SinkPort = oldSink End() StopSink() select { case <-done: s := monitor.Stats() s.Collect() if s.String() == fresh { t.Error("stats not updated?") } return case <-time.After(2 * time.Second): t.Error("Monitor not finished") } }