forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
66 lines (62 loc) · 1.37 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2020 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause
//
// Package vartest contains utility functions for testing uefivars and
// subpackages. It is unlikely to be useful outside of testing.
package vartest
import (
"archive/zip"
"io"
"os"
"github.com/u-root/u-root/pkg/upath"
)
// Extracts testdata zip for use as efivars in tests. Used in uefivars and subpackages.
func SetupVarZip(path string) (efiVarDir string, cleanup func(), err error) {
efiVarDir, err = os.MkdirTemp("", "gotest-uefivars")
if err != nil {
return
}
defer func() {
if err != nil {
os.RemoveAll(efiVarDir)
}
}()
z, err := zip.OpenReader(path)
if err != nil {
return
}
defer z.Close()
for _, zf := range z.File {
var fname string
if fname, err = upath.SafeFilepathJoin(efiVarDir, zf.Name); err != nil {
return
}
if zf.FileInfo().IsDir() {
err = os.MkdirAll(fname, zf.FileInfo().Mode())
if err != nil {
return
}
} else {
var fo *os.File
fo, err = os.Create(fname)
if err != nil {
return
}
var fi io.ReadCloser
fi, err = zf.Open()
if err != nil {
return
}
_, err = io.Copy(fo, fi)
if err != nil {
return
}
fo.Close()
}
}
cleanup = func() { os.RemoveAll(efiVarDir) }
return
}