~samwhited/xmpp

ref: ed13a4c4861ee84d693dae5b80cdf1d1674b57d4 xmpp/jid/fuzz_test.go -rw-r--r-- 1.1 KiB
ed13a4c4Sam Whited internal/integration/ejabberd: component support 1 year, 5 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
48
49
50
51
52
// 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.

//+build fuzz

// This tool is meant to excersize JID parsing with random strings.
// This will hopeful tease out any panics that are hidden throughout the code.
// No care has been taken to try and make this fast, so it is very slow and does
// not get run with the normal tests.

package jid_test

import (
	"math/rand"
	"strings"
	"testing"
	"time"

	"mellium.im/xmpp/jid"
)

const (
	// The maximum length of generated JIDs.
	maxLength = 1000

	// The number of JIDs to generate.
	iterations = 1 << 21
)

func randJID(size int) string {
	var b strings.Builder
	for b.Len() < size {
		b.WriteRune(rune(rand.Uint32()))
	}
	return b.String()
}

func TestFuzz(t *testing.T) {
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < iterations; i++ {
		func() {
			randJID := randJID(rand.Intn(maxLength))
			defer func() {
				if r := recover(); r != nil {
					t.Errorf("Panic recovered %v on JID %x", r, randJID)
				}
			}()
			jid.Parse(randJID)
		}()
	}
}