Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 905810f

Browse files
committed
add fuzzing
Signed-off-by: John O'Loughlin <[email protected]>
1 parent a87e6ff commit 905810f

File tree

9 files changed

+214
-110
lines changed

9 files changed

+214
-110
lines changed

Diff for: .github/workflows/fuzz.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: fuzz-tests
2+
on:
3+
schedule:
4+
- cron: "37 4 * * 0"
5+
pull_request:
6+
paths:
7+
- '**fuzz.yml'
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
fuzz-tests:
14+
name: fuzz-tests
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Set up Go
18+
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
19+
with:
20+
go-version: 1.20.1
21+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
22+
- name: make fuzz
23+
run: make fuzz

Diff for: Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ unit-test:
5454
@$(IMAGE_BUILDER) cp userspacecni-unittest:/root/userspace-cni-network-plugin/cnivpp ./
5555
@$(IMAGE_BUILDER) exec userspacecni-unittest bash -c "go test ./cnivpp/ -v -cover"
5656
@$(IMAGE_BUILDER) rm -f userspacecni-unittest
57-
57+
fuzz:
58+
@cd ./cnivpp/test; go test -fuzz=FuzzLoadNetConf -v -timeout 120s

Diff for: cnivpp/cnivpp_test.go

+9-54
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,17 @@ import (
88
"path/filepath"
99
"testing"
1010

11-
"github.com/containernetworking/cni/pkg/skel"
1211
current "github.com/containernetworking/cni/pkg/types/100"
13-
"github.com/google/uuid"
1412
"github.com/intel/userspace-cni-network-plugin/pkg/types"
13+
"github.com/intel/userspace-cni-network-plugin/userspace/testdata"
1514
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/require"
17-
v1 "k8s.io/api/core/v1"
18-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19-
apitypes "k8s.io/apimachinery/pkg/types"
2016
"k8s.io/client-go/kubernetes/fake"
2117
)
2218

