manager_mutation_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package mtproto
  2. import (
  3. "testing"
  4. )
  5. // TestParseMetricLineBraceBoundary pins the contract of the brace-position
  6. // guard in parseMetricLine (manager.go:425 -> `if end < brace`).
  7. //
  8. // Once a '{' is found at index `brace`, the matching '}' must appear AFTER it.
  9. // A '}' that precedes the '{', or a '{' with no closing '}' at all
  10. // (strings.IndexByte returns -1, which is < brace), is a malformed line and
  11. // must yield an error rather than slicing past the brace.
  12. func TestParseMetricLineBraceBoundary(t *testing.T) {
  13. t.Run("closing brace before opening brace is malformed", func(t *testing.T) {
  14. // '}' at index 8 comes before '{' at index 16: end < brace must hold,
  15. // so this is rejected. Mutating `<` to `>`/`>=` would accept it.
  16. _, _, _, err := parseMetricLine(`mtg_x_a}_b{direction="x"} 5`)
  17. if err == nil {
  18. t.Fatal("expected error for '}' appearing before '{'")
  19. }
  20. })
  21. t.Run("opening brace with no closing brace is malformed", func(t *testing.T) {
  22. // No '}' at all -> end == -1, which is < brace. Must error.
  23. // If the guard were dropped/inverted the code would slice line[brace+1:-1]
  24. // and panic; asserting a clean error keeps that contract.
  25. _, _, _, err := parseMetricLine(`mtg_traffic{direction="x" 5`)
  26. if err == nil {
  27. t.Fatal("expected error for '{' without a closing '}'")
  28. }
  29. })
  30. t.Run("well-formed braces are accepted", func(t *testing.T) {
  31. // '{' at index 11, '}' at index 25: end > brace, so the guard must NOT
  32. // fire and parsing must succeed. Guards against a mutant that always errors.
  33. name, labels, val, err := parseMetricLine(`mtg_traffic{direction="up"} 42`)
  34. if err != nil {
  35. t.Fatalf("well-formed line should parse: %v", err)
  36. }
  37. if name != "mtg_traffic" {
  38. t.Fatalf("name=%q", name)
  39. }
  40. if labels["direction"] != "up" {
  41. t.Fatalf("labels=%v", labels)
  42. }
  43. if val != 42 {
  44. t.Fatalf("val=%v", val)
  45. }
  46. })
  47. }
  48. // TestParseMetricLineLabelEqualsBoundary pins the contract of the '=' guard in
  49. // the per-label loop (manager.go:430 -> `if eq < 0`).
  50. //
  51. // - eq < 0 (no '=' in the segment): the segment is skipped, no label added.
  52. // - eq == 0 (segment begins with '='): the key is empty but the pair is STILL
  53. // parsed, producing labels[""] = value. The boundary is `< 0`, not `<= 0`.
  54. func TestParseMetricLineLabelEqualsBoundary(t *testing.T) {
  55. t.Run("label segment without '=' is skipped, not fatal", func(t *testing.T) {
  56. // "novalue" has no '=' (eq == -1) and must be skipped. A real key=val
  57. // segment in the same line must still be parsed. Mutating `< 0` to `> 0`
  58. // would take kv[:eq] with eq=-1 and panic; mutating away the skip would
  59. // also corrupt parsing.
  60. name, labels, val, err := parseMetricLine(`mtg_traffic{novalue,direction="down"} 9`)
  61. if err != nil {
  62. t.Fatalf("line with a value-less label should still parse: %v", err)
  63. }
  64. if name != "mtg_traffic" {
  65. t.Fatalf("name=%q", name)
  66. }
  67. if _, present := labels["novalue"]; present {
  68. t.Fatalf("value-less segment must not create a label: %v", labels)
  69. }
  70. if labels["direction"] != "down" {
  71. t.Fatalf("real label must still be parsed: %v", labels)
  72. }
  73. if val != 9 {
  74. t.Fatalf("val=%v", val)
  75. }
  76. })
  77. t.Run("label segment beginning with '=' is parsed as empty key", func(t *testing.T) {
  78. // "=onlyvalue": eq == 0. Since the guard is `< 0`, this is NOT skipped:
  79. // it yields labels[""] = "onlyvalue". A mutant changing `< 0` to `<= 0`
  80. // would skip it, losing the empty-key entry.
  81. _, labels, _, err := parseMetricLine(`mtg_traffic{=onlyvalue} 1`)
  82. if err != nil {
  83. t.Fatalf("segment with empty key should still parse: %v", err)
  84. }
  85. v, present := labels[""]
  86. if !present {
  87. t.Fatalf("eq==0 segment must produce an empty-key label: %v", labels)
  88. }
  89. if v != "onlyvalue" {
  90. t.Fatalf("empty-key label value=%q", v)
  91. }
  92. })
  93. }