Skip to content

Commit 0318dd7

Browse files
authored
Merge pull request #40 from skx/39-cleanups
39 cleanups
2 parents a5ba289 + 104d7b1 commit 0318dd7

9 files changed

+695
-196
lines changed

calc/FUZZING.md

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
# Fuzz-Testing
22

3-
If you don't have the appropriate tools installed you can fetch them via:
4-
5-
go get github.com/dvyukov/go-fuzz/go-fuzz
6-
go get github.com/dvyukov/go-fuzz/go-fuzz-build
7-
8-
Now you can build the `calc` package with fuzzing enabled:
9-
10-
$ go-fuzz-build github.com/skx/sysbox/calc
11-
12-
Create a location to hold the work, and give it copies of some sample-programs:
13-
14-
$ mkdir -p workdir/corpus
15-
$ echo "1 + 2" > workdir/corpus/1.txt
16-
$ echo "1 - 2" > workdir/corpus/2.txt
17-
$ echo "1 / 2" > workdir/corpus/3.txt
18-
$ echo "1 * 2" > workdir/corpus/4.txt
19-
$ echo "1 + 2 * 3" > workdir/corpus/5.txt
20-
21-
22-
Now you can actually launch the fuzzer - here I use `-procs 1` so that
23-
my desktop system isn't complete overloaded:
24-
25-
$ go-fuzz -procs 1 -bin calc-fuzz.zip -workdir workdir/
26-
27-
28-
29-
## Output
30-
31-
Once the fuzzer starts running it will generate test-cases, and
32-
run them. The output will look something like this:
33-
34-
2020/06/11 08:25:55 workers: 1, corpus: 240 (2m39s ago), crashers: 0, restarts: 1/9967, execs: 3508543 (4348/sec), cover: 846, uptime: 13m27s
35-
2020/06/11 08:25:58 workers: 1, corpus: 240 (2m42s ago), crashers: 0, restarts: 1/9954, execs: 3514055 (4338/sec), cover: 846, uptime: 13m30s
36-
37-
Here you see the fuzzer been running for 13 minutes, and 30 seconds, 3514055 test-cases have been executed and "crashers: 0" means there have been found no crashes.
38-
39-
If any crashes are detected they'll be saved in `workdir/crashers`.
3+
The 1.18 release of the golang compiler/toolset has integrated support for
4+
fuzz-testing.
5+
6+
Fuzz-testing is basically magical and involves generating new inputs "randomly"
7+
and running test-cases with those inputs.
8+
9+
10+
## Running
11+
12+
If you're running 1.18beta1 or higher you can run the fuzz-testing against
13+
the calculator package like so:
14+
15+
$ go test -fuzztime=300s -parallel=1 -fuzz=FuzzCalculator -v
16+
=== RUN TestBasic
17+
--- PASS: TestBasic (0.00s)
18+
..
19+
..
20+
fuzz: elapsed: 0s, gathering baseline coverage: 0/111 completed
21+
fuzz: elapsed: 0s, gathering baseline coverage: 111/111 completed, now fuzzing with 1 workers
22+
fuzz: elapsed: 3s, execs: 63894 (21292/sec), new interesting: 9 (total: 120)
23+
fuzz: elapsed: 6s, execs: 76044 (4051/sec), new interesting: 12 (total: 123)
24+
fuzz: elapsed: 9s, execs: 76044 (0/sec), new interesting: 12 (total: 123)
25+
fuzz: elapsed: 12s, execs: 76044 (0/sec), new interesting: 12 (total: 123)
26+
..
27+
fuzz: elapsed: 5m0s, execs: 5209274 (12462/sec), new interesting: 162 (total: 273)
28+
fuzz: elapsed: 5m1s, execs: 5209274 (0/sec), new interesting: 162 (total: 273)
29+
--- PASS: FuzzCalculator (301.01s)
30+
PASS
31+
ok github.com/skx/sysbox/calc 301.010s
32+
33+
You'll note that I've added `-parallel=1` to the test, because otherwise my desktop system becomes unresponsive while the testing is going on.

calc/Fuzz.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

calc/evaluator.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,14 @@ func (e *Evaluator) Run() *Token {
335335
return e.peekToken()
336336
}
337337

