gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/dhclient/iscsiuri_test.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package dhclient 6 7 import ( 8 "net" 9 "reflect" 10 "testing" 11 ) 12 13 func TestParseURI(t *testing.T) { 14 for _, tt := range []struct { 15 uri string 16 target *net.TCPAddr 17 volume string 18 want string 19 }{ 20 { 21 uri: "iscsi:192.168.1.1:::1:iqn.com.oracle:boot", 22 target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260}, 23 volume: "iqn.com.oracle:boot", 24 }, 25 { 26 uri: "iscsi:@192.168.1.1::3260::iqn.com.oracle:boot", 27 target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260}, 28 volume: "iqn.com.oracle:boot", 29 }, 30 { 31 uri: "iscsi:[fe80::1]::3260::iqn.com.google:esxi-boot-image", 32 target: &net.TCPAddr{IP: net.ParseIP("fe80::1"), Port: 3260}, 33 volume: "iqn.com.google:esxi-boot-image", 34 }, 35 { 36 uri: "iscsi:[fe80::1]::3260::iqn.com.google:esxi-boot-]:image", 37 target: &net.TCPAddr{IP: net.ParseIP("fe80::1"), Port: 3260}, 38 volume: "iqn.com.google:esxi-boot-]:image", 39 }, 40 { 41 uri: "iscsi:192.168.1.1::3260::iqn.com.google:[fe80::1]", 42 target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260}, 43 volume: "iqn.com.google:[fe80::1]", 44 }, 45 { 46 uri: "iscsi:[fe80::1]::3260::iqn.com.google:[foobar]", 47 target: &net.TCPAddr{IP: net.ParseIP("fe80::1"), Port: 3260}, 48 volume: "iqn.com.google:[foobar]", 49 }, 50 { 51 uri: "iscsi:192.168.1.1::3260::iqn.com.google:esxi-boot-image", 52 target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260}, 53 volume: "iqn.com.google:esxi-boot-image", 54 }, 55 { 56 uri: "iscsi:192.168.1.1::3000::iqn.com.google:esxi-boot-image", 57 target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3000}, 58 volume: "iqn.com.google:esxi-boot-image", 59 }, 60 { 61 uri: "iscsi:192.168.1.1::::iqn.com.google:esxi-boot-image", 62 target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260}, 63 volume: "iqn.com.google:esxi-boot-image", 64 }, 65 { 66 uri: "iscsi:192.168.1.1::::iqn.com.google::::", 67 target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260}, 68 volume: "iqn.com.google::::", 69 }, 70 { 71 uri: "iscsi:192.168.1.1::::", 72 want: "iSCSI URI \"iscsi:192.168.1.1::::\" is missing a volume name", 73 }, 74 { 75 uri: "iscsi:192.168.1.1:::", 76 want: "iSCSI URI \"iscsi:192.168.1.1:::\" failed to parse: fields missing", 77 }, 78 { 79 uri: "iscs:192.168.1.1::::", 80 want: "iSCSI URI \"iscs:192.168.1.1::::\" is missing iscsi scheme prefix, have iscs", 81 }, 82 { 83 uri: "", 84 want: "iSCSI URI \"\" failed to parse: fields missing", 85 }, 86 { 87 uri: "iscsi:192.168.1.1::foobar::volume", 88 want: "iSCSI URI \"iscsi:192.168.1.1::foobar::volume\" has invalid port: strconv.Atoi: parsing \"foobar\": invalid syntax", 89 }, 90 { 91 uri: "iscsi:[fe80::1::::", 92 want: "iSCSI URI \"iscsi:[fe80::1::::\" failed to parse: invalid IPv6 address", 93 }, 94 } { 95 gtarget, gvolume, got := ParseISCSIURI(tt.uri) 96 if (got != nil && got.Error() != tt.want) || (got == nil && len(tt.want) > 0) { 97 t.Errorf("parseISCSIURI(%s) = %v, want %v", tt.uri, got, tt.want) 98 } 99 if gvolume != tt.volume { 100 t.Errorf("parseISCSIURI(%s) = volume %s, want %s", tt.uri, gvolume, tt.volume) 101 } 102 if !reflect.DeepEqual(gtarget, tt.target) { 103 t.Errorf("parseISCSIURI(%s) = target %s, want %s", tt.uri, gtarget, tt.target) 104 } 105 } 106 }