11package vf
22
33import (
4- "bytes"
5- "fmt"
6- "math/rand"
74 "net"
85 "os"
96 "path/filepath"
@@ -14,16 +11,33 @@ import (
1411 "github.com/stretchr/testify/require"
1512)
1613
17- func testConnectUnixgram (t * testing.T ) error {
18- unixSocketPath := filepath .Join ("/tmp" , fmt .Sprintf ("vnet-test-%x.sock" , rand .Int31n (0xffff ))) //#nosec G404 -- no need for crypto/rand here
14+ func sourceSocketPath (t * testing.T , sourcePathLen int ) (string , func ()) {
15+ // the 't.sock' name is chosen to be shorter than what
16+ // localUnixSocketPath will generate so that the source socket path
17+ // will not exceed the 104 byte limit while the destination socket path
18+ // will, and will trigger an error
19+ const sourceSocketName = "t.sock"
20+ tmpDir := "/tmp"
21+ subDirLen := sourcePathLen - len (tmpDir ) - 2 * len ("/" ) - len (sourceSocketName ) - 1
22+ subDir := filepath .Join (tmpDir , strings .Repeat ("a" , subDirLen ))
23+ err := os .Mkdir (subDir , 0700 )
24+ require .NoError (t , err )
25+ unixSocketPath := filepath .Join (subDir , sourceSocketName )
26+ require .Equal (t , len (unixSocketPath ), sourcePathLen - 1 )
27+ return unixSocketPath , func () { os .RemoveAll (subDir ) }
28+
29+ }
30+ func testConnectUnixgram (t * testing.T , sourcePathLen int ) error {
31+ unixSocketPath , closer := sourceSocketPath (t , sourcePathLen )
32+ defer closer ()
33+
1934 addr , err := net .ResolveUnixAddr ("unixgram" , unixSocketPath )
2035 require .NoError (t , err )
2136
2237 l , err := net .ListenUnixgram ("unixgram" , addr )
2338 require .NoError (t , err )
2439
2540 defer l .Close ()
26- defer os .Remove (unixSocketPath )
2741
2842 dev := & VirtioNet {
2943 & config.VirtioNet {
@@ -37,46 +51,31 @@ func testConnectUnixgram(t *testing.T) error {
3751
3852func TestConnectUnixPath (t * testing.T ) {
3953 t .Run ("Successful connection - no error" , func (t * testing.T ) {
40- err := testConnectUnixgram (t )
54+ // 50 is an arbitrary number, small enough for the 104 bytes limit not to be exceeded
55+ err := testConnectUnixgram (t , 50 )
4156 require .NoError (t , err )
4257 })
4358
4459 t .Run ("Failed connection - End socket longer than 104 bytes" , func (t * testing.T ) {
45- // Retrieve HOME env variable (used by the os.UserHomeDir)
46- origUserHome := os .Getenv ("HOME" )
47- defer func () {
48- os .Setenv ("HOME" , origUserHome )
49- }()
50-
51- // Create a string of 100 bytes to update the user home to be sure to create a socket path > 104 bytes
52- b := bytes .Repeat ([]byte ("a" ), 100 )
53- subDir := string (b )
54-
55- // Update HOME env so os.UserHomeDir returns the update path with subfolder
56- updatedUserHome := filepath .Join (origUserHome , subDir )
57- os .Setenv ("HOME" , updatedUserHome )
58- defer os .RemoveAll (updatedUserHome )
59-
60- err := testConnectUnixgram (t )
60+ err := testConnectUnixgram (t , maxUnixgramPathLen )
6161 // It should return an error
6262 require .Error (t , err )
63- require .ErrorContains (t , err , "invalid argument " )
63+ require .ErrorContains (t , err , "is too long " )
6464 })
6565}
6666
6767func TestLocalUnixSocketPath (t * testing.T ) {
6868 t .Run ("Success case - Creates temporary socket path" , func (t * testing.T ) {
6969 // Retrieve HOME env variable (used by the os.UserHomeDir)
70- userHome := os . Getenv ( "HOME" )
70+ socketDir := t . TempDir ( )
7171
72- path , err := localUnixSocketPath ()
72+ path , err := localUnixSocketPath (socketDir )
7373
7474 // Assert successful execution
7575 require .NoError (t , err )
7676
7777 // Check if path starts with the expected prefix
78- expectedPrefix := filepath .Join (userHome , "Library" , "Application Support" , "vfkit" )
79- require .Truef (t , strings .HasPrefix (path , expectedPrefix ), "Path doesn't start with expected prefix: %v" , path )
78+ require .Truef (t , strings .HasPrefix (path , socketDir ), "Path doesn't start with expected prefix: %v" , path )
8079
8180 // Check if path ends with a socket extension
8281 require .Equalf (t , ".sock" , filepath .Ext (path ), "Path doesn't end with .sock extension: %v, ext is %v" , path , filepath .Ext (path ))
0 commit comments