Skip to content

Commit

Permalink
virtionet: testing connectUnixPath
Browse files Browse the repository at this point in the history
it adds tests for connectUnixPath and localUnixSocketPath func in virtionet.go
it mainly test the use case where one end of the unixgram socket is longer of 104 bytes resulting in a failure. On macOS, a path on the filesystem can have a max of 104 bytes length
  • Loading branch information
Luca Stocchi authored and Luca Stocchi committed Oct 2, 2024
1 parent 41bbe77 commit d52b77c
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions pkg/vf/virtionet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package vf

import (
"fmt"
"math/rand"
"net"
"os"
"path/filepath"
"strings"
"testing"

"github.com/crc-org/vfkit/pkg/config"
)

func testConnectUnixgram(t *testing.T) error {
unixSocketPath := filepath.Join("/tmp", fmt.Sprintf("vnet-test-%x.sock", rand.Int31n(0xffff)))

Check failure on line 16 in pkg/vf/virtionet_test.go

View workflow job for this annotation

GitHub Actions / lint

G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)
addr, err := net.ResolveUnixAddr("unixgram", unixSocketPath)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
l, err := net.ListenUnixgram("unixgram", addr)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

defer l.Close()
defer os.Remove(unixSocketPath)

dev := &VirtioNet{
&config.VirtioNet{
UnixSocketPath: unixSocketPath,
},
&net.UnixAddr{},
}

return dev.connectUnixPath()
}

func TestConnectUnixPath(t *testing.T) {
t.Run("Successful connection - no error", func(t *testing.T) {
err := testConnectUnixgram(t)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})

t.Run("Failed connection - End socket longer than 104 bytes", func(t *testing.T) {
// Retrieve HOME env variable (used by the os.UserHomeDir)
origUserHome := os.Getenv("HOME")
defer func() {
os.Setenv("HOME", origUserHome)
}()

// Create a string of 100 bytes to update the user home to be sure to create a socket path > 104 bytes
b := make([]byte, 100)
for i := range b {
b[i] = 'a'
}
subDir := string(b)

// Update HOME env so os.UserHomeDir returns the update path with subfolder
updatedUserHome := filepath.Join(origUserHome, subDir)
os.Setenv("HOME", updatedUserHome)

err := testConnectUnixgram(t)
// It should return an error
if err == nil {
t.Fatalf("Unexpected error: %v", err)
}

if !strings.Contains(err.Error(), "invalid argument") {
t.Fatalf("Unexpected error: %v", err)
}
})
}

func TestLocalUnixSocketPath(t *testing.T) {
t.Run("Success case - Creates temporary socket path", func(t *testing.T) {
// Retrieve HOME env variable (used by the os.UserHomeDir)
userHome := os.Getenv("HOME")

path, err := localUnixSocketPath()

// Assert successful execution
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

// Check if path starts with the expected prefix
expectedPrefix := filepath.Join(userHome, "Library", "Application Support", "vfkit")
if !strings.HasPrefix(path, expectedPrefix) {
t.Fatalf("Path doesn't start with expected prefix: %v", path)
}

// Check if path ends with a socket extension
if filepath.Ext(path) != ".sock" {
t.Fatalf("Path doesn't end with .sock extension: %v, ext is %v", path, filepath.Ext(path))
}
})
}

0 comments on commit d52b77c

Please sign in to comment.