github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/types/asset_test.go (about)

     1  // Copyright (c) 2017 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package types
     7  
     8  import (
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  var assetContent = []byte("FakeAsset fake asset FAKE ASSET")
    19  var assetContentHash = "92549f8d2018a95a294d28a65e795ed7d1a9d150009a28cea108ae10101178676f04ab82a6950d0099e4924f9c5e41dcba8ece56b75fc8b4e0a7492cb2a8c880"
    20  var assetContentWrongHash = "92549f8d2018a95a294d28a65e795ed7d1a9d150009a28cea108ae10101178676f04ab82a6950d0099e4924f9c5e41dcba8ece56b75fc8b4e0a7492cb2a8c881"
    21  
    22  func TestAssetWrongHashType(t *testing.T) {
    23  	assert := assert.New(t)
    24  
    25  	tmpfile, err := ioutil.TempFile("", "virtcontainers-test-")
    26  	assert.Nil(err)
    27  
    28  	defer func() {
    29  		tmpfile.Close()
    30  		os.Remove(tmpfile.Name()) // clean up
    31  	}()
    32  
    33  	_, err = tmpfile.Write(assetContent)
    34  	assert.Nil(err)
    35  
    36  	a := &Asset{
    37  		path: tmpfile.Name(),
    38  	}
    39  
    40  	h, err := a.Hash("shafoo")
    41  	assert.Equal(h, "")
    42  	assert.NotNil(err)
    43  }
    44  
    45  func TestAssetHash(t *testing.T) {
    46  	assert := assert.New(t)
    47  
    48  	tmpfile, err := ioutil.TempFile("", "virtcontainers-test-")
    49  	assert.Nil(err)
    50  
    51  	defer func() {
    52  		tmpfile.Close()
    53  		os.Remove(tmpfile.Name()) // clean up
    54  	}()
    55  
    56  	_, err = tmpfile.Write(assetContent)
    57  	assert.Nil(err)
    58  
    59  	a := &Asset{
    60  		path: tmpfile.Name(),
    61  	}
    62  
    63  	hash, err := a.Hash(annotations.SHA512)
    64  	assert.Nil(err)
    65  	assert.Equal(assetContentHash, hash)
    66  	assert.Equal(assetContentHash, a.computedHash)
    67  }
    68  
    69  func testPath(t *testing.T, a *Asset, correctPath string, msg string) {
    70  	assert := assert.New(t)
    71  
    72  	returnedPath := a.Path()
    73  	assert.Equal(returnedPath, correctPath, msg)
    74  }
    75  
    76  func testType(t *testing.T, a *Asset, correctType AssetType, msg string) {
    77  	assert := assert.New(t)
    78  
    79  	returnedType := a.Type()
    80  	assert.Equal(returnedType, correctType, msg)
    81  }
    82  
    83  func testValid(t *testing.T, a *Asset, msg string) {
    84  	assert := assert.New(t)
    85  
    86  	v := a.Valid()
    87  	assert.True(v, msg)
    88  }
    89  
    90  func TestAssetNew(t *testing.T) {
    91  	assert := assert.New(t)
    92  
    93  	tmpfile, err := ioutil.TempFile("", "virtcontainers-test-")
    94  	assert.Nil(err)
    95  
    96  	defer func() {
    97  		tmpfile.Close()
    98  		os.Remove(tmpfile.Name()) // clean up
    99  	}()
   100  
   101  	_, err = tmpfile.Write(assetContent)
   102  	assert.Nil(err)
   103  
   104  	type testData struct {
   105  		inputPathVar   string
   106  		inputHashVar   string
   107  		inputAssetType AssetType
   108  		inputHash      string
   109  		expectError    bool
   110  		expectNilAsset bool
   111  	}
   112  
   113  	data := []testData{
   114  		// Successful with correct hash
   115  		{annotations.KernelPath, annotations.KernelHash, KernelAsset, assetContentHash, false, false},
   116  		{annotations.ImagePath, annotations.ImageHash, ImageAsset, assetContentHash, false, false},
   117  		{annotations.InitrdPath, annotations.InitrdHash, InitrdAsset, assetContentHash, false, false},
   118  		{annotations.HypervisorPath, annotations.HypervisorHash, HypervisorAsset, assetContentHash, false, false},
   119  		{annotations.HypervisorCtlPath, annotations.HypervisorCtlHash, HypervisorCtlAsset, assetContentHash, false, false},
   120  		{annotations.JailerPath, annotations.JailerHash, JailerAsset, assetContentHash, false, false},
   121  		{annotations.FirmwarePath, annotations.FirmwareHash, FirmwareAsset, assetContentHash, false, false},
   122  
   123  		// Failure with incorrect hash
   124  		{annotations.KernelPath, annotations.KernelHash, KernelAsset, assetContentWrongHash, true, false},
   125  		{annotations.ImagePath, annotations.ImageHash, ImageAsset, assetContentWrongHash, true, false},
   126  		{annotations.InitrdPath, annotations.InitrdHash, InitrdAsset, assetContentWrongHash, true, false},
   127  		{annotations.HypervisorPath, annotations.HypervisorHash, HypervisorAsset, assetContentWrongHash, true, false},
   128  		{annotations.HypervisorCtlPath, annotations.HypervisorCtlHash, HypervisorCtlAsset, assetContentWrongHash, true, false},
   129  		{annotations.JailerPath, annotations.JailerHash, JailerAsset, assetContentWrongHash, true, false},
   130  		{annotations.FirmwarePath, annotations.FirmwareHash, FirmwareAsset, assetContentWrongHash, true, false},
   131  
   132  		// Other failures
   133  		{annotations.KernelPath, annotations.KernelHash, ImageAsset, assetContentHash, false, true},
   134  	}
   135  
   136  	for i, d := range data {
   137  		msg := fmt.Sprintf("test[%d]: %+v", i, d)
   138  
   139  		anno := map[string]string{
   140  			d.inputPathVar: tmpfile.Name(),
   141  			d.inputHashVar: d.inputHash,
   142  		}
   143  
   144  		if d.expectNilAsset {
   145  			a, err := NewAsset(anno, d.inputAssetType)
   146  			assert.NoError(err, msg)
   147  			assert.Nil(a, msg)
   148  		} else if d.expectError {
   149  			_, err := NewAsset(anno, d.inputAssetType)
   150  			assert.NotNil(err, msg)
   151  		} else {
   152  			a, err := NewAsset(anno, d.inputAssetType)
   153  			assert.Nil(err, msg)
   154  			assert.Equal(assetContentHash, a.computedHash, msg)
   155  
   156  			testPath(t, a, tmpfile.Name(), msg)
   157  			testType(t, a, d.inputAssetType, msg)
   158  			testValid(t, a, msg)
   159  		}
   160  	}
   161  }