Skip to content

Commit

Permalink
fixed issue #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Oct 14, 2022
1 parent b6db185 commit 117c12a
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 14 deletions.
19 changes: 15 additions & 4 deletions bootloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package vz
import "C"
import (
"fmt"
"os"
"runtime"
)

Expand Down Expand Up @@ -44,26 +45,31 @@ func (b *LinuxBootLoader) String() string {
)
}

type LinuxBootLoaderOption func(b *LinuxBootLoader)
type LinuxBootLoaderOption func(b *LinuxBootLoader) error

// WithCommandLine sets the command-line parameters.
// see: https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
func WithCommandLine(cmdLine string) LinuxBootLoaderOption {
return func(b *LinuxBootLoader) {
return func(b *LinuxBootLoader) error {
b.cmdLine = cmdLine
cs := charWithGoString(cmdLine)
defer cs.Free()
C.setCommandLineVZLinuxBootLoader(b.Ptr(), cs.CString())
return nil
}
}

// WithInitrd sets the optional initial RAM disk.
func WithInitrd(initrdPath string) LinuxBootLoaderOption {
return func(b *LinuxBootLoader) {
return func(b *LinuxBootLoader) error {
if _, err := os.Stat(initrdPath); err != nil {
return fmt.Errorf("invalid initial RAM disk path: %w", err)
}
b.initrdPath = initrdPath
cs := charWithGoString(initrdPath)
defer cs.Free()
C.setInitialRamdiskURLVZLinuxBootLoader(b.Ptr(), cs.CString())
return nil
}
}

Expand All @@ -75,6 +81,9 @@ func NewLinuxBootLoader(vmlinuz string, opts ...LinuxBootLoaderOption) (*LinuxBo
if macosMajorVersionLessThan(11) {
return nil, ErrUnsupportedOSVersion
}
if _, err := os.Stat(vmlinuz); err != nil {
return nil, fmt.Errorf("invalid linux kernel path: %w", err)
}

vmlinuzPath := charWithGoString(vmlinuz)
defer vmlinuzPath.Free()
Expand All @@ -90,7 +99,9 @@ func NewLinuxBootLoader(vmlinuz string, opts ...LinuxBootLoaderOption) (*LinuxBo
self.Release()
})
for _, opt := range opts {
opt(bootLoader)
if err := opt(bootLoader); err != nil {
return nil, err
}
}
return bootLoader, nil
}
9 changes: 6 additions & 3 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,19 @@ type FileSerialPortAttachment struct {
// NewFileSerialPortAttachment initialize the FileSerialPortAttachment from a path of a file.
// If error is not nil, used to report errors if intialization fails.
//
// - path of the file for the attachment on the local file system.
// - shouldAppend True if the file should be opened in append mode, false otherwise.
// When a file is opened in append mode, writing to that file will append to the end of it.
// - path of the file for the attachment on the local file system.
// - shouldAppend True if the file should be opened in append mode, false otherwise.
// When a file is opened in append mode, writing to that file will append to the end of it.
//
// This is only supported on macOS 11 and newer, ErrUnsupportedOSVersion will
// be returned on older versions.
func NewFileSerialPortAttachment(path string, shouldAppend bool) (*FileSerialPortAttachment, error) {
if macosMajorVersionLessThan(11) {
return nil, ErrUnsupportedOSVersion
}
if _, err := os.Stat(path); err != nil {
return nil, err
}

cpath := charWithGoString(path)
defer cpath.Free()
Expand Down
34 changes: 34 additions & 0 deletions issues_arm64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build darwin && arm64
// +build darwin,arm64

package vz

import (
"errors"
"os"
"testing"
)

func TestIssue43Arm64(t *testing.T) {
const doesNotExists = "/non/existing/path"
t.Run("does not throw NSInvalidArgumentException", func(t *testing.T) {
cases := map[string]func() error{
// This is also fixed issue #71
"NewMacAuxiliaryStorage": func() error {
_, err := NewMacAuxiliaryStorage(doesNotExists)
return err
},
}
for name, run := range cases {
t.Run(name, func(t *testing.T) {
err := run()
if err == nil {
t.Fatal("expected returns error")
}
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("want underlying error %q but got %q", os.ErrNotExist, err)
}
})
}
})
}
69 changes: 64 additions & 5 deletions issues_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package vz

import (
"errors"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -55,9 +57,66 @@ func TestIssue50(t *testing.T) {
})
}

func TestIssue71(t *testing.T) {
_, err := NewFileSerialPortAttachment("/non/existing/path", false)
if err == nil {
t.Error("NewFileSerialPortAttachment should have returned an error")
}
func TestIssue43(t *testing.T) {
const doesNotExists = "/non/existing/path"
t.Run("does not throw NSInvalidArgumentException", func(t *testing.T) {
t.Run("NewLinuxBootLoader", func(t *testing.T) {
_, err := NewLinuxBootLoader(doesNotExists)
if err == nil {
t.Fatal("expected returns error")
}
if !strings.HasPrefix(err.Error(), "invalid linux kernel") {
t.Error(err)
}
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("want underlying error %q but got %q", os.ErrNotExist, err)
}

f, err := os.CreateTemp("", "vmlinuz")
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}

_, err = NewLinuxBootLoader(f.Name(), WithInitrd(doesNotExists))
if err == nil {
t.Fatal("expected returns error")
}
if !strings.HasPrefix(err.Error(), "invalid initial RAM disk") {
t.Error(err)
}
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("want underlying error %q but got %q", os.ErrNotExist, err)
}
})

cases := map[string]func() error{
// This is also fixed issue #71
"NewFileSerialPortAttachment": func() error {
_, err := NewFileSerialPortAttachment(doesNotExists, false)
return err
},
"NewSharedDirectory": func() error {
_, err := NewFileSerialPortAttachment(doesNotExists, false)
return err
},
"NewDiskImageStorageDeviceAttachment": func() error {
_, err := NewDiskImageStorageDeviceAttachment(doesNotExists, false)
return err
},
}
for name, run := range cases {
t.Run(name, func(t *testing.T) {
err := run()
if err == nil {
t.Fatal("expected returns error")
}
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("want underlying error %q but got %q", os.ErrNotExist, err)
}
})
}
})
}
3 changes: 3 additions & 0 deletions osversion_arm64_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build darwin && arm64
// +build darwin,arm64

