File: fuzz.go

package info (click to toggle)
prometheus-mqtt-exporter 0.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 460 kB
  • sloc: sh: 77; makefile: 60
file content (52 lines) | stat: -rw-r--r-- 1,074 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// +build gofuzz

package metric_per_topic

import (
	"fmt"
	"github.com/hikhvar/mqtt2prometheus/pkg/config"
	"github.com/hikhvar/mqtt2prometheus/pkg/metrics"
)

func Fuzz(data []byte) int {
	p := metrics.NewParser([]config.MetricConfig{
		{
			PrometheusName: "temperature",
			ValueType:      "gauge",
		},
		{
			PrometheusName: "enabled",
			ValueType:      "gauge",
			StringValueMapping: &config.StringValueMappingConfig{
				ErrorValue: floatP(12333),
				Map: map[string]float64{
					"foo": 112,
					"bar": 2,
				},
			},
		},
		{
			PrometheusName: "kartoffeln",
			ValueType:      "counter",
		},
	})
	json := metrics.NewMetricPerTopicExtractor(p, config.MustNewRegexp("shellies/(?P<deviceid>.*)/sensor/(?P<metricname>.*)"))

	name := "enabled"
	consumed := 0
	if len(data) > 0 {

		name = []string{"temperature", "enabled", "kartoffel"}[data[0]%3]
		consumed += 1

	}
	mc, err := json(fmt.Sprintf("shellies/bar/sensor/%s", name), data[consumed:], "bar")
	if err != nil && len(mc) > 0 {
		return 1
	}
	return 0
}

func floatP(f float64) *float64 {
	return &f
}