github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/pkg/uroot/initramfs/test/ramfs.go (about) 1 // Copyright 2018 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 test 6 7 import ( 8 "fmt" 9 "os" 10 11 "github.com/u-root/u-root/pkg/cpio" 12 ) 13 14 type ArchiveValidator interface { 15 Validate(a *cpio.Archive) error 16 } 17 18 type HasRecord struct { 19 R cpio.Record 20 } 21 22 func (hr HasRecord) Validate(a *cpio.Archive) error { 23 r, ok := a.Get(hr.R.Name) 24 if !ok { 25 return fmt.Errorf("archive does not contain %v", hr.R) 26 } 27 if !cpio.Equal(r, hr.R) { 28 return fmt.Errorf("archive does not contain %v; instead has %v", hr.R, r) 29 } 30 return nil 31 } 32 33 type HasFile struct { 34 Path string 35 } 36 37 func (hf HasFile) Validate(a *cpio.Archive) error { 38 if _, ok := a.Get(hf.Path); ok { 39 return nil 40 } 41 return fmt.Errorf("archive does not contain %s, but should", hf.Path) 42 } 43 44 type MissingFile struct { 45 Path string 46 } 47 48 func (mf MissingFile) Validate(a *cpio.Archive) error { 49 if _, ok := a.Get(mf.Path); ok { 50 return fmt.Errorf("archive contains %s, but shouldn't", mf.Path) 51 } 52 return nil 53 } 54 55 type IsEmpty struct{} 56 57 func (IsEmpty) Validate(a *cpio.Archive) error { 58 if empty := a.Empty(); !empty { 59 return fmt.Errorf("expected archive to be empty") 60 } 61 return nil 62 } 63 64 func ReadArchive(path string) (*cpio.Archive, error) { 65 f, err := os.Open(path) 66 if err != nil { 67 return nil, err 68 } 69 70 return cpio.ArchiveFromReader(cpio.Newc.Reader(f)) 71 }