338-
// We'll save the result in a magic variable "result"
339-
e.variables["result"] = result.Value.(float64)
338+
// If we evaluated something we'll have a result which
339+
// we'll save in the `result` variable.
340+
//
341+
// (We might receive input such as "", which will result
342+
// in nothing being evaluated)
343+
if result != nil {
344+
e.variables["result"] = result.Value.(float64)
345+
}
340346

341347
// All done.
342348
return result

calc/fuzz_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package calc
5+
6+
import (
7+
"testing"
8+
)
9+
10+
// FuzzCalculator will run a series of random/fuzz-tests against our
11+
// parser and evaluator.
12+
func FuzzCalculator(f *testing.F) {
13+
14+
// Seed some "interesting" inputs
15+
f.Add([]byte(""))
16+
f.Add([]byte("\r"))
17+
f.Add([]byte("\n"))
18+
f.Add([]byte("\t"))
19+
f.Add([]byte("\r \n \t"))
20+
f.Add([]byte("3 / 3\n"))
21+
f.Add([]byte("3 - -3\r\n"))
22+
f.Add([]byte("3 / 0"))
23+
f.Add([]byte(nil))
24+
25+
// Run the fuzzer
26+
f.Fuzz(func(t *testing.T, input []byte) {
27+
28+
// Create
29+
cal := New()
30+
31+
// Parser
32+
cal.Load(string(input))
33+
34+
// Evaluate
35+
cal.Run()
36+
})
37+
}

