-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a bunch of errors in impl and interface & start integrating Pigeo…
…nhole tests suite
- Loading branch information
Showing
22 changed files
with
624 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "tests/pigeonhole"] | ||
path = tests/pigeonhole | ||
url = https://github.com/dovecot/pigeonhole.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package sieve | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/foxcpp/go-sieve/interp" | ||
) | ||
|
||
func RunDovecotTest(t *testing.T, path string) { | ||
svScript, err := os.ReadFile(path) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
opts := DefaultOptions() | ||
opts.Interp.T = t | ||
|
||
script, err := Load(bytes.NewReader(svScript), opts) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
// Empty data. | ||
data := NewRuntimeData(script, interp.DummyPolicy{}, | ||
interp.EnvelopeStatic{}, interp.MessageStatic{}) | ||
data.Namespace = os.DirFS(filepath.Dir(path)) | ||
|
||
err = script.Execute(ctx, data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,12 +49,16 @@ func testExecute(t *testing.T, in string, eml string, intendedResult result) { | |
if err != nil { | ||
t.Fatal(err) | ||
} | ||
data := interp.NewRuntimeData(loadedScript, nil, interp.MessageStatic{ | ||
SMTPFrom: "[email protected]", | ||
SMTPTo: "[email protected]", | ||
Size: len(eml), | ||
Header: msgHdr, | ||
}) | ||
env := interp.EnvelopeStatic{ | ||
From: "[email protected]", | ||
To: "[email protected]", | ||
} | ||
msg := interp.MessageStatic{ | ||
Size: len(eml), | ||
Header: msgHdr, | ||
} | ||
data := interp.NewRuntimeData(loadedScript, interp.DummyPolicy{}, | ||
env, msg) | ||
|
||
ctx := context.Background() | ||
if err := loadedScript.Execute(ctx, data); err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module github.com/foxcpp/go-sieve | ||
|
||
go 1.18 | ||
go 1.20 | ||
|
||
require github.com/davecgh/go-spew v1.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package interp | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"net/textproto" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/foxcpp/go-sieve/lexer" | ||
"github.com/foxcpp/go-sieve/parser" | ||
) | ||
|
||
const DovecotTestExtension = "vnd.dovecot.testsuite" | ||
|
||
type CmdDovecotTest struct { | ||
TestName string | ||
Cmds []Cmd | ||
} | ||
|
||
func (c CmdDovecotTest) Execute(ctx context.Context, d *RuntimeData) error { | ||
testData := d.Copy() | ||
testData.testName = c.TestName | ||
|
||
d.Script.opts.T.Run(c.TestName, func(t *testing.T) { | ||
for _, cmd := range c.Cmds { | ||
if err := cmd.Execute(ctx, testData); err != nil { | ||
if errors.Is(err, ErrStop) { | ||
if testData.testFailMessage != "" { | ||
t.Error("test_fail called:", testData.testFailMessage) | ||
} | ||
return | ||
} | ||
t.Fatal("Test execution error:", err) | ||
} | ||
} | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
type CmdDovecotTestFail struct { | ||
Message string | ||
} | ||
|
||
func (c CmdDovecotTestFail) Execute(ctx context.Context, d *RuntimeData) error { | ||
d.testFailMessage = c.Message | ||
return nil | ||
} | ||
|
||
type CmdDovecotTestSet struct { | ||
VariableName string | ||
VariableValue string | ||
} | ||
|
||
func (c CmdDovecotTestSet) Execute(ctx context.Context, d *RuntimeData) error { | ||
switch c.VariableName { | ||
case "message": | ||
r := textproto.NewReader(bufio.NewReader(strings.NewReader(c.VariableValue))) | ||
msgHdr, err := r.ReadMIMEHeader() | ||
if err != nil { | ||
return fmt.Errorf("failed to parse test message: %v", err) | ||
} | ||
|
||
d.Msg = MessageStatic{ | ||
Size: len(c.VariableValue), | ||
Header: msgHdr, | ||
} | ||
case "envelope.from": | ||
d.Envelope = EnvelopeStatic{ | ||
From: c.VariableValue, | ||
To: d.Envelope.EnvelopeTo(), | ||
} | ||
case "envelope.to": | ||
d.Envelope = EnvelopeStatic{ | ||
From: d.Envelope.EnvelopeFrom(), | ||
To: c.VariableValue, | ||
} | ||
default: | ||
d.Variables[c.VariableName] = c.VariableValue | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type TestDovecotCompile struct { | ||
ScriptPath string | ||
} | ||
|
||
func (t TestDovecotCompile) Check(ctx context.Context, d *RuntimeData) (bool, error) { | ||
if d.Namespace == nil { | ||
return false, fmt.Errorf("RuntimeData.Namespace is not set, cannot load scripts") | ||
} | ||
|
||
svScript, err := fs.ReadFile(d.Namespace, t.ScriptPath) | ||
if err != nil { | ||
return false, nil | ||
} | ||
|
||
toks, err := lexer.Lex(bytes.NewReader(svScript), &lexer.Options{ | ||
MaxTokens: 5000, | ||
}) | ||
if err != nil { | ||
return false, nil | ||
} | ||
|
||
cmds, err := parser.Parse(lexer.NewStream(toks), &parser.Options{ | ||
MaxBlockNesting: d.testMaxNesting, | ||
MaxTestNesting: d.testMaxNesting, | ||
}) | ||
if err != nil { | ||
return false, nil | ||
} | ||
|
||
script, err := LoadScript(cmds, &Options{ | ||
MaxRedirects: d.Script.opts.MaxRedirects, | ||
}) | ||
if err != nil { | ||
return false, nil | ||
} | ||
|
||
d.testScript = script | ||
return true, nil | ||
} | ||
|
||
type TestDovecotRun struct { | ||
} | ||
|
||
func (t TestDovecotRun) Check(ctx context.Context, d *RuntimeData) (bool, error) { | ||
if d.testScript == nil { | ||
return false, nil | ||
} | ||
|
||
testD := d.Copy() | ||
testD.Script = d.testScript | ||
// Note: Loaded script has no test environment available - | ||
// it is a regular Sieve script. | ||
|
||
err := d.testScript.Execute(ctx, testD) | ||
if err != nil { | ||
return false, nil | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
type TestDovecotTestError struct { | ||
} | ||
|
||
func (t TestDovecotTestError) Check(ctx context.Context, d *RuntimeData) (bool, error) { | ||
// go-sieve has a very different error formatting and stops lexing/parsing/loading | ||
// on first error, therefore we skip all test_errors checks as they are | ||
// Pigeonhole-specific. | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.