Exemplo n.º 1
0
func TestManifestType(t *testing.T) {
	pth, _ := ioutil.TempDir("", "_GOTEST_MANIFEST_TYPE")
	defer os.RemoveAll(pth)

	m, err := bagins.NewManifest(pth, "sha512", bagins.PayloadManifest)
	if err != nil {
		t.Error("Manifest could not be created!", err)
	}

	if m.Type() != bagins.PayloadManifest {
		t.Errorf("Type() expected '%s', got '%s'", bagins.PayloadManifest, m.Type())
	}

	pth, _ = ioutil.TempDir("", "_GOTEST_MANIFEST_TYPE/tagmanifest-sha512.txt")
	defer os.RemoveAll(pth)

	m, err = bagins.NewManifest(pth, "sha512", bagins.TagManifest)
	if err != nil {
		t.Error("Manifest could not be created!", err)
	}

	if m.Type() != bagins.TagManifest {
		t.Errorf("Type() expected '%s', got '%s'", bagins.TagManifest, m.Type())
	}

}
Exemplo n.º 2
0
// Make sure that when we add a file to the payload
// that is already in the payload directory, it doesn't
// get clobbered.
func TestPayloadAddInPlace(t *testing.T) {
	pDir, _ := ioutil.TempDir("", "_GOTEST_PayloadAdd_")
	m, _ := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)
	defer os.RemoveAll(pDir)

	p, err := bagins.NewPayload(pDir)
	if err != nil {
		t.Error(err)
	}

	//testFile, _ := ioutil.TempFile("", "_GO_PayloadAdd_TESTFILE_")
	testFile, err := os.Create(filepath.Join(pDir, "_GO_PayloadAdd_TESTFILE_"))
	if err != nil {
		t.Error(err)
	}
	testFile.WriteString("Test the checksum")
	testFile.Close()
	defer os.Remove(testFile.Name())

	chkSum, err := p.Add(testFile.Name(), filepath.Base(testFile.Name()), []*bagins.Manifest{m})
	if err != nil {
		t.Error(err)
	}
	exp := "92d7a9f0f4a30ca782dcae5fe83ca7eb"
	if exp != chkSum["md5"] {
		t.Error("Checksum", chkSum["md5"], "did not match", exp)
	}
}
Exemplo n.º 3
0
func TestNewManifest(t *testing.T) {
	pth, _ := ioutil.TempDir("", "_GOTEST_MANIFEST")
	defer os.RemoveAll(pth)

	_, err := bagins.NewManifest(pth, "sha1", bagins.PayloadManifest)
	if err != nil {
		t.Error("Manifest could not be created!", err)
	}
}
Exemplo n.º 4
0
func TestManifestName(t *testing.T) {

	// Set only Algo should still be blank.
	m, err := bagins.NewManifest(os.TempDir(), "SHA1", bagins.PayloadManifest)
	if err != nil {
		t.Error(err)
	}
	exp := filepath.Join(os.TempDir(), "manifest-sha1.txt")
	if name := m.Name(); name != exp {
		t.Error("Expected mainfest name %s but returned %s", exp, m.Name())
	}
}
Exemplo n.º 5
0
func TestReadManifest(t *testing.T) {

	// Setup a bad manifest name
	badpth := filepath.Join(os.TempDir(), "__GOTEST__BADMANIFEST_manifest-sha156.txt")
	badfile, err := os.Create(badpth)
	if err != nil {
		t.Error(err)
	}
	badfile.Close()
	defer os.Remove(badfile.Name())

	// It should
	_, errs := bagins.ReadManifest(badpth)
	if len(errs) != 1 {
		t.Error("Did not raise error as expected when trying to read bad manifest filename", badpth)
	}

	// Setup a good manfiest file for tests that should pass.
	exp := make(map[string]string)
	for i := 0; i < 40; i++ {
		check := fmt.Sprintf("%x", rand.Int31())
		fname := fmt.Sprintf("data/testfilename with spaces %d.txt", i)
		exp[fname] = check
	}

	// Setup a good test manifest
	mf, err := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)
	if err != nil {
		t.Error(err)
	}
	mf.Data = exp
	err = mf.Create()
	if err != nil {
		t.Error(err)
	}
	defer os.Remove(mf.Name())

	// It should open it and read the values inside without errors.
	m, errs := bagins.ReadManifest(mf.Name())
	if len(errs) != 0 {
		t.Error(errs)
	}
	for fname, check := range exp {
		actual, ok := m.Data[fname]
		if !ok {
			t.Errorf("Expected key %s not found in manifest data", fname)
		}
		if actual != check {
			t.Error("Failed to find file", fname, "in manifest.")
		}
	}
}
Exemplo n.º 6
0
func TestManifestAlgorithm(t *testing.T) {
	pth, _ := ioutil.TempDir("", "_GOTEST_MANIFEST_ALG")
	defer os.RemoveAll(pth)

	m, err := bagins.NewManifest(pth, "SHA256", bagins.PayloadManifest)
	if err != nil {
		t.Error("Manifest could not be created!", err)
	}

	if m.Algorithm() != "sha256" {
		t.Errorf("Algorithm() expected 'sha256', got '%s'", m.Algorithm())
	}
}
Exemplo n.º 7
0
func TestManifestCreate(t *testing.T) {
	m, _ := bagins.NewManifest(os.TempDir(), "sha1", bagins.PayloadManifest)

	testFiles := make([]*os.File, 3)
	for idx := range testFiles {
		testFiles[idx], _ = ioutil.TempFile("", "_GOTEST_")
		testFiles[idx].WriteString(strings.Repeat("test ", rand.Intn(50)))
		m.Data[testFiles[idx].Name()] = ""
		testFiles[idx].Close()
	}

	m.RunChecksums()
	m.Create()

	// Clean it all up.
	for idx := range testFiles {
		os.Remove(testFiles[idx].Name())
	}
	os.Remove(m.Name())
}
Exemplo n.º 8
0
func TestPayloadAddAll(t *testing.T) {
	// Setup directories to test on
	srcDir, _ := ioutil.TempDir("", "_GOTEST_PayloadAddAll_SRCDIR_")
	defer os.RemoveAll(srcDir)
	pDir, _ := ioutil.TempDir("", "_GOTEST_PayloadAddAll_")
	defer os.RemoveAll(pDir)

	m, _ := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)

	// Setup test files
	for i := 0; i < 100; i++ {
		tstFile, _ := ioutil.TempFile(srcDir, "_GOTEST_PayloadAddAll_FILE_")
		tstFile.WriteString("Test the checksum")
		tstFile.Close()
	}

	p, _ := bagins.NewPayload(pDir)
	checksums, errs := p.AddAll(srcDir, []*bagins.Manifest{m})

	// It should not return an error.
	if errs != nil {
		t.Errorf("Add all returned %d errors", len(errs))
	}
	// It should have fixity values for 100 files
	if len(checksums) != 100 {
		t.Errorf("Expected 100 fixity values but returned %d", len(checksums))
	}

	for key := range checksums {
		fileChk, err := bagutil.FileChecksum(filepath.Join(p.Name(), key), md5.New())
		if err != nil {
			t.Errorf(" %s", err)
		}
		if checksums[key]["md5"] != fileChk {
			t.Error("Expected", checksums[key]["md5"], "but returned", fileChk)
		}
	}

}
Exemplo n.º 9
0
func TestRunChecksums(t *testing.T) {
	testFile, _ := ioutil.TempFile("", "_GOTEST_RUNCHECKSUMS.txt")
	testFile.WriteString(test_string)
	testFile.Close()

	mfst, _ := bagins.NewManifest(os.TempDir(), "sha1", bagins.PayloadManifest)
	mfst.Data[filepath.Base(testFile.Name())] = test_list["sha1"]
	errList := mfst.RunChecksums()

	// Checksum for file should now be generated.
	for _, err := range errList {
		t.Error(err)
	}

	// Check that it throws an error if mismatch checksum.
	mfst.Data[testFile.Name()] = "frodo lives!"
	errList = mfst.RunChecksums()
	if len(errList) == 0 {
		t.Error("Invalid Checksums not being detected!")
	}
	os.Remove(testFile.Name()) // Remove the test file.
}
Exemplo n.º 10
0
func BenchmarkPayload(b *testing.B) {
	srcDir, _ := ioutil.TempDir("", "_GOTEST_BenchmarkPayload_SRCDIR_")
	defer os.RemoveAll(srcDir)
	pDir, _ := ioutil.TempDir("", "_GOTEST_BenchmarkPayload_Payload_")
	defer os.RemoveAll(pDir)

	m, _ := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)

	// Make src temp test files
	for i := 0; i < 300; i++ {
		tstFile, _ := ioutil.TempFile(srcDir, "_GOTEST_BenchmarkPayload_FILE_")
		tstFile.WriteString(strings.Repeat("Test the checksum. ", 500000)) // produces ~9 meg text file.
		tstFile.Close()
	}

	b.ResetTimer()

	p, _ := bagins.NewPayload(pDir)

	checksums, err := p.AddAll(srcDir, []*bagins.Manifest{m})
	if err != nil {
		b.Error(err)
	}

	b.StopTimer()

	// Make sure the actual values check out.
	for key := range checksums {
		fileChk, err := bagutil.FileChecksum(filepath.Join(p.Name(), key), md5.New())
		if err != nil {
			b.Errorf(" %s", err)
		}
		if checksums[key]["md5"] != fileChk {
			b.Error("Expected", checksums[key]["md5"], "but returned", fileChk)
		}
	}

}
Exemplo n.º 11
0
func TestManifestToString(t *testing.T) {
	m, _ := bagins.NewManifest(os.TempDir(), "sha1", bagins.PayloadManifest)
	m.Data["FileOne.txt"] = fmt.Sprintf("CHECKSUM 0001")
	m.Data["FileTwo.txt"] = fmt.Sprintf("CHECKSUM 0002")
	m.Data["FileThree.txt"] = fmt.Sprintf("CHECKSUM 0003")

	output := m.ToString()
	lines := []string{
		"CHECKSUM 0001 FileOne.txt\n",
		"CHECKSUM 0002 FileTwo.txt\n",
		"CHECKSUM 0003 FileThree.txt\n",
	}

	for _, line := range lines {
		if !strings.Contains(output, line) {
			t.Errorf("Manifest.ToString() did not return line %s", line)
		}
	}
	expectedLength := len(lines[0]) + len(lines[1]) + len(lines[2])
	if len(output) != expectedLength {
		t.Errorf("Manifest.ToString() returned %d characters, expected %d",
			len(output), expectedLength)
	}
}