uefi_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package uefi
  15. import (
  16. "reflect"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. const (
  21. TestPCOutput = `BootCurrent: 0007
  22. Timeout: 2 seconds
  23. BootOrder: 0003,0002,0001,0000,0004,0005,0007,0008,0009,000A
  24. Boot0000 Windows Boot Manager
  25. Boot0001 ubuntu
  26. Boot0002 ubuntu
  27. Boot0003 CentOS Linux
  28. Boot0004* Onboard NIC(IPV4)
  29. Boot0005 Onboard NIC(IPV6)
  30. Boot0007* PXE IPv4 Intel(R) Ethernet Server Adapter X520-2
  31. Boot0008 PXE IPv6 Intel(R) Ethernet Server Adapter X520-2
  32. Boot0009* PXE IPv4 Intel(R) Ethernet Server Adapter X520-2
  33. Boot000A PXE IPv6 Intel(R) Ethernet Server Adapter X520-2`
  34. TestQemuOutput = `BootCurrent: 0005
  35. Timeout: 0 seconds
  36. BootOrder: 0005,0009,0008,0007,0002,0003,0004,0006,0001,0000
  37. Boot0000* UiApp
  38. Boot0001* UEFI QEMU DVD-ROM QM00003
  39. Boot0002* UEFI Floppy
  40. Boot0003* UEFI Floppy 2
  41. Boot0004* UEFI QEMU HARDDISK QM00001
  42. Boot0005* UEFI PXEv4 (MAC:525400123456)
  43. Boot0006* EFI Internal Shell
  44. Boot0007* CentOS
  45. Boot0008* CentOS Linux
  46. Boot0009* ubuntu`
  47. )
  48. func Test_parseTimeout(t *testing.T) {
  49. tests := []struct {
  50. input string
  51. want int
  52. }{
  53. {
  54. input: "Timeout: 0 seconds",
  55. want: 0,
  56. },
  57. {
  58. input: "Timeout: 30 seconds",
  59. want: 30,
  60. },
  61. {
  62. input: "Timeout: seconds",
  63. want: -1,
  64. },
  65. }
  66. for _, tc := range tests {
  67. if got := parseEFIBootMGRTimeout(tc.input); !reflect.DeepEqual(got, tc.want) {
  68. t.Errorf("parseEFIBootMGRTimeout() = %v, want %v", got, tc.want)
  69. }
  70. }
  71. }
  72. func Test_parseEFIBootMGRBootOrder(t *testing.T) {
  73. tests := []struct {
  74. input string
  75. want []string
  76. }{
  77. {
  78. input: "BootOrder: 0005,0009,0008,0007,0002,0003,0004,0006,0001,0000",
  79. want: []string{"0005", "0009", "0008", "0007", "0002", "0003", "0004", "0006", "0001", "0000"},
  80. },
  81. }
  82. for _, tc := range tests {
  83. if got := parseEFIBootMGRBootOrder(tc.input); !reflect.DeepEqual(got, tc.want) {
  84. t.Errorf("parseEFIBootMGRBootOrder() = %v, want %v", got, tc.want)
  85. }
  86. }
  87. }
  88. func Test_parseEFIBootMGRBootCurrent(t *testing.T) {
  89. tests := []struct {
  90. name string
  91. line string
  92. want string
  93. }{
  94. {
  95. name: "BootCurrent: 0005",
  96. line: "BootCurrent: 0005",
  97. want: "0005",
  98. },
  99. {
  100. name: "BootCurrent: ",
  101. line: "BootCurrent: ",
  102. want: "",
  103. },
  104. {
  105. name: "BootCurrent:",
  106. line: "BootCurrent:",
  107. want: "",
  108. },
  109. }
  110. for _, tt := range tests {
  111. t.Run(tt.name, func(t *testing.T) {
  112. if got := parseEFIBootMGRBootCurrent(tt.line); got != tt.want {
  113. t.Errorf("parseEFIBootMGRBootCurrent() = %v, want %v", got, tt.want)
  114. }
  115. })
  116. }
  117. }
  118. func Test_parseEFIBootMGREntry(t *testing.T) {
  119. tests := []struct {
  120. name string
  121. line string
  122. want *BootEntry
  123. }{
  124. {
  125. name: "Boot0009* ubuntu",
  126. line: "Boot0009* ubuntu",
  127. want: &BootEntry{
  128. BootNum: "0009",
  129. Description: "ubuntu",
  130. IsActive: true,
  131. },
  132. },
  133. {
  134. name: "Boot0005* UEFI PXEv4 (MAC:525400123456)",
  135. line: "Boot0005* UEFI PXEv4 (MAC:525400123456)",
  136. want: &BootEntry{
  137. BootNum: "0005",
  138. Description: "UEFI PXEv4 (MAC:525400123456)",
  139. IsActive: true,
  140. },
  141. },
  142. {
  143. name: "Boot0003 CentOS Linux",
  144. line: "Boot0003 CentOS Linux",
  145. want: &BootEntry{
  146. BootNum: "0003",
  147. Description: "CentOS Linux",
  148. IsActive: false,
  149. },
  150. },
  151. {
  152. name: "Timeout: 2 seconds",
  153. line: "Timeout: 2 seconds",
  154. want: nil,
  155. },
  156. }
  157. for _, tt := range tests {
  158. t.Run(tt.name, func(t *testing.T) {
  159. if got := parseEFIBootMGREntry(tt.line); !reflect.DeepEqual(got, tt.want) {
  160. t.Errorf("parseEFIBootMGREntry() = %v, want %v", got, tt.want)
  161. }
  162. })
  163. }
  164. }
  165. func TestParseEFIBootMGR(t *testing.T) {
  166. tests := []struct {
  167. name string
  168. input string
  169. want *BootMgr
  170. wantErr bool
  171. }{
  172. {
  173. name: "Parse PC output",
  174. input: TestPCOutput,
  175. want: &BootMgr{
  176. bootCurrent: "0007",
  177. timeout: 2,
  178. bootOrder: []string{"0003", "0002", "0001", "0000", "0004", "0005", "0007", "0008", "0009", "000A"},
  179. entries: map[string]*BootEntry{
  180. "0000": {
  181. BootNum: "0000",
  182. IsActive: false,
  183. Description: "Windows Boot Manager",
  184. },
  185. "0001": {
  186. BootNum: "0001",
  187. IsActive: false,
  188. Description: "ubuntu",
  189. },
  190. "0002": {
  191. BootNum: "0002",
  192. IsActive: false,
  193. Description: "ubuntu",
  194. },
  195. "0003": {
  196. BootNum: "0003",
  197. IsActive: false,
  198. Description: "CentOS Linux",
  199. },
  200. "0004": {
  201. BootNum: "0004",
  202. IsActive: true,
  203. Description: "Onboard NIC(IPV4)",
  204. },
  205. "0005": {
  206. BootNum: "0005",
  207. IsActive: false,
  208. Description: "Onboard NIC(IPV6)",
  209. },
  210. "0007": {
  211. BootNum: "0007",
  212. IsActive: true,
  213. Description: "PXE IPv4 Intel(R) Ethernet Server Adapter X520-2",
  214. },
  215. "0008": {
  216. BootNum: "0008",
  217. IsActive: false,
  218. Description: "PXE IPv6 Intel(R) Ethernet Server Adapter X520-2",
  219. },
  220. "0009": {
  221. BootNum: "0009",
  222. IsActive: true,
  223. Description: "PXE IPv4 Intel(R) Ethernet Server Adapter X520-2",
  224. },
  225. "000A": {
  226. BootNum: "000A",
  227. IsActive: false,
  228. Description: "PXE IPv6 Intel(R) Ethernet Server Adapter X520-2",
  229. },
  230. }},
  231. },
  232. }
  233. assert := assert.New(t)
  234. for _, tt := range tests {
  235. t.Run(tt.name, func(t *testing.T) {
  236. got, err := ParseEFIBootMGR(tt.input)
  237. if (err != nil) != tt.wantErr {
  238. t.Errorf("ParseEFIBootMGR() error = %v, wantErr %v", err, tt.wantErr)
  239. return
  240. }
  241. if equal := assert.Equal(tt.want, got); !equal {
  242. t.Errorf("ParseEFIBootMGR() = %v, want %v", got, tt.want)
  243. }
  244. })
  245. }
  246. }
  247. func Test_stringArraryMove(t *testing.T) {
  248. type args struct {
  249. items []string
  250. item string
  251. pos int
  252. }
  253. tests := []struct {
  254. name string
  255. args args
  256. want []string
  257. }{
  258. {
  259. name: "move left to right",
  260. args: args{
  261. items: []string{"1", "2", "3"},
  262. item: "3",
  263. pos: 0,
  264. },
  265. want: []string{"3", "1", "2"},
  266. },
  267. {
  268. name: "move right to left",
  269. args: args{
  270. items: []string{"1", "2", "3"},
  271. item: "1",
  272. pos: 2,
  273. },
  274. want: []string{"2", "3", "1"},
  275. },
  276. {
  277. name: "no move",
  278. args: args{
  279. items: []string{"1", "2", "3"},
  280. item: "2",
  281. pos: 1,
  282. },
  283. want: []string{"1", "2", "3"},
  284. },
  285. {
  286. name: "add item move",
  287. args: args{
  288. items: []string{"1", "2", "3"},
  289. item: "4",
  290. pos: 0,
  291. },
  292. want: []string{"4", "1", "2", "3"},
  293. },
  294. }
  295. for _, tt := range tests {
  296. t.Run(tt.name, func(t *testing.T) {
  297. if got := stringArraryMove(tt.args.items, tt.args.item, tt.args.pos); !reflect.DeepEqual(got, tt.want) {
  298. t.Errorf("stringArraryMove() = %v, want %v", got, tt.want)
  299. }
  300. })
  301. }
  302. }