parse_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package rfc3164
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestPri(t *testing.T) {
  7. tests := []struct {
  8. input string
  9. expected int
  10. expectedErr string
  11. }{
  12. {"<0>", 0, ""},
  13. {"<19>", 19, ""},
  14. {"<200>", 200, ""},
  15. {"<4999>", 0, "PRI must be up to 3 characters long"},
  16. {"<123", 0, "PRI must end with '>'"},
  17. {"123>", 0, "PRI must start with '<'"},
  18. {"<abc>", 0, "PRI must be a number"},
  19. }
  20. for _, test := range tests {
  21. t.Run(test.input, func(t *testing.T) {
  22. r := &RFC3164{}
  23. r.buf = []byte(test.input)
  24. r.len = len(r.buf)
  25. err := r.parsePRI()
  26. if err != nil {
  27. if test.expectedErr != "" {
  28. if err.Error() != test.expectedErr {
  29. t.Errorf("expected error %s, got %s", test.expectedErr, err.Error())
  30. }
  31. } else {
  32. t.Errorf("unexpected error: %s", err.Error())
  33. }
  34. } else {
  35. if test.expectedErr != "" {
  36. t.Errorf("expected error %s, got no error", test.expectedErr)
  37. } else {
  38. if r.PRI != test.expected {
  39. t.Errorf("expected %d, got %d", test.expected, r.PRI)
  40. }
  41. }
  42. }
  43. })
  44. }
  45. }
  46. func TestTimestamp(t *testing.T) {
  47. tests := []struct {
  48. input string
  49. expected string
  50. expectedErr string
  51. currentYear bool
  52. }{
  53. {"May 20 09:33:54", "0000-05-20T09:33:54Z", "", false},
  54. {"May 20 09:33:54", "2022-05-20T09:33:54Z", "", true},
  55. {"May 20 09:33:54 2022", "2022-05-20T09:33:54Z", "", false},
  56. {"May 1 09:33:54 2022", "2022-05-01T09:33:54Z", "", false},
  57. {"May 01 09:33:54 2021", "2021-05-01T09:33:54Z", "", true},
  58. {"foobar", "", "timestamp is not valid", false},
  59. }
  60. for _, test := range tests {
  61. t.Run(test.input, func(t *testing.T) {
  62. opts := []RFC3164Option{}
  63. if test.currentYear {
  64. opts = append(opts, WithCurrentYear())
  65. }
  66. r := NewRFC3164Parser(opts...)
  67. r.buf = []byte(test.input)
  68. r.len = len(r.buf)
  69. err := r.parseTimestamp()
  70. if err != nil {
  71. if test.expectedErr != "" {
  72. if err.Error() != test.expectedErr {
  73. t.Errorf("expected error %s, got %s", test.expectedErr, err.Error())
  74. }
  75. } else {
  76. t.Errorf("unexpected error: %s", err.Error())
  77. }
  78. } else {
  79. if test.expectedErr != "" {
  80. t.Errorf("expected error %s, got no error", test.expectedErr)
  81. } else {
  82. if r.Timestamp.Format(time.RFC3339) != test.expected {
  83. t.Errorf("expected %s, got %s", test.expected, r.Timestamp.Format(time.RFC3339))
  84. }
  85. }
  86. }
  87. })
  88. }
  89. }
  90. func TestHostname(t *testing.T) {
  91. tests := []struct {
  92. input string
  93. expected string
  94. expectedErr string
  95. strictHostname bool
  96. }{
  97. {"127.0.0.1", "127.0.0.1", "", false},
  98. {"::1", "::1", "", false},
  99. {"foo.-bar", "", "hostname is not valid", true},
  100. {"foo-.bar", "", "hostname is not valid", true},
  101. {"foo123.bar", "foo123.bar", "", true},
  102. {"a..", "", "hostname is not valid", true},
  103. {"foo.bar", "foo.bar", "", false},
  104. {"foo,bar", "foo,bar", "", false},
  105. {"foo,bar", "", "hostname is not valid", true},
  106. {"", "", "hostname is empty", false},
  107. {".", ".", "", true},
  108. {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "", "hostname is not valid", true},
  109. {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bla", "", "hostname is not valid", true},
  110. {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bla", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.bla", "", false},
  111. {"a.foo-", "", "hostname is not valid", true},
  112. }
  113. for _, test := range tests {
  114. t.Run(test.input, func(t *testing.T) {
  115. opts := []RFC3164Option{}
  116. if test.strictHostname {
  117. opts = append(opts, WithStrictHostname())
  118. }
  119. r := NewRFC3164Parser(opts...)
  120. r.buf = []byte(test.input)
  121. r.len = len(r.buf)
  122. err := r.parseHostname()
  123. if err != nil {
  124. if test.expectedErr != "" {
  125. if err.Error() != test.expectedErr {
  126. t.Errorf("expected error %s, got %s", test.expectedErr, err.Error())
  127. }
  128. } else {
  129. t.Errorf("unexpected error: %s", err.Error())
  130. }
  131. } else {
  132. if test.expectedErr != "" {
  133. t.Errorf("expected error %s, got no error", test.expectedErr)
  134. } else {
  135. if r.Hostname != test.expected {
  136. t.Errorf("expected %s, got %s", test.expected, r.Hostname)
  137. }
  138. }
  139. }
  140. })
  141. }
  142. }
  143. func TestTag(t *testing.T) {
  144. tests := []struct {
  145. input string
  146. expected string
  147. expectedPID string
  148. expectedErr string
  149. }{
  150. {"foobar", "foobar", "", ""},
  151. {"foobar[42]", "foobar", "42", ""},
  152. {"", "", "", "tag is empty"},
  153. {"foobar[", "", "", "pid inside tag must be closed with ']'"},
  154. {"foobar[42", "", "", "pid inside tag must be closed with ']'"},
  155. {"foobar[asd]", "foobar", "", "pid inside tag must be a number"},
  156. }
  157. for _, test := range tests {
  158. t.Run(test.input, func(t *testing.T) {
  159. r := &RFC3164{}
  160. r.buf = []byte(test.input)
  161. r.len = len(r.buf)
  162. err := r.parseTag()
  163. if err != nil {
  164. if test.expectedErr != "" {
  165. if err.Error() != test.expectedErr {
  166. t.Errorf("expected error %s, got %s", test.expectedErr, err.Error())
  167. }
  168. } else {
  169. t.Errorf("unexpected error: %s", err.Error())
  170. }
  171. } else {
  172. if test.expectedErr != "" {
  173. t.Errorf("expected error %s, got no error", test.expectedErr)
  174. } else {
  175. if r.Tag != test.expected {
  176. t.Errorf("expected %s, got %s", test.expected, r.Tag)
  177. }
  178. if r.PID != test.expectedPID {
  179. t.Errorf("expected %s, got %s", test.expected, r.Message)
  180. }
  181. }
  182. }
  183. })
  184. }
  185. }
  186. func TestMessage(t *testing.T) {
  187. tests := []struct {
  188. input string
  189. expected string
  190. expectedErr string
  191. }{
  192. {"foobar: pouet", "pouet", ""},
  193. {"foobar[42]: test", "test", ""},
  194. {"foobar[123]: this is a test", "this is a test", ""},
  195. {"foobar[123]: ", "", "message is empty"},
  196. {"foobar[123]:", "", "message is empty"},
  197. }
  198. for _, test := range tests {
  199. t.Run(test.input, func(t *testing.T) {
  200. r := &RFC3164{}
  201. r.buf = []byte(test.input)
  202. r.len = len(r.buf)
  203. err := r.parseMessage()
  204. if err != nil {
  205. if test.expectedErr != "" {
  206. if err.Error() != test.expectedErr {
  207. t.Errorf("expected error %s, got %s", test.expectedErr, err.Error())
  208. }
  209. } else {
  210. t.Errorf("unexpected error: %s", err.Error())
  211. }
  212. } else {
  213. if test.expectedErr != "" {
  214. t.Errorf("expected error %s, got no error", test.expectedErr)
  215. } else {
  216. if r.Message != test.expected {
  217. t.Errorf("expected message %s, got %s", test.expected, r.Tag)
  218. }
  219. }
  220. }
  221. })
  222. }
  223. }
  224. func TestParse(t *testing.T) {
  225. type expected struct {
  226. Timestamp time.Time
  227. Hostname string
  228. Tag string
  229. PID string
  230. Message string
  231. PRI int
  232. }
  233. tests := []struct {
  234. input string
  235. expected expected
  236. expectedErr string
  237. opts []RFC3164Option
  238. }{
  239. {
  240. "<12>May 20 09:33:54 UDMPRO,a2edd0c6ae48,udm-1.10.0.3686 kernel: foo", expected{
  241. Timestamp: time.Date(0, time.May, 20, 9, 33, 54, 0, time.UTC),
  242. Hostname: "UDMPRO,a2edd0c6ae48,udm-1.10.0.3686",
  243. Tag: "kernel",
  244. PID: "",
  245. Message: "foo",
  246. PRI: 12,
  247. }, "", []RFC3164Option{},
  248. },
  249. {
  250. "<12>May 20 09:33:54 UDMPRO,a2edd0c6ae48,udm-1.10.0.3686 kernel: foo", expected{
  251. Timestamp: time.Date(2022, time.May, 20, 9, 33, 54, 0, time.UTC),
  252. Hostname: "UDMPRO,a2edd0c6ae48,udm-1.10.0.3686",
  253. Tag: "kernel",
  254. PID: "",
  255. Message: "foo",
  256. PRI: 12,
  257. }, "", []RFC3164Option{WithCurrentYear()},
  258. },
  259. {
  260. "<12>May 20 09:33:54 UDMPRO,a2edd0c6ae48,udm-1.10.0.3686 kernel: foo", expected{}, "hostname is not valid", []RFC3164Option{WithStrictHostname()},
  261. },
  262. {
  263. "foobar", expected{}, "PRI must start with '<'", []RFC3164Option{},
  264. },
  265. {
  266. "<12>", expected{}, "timestamp is not valid", []RFC3164Option{},
  267. },
  268. {
  269. "<12 May 02 09:33:54 foo.bar", expected{}, "PRI must be a number", []RFC3164Option{},
  270. },
  271. {
  272. "<12>May 02 09:33:54", expected{}, "hostname is empty", []RFC3164Option{},
  273. },
  274. {
  275. "<12>May 02 09:33:54 foo.bar", expected{}, "tag is empty", []RFC3164Option{},
  276. },
  277. {
  278. "<12>May 02 09:33:54 foo.bar bla[42", expected{}, "pid inside tag must be closed with ']'", []RFC3164Option{},
  279. },
  280. {
  281. "<12>May 02 09:33:54 foo.bar bla[42]", expected{}, "message is empty", []RFC3164Option{},
  282. },
  283. {
  284. "<12>May 02 09:33:54 foo.bar bla[42]: ", expected{}, "message is empty", []RFC3164Option{},
  285. },
  286. {
  287. "<12>May 02 09:33:54 foo.bar bla", expected{}, "message is empty", []RFC3164Option{},
  288. },
  289. {
  290. "<12>May 02 09:33:54 foo.bar bla:", expected{}, "message is empty", []RFC3164Option{},
  291. },
  292. {
  293. "", expected{}, "message is empty", []RFC3164Option{},
  294. },
  295. {
  296. `<13>1 2021-05-18T11:58:40.828081+02:00 mantis sshd 49340 - [timeQuality isSynced="0" tzKnown="1"] blabla`, expected{}, "timestamp is not valid", []RFC3164Option{},
  297. },
  298. {
  299. `<46>Jun 2 06:55:39 localhost haproxy[27213]: Connect from 100.100.100.99:52611 to 100.100.100.99:443 (https_shared-merged/HTTP)\\n 10.0.0.1}`, expected{
  300. Timestamp: time.Date(time.Now().Year(), time.June, 2, 6, 55, 39, 0, time.UTC),
  301. Hostname: "localhost",
  302. Tag: "haproxy",
  303. PID: "27213",
  304. Message: `Connect from 100.100.100.99:52611 to 100.100.100.99:443 (https_shared-merged/HTTP)\\n 10.0.0.1}`,
  305. PRI: 46,
  306. }, "", []RFC3164Option{WithCurrentYear()},
  307. },
  308. {
  309. `<46>Jun 2 06:55:39 2022 localhost haproxy[27213]: Connect from 100.100.100.99:52611 to 100.100.100.99:443 (https_shared-merged/HTTP)\\n 10.0.0.1}`, expected{
  310. Timestamp: time.Date(2022, time.June, 2, 6, 55, 39, 0, time.UTC),
  311. Hostname: "localhost",
  312. Tag: "haproxy",
  313. PID: "27213",
  314. Message: `Connect from 100.100.100.99:52611 to 100.100.100.99:443 (https_shared-merged/HTTP)\\n 10.0.0.1}`,
  315. PRI: 46,
  316. }, "", []RFC3164Option{},
  317. },
  318. }
  319. for _, test := range tests {
  320. t.Run(test.input, func(t *testing.T) {
  321. r := NewRFC3164Parser(test.opts...)
  322. err := r.Parse([]byte(test.input))
  323. if err != nil {
  324. if test.expectedErr != "" {
  325. if err.Error() != test.expectedErr {
  326. t.Errorf("expected error '%s', got '%s'", test.expectedErr, err.Error())
  327. }
  328. } else {
  329. t.Errorf("unexpected error: '%s'", err.Error())
  330. }
  331. } else {
  332. if test.expectedErr != "" {
  333. t.Errorf("expected error '%s', got no error", test.expectedErr)
  334. } else {
  335. if r.Timestamp != test.expected.Timestamp {
  336. t.Errorf("expected timestamp '%s', got '%s'", test.expected.Timestamp, r.Timestamp)
  337. }
  338. if r.Hostname != test.expected.Hostname {
  339. t.Errorf("expected hostname '%s', got '%s'", test.expected.Hostname, r.Hostname)
  340. }
  341. if r.Tag != test.expected.Tag {
  342. t.Errorf("expected tag '%s', got '%s'", test.expected.Tag, r.Tag)
  343. }
  344. if r.PID != test.expected.PID {
  345. t.Errorf("expected pid '%s', got '%s'", test.expected.PID, r.PID)
  346. }
  347. if r.Message != test.expected.Message {
  348. t.Errorf("expected message '%s', got '%s'", test.expected.Message, r.Message)
  349. }
  350. if r.PRI != test.expected.PRI {
  351. t.Errorf("expected pri '%d', got '%d'", test.expected.PRI, r.PRI)
  352. }
  353. }
  354. }
  355. })
  356. }
  357. }