cmd_torrent.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/anacrolix/log"
109
"github.com/anacrolix/torrent"
1110
"github.com/dustin/go-humanize"
1211
"github.com/skx/subcommands"
@@ -74,7 +73,7 @@ func (s *torrentCommand) Execute(args []string) int {
7473
// Ensure we have only a single argument.
7574
//
7675
if len(args) != 1 {
77-
fmt.Printf("You must specify a single magnet link")
76+
fmt.Printf("You must specify a magnet link to download\n")
7877
return 1
7978
}
8079

@@ -90,7 +89,6 @@ func (s *torrentCommand) Execute(args []string) int {
9089
// Create the default client-configuration.
9190
//
9291
clientConfig := torrent.NewDefaultClientConfig()
93-
clientConfig.Logger = log.Discard
9492

9593
//
9694
// Create the client.

cmd_version.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
//go:build !go1.18
2+
// +build !go1.18
3+
14
package main
25

3-
//go:generate echo Hello, Go Generate!
46
import (
57
"fmt"
6-
"runtime/debug"
7-
"strings"
88

99
"github.com/skx/subcommands"
1010
)
1111

1212
var (
13-
versionString = "unreleased"
13+
version = "unreleased"
1414
)
1515

1616
// Structure for our options and state.
@@ -22,34 +22,18 @@ type versionCommand struct {
2222

2323
// Info returns the name of this subcommand.
2424
func (t *versionCommand) Info() (string, string) {
25-
return "version", `Show the version of the binary.
25+
return "version", `Show the version of this binary.
2626
2727
Details:
2828
29-
This reports upon the version of the sysbox application.
30-
31-
Usage:
32-
33-
$ sysbox version
34-
29+
This reports upon the version of the application.
3530
`
3631
}
3732

3833
// Execute is invoked if the user specifies `version` as the subcommand.
3934
func (t *versionCommand) Execute(args []string) int {
4035

41-
fmt.Printf("%s\n", versionString)
42-
43-
// Show VCS information
44-
info, ok := debug.ReadBuildInfo()
45-
46-
if ok {
47-
for _, settings := range info.Settings {
48-
if strings.Contains(settings.Key, "vcs") {
49-
fmt.Printf("%s: %s\n", settings.Key, settings.Value)
50-
}
51-
}
52-
}
36+
fmt.Printf("%s\n", version)
5337

5438
return 0
5539
}

cmd_version_18.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"runtime/debug"
9+
"strings"
10+
11+
"github.com/skx/subcommands"
12+
)
13+
14+
var (
15+
version = "unreleased"
16+
)
17+
18+
// Structure for our options and state.
19+
type versionCommand struct {
20+
21+
// We embed the NoFlags option, because we accept no command-line flags.
22+
subcommands.NoFlags
23+
}
24+
25+
// Info returns the name of this subcommand.
26+
func (t *versionCommand) Info() (string, string) {
27+
return "version", `Show the version of this binary.
28+
29+
Details:
30+
31+
This reports upon the version of the application.
32+
`
33+
}
34+
35+
// Execute is invoked if the user specifies `version` as the subcommand.
36+
func (t *versionCommand) Execute(args []string) int {
37+
38+
fmt.Printf("%s\n", version)
39+
40+
info, ok := debug.ReadBuildInfo()
41+
42+
if ok {
43+
for _, settings := range info.Settings {
44+
if strings.Contains(settings.Key, "vcs") {
45+
fmt.Printf("%s: %s\n", settings.Key, settings.Value)
46+
}
47+
}
48+
}
49+
50+
return 0
51+
}

go.mod

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,44 @@ module github.com/skx/sysbox
33
go 1.16
44

55
require (
6-
github.com/anacrolix/log v0.9.0 // indirect
7-
github.com/anacrolix/torrent v1.33.0 // indirect
8-
github.com/armon/go-metrics v0.3.9 // indirect
6+
github.com/anacrolix/dht/v2 v2.17.0 // indirect
7+
github.com/anacrolix/envpprof v1.2.1 // indirect
8+
github.com/anacrolix/log v0.13.1 // indirect
9+
github.com/anacrolix/missinggo/v2 v2.6.0 // indirect
10+
github.com/anacrolix/multiless v0.3.0 // indirect
11+
github.com/anacrolix/torrent v1.41.1-0.20220309095723-02b6ee995497
12+
github.com/armon/go-metrics v0.3.10 // indirect
13+
github.com/bits-and-blooms/bitset v1.2.2 // indirect
914
github.com/creack/pty v1.1.17
10-
github.com/dustin/go-humanize v1.0.0 // indirect
15+
github.com/dustin/go-humanize v1.0.0
16+
github.com/edsrzf/mmap-go v1.1.0 // indirect
1117
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
12-
github.com/google/btree v1.0.1 // indirect
1318
github.com/google/goexpect v0.0.0-20210430020637-ab937bf7fd6f
1419
github.com/google/goterm v0.0.0-20200907032337-555d40f16ae2 // indirect
20+
github.com/gorilla/websocket v1.5.0 // indirect
1521
github.com/hashicorp/errwrap v1.1.0 // indirect
1622
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
1723
github.com/hashicorp/go-msgpack v1.1.5 // indirect
1824
github.com/hashicorp/go-multierror v1.1.1 // indirect
1925
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
20-
github.com/hashicorp/go-uuid v1.0.1 // indirect
21-
github.com/hashicorp/golang-lru v0.5.4 // indirect
22-
github.com/hashicorp/memberlist v0.2.4
23-
github.com/miekg/dns v1.1.43 // indirect
26+
github.com/hashicorp/memberlist v0.3.1
27+
github.com/mattn/go-isatty v0.0.14 // indirect
28+
github.com/miekg/dns v1.1.47 // indirect
2429
github.com/nightlyone/lockfile v1.0.0
25-
github.com/peterh/liner v1.2.1
26-
github.com/rivo/tview v0.0.0-20211001102648-5508f4b00266
30+
github.com/peterh/liner v1.2.2
31+
github.com/pion/webrtc/v3 v3.1.26 // indirect
32+
github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8
33+
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect
2734
github.com/skx/subcommands v0.9.2
28-
github.com/stretchr/testify v1.7.0 // indirect
29-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
30-
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f
31-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
32-
golang.org/x/sys v0.0.0-20211015200801-69063c4bb744 // indirect
35+
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
36+
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
37+
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
3338
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
34-
golang.org/x/text v0.3.7 // indirect
35-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
36-
google.golang.org/genproto v0.0.0-20211016002631-37fc39342514 // indirect
37-
google.golang.org/grpc v1.41.0 // indirect
38-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
39+
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
40+
golang.org/x/tools v0.1.10 // indirect
41+
google.golang.org/genproto v0.0.0-20220322021311-435b647f9ef2 // indirect
42+
google.golang.org/protobuf v1.28.0 // indirect
3943
gopkg.in/yaml.v2 v2.4.0
44+
modernc.org/sqlite v1.15.3 // indirect
45+
zombiezen.com/go/sqlite v0.9.2 // indirect
4046
)

0 commit comments

Comments
 (0)