github.com/pkalwak/bagins@v0.0.0-20210317172317-694ac5ce2f54/manifest_test.go (about)

     1  // manifest_test
     2  package bagins_test
     3  
     4  import (
     5  	"fmt"
     6  	"github.com/pkalwak/bagins"
     7  	"io/ioutil"
     8  	"math/rand"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  var test_list = map[string]string{
    16  	"md5":    "9e107d9d372bb6826bd81d3542a419d6",
    17  	"sha1":   "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
    18  	"sha256": "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592",
    19  	"sha512": "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6",
    20  	"sha224": "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
    21  	"sha384": "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1",
    22  }
    23  var test_string = "The quick brown fox jumps over the lazy dog"
    24  
    25  func TestNewManifest(t *testing.T) {
    26  	pth, _ := ioutil.TempDir("", "_GOTEST_MANIFEST")
    27  	defer os.RemoveAll(pth)
    28  
    29  	_, err := bagins.NewManifest(pth, "sha1", bagins.PayloadManifest)
    30  	if err != nil {
    31  		t.Error("Manifest could not be created!", err)
    32  	}
    33  }
    34  
    35  func TestManifestAlgorithm(t *testing.T) {
    36  	pth, _ := ioutil.TempDir("", "_GOTEST_MANIFEST_ALG")
    37  	defer os.RemoveAll(pth)
    38  
    39  	m, err := bagins.NewManifest(pth, "SHA256", bagins.PayloadManifest)
    40  	if err != nil {
    41  		t.Error("Manifest could not be created!", err)
    42  	}
    43  
    44  	if m.Algorithm() != "sha256" {
    45  		t.Errorf("Algorithm() expected 'sha256', got '%s'", m.Algorithm())
    46  	}
    47  }
    48  
    49  func TestManifestType(t *testing.T) {
    50  	pth, _ := ioutil.TempDir("", "_GOTEST_MANIFEST_TYPE")
    51  	defer os.RemoveAll(pth)
    52  
    53  	m, err := bagins.NewManifest(pth, "sha512", bagins.PayloadManifest)
    54  	if err != nil {
    55  		t.Error("Manifest could not be created!", err)
    56  	}
    57  
    58  	if m.Type() != bagins.PayloadManifest {
    59  		t.Errorf("Type() expected '%s', got '%s'", bagins.PayloadManifest, m.Type())
    60  	}
    61  
    62  	pth, _ = ioutil.TempDir("", "_GOTEST_MANIFEST_TYPE/tagmanifest-sha512.txt")
    63  	defer os.RemoveAll(pth)
    64  
    65  	m, err = bagins.NewManifest(pth, "sha512", bagins.TagManifest)
    66  	if err != nil {
    67  		t.Error("Manifest could not be created!", err)
    68  	}
    69  
    70  	if m.Type() != bagins.TagManifest {
    71  		t.Errorf("Type() expected '%s', got '%s'", bagins.TagManifest, m.Type())
    72  	}
    73  
    74  }
    75  
    76  func TestReadManifest(t *testing.T) {
    77  
    78  	// Setup a bad manifest name
    79  	badpth := filepath.Join(os.TempDir(), "__GOTEST__BADMANIFEST_manifest-sha156.txt")
    80  	badfile, err := os.Create(badpth)
    81  	if err != nil {
    82  		t.Error(err)
    83  	}
    84  	badfile.Close()
    85  	defer os.Remove(badfile.Name())
    86  
    87  	// It should
    88  	_, errs := bagins.ReadManifest(badpth)
    89  	if len(errs) != 1 {
    90  		t.Error("Did not raise error as expected when trying to read bad manifest filename", badpth)
    91  	}
    92  
    93  	// Setup a good manfiest file for tests that should pass.
    94  	exp := make(map[string]string)
    95  	for i := 0; i < 40; i++ {
    96  		check := fmt.Sprintf("%x", rand.Int31())
    97  		fname := fmt.Sprintf("data/testfilename with spaces %d.txt", i)
    98  		exp[fname] = check
    99  	}
   100  
   101  	// Setup a good test manifest
   102  	mf, err := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)
   103  	if err != nil {
   104  		t.Error(err)
   105  	}
   106  	mf.Data = exp
   107  	err = mf.Create()
   108  	if err != nil {
   109  		t.Error(err)
   110  	}
   111  	defer os.Remove(mf.Name())
   112  
   113  	// It should open it and read the values inside without errors.
   114  	m, errs := bagins.ReadManifest(mf.Name())
   115  	if len(errs) != 0 {
   116  		t.Error(errs)
   117  	}
   118  	for fname, check := range exp {
   119  		actual, ok := m.Data[fname]
   120  		if !ok {
   121  			t.Errorf("Expected key %s not found in manifest data", fname)
   122  		}
   123  		if actual != check {
   124  			t.Error("Failed to find file", fname, "in manifest.")
   125  		}
   126  	}
   127  }
   128  
   129  func TestRunChecksums(t *testing.T) {
   130  	testFile, _ := ioutil.TempFile("", "_GOTEST_RUNCHECKSUMS.txt")
   131  	testFile.WriteString(test_string)
   132  	testFile.Close()
   133  
   134  	mfst, _ := bagins.NewManifest(os.TempDir(), "sha1", bagins.PayloadManifest)
   135  	mfst.Data[filepath.Base(testFile.Name())] = test_list["sha1"]
   136  	errList := mfst.RunChecksums()
   137  
   138  	// Checksum for file should now be generated.
   139  	for _, err := range errList {
   140  		t.Error(err)
   141  	}
   142  
   143  	// Check that it throws an error if mismatch checksum.
   144  	mfst.Data[testFile.Name()] = "frodo lives!"
   145  	errList = mfst.RunChecksums()
   146  	if len(errList) == 0 {
   147  		t.Error("Invalid Checksums not being detected!")
   148  	}
   149  	os.Remove(testFile.Name()) // Remove the test file.
   150  }
   151  
   152  func TestManifestCreate(t *testing.T) {
   153  	m, _ := bagins.NewManifest(os.TempDir(), "sha1", bagins.PayloadManifest)
   154  
   155  	testFiles := make([]*os.File, 3)
   156  	for idx := range testFiles {
   157  		testFiles[idx], _ = ioutil.TempFile("", "_GOTEST_")
   158  		testFiles[idx].WriteString(strings.Repeat("test ", rand.Intn(50)))
   159  		m.Data[testFiles[idx].Name()] = ""
   160  		testFiles[idx].Close()
   161  	}
   162  
   163  	m.RunChecksums()
   164  	m.Create()
   165  
   166  	// Clean it all up.
   167  	for idx := range testFiles {
   168  		os.Remove(testFiles[idx].Name())
   169  	}
   170  	os.Remove(m.Name())
   171  }
   172  
   173  func TestManifestName(t *testing.T) {
   174  
   175  	// Set only Algo should still be blank.
   176  	m, err := bagins.NewManifest(os.TempDir(), "SHA1", bagins.PayloadManifest)
   177  	if err != nil {
   178  		t.Error(err)
   179  	}
   180  	exp := filepath.Join(os.TempDir(), "manifest-sha1.txt")
   181  	if name := m.Name(); name != exp {
   182  		t.Error("Expected mainfest name %s but returned %s", exp, m.Name())
   183  	}
   184  }
   185  
   186  func TestManifestToString(t *testing.T) {
   187  	m, _ := bagins.NewManifest(os.TempDir(), "sha1", bagins.PayloadManifest)
   188  	m.Data["FileOne.txt"] = fmt.Sprintf("CHECKSUM 0001")
   189  	m.Data["FileTwo.txt"] = fmt.Sprintf("CHECKSUM 0002")
   190  	m.Data["FileThree.txt"] = fmt.Sprintf("CHECKSUM 0003")
   191  
   192  	output := m.ToString()
   193  	lines := []string{
   194  		"CHECKSUM 0001 FileOne.txt\n",
   195  		"CHECKSUM 0002 FileTwo.txt\n",
   196  		"CHECKSUM 0003 FileThree.txt\n",
   197  	}
   198  
   199  	for _, line := range lines {
   200  		if !strings.Contains(output, line) {
   201  			t.Errorf("Manifest.ToString() did not return line %s", line)
   202  		}
   203  	}
   204  	expectedLength := len(lines[0]) + len(lines[1]) + len(lines[2])
   205  	if len(output) != expectedLength {
   206  		t.Errorf("Manifest.ToString() returned %d characters, expected %d",
   207  			len(output), expectedLength)
   208  	}
   209  }