parse_test.go 11 KB

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