|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ProtonMail/gluon/rfcparser" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func continuationChecker(continued *bool) func() error { |
| 14 | + return func() error { *continued = true; return nil } |
| 15 | +} |
| 16 | + |
| 17 | +func TestParser_Authenticate(t *testing.T) { |
| 18 | + testData := []*Authenticate{ |
| 19 | + { UserID: "[email protected]", Password: "pass"}, |
| 20 | + { UserID: "[email protected]", Password: ""}, |
| 21 | + {UserID: "", Password: "pass"}, |
| 22 | + {UserID: "", Password: ""}, |
| 23 | + } |
| 24 | + |
| 25 | + for i, data := range testData { |
| 26 | + var continued bool |
| 27 | + |
| 28 | + tag := fmt.Sprintf("A%04d", i) |
| 29 | + authString := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("\x00%s\x00%s", data.UserID, data.Password))) |
| 30 | + input := toIMAPLine(tag+` AUTHENTICATE PLAIN`, authString) |
| 31 | + s := rfcparser.NewScanner(bytes.NewReader(input)) |
| 32 | + p := NewParserWithLiteralContinuationCb(s, continuationChecker(&continued)) |
| 33 | + cmd, err := p.Parse() |
| 34 | + message := fmt.Sprintf(" test failed for input %#v", data) |
| 35 | + |
| 36 | + require.NoError(t, err, "error"+message) |
| 37 | + require.True(t, continued, "continuation"+message) |
| 38 | + require.Equal(t, data, cmd.Payload, "payload"+message) |
| 39 | + require.Equal(t, "authenticate", p.LastParsedCommand(), "command"+message) |
| 40 | + require.Equal(t, tag, p.LastParsedTag(), "tag"+message) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestParser_AuthenticationWithIdentity(t *testing.T) { |
| 45 | + var continued bool |
| 46 | + |
| 47 | + authString := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("identity\x00user\x00pass"))) |
| 48 | + s := rfcparser.NewScanner(bytes.NewReader(toIMAPLine(`A0001 authenticate plain`, authString))) |
| 49 | + p := NewParserWithLiteralContinuationCb(s, continuationChecker(&continued)) |
| 50 | + cmd, err := p.Parse() |
| 51 | + |
| 52 | + require.NoError(t, err, "error test failed") |
| 53 | + require.True(t, continued, "continuation test failed") |
| 54 | + require.Equal(t, &Authenticate{UserID: "user", Password: "pass"}, cmd.Payload, "payload test failed") |
| 55 | + require.Equal(t, "authenticate", p.LastParsedCommand(), "command test failed") |
| 56 | + require.Equal(t, "A0001", p.LastParsedTag(), "tag test failed") |
| 57 | +} |
| 58 | + |
| 59 | +func TestParser_AuthenticateFailures(t *testing.T) { |
| 60 | + testData := []struct { |
| 61 | + input []string |
| 62 | + expectedMessage string |
| 63 | + continuationExpected bool |
| 64 | + description string |
| 65 | + }{ |
| 66 | + { |
| 67 | + input: []string{`A003 AUTHENTICATE PLAIN`, `*`}, |
| 68 | + expectedMessage: messageClientAbortedAuthentication, |
| 69 | + continuationExpected: true, |
| 70 | + description: "AUTHENTICATE abortion should return an error", |
| 71 | + }, |
| 72 | + { |
| 73 | + input: []string{`A003 AUTHENTICATE NONE`, `*`}, |
| 74 | + expectedMessage: messageUnsupportedAuthenticationMechanism, |
| 75 | + continuationExpected: false, |
| 76 | + description: "AUTHENTICATE with unknown mechanism should fail", |
| 77 | + }, |
| 78 | + { |
| 79 | + input: []string{`A003 AUTHENTICATE PLAIN GARBAGE`, `*`}, |
| 80 | + expectedMessage: "expected CR", |
| 81 | + continuationExpected: false, |
| 82 | + description: "AUTHENTICATE with garbage before CRLF should fail", |
| 83 | + }, |
| 84 | + { |
| 85 | + input: []string{`A003 AUTHENTICATE PLAIN `, `*`}, |
| 86 | + expectedMessage: "expected CR", |
| 87 | + continuationExpected: false, |
| 88 | + description: "AUTHENTICATE with extra space before CRLF should fail", |
| 89 | + }, |
| 90 | + { |
| 91 | + input: []string{`A003 AUTHENTICATE PLAIN`, `* `}, |
| 92 | + expectedMessage: messageInvalidBase64Content, |
| 93 | + continuationExpected: true, |
| 94 | + description: "AUTHENTICATE with extra space after the abort `*` should fail", |
| 95 | + }, |
| 96 | + { |
| 97 | + input: []string{`A003 AUTHENTICATE PLAIN`, `* `}, |
| 98 | + expectedMessage: messageInvalidBase64Content, |
| 99 | + continuationExpected: true, |
| 100 | + description: "AUTHENTICATE with extra space after the abort `*` should fail", |
| 101 | + }, |
| 102 | + { |
| 103 | + input: []string{`A003 AUTHENTICATE PLAIN`, `not-base64`}, |
| 104 | + expectedMessage: messageInvalidBase64Content, |
| 105 | + continuationExpected: true, |
| 106 | + description: "AUTHENTICATE with invalid base 64 message after continuation should fail", |
| 107 | + }, |
| 108 | + { |
| 109 | + input: []string{`A003 AUTHENTICATE PLAIN`, base64.StdEncoding.EncodeToString([]byte("username+password"))}, |
| 110 | + expectedMessage: messageInvalidAuthenticationData, |
| 111 | + continuationExpected: true, |
| 112 | + description: "AUTHENTICATE with invalid decoded base64 content should fail", |
| 113 | + }, |
| 114 | + { |
| 115 | + input: []string{`A003 AUTHENTICATE PLAIN`, base64.StdEncoding.EncodeToString([]byte("\x00username\x00password")) + " "}, |
| 116 | + expectedMessage: "expected CR", |
| 117 | + continuationExpected: true, |
| 118 | + description: "AUTHENTICATE with trailing spaces after a valid base64 message should fail", |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + for _, test := range testData { |
| 123 | + var continued bool |
| 124 | + |
| 125 | + s := rfcparser.NewScanner(bytes.NewReader(toIMAPLine(test.input...))) |
| 126 | + p := NewParserWithLiteralContinuationCb(s, continuationChecker(&continued)) |
| 127 | + _, err := p.Parse() |
| 128 | + failureDescription := fmt.Sprintf(" test failed for input %#v", test) |
| 129 | + |
| 130 | + var parserError *rfcparser.Error |
| 131 | + |
| 132 | + require.ErrorAs(t, err, &parserError, "error"+failureDescription) |
| 133 | + require.Equal(t, test.expectedMessage, parserError.Message, "error message"+failureDescription) |
| 134 | + require.Equal(t, test.continuationExpected, continued, "continuation"+failureDescription) |
| 135 | + } |
| 136 | +} |
0 commit comments