23-
func GetTestPod(sharedDir string) *v1.Pod {
24-
id, _ := uuid.NewUUID()
25-
pod := &v1.Pod{
26-
TypeMeta: metav1.TypeMeta{
27-
Kind: "Pod",
28-
APIVersion: "v1",
29-
},
30-
ObjectMeta: metav1.ObjectMeta{
31-
UID: apitypes.UID(id.String()),
32-
Name: fmt.Sprintf("pod-%v", id[:8]),
33-
Namespace: fmt.Sprintf("namespace-%v", id[:8]),
34-
},
35-
}
36-
if sharedDir != "" {
37-
pod.Spec.Volumes = append(pod.Spec.Volumes,
38-
v1.Volume{
39-
Name: "shared-dir",
40-
VolumeSource: v1.VolumeSource{
41-
HostPath: &v1.HostPathVolumeSource{
42-
Path: sharedDir,
43-
},
44-
},
45-
})
46-
pod.Spec.Containers = append(pod.Spec.Containers,
47-
v1.Container{
48-
Name: "container",
49-
VolumeMounts: []v1.VolumeMount{{Name: "shared-dir", MountPath: sharedDir}},
50-
})
51-
}
52-
return pod
53-
}
54-
55-
func GetTestArgs() *skel.CmdArgs {
56-
id, _ := uuid.NewUUID()
57-
return &skel.CmdArgs{
58-
ContainerID: id.String(),
59-
IfName: fmt.Sprintf("eth%v", int(id[7])),
60-
StdinData: []byte("{}"),
61-
}
62-
}
63-
6419
func TestGetMemifSocketfileName(t *testing.T) {
6520
t.Run("get Memif Socker File Name", func(t *testing.T) {
66-
args := GetTestArgs()
21+
args := testdata.GetTestArgs()
6722

6823
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
6924
require.NoError(t, dirErr, "Can't create temporary directory")
@@ -83,14 +38,14 @@ func TestGetMemifSocketfileName(t *testing.T) {
8338
func TestAddOnContainer(t *testing.T) {
8439
t.Run("save container data to file", func(t *testing.T) {
8540
var result *current.Result
86-
args := GetTestArgs()
41+
args := testdata.GetTestArgs()
8742
cniVpp := CniVpp{}
8843

8944
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
9045
require.NoError(t, dirErr, "Can't create temporary directory")
9146
defer os.RemoveAll(sharedDir)
9247

93-
pod := GetTestPod(sharedDir)
48+
pod := testdata.GetTestPod(sharedDir)
9449
resPod, resErr := cniVpp.AddOnContainer(&types.NetConf{}, args, nil, sharedDir, pod, result)
9550
assert.NoError(t, resErr, "Unexpected error")
9651
assert.Equal(t, pod, resPod, "Unexpected change of pod data")
@@ -101,7 +56,7 @@ func TestAddOnContainer(t *testing.T) {
10156

10257
func TestDelOnContainer(t *testing.T) {
10358
t.Run("remove container configuration", func(t *testing.T) {
104-
args := GetTestArgs()
59+
args := testdata.GetTestArgs()
10560
cniVpp := CniVpp{}
10661

10762
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
@@ -220,13 +175,13 @@ func TestAddOnHost(t *testing.T) {
220175
for _, tc := range testCases {
221176
t.Run(tc.name, func(t *testing.T) {
222177
var result *current.Result
223-
args := GetTestArgs()
178+
args := testdata.GetTestArgs()
224179

225180
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cnivpp-")
226181
require.NoError(t, dirErr, "Can't create temporary directory")
227182
defer os.RemoveAll(sharedDir)
228183

229-
pod := GetTestPod(sharedDir)
184+
pod := testdata.GetTestPod(sharedDir)
230185
kubeClient := fake.NewSimpleClientset(pod)
231186

232187
err := cniVpp.AddOnHost(tc.netConf, args, kubeClient, sharedDir, result)
@@ -294,14 +249,14 @@ func TestDelFromHost(t *testing.T) {
294249
}
295250
for _, tc := range testCases {
296251
t.Run(tc.name, func(t *testing.T) {
297-
args := GetTestArgs()
252+
args := testdata.GetTestArgs()
298253
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cnivpp-")
299254
require.NoError(t, dirErr, "Can't create temporary directory")
300255
defer os.RemoveAll(sharedDir)
301256

302257
var result *current.Result
303258

304-
pod := GetTestPod(sharedDir)
259+
pod := testdata.GetTestPod(sharedDir)
305260
kubeClient := fake.NewSimpleClientset(pod)
306261

307262
_ = cniVpp.AddOnHost(tc.netConf, args, kubeClient, sharedDir, result)

Diff for: cnivpp/test/cni_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright(c) 2022 Intel Corporation.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package fuzz
17+
18+
import (
19+
"strings"
20+
"testing"
21+
22+
"github.com/intel/userspace-cni-network-plugin/userspace/cni"
23+
)
24+
25+
func FuzzLoadNetConf(f *testing.F) {
26+
27+
seed := `
28+
{
29+
"cniVersion": "0.3.1",
30+
"type": "userspace",
31+
"name": "userspace-ovs-net-1",
32+
"kubeconfig": "/etc/cni/net.d/multus.d/multus.kubeconfig",
33+
"logFile": "/var/log/userspace-ovs-net-1-cni.log",
34+
"logLevel": "debug",
35+
"host": {
36+
"engine": "ovs-dpdk",
37+
"iftype": "vhostuser",
38+
"netType": "bridge",
39+
"vhost": {
40+
"mode": "client"
41+
},
42+
"bridge": {
43+
"bridgeName": "br-4"
44+
}
45+
},
46+
"container": {
47+
"engine": "ovs-dpdk",
48+
"iftype": "vhostuser",
49+
"netType": "interface",
50+
"vhost": {
51+
"mode": "server"
52+
}
53+
},
54+
"ipam": {
55+
"type": "host-local",
56+
"subnet": "10.56.217.0/24",
57+
"rangeStart": "10.56.217.131",
58+
"rangeEnd": "10.56.217.190",
59+
"routes": [
60+
{
61+
"dst": "0.0.0.0/0"
62+
}
63+
],
64+
"gateway": "10.56.217.1"
65+
}
66+
}
67+
`
68+
69+
f.Add([]byte(seed))
70+
71+
f.Fuzz(func(t *testing.T, fcfg []byte) {
72+
_, err := cni.LoadNetConf(fcfg)
73+
if err != nil {
74+
if strings.Contains(err.Error(), "failed to load netconf:") {
75+
return
76+
} else {
77+
t.Errorf("Error: %s, for input %s", err.Error(), string(fcfg))
78+
}
79+
80+
} else {
81+
return
82+
}
83+
})
84+
}

Diff for: go.mod

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/containernetworking/cni v1.1.2
77
github.com/containernetworking/plugins v1.4.0
88
github.com/go-logfmt/logfmt v0.6.0
9-
github.com/google/uuid v1.5.0
109
github.com/pkg/errors v0.9.1
1110
github.com/sirupsen/logrus v1.9.3
1211
github.com/stretchr/testify v1.8.4
@@ -32,6 +31,7 @@ require (
3231
github.com/golang/protobuf v1.5.3 // indirect
3332
github.com/google/gnostic-models v0.6.8 // indirect
3433
github.com/google/gofuzz v1.2.0 // indirect
34+
github.com/google/uuid v1.5.0 // indirect
3535
github.com/imdario/mergo v0.3.6 // indirect
3636
github.com/josharian/intern v1.0.0 // indirect
3737
github.com/json-iterator/go v1.1.12 // indirect
@@ -44,11 +44,12 @@ require (
4444
github.com/safchain/ethtool v0.3.0 // indirect
4545
github.com/spf13/pflag v1.0.5 // indirect
4646
github.com/vishvananda/netns v0.0.4 // indirect
47-
golang.org/x/net v0.17.0 // indirect
47+
golang.org/x/net v0.19.0 // indirect
4848
golang.org/x/oauth2 v0.8.0 // indirect
49-
golang.org/x/term v0.13.0 // indirect
50-
golang.org/x/text v0.13.0 // indirect
49+
golang.org/x/term v0.15.0 // indirect
50+
golang.org/x/text v0.14.0 // indirect
5151
golang.org/x/time v0.3.0 // indirect
52+
golang.org/x/tools v0.16.1 // indirect
5253
google.golang.org/appengine v1.6.7 // indirect
5354
google.golang.org/protobuf v1.31.0 // indirect
5455
gopkg.in/inf.v0 v0.9.1 // indirect

Diff for: go.sum

+8-7
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
142142
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
143143
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
144144
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
145-
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
146-
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
145+
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
146+
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
147147
golang.org/x/oauth2 v0.8.0 h1:6dkIjl3j3LtZ/O3sTgZTMsLKSftL/B8Zgq4huOIIUu8=
148148
golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE=
149149
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -169,22 +169,23 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
169169
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
170170
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
171171
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
172-
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
173-
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
172+
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
173+
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
174174
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
175175
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
176176
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
177177
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
178-
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
179-
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
178+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
179+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
180180
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
181181
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
182182
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
183183
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
184184
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
185185
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
186186
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
187-
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
187+
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
188+
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
188189
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
189190
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
190191
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)