-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a8be0b
commit 362c741
Showing
7 changed files
with
250 additions
and
0 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,96 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ProtonMail/gluon/rfcparser" | ||
) | ||
|
||
type IDGet struct{} | ||
|
||
func (l IDGet) String() string { | ||
return fmt.Sprintf("ID") | ||
} | ||
|
||
func (l IDGet) SanitizedString() string { | ||
return l.String() | ||
} | ||
|
||
type IDSet struct { | ||
Values map[string]string | ||
} | ||
|
||
func (l IDSet) String() string { | ||
if len(l.Values) == 0 { | ||
return "ID" | ||
} | ||
|
||
return fmt.Sprintf("ID %v", l.Values) | ||
} | ||
|
||
func (l IDSet) SanitizedString() string { | ||
return l.String() | ||
} | ||
|
||
type IDCommandParser struct{} | ||
|
||
func (IDCommandParser) FromParser(p *rfcparser.Parser) (Payload, error) { | ||
// nolint:dupword | ||
// id ::= "ID" SPACE id_params_list | ||
// id_params_list ::= "(" #(string SPACE nstring) ")" / nil | ||
// ;; list of field value pairs | ||
if err := p.Consume(rfcparser.TokenTypeSP, "expected space after command"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if p.Check(rfcparser.TokenTypeChar) { | ||
if err := p.ConsumeBytesFold('N', 'I', 'L'); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &IDGet{}, nil | ||
} | ||
|
||
values := make(map[string]string) | ||
|
||
if err := p.Consume(rfcparser.TokenTypeLParen, "expected ( for id values start"); err != nil { | ||
return nil, err | ||
} | ||
|
||
for { | ||
key, ok, err := p.TryParseString() | ||
if err != nil { | ||
return nil, err | ||
} else if !ok { | ||
break | ||
} | ||
|
||
if err := p.Consume(rfcparser.TokenTypeSP, "expected space after ID key"); err != nil { | ||
return nil, err | ||
} | ||
|
||
value, isNil, err := ParseNString(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !isNil { | ||
values[key.Value] = value.Value | ||
} else { | ||
values[key.Value] = "" | ||
} | ||
|
||
if !p.Check(rfcparser.TokenTypeRParen) { | ||
if err := p.Consume(rfcparser.TokenTypeSP, "expected space after ID value"); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
if err := p.Consume(rfcparser.TokenTypeRParen, "expected ) for id values end"); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &IDSet{ | ||
Values: values, | ||
}, 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package command | ||
|
||
import ( | ||
"bytes" | ||
"github.com/ProtonMail/gluon/rfcparser" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestParser_IDCommandGet(t *testing.T) { | ||
input := toIMAPLine(`tag ID NIL`) | ||
s := rfcparser.NewScanner(bytes.NewReader(input)) | ||
p := NewParser(s) | ||
|
||
expected := Command{Tag: "tag", Payload: &IDGet{}} | ||
|
||
cmd, err := p.Parse() | ||
require.NoError(t, err) | ||
require.Equal(t, expected, cmd) | ||
require.Equal(t, "id", p.LastParsedCommand()) | ||
require.Equal(t, "tag", p.LastParsedTag()) | ||
} | ||
|
||
func TestParser_IDCommandSetOne(t *testing.T) { | ||
input := toIMAPLine(`tag ID ("foo" "bar")`) | ||
s := rfcparser.NewScanner(bytes.NewReader(input)) | ||
p := NewParser(s) | ||
|
||
expected := Command{Tag: "tag", Payload: &IDSet{Values: map[string]string{"foo": "bar"}}} | ||
|
||
cmd, err := p.Parse() | ||
require.NoError(t, err) | ||
require.Equal(t, expected, cmd) | ||
require.Equal(t, "id", p.LastParsedCommand()) | ||
require.Equal(t, "tag", p.LastParsedTag()) | ||
} | ||
|
||
func TestParser_IDCommandSetEmpty(t *testing.T) { | ||
expected := Command{Tag: "tag", Payload: &IDSet{ | ||
Values: map[string]string{}, | ||
}} | ||
|
||
cmd, err := testParseCommand(`tag ID ()`) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, cmd) | ||
} | ||
|
||
func TestParser_IDCommandSetMany(t *testing.T) { | ||
expected := Command{Tag: "tag", Payload: &IDSet{ | ||
Values: map[string]string{ | ||
"foo": "bar", | ||
"a": "", | ||
"c": "d", | ||
}, | ||
}} | ||
|
||
cmd, err := testParseCommand(`tag ID ("foo" "bar" "a" NIL "c" "d")`) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, cmd) | ||
} | ||
|
||
func TestParser_IDCommandFailures(t *testing.T) { | ||
inputs := []string{ | ||
"tag ID", | ||
"tag ID ", | ||
"tag ID N", | ||
"tag ID (", | ||
`tag ID ("foo")`, | ||
`tag ID ("foo" )`, | ||
`tag ID ("foo""bar")`, | ||
`tag ID (nil nil)`, | ||
`tag ID ("foo" "bar"`, | ||
`tag ID ("foo" "bar" "z")`, | ||
} | ||
|
||
for _, i := range inputs { | ||
_, err := testParseCommand(i) | ||
require.Error(t, 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package command | ||
|
||
import "github.com/ProtonMail/gluon/rfcparser" | ||
|
||
// ParseNString pareses a string or NIL. If NIL was parsed the boolean return is set to false. | ||
func ParseNString(p *rfcparser.Parser) (rfcparser.String, bool, error) { | ||
// nstring = string / nil | ||
if s, ok, err := p.TryParseString(); err != nil { | ||
return rfcparser.String{}, false, err | ||
} else if ok { | ||
return s, false, nil | ||
} | ||
|
||
if err := p.ConsumeBytesFold('N', 'I', 'L'); err != nil { | ||
return rfcparser.String{}, false, err | ||
} | ||
|
||
return rfcparser.String{}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package command | ||
|
||
import ( | ||
"bytes" | ||
"github.com/ProtonMail/gluon/rfcparser" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestParseNStringString(t *testing.T) { | ||
input := []byte(`"foo"`) | ||
|
||
p := rfcparser.NewParser(rfcparser.NewScanner(bytes.NewReader(input))) | ||
// Advance at least once to prepare first token. | ||
err := p.Advance() | ||
require.NoError(t, err) | ||
|
||
v, isNil, err := ParseNString(p) | ||
require.NoError(t, err) | ||
require.Equal(t, "foo", v.Value) | ||
require.False(t, isNil) | ||
} | ||
|
||
func TestParseNStringNIL(t *testing.T) { | ||
input := []byte(`NIL`) | ||
|
||
p := rfcparser.NewParser(rfcparser.NewScanner(bytes.NewReader(input))) | ||
// Advance at least once to prepare first token. | ||
err := p.Advance() | ||
require.NoError(t, err) | ||
|
||
_, isNil, err := ParseNString(p) | ||
require.NoError(t, err) | ||
require.True(t, isNil) | ||
} |
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