~samwhited/xmpp

ref: de40e6f9107a5d25ae8c8edad9289fd0c4b12e2f xmpp/styling/fuzz_test.go -rw-r--r-- 1023 bytes
de40e6f9Sam Whited roster: add type to IQ response in test 10 months ago
                                                                                
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
// Copyright 2020 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

//go:build gofuzzbeta
// +build gofuzzbeta

package styling_test

import (
	"bytes"
	"io"
	"testing"

	"mellium.im/xmpp/styling"
)

func FuzzParseDocument(f *testing.F) {
	f.Add([]byte{'*'})
	f.Add([]byte{'_'})
	f.Add([]byte{'`'})
	f.Add([]byte("```"))
	f.Add([]byte{'~'})
	f.Add([]byte{'>'})
	f.Add([]byte{'\n'})
	f.Fuzz(func(t *testing.T, doc []byte) {
		d := styling.NewDecoder(bytes.NewReader(doc))
		n := 0
		for {
			tok, err := d.Token()
			if err == io.EOF {
				if n+len(tok.Data) != len(doc) {
					t.Fatalf("got early EOF at %d for input:\n%v", n, doc)
				}
				break
			}
			if err != nil {
				t.Fatalf("error decoding: %v\nOriginal bytes:\n%v", err, doc)
				break
			}
			if !bytes.Equal([]byte(doc[n:n+len(tok.Data)]), tok.Data) {
				t.Fatalf("output bytes did not equal input bytes at %d for input:\n%v", n, doc)
			}
			n += len(tok.Data)
		}
	})
}