github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/repo/test/generator.go (about) 1 package test 2 3 import ( 4 "archive/zip" 5 "io" 6 "os" 7 "path/filepath" 8 9 "github.com/qri-io/qri/auth/key" 10 testkeys "github.com/qri-io/qri/auth/key/test" 11 ) 12 13 // NewTestCrypto returns a mocked cryptographic generator for tests 14 func NewTestCrypto() key.CryptoGenerator { 15 return &testCryptoGenerator{} 16 } 17 18 var _ key.CryptoGenerator = (*testCryptoGenerator)(nil) 19 20 type testCryptoGenerator struct { 21 count int 22 } 23 24 func (g *testCryptoGenerator) GeneratePrivateKeyAndPeerID() (string, string) { 25 kd := testkeys.GetKeyData(g.count) 26 g.count++ 27 return kd.EncodedPrivKey, kd.EncodedPeerID 28 } 29 30 // InitIPFSRepo creates an IPFS repo by un-zipping a preconstructed IPFS repo 31 func InitIPFSRepo(repoPath, configPath string) error { 32 unzipFile(TestdataPath("empty_ipfs_repo.zip"), repoPath) 33 return nil 34 } 35 36 func unzipFile(sourceZip, destDir string) { 37 r, err := zip.OpenReader(sourceZip) 38 if err != nil { 39 panic(err) 40 } 41 defer r.Close() 42 43 for _, f := range r.File { 44 rc, err := f.Open() 45 if err != nil { 46 panic(err) 47 } 48 defer rc.Close() 49 50 fpath := filepath.Join(destDir, f.Name) 51 if f.FileInfo().IsDir() { 52 os.MkdirAll(fpath, os.ModePerm) 53 } else { 54 if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil { 55 panic(err) 56 } 57 outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) 58 if err != nil { 59 panic(err) 60 } 61 _, err = io.Copy(outFile, rc) 62 outFile.Close() 63 } 64 } 65 }