parse_test.go 11 KB

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