parse_test.go 11 KB

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