| 1 |
package rfc5424 |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/jeromer/syslogparser" |
| 10 |
"github.com/jeromer/syslogparser/parsercommon" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
func TestParser(t *testing.T) { |
| 15 |
tmpTZ, err := time.Parse("-07:00", "-07:00") |
| 16 |
require.Nil(t, err) |
| 17 |
|
| 18 |
testCases := []struct { |
| 19 |
description string |
| 20 |
input string |
| 21 |
expectedParts syslogparser.LogParts |
| 22 |
}{ |
| 23 |
{ |
| 24 |
description: "no STRUCTURED-DATA 1/2", |
| 25 |
input: "<34>1 2003-10-11T22:14:15.003Z mymachine.example.com su - ID47 - 'su root' failed for lonvick on /dev/pts/8", |
| 26 |
expectedParts: syslogparser.LogParts{ |
| 27 |
"priority": 34, |
| 28 |
"facility": 4, |
| 29 |
"severity": 2, |
| 30 |
"version": 1, |
| 31 |
"timestamp": time.Date( |
| 32 |
2003, time.October, 11, |
| 33 |
22, 14, 15, 3*10e5, |
| 34 |
time.UTC, |
| 35 |
), |
| 36 |
"hostname": "mymachine.example.com", |
| 37 |
"app_name": "su", |
| 38 |
"proc_id": "-", |
| 39 |
"msg_id": "ID47", |
| 40 |
"structured_data": "-", |
| 41 |
"message": "'su root' failed for lonvick on /dev/pts/8", |
| 42 |
}, |
| 43 |
}, |
| 44 |
{ |
| 45 |
description: "no STRUCTURED_DATA 2/2", |
| 46 |
input: "<165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1 myproc 8710 - - %% It's time to make the do-nuts.", |
| 47 |
expectedParts: syslogparser.LogParts{ |
| 48 |
"priority": 165, |
| 49 |
"facility": 20, |
| 50 |
"severity": 5, |
| 51 |
"version": 1, |
| 52 |
"timestamp": time.Date( |
| 53 |
2003, time.August, 24, |
| 54 |
5, 14, 15, 3*10e2, |
| 55 |
tmpTZ.Location(), |
| 56 |
), |
| 57 |
"hostname": "192.0.2.1", |
| 58 |
"app_name": "myproc", |
| 59 |
"proc_id": "8710", |
| 60 |
"msg_id": "-", |
| 61 |
"structured_data": "-", |
| 62 |
"message": "%% It's time to make the do-nuts.", |
| 63 |
}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
description: "with STRUCTURED_DATA", |
| 67 |
input: `<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"] An application event log entry...`, |
| 68 |
expectedParts: syslogparser.LogParts{ |
| 69 |
"priority": 165, |
| 70 |
"facility": 20, |
| 71 |
"severity": 5, |
| 72 |
"version": 1, |
| 73 |
"timestamp": time.Date( |
| 74 |
2003, time.October, 11, |
| 75 |
22, 14, 15, 3*10e5, |
| 76 |
time.UTC, |
| 77 |
), |
| 78 |
"hostname": "mymachine.example.com", |
| 79 |
"app_name": "evntslog", |
| 80 |
"proc_id": "-", |
| 81 |
"msg_id": "ID47", |
| 82 |
"structured_data": `[exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"]`, |
| 83 |
"message": "An application event log entry...", |
| 84 |
}, |
| 85 |
}, |
| 86 |
{ |
| 87 |
description: "STRUCTURED_DATA only", |
| 88 |
input: `<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource= "Application" eventID="1011"][examplePriority@32473 class="high"]`, |
| 89 |
expectedParts: syslogparser.LogParts{ |
| 90 |
"priority": 165, |
| 91 |
"facility": 20, |
| 92 |
"severity": 5, |
| 93 |
"version": 1, |
| 94 |
"timestamp": time.Date( |
| 95 |
2003, time.October, 11, |
| 96 |
22, 14, 15, 3*10e5, |
| 97 |
time.UTC, |
| 98 |
), |
| 99 |
"hostname": "mymachine.example.com", |
| 100 |
"app_name": "evntslog", |
| 101 |
"proc_id": "-", |
| 102 |
"msg_id": "ID47", |
| 103 |
"structured_data": `[exampleSDID@32473 iut="3" eventSource= "Application" eventID="1011"][examplePriority@32473 class="high"]`, |
| 104 |
"message": "", |
| 105 |
}, |
| 106 |
}, |
| 107 |
} |
| 108 |
|
| 109 |
for _, tc := range testCases { |
| 110 |
buff := []byte(tc.input) |
| 111 |
|
| 112 |
p := NewParser(buff) |
| 113 |
require.Equal( |
| 114 |
t, |
| 115 |
&Parser{ |
| 116 |
buff: buff, |
| 117 |
cursor: 0, |
| 118 |
l: len(tc.input), |
| 119 |
}, |
| 120 |
p, |
| 121 |
tc.description, |
| 122 |
) |
| 123 |
|
| 124 |
err := p.Parse() |
| 125 |
require.Nil(t, err) |
| 126 |
|
| 127 |
obtained := p.Dump() |
| 128 |
for k, v := range obtained { |
| 129 |
require.Equal( |
| 130 |
t, tc.expectedParts[k], v, tc.description, |
| 131 |
) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func TestParseWithHostname(t *testing.T) { |
| 137 |
buff := []byte( |
| 138 |
"<34>1 2003-10-11T22:14:15.003Z su - ID47 - 'su root' failed for lonvick on /dev/pts/8", |
| 139 |
) |
| 140 |
|
| 141 |
p := NewParser(buff) |
| 142 |
p.WithHostname("mymachine.example.com") |
| 143 |
|
| 144 |
require.Equal( |
| 145 |
t, |
| 146 |
&Parser{ |
| 147 |
buff: buff, |
| 148 |
cursor: 0, |
| 149 |
l: len(buff), |
| 150 |
tmpHostname: "mymachine.example.com", |
| 151 |
}, |
| 152 |
p, |
| 153 |
) |
| 154 |
|
| 155 |
err := p.Parse() |
| 156 |
require.Nil(t, err) |
| 157 |
|
| 158 |
require.Equal( |
| 159 |
t, syslogparser.LogParts{ |
| 160 |
"priority": 34, |
| 161 |
"facility": 4, |
| 162 |
"severity": 2, |
| 163 |
"version": 1, |
| 164 |
"timestamp": time.Date( |
| 165 |
2003, time.October, 11, |
| 166 |
22, 14, 15, 3*10e5, |
| 167 |
time.UTC, |
| 168 |
), |
| 169 |
"hostname": "mymachine.example.com", |
| 170 |
"app_name": "su", |
| 171 |
"proc_id": "-", |
| 172 |
"msg_id": "ID47", |
| 173 |
"structured_data": "-", |
| 174 |
"message": "'su root' failed for lonvick on /dev/pts/8", |
| 175 |
}, p.Dump(), |
| 176 |
) |
| 177 |
} |
| 178 |
|
| 179 |
func TestParseWithPriority(t *testing.T) { |
| 180 |
buff := []byte( |
| 181 |
"1 2003-10-11T22:14:15.003Z mymachine.example.com su - ID47 - 'su root' failed for lonvick on /dev/pts/8", |
| 182 |
) |
| 183 |
|
| 184 |
pri := parsercommon.NewPriority(34) |
| 185 |
|
| 186 |
p := NewParser(buff) |
| 187 |
p.WithPriority(pri) |
| 188 |
|
| 189 |
require.Equal( |
| 190 |
t, |
| 191 |
&Parser{ |
| 192 |
buff: buff, |
| 193 |
cursor: 0, |
| 194 |
l: len(buff), |
| 195 |
tmpPriority: pri, |
| 196 |
}, |
| 197 |
p, |
| 198 |
) |
| 199 |
|
| 200 |
err := p.Parse() |
| 201 |
require.Nil(t, err) |
| 202 |
|
| 203 |
require.Equal( |
| 204 |
t, syslogparser.LogParts{ |
| 205 |
"priority": 34, |
| 206 |
"facility": 4, |
| 207 |
"severity": 2, |
| 208 |
"version": 1, |
| 209 |
"timestamp": time.Date( |
| 210 |
2003, time.October, 11, |
| 211 |
22, 14, 15, 3*10e5, |
| 212 |
time.UTC, |
| 213 |
), |
| 214 |
"hostname": "mymachine.example.com", |
| 215 |
"app_name": "su", |
| 216 |
"proc_id": "-", |
| 217 |
"msg_id": "ID47", |
| 218 |
"structured_data": "-", |
| 219 |
"message": "'su root' failed for lonvick on /dev/pts/8", |
| 220 |
}, p.Dump(), |
| 221 |
) |
| 222 |
} |
| 223 |
|
| 224 |
func TestParseWithPriorityAndHostname(t *testing.T) { |
| 225 |
buff := []byte( |
| 226 |
"1 2003-10-11T22:14:15.003Z su - ID47 - 'su root' failed for lonvick on /dev/pts/8", |
| 227 |
) |
| 228 |
|
| 229 |
pri := parsercommon.NewPriority(34) |
| 230 |
|
| 231 |
p := NewParser(buff) |
| 232 |
p.WithPriority(pri) |
| 233 |
p.WithHostname("mymachine.example.com") |
| 234 |
|
| 235 |
require.Equal( |
| 236 |
t, |
| 237 |
&Parser{ |
| 238 |
buff: buff, |
| 239 |
cursor: 0, |
| 240 |
l: len(buff), |
| 241 |
tmpHostname: "mymachine.example.com", |
| 242 |
tmpPriority: pri, |
| 243 |
}, |
| 244 |
p, |
| 245 |
) |
| 246 |
|
| 247 |
err := p.Parse() |
| 248 |
require.Nil(t, err) |
| 249 |
|
| 250 |
require.Equal( |
| 251 |
t, syslogparser.LogParts{ |
| 252 |
"priority": 34, |
| 253 |
"facility": 4, |
| 254 |
"severity": 2, |
| 255 |
"version": 1, |
| 256 |
"timestamp": time.Date( |
| 257 |
2003, time.October, 11, |
| 258 |
22, 14, 15, 3*10e5, |
| 259 |
time.UTC, |
| 260 |
), |
| 261 |
"hostname": "mymachine.example.com", |
| 262 |
"app_name": "su", |
| 263 |
"proc_id": "-", |
| 264 |
"msg_id": "ID47", |
| 265 |
"structured_data": "-", |
| 266 |
"message": "'su root' failed for lonvick on /dev/pts/8", |
| 267 |
}, p.Dump(), |
| 268 |
) |
| 269 |
} |
| 270 |
|
| 271 |
func TestParseHeader(t *testing.T) { |
| 272 |
ts := time.Date(2003, time.October, 11, 22, 14, 15, 3*10e5, time.UTC) |
| 273 |
tsString := "2003-10-11T22:14:15.003Z" |
| 274 |
hostname := "mymachine.example.com" |
| 275 |
appName := "su" |
| 276 |
procId := "123" |
| 277 |
msgId := "ID47" |
| 278 |
nilValue := string(NILVALUE) |
| 279 |
headerFmt := "<165>1 %s %s %s %s %s " |
| 280 |
pri := &parsercommon.Priority{ |
| 281 |
P: 165, |
| 282 |
F: parsercommon.Facility{Value: 20}, |
| 283 |
S: parsercommon.Severity{Value: 5}, |
| 284 |
} |
| 285 |
testCases := []struct { |
| 286 |
description string |
| 287 |
input string |
| 288 |
expectedHdr *header |
| 289 |
}{ |
| 290 |
{ |
| 291 |
description: "HEADER complete", |
| 292 |
input: fmt.Sprintf(headerFmt, tsString, hostname, appName, procId, msgId), |
| 293 |
expectedHdr: &header{ |
| 294 |
priority: pri, |
| 295 |
version: 1, |
| 296 |
timestamp: ts, |
| 297 |
hostname: hostname, |
| 298 |
appName: appName, |
| 299 |
procId: procId, |
| 300 |
msgId: msgId, |
| 301 |
}, |
| 302 |
}, |
| 303 |
{ |
| 304 |
description: "TIMESTAMP as NILVALUE", |
| 305 |
input: fmt.Sprintf(headerFmt, nilValue, hostname, appName, procId, msgId), |
| 306 |
expectedHdr: &header{ |
| 307 |
priority: pri, |
| 308 |
version: 1, |
| 309 |
timestamp: time.Time{}, |
| 310 |
hostname: hostname, |
| 311 |
appName: appName, |
| 312 |
procId: procId, |
| 313 |
msgId: msgId, |
| 314 |
}, |
| 315 |
}, |
| 316 |
{ |
| 317 |
description: "HOSTNAME as NILVALUE", |
| 318 |
input: fmt.Sprintf(headerFmt, tsString, nilValue, appName, procId, msgId), |
| 319 |
expectedHdr: &header{ |
| 320 |
priority: pri, |
| 321 |
version: 1, |
| 322 |
timestamp: ts, |
| 323 |
hostname: nilValue, |
| 324 |
appName: appName, |
| 325 |
procId: procId, |
| 326 |
msgId: msgId, |
| 327 |
}, |
| 328 |
}, |
| 329 |
{ |
| 330 |
description: "APP-NAME as NILVALUE", |
| 331 |
input: fmt.Sprintf(headerFmt, tsString, hostname, nilValue, procId, msgId), |
| 332 |
expectedHdr: &header{ |
| 333 |
priority: pri, |
| 334 |
version: 1, |
| 335 |
timestamp: ts, |
| 336 |
hostname: hostname, |
| 337 |
appName: nilValue, |
| 338 |
procId: procId, |
| 339 |
msgId: msgId, |
| 340 |
}, |
| 341 |
}, |
| 342 |
{ |
| 343 |
description: "PROCID as NILVALUE", |
| 344 |
input: fmt.Sprintf(headerFmt, tsString, hostname, appName, nilValue, msgId), |
| 345 |
expectedHdr: &header{ |
| 346 |
priority: pri, |
| 347 |
version: 1, |
| 348 |
timestamp: ts, |
| 349 |
hostname: hostname, |
| 350 |
appName: appName, |
| 351 |
procId: nilValue, |
| 352 |
msgId: msgId, |
| 353 |
}, |
| 354 |
}, |
| 355 |
{ |
| 356 |
description: "MSGID as NILVALUE", |
| 357 |
input: fmt.Sprintf(headerFmt, tsString, hostname, appName, procId, nilValue), |
| 358 |
expectedHdr: &header{ |
| 359 |
priority: pri, |
| 360 |
version: 1, |
| 361 |
timestamp: ts, |
| 362 |
hostname: hostname, |
| 363 |
appName: appName, |
| 364 |
procId: procId, |
| 365 |
msgId: nilValue, |
| 366 |
}, |
| 367 |
}, |
| 368 |
} |
| 369 |
|
| 370 |
for _, tc := range testCases { |
| 371 |
p := NewParser([]byte(tc.input)) |
| 372 |
obtained, err := p.parseHeader() |
| 373 |
|
| 374 |
require.Nil( |
| 375 |
t, err, tc.description, |
| 376 |
) |
| 377 |
|
| 378 |
require.Equal( |
| 379 |
t, tc.expectedHdr, obtained, tc.description, |
| 380 |
) |
| 381 |
|
| 382 |
require.Equal( |
| 383 |
t, len(tc.input), p.cursor, tc.description, |
| 384 |
) |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
func TestParseTimestamp(t *testing.T) { |
| 389 |
tz := "-04:00" |
| 390 |
tmpTZ, err := time.Parse("-07:00", tz) |
| 391 |
require.Nil(t, err) |
| 392 |
require.NotNil(t, tmpTZ) |
| 393 |
|
| 394 |
dt1 := time.Date( |
| 395 |
1985, time.April, 12, |
| 396 |
23, 20, 50, 52*10e6, |
| 397 |
time.UTC, |
| 398 |
) |
| 399 |
|
| 400 |
dt2 := time.Date( |
| 401 |
1985, time.April, 12, |
| 402 |
19, 20, 50, 52*10e6, |
| 403 |
tmpTZ.Location(), |
| 404 |
) |
| 405 |
|
| 406 |
dt3 := time.Date( |
| 407 |
2003, time.October, 11, |
| 408 |
22, 14, 15, 3*10e5, |
| 409 |
time.UTC, |
| 410 |
) |
| 411 |
|
| 412 |
dt4 := time.Date( |
| 413 |
2003, time.August, 24, |
| 414 |
5, 14, 15, 3*10e2, |
| 415 |
tmpTZ.Location(), |
| 416 |
) |
| 417 |
testCases := []struct { |
| 418 |
description string |
| 419 |
input string |
| 420 |
expectedTS *time.Time |
| 421 |
expectedCursorPos int |
| 422 |
expectedErr error |
| 423 |
}{ |
| 424 |
{ |
| 425 |
description: "UTC timestamp", |
| 426 |
input: "1985-04-12T23:20:50.52Z", |
| 427 |
expectedTS: &dt1, |
| 428 |
expectedCursorPos: 23, |
| 429 |
expectedErr: nil, |
| 430 |
}, |
| 431 |
{ |
| 432 |
description: "numeric timezone", |
| 433 |
input: "1985-04-12T19:20:50.52" + tz, |
| 434 |
expectedTS: &dt2, |
| 435 |
expectedCursorPos: 28, |
| 436 |
expectedErr: nil, |
| 437 |
}, |
| 438 |
{ |
| 439 |
description: "timestamp with ms", |
| 440 |
input: "2003-10-11T22:14:15.003Z", |
| 441 |
expectedTS: &dt3, |
| 442 |
expectedCursorPos: 24, |
| 443 |
expectedErr: nil, |
| 444 |
}, |
| 445 |
{ |
| 446 |
description: "timestamp with us", |
| 447 |
input: "2003-08-24T05:14:15.000003" + tz, |
| 448 |
expectedTS: &dt4, |
| 449 |
expectedCursorPos: 32, |
| 450 |
expectedErr: nil, |
| 451 |
}, |
| 452 |
{ |
| 453 |
description: "timestamp with ns", |
| 454 |
input: "2003-08-24T05:14:15.000000003-07:00", |
| 455 |
expectedCursorPos: 26, |
| 456 |
expectedTS: nil, |
| 457 |
expectedErr: parsercommon.ErrTimestampUnknownFormat, |
| 458 |
}, |
| 459 |
{ |
| 460 |
description: "nil timestamp", |
| 461 |
input: "-", |
| 462 |
expectedCursorPos: 1, |
| 463 |
expectedTS: nil, |
| 464 |
expectedErr: nil, |
| 465 |
}, |
| 466 |
} |
| 467 |
|
| 468 |
for _, tc := range testCases { |
| 469 |
p := NewParser([]byte(tc.input)) |
| 470 |
obtained, err := p.parseTimestamp() |
| 471 |
|
| 472 |
require.Equal( |
| 473 |
t, tc.expectedErr, err, tc.description, |
| 474 |
) |
| 475 |
|
| 476 |
require.Equal( |
| 477 |
t, |
| 478 |
tc.expectedCursorPos, |
| 479 |
p.cursor, |
| 480 |
tc.description, |
| 481 |
) |
| 482 |
|
| 483 |
if tc.expectedErr != nil { |
| 484 |
require.Nil( |
| 485 |
t, obtained, tc.description, |
| 486 |
) |
| 487 |
|
| 488 |
continue |
| 489 |
} |
| 490 |
|
| 491 |
if tc.description == "nil timestamp" { |
| 492 |
continue |
| 493 |
} |
| 494 |
|
| 495 |
tfmt := time.RFC3339Nano |
| 496 |
require.Equal( |
| 497 |
t, |
| 498 |
tc.expectedTS.Format(tfmt), |
| 499 |
obtained.Format(tfmt), |
| 500 |
tc.description, |
| 501 |
) |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
func TestParseYear(t *testing.T) { |
| 506 |
testCases := []struct { |
| 507 |
description string |
| 508 |
input string |
| 509 |
expectedYear int |
| 510 |
expectedCursorPos int |
| 511 |
expectedErr error |
| 512 |
}{ |
| 513 |
{ |
| 514 |
description: "invalid year", |
| 515 |
input: "1a2b", |
| 516 |
expectedYear: 0, |
| 517 |
expectedCursorPos: 4, |
| 518 |
expectedErr: ErrYearInvalid, |
| 519 |
}, |
| 520 |
{ |
| 521 |
description: "year too short", |
| 522 |
input: "123", |
| 523 |
expectedYear: 0, |
| 524 |
expectedCursorPos: 0, |
| 525 |
expectedErr: parsercommon.ErrEOL, |
| 526 |
}, |
| 527 |
{ |
| 528 |
description: "valid", |
| 529 |
input: "2013", |
| 530 |
expectedYear: 2013, |
| 531 |
expectedCursorPos: 4, |
| 532 |
expectedErr: nil, |
| 533 |
}, |
| 534 |
} |
| 535 |
|
| 536 |
for _, tc := range testCases { |
| 537 |
cursor := 0 |
| 538 |
obtained, err := parseYear( |
| 539 |
[]byte(tc.input), |
| 540 |
&cursor, |
| 541 |
len(tc.input), |
| 542 |
) |
| 543 |
|
| 544 |
require.Equal( |
| 545 |
t, tc.expectedYear, obtained, tc.description, |
| 546 |
) |
| 547 |
|
| 548 |
require.Equal( |
| 549 |
t, tc.expectedErr, err, tc.description, |
| 550 |
) |
| 551 |
|
| 552 |
require.Equal( |
| 553 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 554 |
) |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
func TestParseMonth(t *testing.T) { |
| 559 |
testCases := []struct { |
| 560 |
description string |
| 561 |
input string |
| 562 |
expectedMonth int |
| 563 |
expectedCursorPos int |
| 564 |
expectedErr error |
| 565 |
}{ |
| 566 |
{ |
| 567 |
description: "invalid string", |
| 568 |
input: "ab", |
| 569 |
expectedMonth: 0, |
| 570 |
expectedCursorPos: 2, |
| 571 |
expectedErr: ErrMonthInvalid, |
| 572 |
}, |
| 573 |
{ |
| 574 |
description: "invalid range 1/2", |
| 575 |
input: "00", |
| 576 |
expectedMonth: 0, |
| 577 |
expectedCursorPos: 2, |
| 578 |
expectedErr: ErrMonthInvalid, |
| 579 |
}, |
| 580 |
{ |
| 581 |
description: "invalid range 2/2", |
| 582 |
input: "13", |
| 583 |
expectedMonth: 0, |
| 584 |
expectedCursorPos: 2, |
| 585 |
expectedErr: ErrMonthInvalid, |
| 586 |
}, |
| 587 |
{ |
| 588 |
description: "too short", |
| 589 |
input: "1", |
| 590 |
expectedMonth: 0, |
| 591 |
expectedCursorPos: 0, |
| 592 |
expectedErr: parsercommon.ErrEOL, |
| 593 |
}, |
| 594 |
{ |
| 595 |
description: "valid", |
| 596 |
input: "02", |
| 597 |
expectedMonth: 2, |
| 598 |
expectedCursorPos: 2, |
| 599 |
expectedErr: nil, |
| 600 |
}, |
| 601 |
} |
| 602 |
|
| 603 |
for _, tc := range testCases { |
| 604 |
cursor := 0 |
| 605 |
obtained, err := parseMonth( |
| 606 |
[]byte(tc.input), |
| 607 |
&cursor, |
| 608 |
len(tc.input), |
| 609 |
) |
| 610 |
|
| 611 |
require.Equal( |
| 612 |
t, tc.expectedMonth, obtained, tc.description, |
| 613 |
) |
| 614 |
|
| 615 |
require.Equal( |
| 616 |
t, tc.expectedErr, err, tc.description, |
| 617 |
) |
| 618 |
|
| 619 |
require.Equal( |
| 620 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 621 |
) |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
func TestParseDay(t *testing.T) { |
| 626 |
testCases := []struct { |
| 627 |
description string |
| 628 |
input string |
| 629 |
expectedDay int |
| 630 |
expectedCursorPos int |
| 631 |
expectedErr error |
| 632 |
}{ |
| 633 |
{ |
| 634 |
description: "invalid string", |
| 635 |
input: "ab", |
| 636 |
expectedDay: 0, |
| 637 |
expectedCursorPos: 2, |
| 638 |
expectedErr: ErrDayInvalid, |
| 639 |
}, |
| 640 |
{ |
| 641 |
description: "too short", |
| 642 |
input: "1", |
| 643 |
expectedDay: 0, |
| 644 |
expectedCursorPos: 0, |
| 645 |
expectedErr: parsercommon.ErrEOL, |
| 646 |
}, |
| 647 |
{ |
| 648 |
description: "invalid range 1/2", |
| 649 |
input: "00", |
| 650 |
expectedDay: 0, |
| 651 |
expectedCursorPos: 2, |
| 652 |
expectedErr: ErrDayInvalid, |
| 653 |
}, |
| 654 |
{ |
| 655 |
description: "invalid range 2/2", |
| 656 |
input: "32", |
| 657 |
expectedDay: 0, |
| 658 |
expectedCursorPos: 2, |
| 659 |
expectedErr: ErrDayInvalid, |
| 660 |
}, |
| 661 |
{ |
| 662 |
description: "valid", |
| 663 |
input: "02", |
| 664 |
expectedDay: 2, |
| 665 |
expectedCursorPos: 2, |
| 666 |
expectedErr: nil, |
| 667 |
}, |
| 668 |
} |
| 669 |
|
| 670 |
for _, tc := range testCases { |
| 671 |
cursor := 0 |
| 672 |
obtained, err := parseDay( |
| 673 |
[]byte(tc.input), |
| 674 |
&cursor, |
| 675 |
len(tc.input), |
| 676 |
) |
| 677 |
|
| 678 |
require.Equal( |
| 679 |
t, tc.expectedDay, obtained, tc.description, |
| 680 |
) |
| 681 |
|
| 682 |
require.Equal( |
| 683 |
t, tc.expectedErr, err, tc.description, |
| 684 |
) |
| 685 |
|
| 686 |
require.Equal( |
| 687 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 688 |
) |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
func TestParseFullDate(t *testing.T) { |
| 693 |
testCases := []struct { |
| 694 |
description string |
| 695 |
input string |
| 696 |
expectedDate fullDate |
| 697 |
expectedCursorPos int |
| 698 |
expectedErr error |
| 699 |
}{ |
| 700 |
{ |
| 701 |
description: "invalid separator 1/2", |
| 702 |
input: "2013+10-28", |
| 703 |
expectedDate: fullDate{}, |
| 704 |
expectedCursorPos: 4, |
| 705 |
expectedErr: parsercommon.ErrTimestampUnknownFormat, |
| 706 |
}, |
| 707 |
{ |
| 708 |
description: "invalid separator 2/2", |
| 709 |
input: "2013-10+28", |
| 710 |
expectedDate: fullDate{}, |
| 711 |
expectedCursorPos: 7, |
| 712 |
expectedErr: parsercommon.ErrTimestampUnknownFormat, |
| 713 |
}, |
| 714 |
{ |
| 715 |
description: "valid", |
| 716 |
input: "2013-10-28", |
| 717 |
expectedDate: fullDate{2013, 10, 28}, |
| 718 |
expectedCursorPos: 10, |
| 719 |
expectedErr: nil, |
| 720 |
}, |
| 721 |
} |
| 722 |
|
| 723 |
for _, tc := range testCases { |
| 724 |
cursor := 0 |
| 725 |
obtained, err := parseFullDate( |
| 726 |
[]byte(tc.input), |
| 727 |
&cursor, |
| 728 |
len(tc.input), |
| 729 |
) |
| 730 |
|
| 731 |
require.Equal( |
| 732 |
t, tc.expectedErr, err, tc.description, |
| 733 |
) |
| 734 |
|
| 735 |
require.Equal( |
| 736 |
t, tc.expectedDate, obtained, tc.description, |
| 737 |
) |
| 738 |
|
| 739 |
require.Equal( |
| 740 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 741 |
) |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
func TestParseHour(t *testing.T) { |
| 746 |
testCases := []struct { |
| 747 |
description string |
| 748 |
input string |
| 749 |
expectedHour int |
| 750 |
expectedCursorPos int |
| 751 |
expectedErr error |
| 752 |
}{ |
| 753 |
{ |
| 754 |
description: "invalid", |
| 755 |
input: "azer", |
| 756 |
expectedHour: 0, |
| 757 |
expectedCursorPos: 2, |
| 758 |
expectedErr: ErrHourInvalid, |
| 759 |
}, |
| 760 |
{ |
| 761 |
description: "too short", |
| 762 |
input: "1", |
| 763 |
expectedHour: 0, |
| 764 |
expectedCursorPos: 0, |
| 765 |
expectedErr: parsercommon.ErrEOL, |
| 766 |
}, |
| 767 |
{ |
| 768 |
description: "invalid range 1/2", |
| 769 |
input: "-1", |
| 770 |
expectedHour: 0, |
| 771 |
expectedCursorPos: 2, |
| 772 |
expectedErr: ErrHourInvalid, |
| 773 |
}, |
| 774 |
{ |
| 775 |
description: "invalid range 2/2", |
| 776 |
input: "24", |
| 777 |
expectedHour: 0, |
| 778 |
expectedCursorPos: 2, |
| 779 |
expectedErr: ErrHourInvalid, |
| 780 |
}, |
| 781 |
{ |
| 782 |
description: "valid", |
| 783 |
input: "12", |
| 784 |
expectedHour: 12, |
| 785 |
expectedCursorPos: 2, |
| 786 |
expectedErr: nil, |
| 787 |
}, |
| 788 |
} |
| 789 |
|
| 790 |
for _, tc := range testCases { |
| 791 |
cursor := 0 |
| 792 |
obtained, err := parseHour( |
| 793 |
[]byte(tc.input), |
| 794 |
&cursor, |
| 795 |
len(tc.input), |
| 796 |
) |
| 797 |
|
| 798 |
require.Equal( |
| 799 |
t, tc.expectedHour, obtained, tc.description, |
| 800 |
) |
| 801 |
|
| 802 |
require.Equal( |
| 803 |
t, tc.expectedErr, err, tc.description, |
| 804 |
) |
| 805 |
|
| 806 |
require.Equal( |
| 807 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 808 |
) |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
func TestParseMinute(t *testing.T) { |
| 813 |
testCases := []struct { |
| 814 |
description string |
| 815 |
input string |
| 816 |
expectedMinute int |
| 817 |
expectedCursorPos int |
| 818 |
expectedErr error |
| 819 |
}{ |
| 820 |
{ |
| 821 |
description: "invalid", |
| 822 |
input: "azer", |
| 823 |
expectedMinute: 0, |
| 824 |
expectedCursorPos: 2, |
| 825 |
expectedErr: ErrMinuteInvalid, |
| 826 |
}, |
| 827 |
{ |
| 828 |
description: "too short", |
| 829 |
input: "1", |
| 830 |
expectedMinute: 0, |
| 831 |
expectedCursorPos: 0, |
| 832 |
expectedErr: parsercommon.ErrEOL, |
| 833 |
}, |
| 834 |
{ |
| 835 |
description: "invalid range 1/2", |
| 836 |
input: "-1", |
| 837 |
expectedMinute: 0, |
| 838 |
expectedCursorPos: 2, |
| 839 |
expectedErr: ErrMinuteInvalid, |
| 840 |
}, |
| 841 |
{ |
| 842 |
description: "invalid range 2/2", |
| 843 |
input: "60", |
| 844 |
expectedMinute: 0, |
| 845 |
expectedCursorPos: 2, |
| 846 |
expectedErr: ErrMinuteInvalid, |
| 847 |
}, |
| 848 |
{ |
| 849 |
description: "valid", |
| 850 |
input: "12", |
| 851 |
expectedMinute: 12, |
| 852 |
expectedCursorPos: 2, |
| 853 |
expectedErr: nil, |
| 854 |
}, |
| 855 |
} |
| 856 |
|
| 857 |
for _, tc := range testCases { |
| 858 |
cursor := 0 |
| 859 |
obtained, err := parseMinute( |
| 860 |
[]byte(tc.input), |
| 861 |
&cursor, |
| 862 |
len(tc.input), |
| 863 |
) |
| 864 |
|
| 865 |
require.Equal( |
| 866 |
t, tc.expectedMinute, obtained, tc.description, |
| 867 |
) |
| 868 |
|
| 869 |
require.Equal( |
| 870 |
t, tc.expectedErr, err, tc.description, |
| 871 |
) |
| 872 |
|
| 873 |
require.Equal( |
| 874 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 875 |
) |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
func TestParseSecond(t *testing.T) { |
| 880 |
testCases := []struct { |
| 881 |
description string |
| 882 |
input string |
| 883 |
expectedSecond int |
| 884 |
expectedCursorPos int |
| 885 |
expectedErr error |
| 886 |
}{ |
| 887 |
{ |
| 888 |
description: "invalid", |
| 889 |
input: "azer", |
| 890 |
expectedSecond: 0, |
| 891 |
expectedCursorPos: 2, |
| 892 |
expectedErr: ErrSecondInvalid, |
| 893 |
}, |
| 894 |
{ |
| 895 |
description: "too short", |
| 896 |
input: "1", |
| 897 |
expectedSecond: 0, |
| 898 |
expectedCursorPos: 0, |
| 899 |
expectedErr: parsercommon.ErrEOL, |
| 900 |
}, |
| 901 |
{ |
| 902 |
description: "invalid range 1/2", |
| 903 |
input: "-1", |
| 904 |
expectedSecond: 0, |
| 905 |
expectedCursorPos: 2, |
| 906 |
expectedErr: ErrSecondInvalid, |
| 907 |
}, |
| 908 |
{ |
| 909 |
description: "invalid range 2/2", |
| 910 |
input: "60", |
| 911 |
expectedSecond: 0, |
| 912 |
expectedCursorPos: 2, |
| 913 |
expectedErr: ErrSecondInvalid, |
| 914 |
}, |
| 915 |
{ |
| 916 |
description: "valid", |
| 917 |
input: "12", |
| 918 |
expectedSecond: 12, |
| 919 |
expectedCursorPos: 2, |
| 920 |
expectedErr: nil, |
| 921 |
}, |
| 922 |
} |
| 923 |
|
| 924 |
for _, tc := range testCases { |
| 925 |
cursor := 0 |
| 926 |
obtained, err := parseSecond( |
| 927 |
[]byte(tc.input), |
| 928 |
&cursor, |
| 929 |
len(tc.input), |
| 930 |
) |
| 931 |
|
| 932 |
require.Equal( |
| 933 |
t, tc.expectedSecond, obtained, tc.description, |
| 934 |
) |
| 935 |
|
| 936 |
require.Equal( |
| 937 |
t, tc.expectedErr, err, tc.description, |
| 938 |
) |
| 939 |
|
| 940 |
require.Equal( |
| 941 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 942 |
) |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
func TestParseSecFrac(t *testing.T) { |
| 947 |
testCases := []struct { |
| 948 |
description string |
| 949 |
input string |
| 950 |
expectedSecFrac float64 |
| 951 |
expectedCursorPos int |
| 952 |
expectedErr error |
| 953 |
}{ |
| 954 |
{ |
| 955 |
description: "invalid", |
| 956 |
input: "azerty", |
| 957 |
expectedSecFrac: 0, |
| 958 |
expectedCursorPos: 0, |
| 959 |
expectedErr: ErrSecFracInvalid, |
| 960 |
}, |
| 961 |
{ |
| 962 |
description: "nanoseconds", |
| 963 |
input: "123456789", |
| 964 |
expectedSecFrac: 0.123456, |
| 965 |
expectedCursorPos: 6, |
| 966 |
expectedErr: nil, |
| 967 |
}, |
| 968 |
{ |
| 969 |
description: "valid 1/4", |
| 970 |
input: "0", |
| 971 |
expectedSecFrac: 0, |
| 972 |
expectedCursorPos: 1, |
| 973 |
expectedErr: nil, |
| 974 |
}, |
| 975 |
{ |
| 976 |
description: "valid 2/4", |
| 977 |
input: "52", |
| 978 |
expectedSecFrac: 0.52, |
| 979 |
expectedCursorPos: 2, |
| 980 |
expectedErr: nil, |
| 981 |
}, |
| 982 |
{ |
| 983 |
description: "valid 3/4", |
| 984 |
input: "003", |
| 985 |
expectedSecFrac: 0.003, |
| 986 |
expectedCursorPos: 3, |
| 987 |
expectedErr: nil, |
| 988 |
}, |
| 989 |
{ |
| 990 |
description: "valid 4/4", |
| 991 |
input: "000003", |
| 992 |
expectedSecFrac: 0.000003, |
| 993 |
expectedCursorPos: 6, |
| 994 |
expectedErr: nil, |
| 995 |
}, |
| 996 |
} |
| 997 |
|
| 998 |
for _, tc := range testCases { |
| 999 |
cursor := 0 |
| 1000 |
obtained, err := parseSecFrac( |
| 1001 |
[]byte(tc.input), |
| 1002 |
&cursor, |
| 1003 |
len(tc.input), |
| 1004 |
) |
| 1005 |
|
| 1006 |
require.Equal( |
| 1007 |
t, tc.expectedSecFrac, obtained, tc.description, |
| 1008 |
) |
| 1009 |
|
| 1010 |
require.Equal( |
| 1011 |
t, tc.expectedErr, err, tc.description, |
| 1012 |
) |
| 1013 |
|
| 1014 |
require.Equal( |
| 1015 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 1016 |
) |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
func TestParseNumericalTimeOffset(t *testing.T) { |
| 1021 |
buff := []byte("+02:00") |
| 1022 |
cursor := 0 |
| 1023 |
l := len(buff) |
| 1024 |
|
| 1025 |
tmpTs, err := time.Parse("-07:00", string(buff)) |
| 1026 |
require.Nil(t, err) |
| 1027 |
|
| 1028 |
obtained, err := parseNumericalTimeOffset( |
| 1029 |
buff, &cursor, l, |
| 1030 |
) |
| 1031 |
|
| 1032 |
require.Nil(t, err) |
| 1033 |
|
| 1034 |
expected := tmpTs.Location() |
| 1035 |
require.Equal(t, expected, obtained) |
| 1036 |
require.Equal(t, 6, cursor) |
| 1037 |
} |
| 1038 |
|
| 1039 |
func TestParseTimeOffset(t *testing.T) { |
| 1040 |
buff := []byte("Z") |
| 1041 |
cursor := 0 |
| 1042 |
l := len(buff) |
| 1043 |
|
| 1044 |
obtained, err := parseTimeOffset( |
| 1045 |
buff, &cursor, l, |
| 1046 |
) |
| 1047 |
|
| 1048 |
require.Nil(t, err) |
| 1049 |
require.Equal(t, time.UTC, obtained) |
| 1050 |
require.Equal(t, 1, cursor) |
| 1051 |
} |
| 1052 |
|
| 1053 |
func TestGetHourMin(t *testing.T) { |
| 1054 |
buff := []byte("12:34") |
| 1055 |
cursor := 0 |
| 1056 |
l := len(buff) |
| 1057 |
|
| 1058 |
expectedH := 12 |
| 1059 |
expectedM := 34 |
| 1060 |
|
| 1061 |
obtainedH, obtainedM, err := getHourMinute( |
| 1062 |
buff, &cursor, l, |
| 1063 |
) |
| 1064 |
|
| 1065 |
require.Nil(t, err) |
| 1066 |
require.Equal(t, expectedH, obtainedH) |
| 1067 |
require.Equal(t, expectedM, obtainedM) |
| 1068 |
require.Equal(t, l, cursor) |
| 1069 |
} |
| 1070 |
|
| 1071 |
func TestParsePartialTime(t *testing.T) { |
| 1072 |
buff := []byte("05:14:15.000003") |
| 1073 |
cursor := 0 |
| 1074 |
l := len(buff) |
| 1075 |
|
| 1076 |
obtained, err := parsePartialTime( |
| 1077 |
buff, &cursor, l, |
| 1078 |
) |
| 1079 |
|
| 1080 |
expected := &partialTime{ |
| 1081 |
hour: 5, |
| 1082 |
minute: 14, |
| 1083 |
seconds: 15, |
| 1084 |
secFrac: 0.000003, |
| 1085 |
} |
| 1086 |
|
| 1087 |
require.Nil(t, err) |
| 1088 |
require.Equal(t, expected, obtained) |
| 1089 |
require.Equal(t, l, cursor) |
| 1090 |
} |
| 1091 |
|
| 1092 |
func TestParseFullTime(t *testing.T) { |
| 1093 |
tz := "-02:00" |
| 1094 |
buff := []byte("05:14:15.000003" + tz) |
| 1095 |
cursor := 0 |
| 1096 |
l := len(buff) |
| 1097 |
|
| 1098 |
tmpTs, err := time.Parse("-07:00", string(tz)) |
| 1099 |
require.Nil(t, err) |
| 1100 |
|
| 1101 |
obtained, err := parseFullTime( |
| 1102 |
buff, &cursor, l, |
| 1103 |
) |
| 1104 |
|
| 1105 |
expected := &fullTime{ |
| 1106 |
pt: &partialTime{ |
| 1107 |
hour: 5, |
| 1108 |
minute: 14, |
| 1109 |
seconds: 15, |
| 1110 |
secFrac: 0.000003, |
| 1111 |
}, |
| 1112 |
loc: tmpTs.Location(), |
| 1113 |
} |
| 1114 |
|
| 1115 |
require.Nil(t, err) |
| 1116 |
require.Equal(t, expected, obtained) |
| 1117 |
require.Equal(t, 21, cursor) |
| 1118 |
} |
| 1119 |
|
| 1120 |
func TestToNSec(t *testing.T) { |
| 1121 |
testCases := map[float64]int{ |
| 1122 |
0.52: 520000000, |
| 1123 |
0.003: 3000000, |
| 1124 |
0.000003: 3000, |
| 1125 |
} |
| 1126 |
|
| 1127 |
for src, expected := range testCases { |
| 1128 |
obtained, err := toNSec(src) |
| 1129 |
require.Nil(t, err) |
| 1130 |
require.Equal(t, expected, obtained) |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
func TestParseAppName(t *testing.T) { |
| 1135 |
testCases := []struct { |
| 1136 |
description string |
| 1137 |
input string |
| 1138 |
expectedAppName string |
| 1139 |
expectedCursorPos int |
| 1140 |
expectedErr error |
| 1141 |
}{ |
| 1142 |
{ |
| 1143 |
description: "valid", |
| 1144 |
input: "su ", |
| 1145 |
expectedAppName: "su", |
| 1146 |
expectedCursorPos: 2, |
| 1147 |
expectedErr: nil, |
| 1148 |
}, |
| 1149 |
{ |
| 1150 |
description: "too long", |
| 1151 |
input: "suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu ", |
| 1152 |
expectedAppName: "", |
| 1153 |
expectedCursorPos: 48, |
| 1154 |
expectedErr: ErrInvalidAppName, |
| 1155 |
}, |
| 1156 |
} |
| 1157 |
|
| 1158 |
for _, tc := range testCases { |
| 1159 |
p := NewParser([]byte(tc.input)) |
| 1160 |
obtained, err := p.parseAppName() |
| 1161 |
|
| 1162 |
require.Equal( |
| 1163 |
t, tc.expectedErr, err, tc.description, |
| 1164 |
) |
| 1165 |
|
| 1166 |
require.Equal( |
| 1167 |
t, tc.expectedAppName, obtained, tc.description, |
| 1168 |
) |
| 1169 |
|
| 1170 |
require.Equal( |
| 1171 |
t, tc.expectedCursorPos, p.cursor, tc.description, |
| 1172 |
) |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
func TestParseProcID(t *testing.T) { |
| 1177 |
testCases := []struct { |
| 1178 |
description string |
| 1179 |
input string |
| 1180 |
expectedProcID string |
| 1181 |
expectedCursorPos int |
| 1182 |
expectedErr error |
| 1183 |
}{ |
| 1184 |
{ |
| 1185 |
description: "valid", |
| 1186 |
input: "123foo ", |
| 1187 |
expectedProcID: "123foo", |
| 1188 |
expectedCursorPos: 6, |
| 1189 |
expectedErr: nil, |
| 1190 |
}, |
| 1191 |
{ |
| 1192 |
description: "too long", |
| 1193 |
input: strings.Repeat("a", 129), |
| 1194 |
expectedProcID: "", |
| 1195 |
expectedCursorPos: 128, |
| 1196 |
expectedErr: ErrInvalidProcId, |
| 1197 |
}, |
| 1198 |
} |
| 1199 |
|
| 1200 |
for _, tc := range testCases { |
| 1201 |
p := NewParser([]byte(tc.input)) |
| 1202 |
obtained, err := p.parseProcId() |
| 1203 |
|
| 1204 |
require.Equal( |
| 1205 |
t, tc.expectedErr, err, tc.description, |
| 1206 |
) |
| 1207 |
|
| 1208 |
require.Equal( |
| 1209 |
t, tc.expectedProcID, obtained, tc.description, |
| 1210 |
) |
| 1211 |
|
| 1212 |
require.Equal( |
| 1213 |
t, tc.expectedCursorPos, p.cursor, tc.description, |
| 1214 |
) |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
func TestParseMsgID(t *testing.T) { |
| 1219 |
testCases := []struct { |
| 1220 |
description string |
| 1221 |
input string |
| 1222 |
expectedMsgID string |
| 1223 |
expectedCursorPos int |
| 1224 |
expectedErr error |
| 1225 |
}{ |
| 1226 |
{ |
| 1227 |
description: "valid", |
| 1228 |
input: "123foo ", |
| 1229 |
expectedMsgID: "123foo", |
| 1230 |
expectedCursorPos: 6, |
| 1231 |
expectedErr: nil, |
| 1232 |
}, |
| 1233 |
{ |
| 1234 |
description: "too long", |
| 1235 |
input: strings.Repeat("a", 33), |
| 1236 |
expectedMsgID: "", |
| 1237 |
expectedCursorPos: 32, |
| 1238 |
expectedErr: ErrInvalidMsgId, |
| 1239 |
}, |
| 1240 |
} |
| 1241 |
|
| 1242 |
for _, tc := range testCases { |
| 1243 |
p := NewParser([]byte(tc.input)) |
| 1244 |
obtained, err := p.parseMsgId() |
| 1245 |
|
| 1246 |
require.Equal( |
| 1247 |
t, tc.expectedErr, err, tc.description, |
| 1248 |
) |
| 1249 |
|
| 1250 |
require.Equal( |
| 1251 |
t, tc.expectedMsgID, obtained, tc.description, |
| 1252 |
) |
| 1253 |
|
| 1254 |
require.Equal( |
| 1255 |
t, tc.expectedCursorPos, p.cursor, tc.description, |
| 1256 |
) |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
func TestParseStructuredData(t *testing.T) { |
| 1261 |
testCases := []struct { |
| 1262 |
description string |
| 1263 |
input string |
| 1264 |
expectedData string |
| 1265 |
expectedCursorPos int |
| 1266 |
expectedErr error |
| 1267 |
}{ |
| 1268 |
{ |
| 1269 |
description: "nil", |
| 1270 |
input: "-", |
| 1271 |
expectedData: "-", |
| 1272 |
expectedCursorPos: 1, |
| 1273 |
expectedErr: nil, |
| 1274 |
}, |
| 1275 |
{ |
| 1276 |
description: "single", |
| 1277 |
input: `[exampleSDID@32473 iut="3" eventSource="Application"eventID="1011"]`, |
| 1278 |
expectedData: `[exampleSDID@32473 iut="3" eventSource="Application"eventID="1011"]`, |
| 1279 |
expectedCursorPos: 67, |
| 1280 |
expectedErr: nil, |
| 1281 |
}, |
| 1282 |
{ |
| 1283 |
description: "multiple", |
| 1284 |
input: `[exampleSDID@32473 iut="3" eventSource="Application"eventID="1011"][examplePriority@32473 class="high"]`, |
| 1285 |
expectedData: `[exampleSDID@32473 iut="3" eventSource="Application"eventID="1011"][examplePriority@32473 class="high"]`, |
| 1286 |
expectedCursorPos: 103, |
| 1287 |
expectedErr: nil, |
| 1288 |
}, |
| 1289 |
{ |
| 1290 |
description: "multiple invalid", |
| 1291 |
input: `[exampleSDID@32473 iut="3" eventSource="Application"eventID="1011"] [examplePriority@32473 class="high"]`, |
| 1292 |
expectedData: `[exampleSDID@32473 iut="3" eventSource="Application"eventID="1011"]`, |
| 1293 |
expectedCursorPos: 67, |
| 1294 |
expectedErr: nil, |
| 1295 |
}, |
| 1296 |
} |
| 1297 |
|
| 1298 |
for _, tc := range testCases { |
| 1299 |
cursor := 0 |
| 1300 |
obtained, err := parseStructuredData( |
| 1301 |
[]byte(tc.input), |
| 1302 |
&cursor, |
| 1303 |
len(tc.input), |
| 1304 |
) |
| 1305 |
|
| 1306 |
require.Equal( |
| 1307 |
t, tc.expectedErr, err, tc.description, |
| 1308 |
) |
| 1309 |
|
| 1310 |
require.Equal( |
| 1311 |
t, tc.expectedData, obtained, tc.description, |
| 1312 |
) |
| 1313 |
|
| 1314 |
require.Equal( |
| 1315 |
t, tc.expectedCursorPos, cursor, tc.description, |
| 1316 |
) |
| 1317 |
} |
| 1318 |
} |
| 1319 |
|
| 1320 |
func TestParseMessageSizeChecks(t *testing.T) { |
| 1321 |
start := `<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"] ` |
| 1322 |
msg := start + strings.Repeat("a", MAX_PACKET_LEN) |
| 1323 |
|
| 1324 |
p := NewParser([]byte(msg)) |
| 1325 |
err := p.Parse() |
| 1326 |
fields := p.Dump() |
| 1327 |
|
| 1328 |
require.Nil( |
| 1329 |
t, err, |
| 1330 |
) |
| 1331 |
|
| 1332 |
require.Len( |
| 1333 |
t, |
| 1334 |
fields["message"], |
| 1335 |
MAX_PACKET_LEN-len(start), |
| 1336 |
) |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
msg = start + " hello " |
| 1341 |
p = NewParser([]byte(msg)) |
| 1342 |
err = p.Parse() |
| 1343 |
fields = p.Dump() |
| 1344 |
|
| 1345 |
require.Nil(t, err) |
| 1346 |
require.Equal(t, "hello", fields["message"]) |
| 1347 |
} |
| 1348 |
|
| 1349 |
func BenchmarkParseTimestamp(b *testing.B) { |
| 1350 |
buff := []byte("2003-08-24T05:14:15.000003-07:00") |
| 1351 |
|
| 1352 |
p := NewParser(buff) |
| 1353 |
|
| 1354 |
for i := 0; i < b.N; i++ { |
| 1355 |
_, err := p.parseTimestamp() |
| 1356 |
if err != nil { |
| 1357 |
panic(err) |
| 1358 |
} |
| 1359 |
|
| 1360 |
p.cursor = 0 |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
func BenchmarkParseHeader(b *testing.B) { |
| 1365 |
buff := []byte( |
| 1366 |
"<165>1 2003-10-11T22:14:15.003Z mymachine.example.com su 123 ID47 ", |
| 1367 |
) |
| 1368 |
|
| 1369 |
p := NewParser(buff) |
| 1370 |
|
| 1371 |
for i := 0; i < b.N; i++ { |
| 1372 |
_, err := p.parseHeader() |
| 1373 |
if err != nil { |
| 1374 |
panic(err) |
| 1375 |
} |
| 1376 |
|
| 1377 |
p.cursor = 0 |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
func BenchmarkParseFull(b *testing.B) { |
| 1382 |
msg := `<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"] An application event log entry...` |
| 1383 |
|
| 1384 |
for i := 0; i < b.N; i++ { |
| 1385 |
p := NewParser( |
| 1386 |
[]byte(msg), |
| 1387 |
) |
| 1388 |
|
| 1389 |
err := p.Parse() |
| 1390 |
if err != nil { |
| 1391 |
panic(err) |
| 1392 |
} |
| 1393 |
} |
| 1394 |
} |