Beispiel #1
0
func testLogging() {
	// Check that os.Stderr is the (only) default logger
	check(util.LoggersCount(), 1)
	util.LoggerRemove(os.Stderr)
	check(util.LoggersCount(), 0)

	// Check that default loglevel is 0
	check(util.LogLevel, 0)

	flushy := new(FlushableBuffer)
	synchy := new(SyncableBuffer)

	util.LoggerAdd(flushy)
	check(util.LoggersCount(), 1)
	util.LoggerAdd(synchy)
	check(util.LoggersCount(), 2)

	util.LogLevel = 4
	defer func() { util.LogLevel = 0 }()
	oldfac := util.BacklogFactor
	defer func() { util.BacklogFactor = oldfac }()
	util.BacklogFactor = 4

	util.Log(0, "a0") // 0
	time.Sleep(200 * time.Millisecond)
	for i := 1; i <= 4; i++ {
		util.Log(i, "a%d", i)
	} //   1,2,3,4
	for i := 0; i <= 4; i++ {
		util.Log(i, "b%d", i)
	} // 0,1,2,3
	for i := 0; i <= 4; i++ {
		util.Log(i, "c%d", i)
	} // 0,1,2
	for i := 0; i <= 4; i++ {
		util.Log(i, "d%d", i)
	} // 0,1
	for i := 0; i <= 4; i++ {
		util.Log(i, "e%d", i)
	} // 0,1
	util.Log(1, "x") // should be logged because when this Log() is executed, the backlog is only 15 long
	util.Log(1, "y") // should NOT be logged after the previous "x" the backlog is 16=4*BacklogFactor long

	check(flushy.Flushes, 0)
	check(synchy.Flushes, 0)

	time.Sleep(2 * time.Second)
	check(flushy.Flushes, 1)
	check(synchy.Flushes, 1)

	util.Log(5, "Shouldnotbelogged!")
	util.Log(4, "Shouldbelogged!")
	time.Sleep(200 * time.Millisecond)
	check(flushy.Flushes, 2)
	check(synchy.Flushes, 2)

	util.LoggersSuspend()
	check(util.LoggersCount(), 0)
	util.LoggerAdd(os.Stderr)
	check(util.LoggersCount(), 1)
	util.LoggersSuspend()
	check(util.LoggersCount(), 0)
	util.Log(0, "This should disappear in the void")
	buffy := new(bytes.Buffer)
	util.LoggerAdd(buffy)
	joke := "Sagt die Katze zum Verkäufer: Ich hab nicht genug Mäuse. Kann ich auch in Ratten zahlen?"
	util.Log(0, joke)
	time.Sleep(200 * time.Millisecond)
	check(strings.Index(buffy.String(), joke) >= 0, true)

	check(util.LoggersCount(), 1)
	util.LoggersRestore()
	check(util.LoggersCount(), 1)
	util.LoggersRestore()
	check(util.LoggersCount(), 2)

	util.Log(0, "foo")
	time.Sleep(200 * time.Millisecond)

	lines := flushy.Buf.Split("\n")
	for i := range lines {
		if strings.Index(lines[i], "missing") < 0 {
			idx := strings.LastIndex(lines[i], " ")
			lines[i] = lines[i][idx+1:]
		} else {
			idx := strings.Index(lines[i], " missing")
			lines[i] = lines[i][idx-2 : idx]
		}
	}

	check(lines, []string{"a0", "a1", "a2", "a3", "a4", "b0", "b1", "b2", "b3", "c0", "c1", "c2", "d0", "d1", "e0", "e1", "x", "10", "Shouldbelogged!", "foo", ""})

	check(flushy.Buf.String(), synchy.Buf.String())

	// Reset loggers so that only os.Stderr is a logger
	for util.LoggersCount() > 0 {
		util.LoggersRestore()
	}
	util.LoggerAdd(os.Stderr)
}
Beispiel #2
0
// Unit tests for the package security.
func Security_test() {
	fmt.Printf("\n==== security ===\n\n")

	config.CACertPath = []string{"testdata/certs/ca.cert"}

	// do not spam console with expected errors but do
	// store them in the log file (if any is configured)
	util.LoggerRemove(os.Stderr)
	defer util.LoggerAdd(os.Stderr)

	cli, srv := tlsTest("1", "1")
	check(cli != nil, true)
	check(srv != nil, true)

	cli, srv = tlsTest("1", "2")
	check(cli != nil, true)
	check(srv != nil, true)

	cli, srv = tlsTest("nocert", "1")
	check(cli, nil)
	check(srv, nil)

	cli, srv = tlsTest("signedbywrongca", "1")
	check(cli != nil, true)
	check(srv, nil)

	cli, srv = tlsTest("1", "signedbywrongca")
	check(cli, nil)
	check(srv != nil, true)

	cli, srv = tlsTest("local", "2")
	check(cli != nil, true)
	check(srv != nil, true)

	cli, srv = tlsTest("badip", "2")
	check(cli != nil, true)
	check(srv, nil)

	cli, srv = tlsTest("badname", "2")
	check(cli != nil, true)
	check(srv, nil)

	cli, srv = tlsTest("localname", "2")
	check(cli != nil, true)
	check(srv, nil) // because *ocalhost does not match the actual resolved name

	security.SetMyServer("8.8.8.8")
	cli, srv = tlsTest("myserver", "2")
	check(cli != nil, true)
	check(srv, nil)

	security.SetMyServer("127.0.0.1")
	cli, srv = tlsTest("myserver", "2")
	check(cli != nil, true)
	check(srv != nil, true)

	cli, srv = tlsTest("limits", "2")
	check(cli != nil, true)
	if check(srv != nil, true) {
		check(srv.Limits.TotalTime, time.Duration(98765)*time.Millisecond)
		check(srv.Limits.TotalBytes, 123456789012345)
		check(srv.Limits.MessageBytes, 76767542)
		check(srv.Limits.ConnPerHour, 3289)
		check(srv.Limits.ConnParallel, 348201284)
		check(srv.Limits.MaxLogFiles, 700499)
		check(srv.Limits.MaxAnswers, 4)
		check(srv.Limits.CommunicateWith, []string{"foo.tvc.muenchen.de:8089", "nobody", "1.2.3.4", "*"})
	}
}