...

Source file src/github.com/klauspost/compress/zip/internal/obscuretestdata/obscuretestdata.go

Documentation: github.com/klauspost/compress/zip/internal/obscuretestdata

     1  // Copyright 2019 The Go 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  //go:build go1.16
     6  // +build go1.16
     7  
     8  // Package obscuretestdata contains functionality used by tests to more easily
     9  // work with testdata that must be obscured primarily due to
    10  // golang.org/issue/34986.
    11  package obscuretestdata
    12  
    13  import (
    14  	"encoding/base64"
    15  	"io"
    16  	"os"
    17  )
    18  
    19  // DecodeToTempFile decodes the named file to a temporary location.
    20  // If successful, it returns the path of the decoded file.
    21  // The caller is responsible for ensuring that the temporary file is removed.
    22  func DecodeToTempFile(name string) (path string, err error) {
    23  	f, err := os.Open(name)
    24  	if err != nil {
    25  		return "", err
    26  	}
    27  	defer f.Close()
    28  
    29  	tmp, err := os.CreateTemp("", "obscuretestdata-decoded-")
    30  	if err != nil {
    31  		return "", err
    32  	}
    33  	if _, err := io.Copy(tmp, base64.NewDecoder(base64.StdEncoding, f)); err != nil {
    34  		tmp.Close()
    35  		os.Remove(tmp.Name())
    36  		return "", err
    37  	}
    38  	if err := tmp.Close(); err != nil {
    39  		os.Remove(tmp.Name())
    40  		return "", err
    41  	}
    42  	return tmp.Name(), nil
    43  }
    44  
    45  // ReadFile reads the named file and returns its decoded contents.
    46  func ReadFile(name string) ([]byte, error) {
    47  	f, err := os.Open(name)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	defer f.Close()
    52  	return io.ReadAll(base64.NewDecoder(base64.StdEncoding, f))
    53  }
    54  

View as plain text