-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_test.go
More file actions
67 lines (62 loc) · 1.98 KB
/
fuzz_test.go
File metadata and controls
67 lines (62 loc) · 1.98 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2026 Joshua Jones <joshua.jones.software@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hl7
import "testing"
func FuzzParseMessage(f *testing.F) {
// Seed corpus with valid messages.
f.Add([]byte("MSH|^~\\&|SYS|FAC|REC|FAC|202301011200||ADT^A01|123|P|2.5\rPID|||123456||Doe^John\r"))
f.Add([]byte("MSH|^~\\&|S|F|R|RF|20240101||ORU^R01|1|P|2.5.1\rOBX|1|NM|code||42\r"))
f.Add([]byte("MSH|^~\\&|S|F|R|RF|20240101||ADT^A01|1|P|2.5.1"))
f.Add([]byte("MSH|^~\\&||||||||||||"))
f.Add([]byte("MSH|^~\\&|test\\F\\embed|F|R|RF|20240101||ADT^A01|1|P|2.5.1"))
f.Fuzz(func(t *testing.T, data []byte) {
msg, err := ParseMessage(data)
if err != nil {
return // Invalid messages are expected.
}
// Should not panic on any access pattern.
for _, seg := range msg.Segments() {
_ = seg.Type()
_ = seg.Raw()
fc := seg.FieldCount()
for i := 0; i <= fc; i++ {
f := seg.Field(i)
_ = f.String()
_ = f.Bytes()
_ = f.IsNull()
_ = f.IsEmpty()
_ = f.HasValue()
rc := f.RepetitionCount()
for j := 0; j < rc && j < 10; j++ {
r := f.Rep(j)
_ = r.String()
cc := r.ComponentCount()
for k := 1; k <= cc && k <= 10; k++ {
c := r.Component(k)
_ = c.String()
sc := c.SubComponentCount()
for l := 1; l <= sc && l <= 10; l++ {
s := c.SubComponent(l)
_ = s.String()
}
}
}
}
}
// Test accessor.
_ = msg.Get("MSH-9")
_ = msg.Get("MSH-9.1")
_ = msg.Get("PID-3.1.2")
})
}