| 1 |
package irc_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"io/ioutil" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
yaml "gopkg.in/yaml.v2" |
| 11 |
|
| 12 |
"gopkg.in/irc.v4" |
| 13 |
) |
| 14 |
|
| 15 |
func BenchmarkParseMessage(b *testing.B) { |
| 16 |
for i := 0; i < b.N; i++ { |
| 17 |
irc.MustParseMessage("@tag1=something :nick!user@host PRIVMSG #channel :some message") |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
func TestParseMessage(t *testing.T) { |
| 22 |
t.Parallel() |
| 23 |
|
| 24 |
var messageTests = []struct { //nolint:gofumpt |
| 25 |
Input string |
| 26 |
Err error |
| 27 |
}{ |
| 28 |
{ |
| 29 |
Input: "", |
| 30 |
Err: irc.ErrZeroLengthMessage, |
| 31 |
}, |
| 32 |
{ |
| 33 |
Input: "@asdf", |
| 34 |
Err: irc.ErrMissingDataAfterTags, |
| 35 |
}, |
| 36 |
{ |
| 37 |
Input: ":asdf", |
| 38 |
Err: irc.ErrMissingDataAfterPrefix, |
| 39 |
}, |
| 40 |
{ |
| 41 |
Input: " :", |
| 42 |
Err: irc.ErrMissingCommand, |
| 43 |
}, |
| 44 |
{ |
| 45 |
Input: "PING :asdf", |
| 46 |
}, |
| 47 |
} |
| 48 |
|
| 49 |
for i, test := range messageTests { |
| 50 |
m, err := irc.ParseMessage(test.Input) |
| 51 |
assert.Equal(t, test.Err, err, "%d. Error didn't match expected", i) |
| 52 |
|
| 53 |
if test.Err != nil { |
| 54 |
assert.Nil(t, m, "%d. Didn't get nil message", i) |
| 55 |
} else { |
| 56 |
assert.NotNil(t, m, "%d. Got nil message", i) |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestMustParseMessage(t *testing.T) { |
| 62 |
t.Parallel() |
| 63 |
|
| 64 |
assert.Panics(t, func() { |
| 65 |
irc.MustParseMessage("") |
| 66 |
}, "Didn't get expected panic") |
| 67 |
|
| 68 |
assert.NotPanics(t, func() { |
| 69 |
irc.MustParseMessage("PING :asdf") |
| 70 |
}, "Got unexpected panic") |
| 71 |
} |
| 72 |
|
| 73 |
func TestMessageParam(t *testing.T) { |
| 74 |
t.Parallel() |
| 75 |
|
| 76 |
m := irc.MustParseMessage("PING :test") |
| 77 |
assert.Equal(t, m.Param(0), "test") |
| 78 |
assert.Equal(t, m.Param(-1), "") |
| 79 |
assert.Equal(t, m.Param(2), "") |
| 80 |
} |
| 81 |
|
| 82 |
func TestMessageTrailing(t *testing.T) { |
| 83 |
t.Parallel() |
| 84 |
|
| 85 |
m := irc.MustParseMessage("PING :helloworld") |
| 86 |
assert.Equal(t, "helloworld", m.Trailing()) |
| 87 |
|
| 88 |
m = irc.MustParseMessage("PING") |
| 89 |
assert.Equal(t, "", m.Trailing()) |
| 90 |
} |
| 91 |
|
| 92 |
func TestMessageCopy(t *testing.T) { |
| 93 |
t.Parallel() |
| 94 |
|
| 95 |
m := irc.MustParseMessage("@tag=val :user@host PING :helloworld") |
| 96 |
|
| 97 |
// Ensure copied messages are equal |
| 98 |
c := m.Copy() |
| 99 |
assert.EqualValues(t, m, c, "Copied values are not equal") |
| 100 |
|
| 101 |
// Ensure messages with modified tags don't match |
| 102 |
c = m.Copy() |
| 103 |
for k := range c.Tags { |
| 104 |
c.Tags[k] += "junk" |
| 105 |
} |
| 106 |
assert.False(t, assert.ObjectsAreEqualValues(m, c), "Copied with modified tags should not match") |
| 107 |
|
| 108 |
// Ensure messages with modified prefix don't match |
| 109 |
c = m.Copy() |
| 110 |
c.Prefix.Name += "junk" |
| 111 |
assert.False(t, assert.ObjectsAreEqualValues(m, c), "Copied with modified identity should not match") |
| 112 |
|
| 113 |
// Ensure messages with modified params don't match |
| 114 |
c = m.Copy() |
| 115 |
c.Params = append(c.Params, "junk") |
| 116 |
assert.False(t, assert.ObjectsAreEqualValues(m, c), "Copied with additional params should not match") |
| 117 |
|
| 118 |
// The message itself doesn't matter, we just need to make sure we |
| 119 |
// don't error if the user does something crazy and makes Params |
| 120 |
// nil. |
| 121 |
m = irc.MustParseMessage("PING :hello world") |
| 122 |
m.Prefix = nil |
| 123 |
c = m.Copy() |
| 124 |
assert.EqualValues(t, m, c, "nil prefix copy failed") |
| 125 |
|
| 126 |
// Ensure an empty Params is copied as nil |
| 127 |
m = irc.MustParseMessage("PING") |
| 128 |
m.Params = []string{} |
| 129 |
c = m.Copy() |
| 130 |
assert.Nil(t, c.Params, "Expected nil for empty params") |
| 131 |
} |
| 132 |
|
| 133 |
// Everything beyond here comes from the testcases repo |
| 134 |
|
| 135 |
type MsgSplitTests struct { |
| 136 |
Tests []struct { |
| 137 |
Desc string |
| 138 |
Input string |
| 139 |
Atoms struct { |
| 140 |
Source *string |
| 141 |
Verb string |
| 142 |
Params []string |
| 143 |
Tags map[string]interface{} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestMsgSplit(t *testing.T) { |
| 149 |
t.Parallel() |
| 150 |
|
| 151 |
data, err := ioutil.ReadFile("./_testcases/tests/msg-split.yaml") |
| 152 |
require.NoError(t, err) |
| 153 |
|
| 154 |
var splitTests MsgSplitTests |
| 155 |
err = yaml.Unmarshal(data, &splitTests) |
| 156 |
require.NoError(t, err) |
| 157 |
|
| 158 |
for _, test := range splitTests.Tests { |
| 159 |
msg, err := irc.ParseMessage(test.Input) |
| 160 |
assert.NoError(t, err, "%s: Failed to parse: %s (%s)", test.Desc, test.Input, err) |
| 161 |
|
| 162 |
assert.Equal(t, |
| 163 |
strings.ToUpper(test.Atoms.Verb), msg.Command, |
| 164 |
"%s: Wrong command for input: %s", test.Desc, test.Input, |
| 165 |
) |
| 166 |
assert.Equal(t, |
| 167 |
test.Atoms.Params, msg.Params, |
| 168 |
"%s: Wrong params for input: %s", test.Desc, test.Input, |
| 169 |
) |
| 170 |
|
| 171 |
if test.Atoms.Source != nil { |
| 172 |
assert.Equal(t, *test.Atoms.Source, msg.Prefix.String()) |
| 173 |
} |
| 174 |
|
| 175 |
assert.Equal(t, |
| 176 |
len(test.Atoms.Tags), len(msg.Tags), |
| 177 |
"%s: Wrong number of tags", |
| 178 |
test.Desc, |
| 179 |
) |
| 180 |
|
| 181 |
for k, v := range test.Atoms.Tags { |
| 182 |
tag, ok := msg.Tags[k] |
| 183 |
assert.True(t, ok, "Missing tag") |
| 184 |
if v == nil { |
| 185 |
assert.EqualValues(t, "", tag, "%s: Tag %q differs: %s != \"\"", test.Desc, k, tag) |
| 186 |
} else { |
| 187 |
assert.EqualValues(t, v, tag, "%s: Tag %q differs: %s != %s", test.Desc, k, v, tag) |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
type MsgJoinTests struct { |
| 194 |
Tests []struct { |
| 195 |
Desc string |
| 196 |
Atoms struct { |
| 197 |
Source string |
| 198 |
Verb string |
| 199 |
Params []string |
| 200 |
Tags map[string]interface{} |
| 201 |
} |
| 202 |
Matches []string |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func TestMsgJoin(t *testing.T) { |
| 207 |
var ok bool |
| 208 |
|
| 209 |
t.Parallel() |
| 210 |
|
| 211 |
data, err := ioutil.ReadFile("./_testcases/tests/msg-join.yaml") |
| 212 |
require.NoError(t, err) |
| 213 |
|
| 214 |
var splitTests MsgJoinTests |
| 215 |
err = yaml.Unmarshal(data, &splitTests) |
| 216 |
require.NoError(t, err) |
| 217 |
|
| 218 |
for _, test := range splitTests.Tests { |
| 219 |
msg := &irc.Message{ |
| 220 |
Prefix: irc.ParsePrefix(test.Atoms.Source), |
| 221 |
Command: test.Atoms.Verb, |
| 222 |
Params: test.Atoms.Params, |
| 223 |
Tags: make(map[string]string), |
| 224 |
} |
| 225 |
|
| 226 |
for k, v := range test.Atoms.Tags { |
| 227 |
if v == nil { |
| 228 |
msg.Tags[k] = "" |
| 229 |
} else { |
| 230 |
msg.Tags[k], ok = v.(string) |
| 231 |
assert.True(t, ok) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
assert.Contains(t, test.Matches, msg.String()) |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
type UserhostSplitTests struct { |
| 240 |
Tests []struct { |
| 241 |
Desc string |
| 242 |
Source string |
| 243 |
Atoms struct { |
| 244 |
Nick string |
| 245 |
User string |
| 246 |
Host string |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func TestUserhostSplit(t *testing.T) { |
| 252 |
t.Parallel() |
| 253 |
|
| 254 |
data, err := ioutil.ReadFile("./_testcases/tests/userhost-split.yaml") |
| 255 |
require.NoError(t, err) |
| 256 |
|
| 257 |
var userhostTests UserhostSplitTests |
| 258 |
err = yaml.Unmarshal(data, &userhostTests) |
| 259 |
require.NoError(t, err) |
| 260 |
|
| 261 |
for _, test := range userhostTests.Tests { |
| 262 |
prefix := irc.ParsePrefix(test.Source) |
| 263 |
|
| 264 |
assert.Equal(t, |
| 265 |
test.Atoms.Nick, prefix.Name, |
| 266 |
"%s: Name did not match for input: %q", test.Desc, test.Source, |
| 267 |
) |
| 268 |
assert.Equal(t, |
| 269 |
test.Atoms.User, prefix.User, |
| 270 |
"%s: User did not match for input: %q", test.Desc, test.Source, |
| 271 |
) |
| 272 |
assert.Equal(t, |
| 273 |
test.Atoms.Host, prefix.Host, |
| 274 |
"%s: Host did not match for input: %q", test.Desc, test.Source, |
| 275 |
) |
| 276 |
} |
| 277 |
} |