package vz

import (
Expand Down
11 changes: 10 additions & 1 deletion shared_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package vz
# include "virtualization.h"
*/
import "C"
import "runtime"
import (
"os"
"runtime"
)

// DirectorySharingDeviceConfiguration for a directory sharing device configuration.
type DirectorySharingDeviceConfiguration interface {
Expand Down Expand Up @@ -69,6 +72,10 @@ func NewSharedDirectory(dirPath string, readOnly bool) (*SharedDirectory, error)
if macosMajorVersionLessThan(12) {
return nil, ErrUnsupportedOSVersion
}
if _, err := os.Stat(dirPath); err != nil {
return nil, err
}

dirPathChar := charWithGoString(dirPath)
defer dirPathChar.Free()
sd := &SharedDirectory{
Expand Down Expand Up @@ -125,6 +132,8 @@ type MultipleDirectoryShare struct {
*baseDirectoryShare
}

var _ DirectoryShare = (*MultipleDirectoryShare)(nil)

// NewMultipleDirectoryShare creates a new multiple directories share.
func NewMultipleDirectoryShare(shares map[string]*SharedDirectory) (*MultipleDirectoryShare, error) {
if macosMajorVersionLessThan(12) {
Expand Down
8 changes: 7 additions & 1 deletion storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package vz
# include "virtualization.h"
*/
import "C"
import "runtime"
import (
"os"
"runtime"
)

type baseStorageDeviceAttachment struct{}

Expand Down Expand Up @@ -47,6 +50,9 @@ func NewDiskImageStorageDeviceAttachment(diskPath string, readOnly bool) (*DiskI
if macosMajorVersionLessThan(11) {
return nil, ErrUnsupportedOSVersion
}
if _, err := os.Stat(diskPath); err != nil {
return nil, err
}

nserr := newNSErrorAsNil()
nserrPtr := nserr.Ptr()
Expand Down
10 changes: 10 additions & 0 deletions virtualization_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,17 @@ func NewMacAuxiliaryStorage(storagePath string, opts ...NewMacAuxiliaryStorageOp
if macosMajorVersionLessThan(12) {
return nil, ErrUnsupportedOSVersion
}
if _, err := os.Stat(storagePath); err != nil {
return nil, err
}

storage := &MacAuxiliaryStorage{storagePath: storagePath}
for _, opt := range opts {
if err := opt(storage); err != nil {
return nil, err
}
}

if storage.pointer.ptr == nil {
cpath := charWithGoString(storagePath)
defer cpath.Free()
Expand Down Expand Up @@ -423,6 +427,9 @@ func LoadMacOSRestoreImageFromPath(imagePath string) (retImage *MacOSRestoreImag
if macosMajorVersionLessThan(12) {
return nil, ErrUnsupportedOSVersion
}
if _, err := os.Stat(imagePath); err != nil {
return nil, err
}

waitCh := make(chan struct{})
handler := macOSRestoreImageHandler(func(restoreImage *MacOSRestoreImage, err error) {
Expand Down Expand Up @@ -462,6 +469,9 @@ func NewMacOSInstaller(vm *VirtualMachine, restoreImageIpsw string) (*MacOSInsta
if macosMajorVersionLessThan(12) {
return nil, ErrUnsupportedOSVersion
}
if _, err := os.Stat(restoreImageIpsw); err != nil {
return nil, err
}

cs := charWithGoString(restoreImageIpsw)
defer cs.Free()
Expand Down

0 comments on commit 117c12a

Please sign in to comment.