| // Copyright 2020 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| package virtual_device | |
| import ( | |
| "testing" | |
| "go.fuchsia.dev/fuchsia/tools/lib/productbundle" | |
| fvdpb "go.fuchsia.dev/fuchsia/tools/virtual_device/proto" | |
| ) | |
| func TestValidate(t *testing.T) { | |
| // An Image list shared by all test cases. This is not modified during testing. | |
| testProductBundle := &productbundle.ProductBundle{ | |
| BaseDir: "/base/dir", | |
| SystemA: []productbundle.SystemImage{ | |
| {Name: "qemu-kernel", Path: "/kernel", Type: "kernel"}, | |
| {Name: "storage-full", Path: "/fvm", Type: "blk"}, | |
| {Name: "zircon-a", Path: "/ramdisk", Type: "zbi"}, | |
| // Images for testing error cases. | |
| {Name: "no-path", Type: "kernel"}, | |
| {Name: "duplicate", Path: "/duplicate", Type: "kernel"}, | |
| {Name: "duplicate", Path: "/duplicate", Type: "kernel"}, | |
| }, | |
| } | |
| // Test cases. | |
| // | |
| // setupFVD is used to initialize the FVD to pass to Validate. It is called with a | |
| // known-good FVD as input, during test setup. | |
| tests := []struct { | |
| name string | |
| setupFVD func(*fvdpb.VirtualDevice) | |
| wantErr bool | |
| }{{ | |
| name: "ok", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { return }, | |
| }, { | |
| name: "missing kernel", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Kernel = "" }, | |
| wantErr: true, | |
| }, { | |
| name: "missing drive name", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Drive.Image = "" }, | |
| wantErr: true, | |
| }, { | |
| name: "missing MAC", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Hw.Mac = "" }, | |
| wantErr: true, | |
| }, { | |
| name: "missing initial ramdisk", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Initrd = "" }, | |
| wantErr: true, | |
| }, { | |
| name: "invalid ram", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Hw.Ram = "-1G" }, | |
| wantErr: true, | |
| }, { | |
| name: "invalid arch", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Hw.Arch = "MIPS" }, | |
| wantErr: true, | |
| }, { | |
| name: "invalid mac", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Hw.Mac = "0::12345" }, | |
| wantErr: true, | |
| }, { | |
| name: "image is missing path", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Kernel = "no-path" }, | |
| wantErr: true, | |
| }, { | |
| name: "image has non-unique name", | |
| setupFVD: func(fvd *fvdpb.VirtualDevice) { fvd.Kernel = "duplicate" }, | |
| wantErr: true, | |
| }} | |
| for _, tt := range tests { | |
| t.Run(tt.name, func(t *testing.T) { | |
| inputFVD := Default() | |
| tt.setupFVD(inputFVD) | |
| if err := Validate(inputFVD, testProductBundle, nil); err != nil != tt.wantErr { | |
| if tt.wantErr { | |
| t.Fatalf("wanted an error but got nil") | |
| } | |
| t.Fatalf("got error %v", err) | |
| } | |
| }) | |
| } | |
| // Nil cases. | |
| if err := Validate(nil, testProductBundle, nil); err == nil { | |
| t.Fatalf("Validate(nil, /*pb*/) wanted an error but got nil") | |
| } | |
| if err := Validate(Default(), nil, nil); err == nil { | |
| t.Fatalf("Validate(Default(), nil) wanted an error but got nil") | |
| } | |
| } | |
| func TestIsValidRAM(t *testing.T) { | |
| tests := []struct { | |
| ram string | |
| want bool | |
| }{ | |
| {"", false}, | |
| {"0", false}, | |
| {"1", false}, | |
| {"1Z", false}, | |
| {"b", false}, | |
| {"-22G", false}, | |
| {"0b", false}, | |
| {"0B", false}, | |
| {"1k", false}, | |
| {"1K", false}, | |
| {"16m", true}, | |
| {"16M", true}, | |
| {"256g", true}, | |
| {"256G", true}, | |
| } | |
| for _, tt := range tests { | |
| t.Run(tt.ram, func(t *testing.T) { | |
| if isValidRAM(tt.ram) != tt.want { | |
| t.Fatalf("isValidRAM(%q) got %v but wanted %v", tt.ram, !tt.want, tt.want) | |
| } | |
| }) | |
| } | |
| } | |
| func TestIsValidArch(t *testing.T) { | |
| tests := []struct { | |
| arch string | |
| want bool | |
| }{ | |
| {"", false}, | |
| {"MIPS", false}, | |
| {"x64", true}, | |
| {"arm64", true}, | |
| } | |
| for _, tt := range tests { | |
| t.Run(tt.arch, func(t *testing.T) { | |
| if isValidArch(tt.arch) != tt.want { | |
| t.Fatalf("isValidArch(%q) got %v but wanted %v", tt.arch, !tt.want, tt.want) | |
| } | |
| }) | |
| } | |
| } | |
| func TestIsValidMAC(t *testing.T) { | |
| tests := []struct { | |
| mac string | |
| want bool | |
| }{ | |
| {"00", false}, | |
| {"00:11", false}, | |
| {"00:11:22", false}, | |
| {"00:11:22:33", false}, | |
| {"00:11:22:33:44", false}, | |
| {"00:11:22:33:44:55:", false}, | |
| {":00:11:22:33:44:55:", false}, | |
| {"00:11:22:33:44:55:66", false}, | |
| {"00:11:22:33:44:55:66", false}, | |
| {"00:11:22:33:44:55", true}, | |
| {"7d:af:25:e3:dd:0b", true}, | |
| } | |
| for _, tt := range tests { | |
| t.Run(tt.mac, func(t *testing.T) { | |
| if isValidMAC(tt.mac) != tt.want { | |
| t.Fatalf("isValidMAC(%q) got %v but wanted %v", tt.mac, !tt.want, tt.want) | |
| } | |
| }) | |
| } | |
| } | |
| func TestParseRAMBytes(t *testing.T) { | |
| tests := []struct { | |
| ram string | |
| wantBytes float64 // float64 simplfies using math.Pow without casting to int. | |
| wantErr bool | |
| }{ | |
| {"5m", 5, false}, | |
| {"5M", 5, false}, | |
| {"5g", 1024 * 5, false}, | |
| {"5G", 1024 * 5, false}, | |
| {"5b", -1, true}, | |
| {"5B", -1, true}, | |
| {"5k", -1, true}, | |
| {"5K", -1, true}, | |
| {"", -1, true}, | |
| {"-5G", -1, true}, | |
| {"5z", -1, true}, | |
| } | |
| for _, tt := range tests { | |
| t.Run(tt.ram, func(t *testing.T) { | |
| gotBytes, err := parseRAMBytes(tt.ram) | |
| if err != nil != tt.wantErr { | |
| if tt.wantErr { | |
| t.Fatalf("parseRAMBytes(%q) wanted an error but got nil", tt.ram) | |
| } | |
| t.Fatalf("parseRAMBytes(%q) got error %v", tt.ram, err) | |
| } | |
| wantBytes := int(tt.wantBytes) | |
| if gotBytes != wantBytes { | |
| t.Fatalf("parseRAMBytes(%q) got %d but wanted %d", tt.ram, gotBytes, wantBytes) | |
| } | |
| }) | |
| } | |
| } |