Esempio n. 1
0
func TestNewMailWriteValid(t *testing.T) {

	testDir := path.Join(".", "testdata", time.Now().String())
	defer func() {
		if err := os.RemoveAll(testDir); err != nil {
			t.Fatal(err)
		}
	}()
	l, err := maillog.New(testDir, "").Init("http://schumacherfm.local")
	if err != nil {
		t.Fatal(err)
	}

	wc := l.NewWriter()
	defer func() {
		if err := wc.Close(); err != nil {
			t.Fatal(err)
		}
	}()

	var testData = []byte(`Snowden: The @FBI is creating a world where citizens rely on #Apple to defend their rights, rather than the other way around. https://t.co/vdjB6CuB7k`)
	n, err := wc.Write(testData)
	if err != nil {
		t.Fatal(err)
	}
	assert.Exactly(t, len(testData), n)
}
Esempio n. 2
0
func TestNewFail(t *testing.T) {

	testDir := path.Join(string(os.PathSeparator), "testdata") // try to create dir in root
	l, err := maillog.New(testDir, testDir).Init()
	assert.NotNil(t, l)
	assert.EqualError(t, err, "Cannot create directory \"/testdata\" because of: mkdir /testdata: permission denied")
}
Esempio n. 3
0
func TestNewEmpty(t *testing.T) {

	l, err := maillog.New("", "").Init()
	assert.NotNil(t, l)
	assert.Nil(t, err)
	l.Errorf("hello %d", 4711)
	wc := l.NewWriter()
	n, err := wc.Write([]byte("H3ll0"))
	assert.NoError(t, err)
	assert.Exactly(t, 5, n)
}
Esempio n. 4
0
func TestNewErrorfValid(t *testing.T) {

	testDir := path.Join(".", "testdata", time.Now().String())
	defer func() {
		if err := os.RemoveAll(testDir); err != nil {
			t.Fatal(err)
		}
	}()
	l, err := maillog.New("", testDir).Init()
	if err != nil {
		t.Fatal(err)
	}

	const testData = `Snowden: The @FBI is creating a world where citizens rely on #Apple to defend their rights, rather than the other way around. https://t.co/vdjB6CuB7k`
	l.Errorf(testData)

	logContent, err := ioutil.ReadFile(l.ErrFile)
	if err != nil {
		t.Fatal(err)
	}
	assert.Contains(t, string(logContent), testData)
}
Esempio n. 5
0
func parse(c *caddy.Controller) (mc *config, _ error) {
	// This parses the following config blocks
	mc = newConfig()

	for c.Next() {
		args := c.RemainingArgs()

		switch len(args) {
		case 1:
			mc.endpoint = args[0]
		}

		for c.NextBlock() {
			var err error
			switch c.Val() {
			case "publickeyAttachmentFileName":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.pgpAttachmentName = c.Val()
			case "maillog":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				if mc.maillog.IsNil() {
					mc.maillog = maillog.New(c.Val(), "")
				} else {
					mc.maillog.MailDir = c.Val()
				}
			case "errorlog":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				if mc.maillog.IsNil() {
					mc.maillog = maillog.New("", c.Val())
				} else {
					mc.maillog.ErrDir = c.Val()
				}
			case "to":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.to, err = splitEmailAddresses(c.Val())
				if err != nil {
					return nil, err
				}
			case "cc":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.cc, err = splitEmailAddresses(c.Val())
				if err != nil {
					return nil, err
				}
			case "bcc":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.bcc, err = splitEmailAddresses(c.Val())
				if err != nil {
					return nil, err
				}
			case "subject":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.subject = c.Val()
			case "body":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.body = c.Val()
			case "username":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.username = c.Val()
			case "password":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.password = c.Val()
			case "host":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.host = c.Val()
			case "port":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				mc.portRaw = c.Val()
			case "ratelimit_interval":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				var rli time.Duration
				rli, err = time.ParseDuration(c.Val())
				if err != nil {
					return nil, err
				}
				if rli.Nanoseconds() != 0 {
					mc.rateLimitInterval = rli
				}
			case "ratelimit_capacity":
				if !c.NextArg() {
					return nil, c.ArgErr()
				}
				var rlc int64
				rlc, err = strconv.ParseInt(c.Val(), 10, 64)
				if err != nil {
					return nil, err
				}
				if rlc > 0 {
					mc.rateLimitCapacity = rlc
				}
			default:
				anyKey := c.Val()
				if isValidEmail(anyKey) {
					if !c.NextArg() {
						return nil, c.ArgErr()
					}
					pgpPublicKey := c.Val()
					mc.pgpEmailKeys = append(mc.pgpEmailKeys, anyKey, pgpPublicKey)
				}
			}
		}
	}
	return
}