github.com/sap/cf-mta-plugin@v2.6.3+incompatible/util/digest_test.go (about)

     1  package util_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/cloudfoundry-incubator/multiapps-cli-plugin/testutil"
     9  	"github.com/cloudfoundry-incubator/multiapps-cli-plugin/util"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  )
    13  
    14  var _ = Describe("Digest", func() {
    15  
    16  	Describe("ComputeFileChecksum", func() {
    17  		const testFileName = "test-file.txt"
    18  
    19  		var testFilePath string
    20  		var testFile *os.File
    21  
    22  		BeforeEach(func() {
    23  			testFile, _ = os.Create(testFileName)
    24  			testFilePath, _ = filepath.Abs(testFileName)
    25  		})
    26  
    27  		Context("with an unsupported digest alghoritm", func() {
    28  			It("should return an error", func() {
    29  				digest, err := util.ComputeFileChecksum(testFilePath, "unsupported-alghoritm-name")
    30  				testutil.ExpectErrorAndZeroResult(err, digest)
    31  			})
    32  		})
    33  
    34  		Context("with a supported digest alghoritm and an empty file", func() {
    35  			It("should return the digest of the file", func() {
    36  				digest, err := util.ComputeFileChecksum(testFilePath, "MD5")
    37  				testutil.ExpectNoErrorAndResult(err, digest, "d41d8cd98f00b204e9800998ecf8427e")
    38  			})
    39  		})
    40  
    41  		Context("with a supported digest alghoritm and a non-empty file", func() {
    42  			It("should calculate the digest of the file and exit with zero status", func() {
    43  				const testFileContent = "test file content"
    44  				ioutil.WriteFile(testFile.Name(), []byte(testFileContent), 0644)
    45  				digest, err := util.ComputeFileChecksum(testFilePath, "SHA1")
    46  				testutil.ExpectNoErrorAndResult(err, digest, "9032bbc224ed8b39183cb93b9a7447727ce67f9d")
    47  			})
    48  		})
    49  
    50  		AfterEach(func() {
    51  			testFile.Close()
    52  			os.Remove(testFileName)
    53  		})
    54  	})
    55  })