// We should parse the tags in the specified files. // We should not parse other tag files func TestVirtualBagTagFileOptions(t *testing.T) { tarFilePath := testhelper.VbagGetPath("example.edu.tagsample_good.tar") files := []string{} vbag := models.NewVirtualBag(tarFilePath, files, true, true) assert.NotNil(t, vbag) obj, _ := vbag.Read() assert.Equal(t, 0, len(obj.IngestTags)) files = []string{"bagit.txt"} vbag = models.NewVirtualBag(tarFilePath, files, true, true) assert.NotNil(t, vbag) obj, _ = vbag.Read() assert.Equal(t, 2, len(obj.IngestTags)) files = []string{"bagit.txt", "bag-info.txt"} vbag = models.NewVirtualBag(tarFilePath, files, true, true) assert.NotNil(t, vbag) obj, _ = vbag.Read() assert.Equal(t, 8, len(obj.IngestTags)) files = []string{"bagit.txt", "bag-info.txt", "aptrust-info.txt"} vbag = models.NewVirtualBag(tarFilePath, files, true, true) assert.NotNil(t, vbag) obj, _ = vbag.Read() assert.Equal(t, 10, len(obj.IngestTags)) }
func TestVirtualBagRead_ChecksumOptions(t *testing.T) { tempDir, bagPath, err := testhelper.UntarTestBag("example.edu.tagsample_good.tar") if err != nil { assert.Fail(t, err.Error()) } if tempDir != "" { defer os.RemoveAll(tempDir) } files := []string{"bagit.txt", "bag-info.txt", "aptrust-info.txt"} vbag := models.NewVirtualBag(bagPath, files, true, false) assert.NotNil(t, vbag) obj, _ := vbag.Read() // Should calculate md5 only for _, gf := range obj.GenericFiles { assert.NotEmpty(t, gf.IngestMd5) assert.Empty(t, gf.IngestSha256) } vbag = models.NewVirtualBag(bagPath, files, false, true) assert.NotNil(t, vbag) obj, _ = vbag.Read() // Should calculate sha256 only for _, gf := range obj.GenericFiles { assert.Empty(t, gf.IngestMd5) assert.NotEmpty(t, gf.IngestSha256) } }
// With md5 manifest only, sha256 only, and both func TestVirtualBagRead_ManifestOptions(t *testing.T) { tempDir, bagPath, err := testhelper.UntarTestBag("example.edu.tagsample_good.tar") if err != nil { assert.Fail(t, err.Error()) } if tempDir != "" { defer os.RemoveAll(tempDir) } // Delete the md5 manifest os.Remove(filepath.Join(bagPath, "manifest-md5.txt")) files := []string{"bagit.txt", "bag-info.txt", "aptrust-info.txt"} vbag := models.NewVirtualBag(bagPath, files, true, true) assert.NotNil(t, vbag) obj, _ := vbag.Read() // Should have manifest values for sha256 for _, gf := range obj.GenericFiles { if gf.IngestFileType == constants.PAYLOAD_FILE { assert.Empty(t, gf.IngestManifestMd5) assert.NotEmpty(t, gf.IngestManifestSha256) } } tempDir, bagPath, err = testhelper.UntarTestBag("example.edu.tagsample_good.tar") if err != nil { assert.Fail(t, err.Error()) } if tempDir != "" { defer os.RemoveAll(tempDir) } // Delete the sha256 manifest os.Remove(filepath.Join(bagPath, "manifest-sha256.txt")) files = []string{"bagit.txt", "bag-info.txt", "aptrust-info.txt"} vbag = models.NewVirtualBag(bagPath, files, true, true) assert.NotNil(t, vbag) obj, _ = vbag.Read() // Should have manifest values for sha256 for _, gf := range obj.GenericFiles { if gf.IngestFileType == constants.PAYLOAD_FILE { assert.NotEmpty(t, gf.IngestManifestMd5) assert.Empty(t, gf.IngestManifestSha256) } } }
// NewBagValidator creates a new BagValidator. Param pathToBag // should be an absolute path to either the tarred bag (.tar file) // or to the untarred bag (a directory). Param bagValidationConfig // defines what we need to validate, in addition to the checksums in the // manifests. func NewBagValidator(pathToBag string, bagValidationConfig *BagValidationConfig) (*BagValidator, error) { if !fileutil.FileExists(pathToBag) { return nil, fmt.Errorf("Bag does not exist at %s", pathToBag) } if bagValidationConfig == nil { return nil, fmt.Errorf("Param bagValidationConfig cannot be nil") } configErrors := bagValidationConfig.ValidateConfig() if len(configErrors) > 0 { errString := "BagValidationConfig has the following errors:" for _, e := range configErrors { errString += fmt.Sprintf("\n%s", e.Error()) } return nil, fmt.Errorf(errString) } err := bagValidationConfig.CompileFileNameRegex() if err != nil { return nil, fmt.Errorf("Error in BagValidationConfig: %v", err) } calculateMd5 := util.StringListContains(bagValidationConfig.FixityAlgorithms, constants.AlgMd5) calculateSha256 := util.StringListContains(bagValidationConfig.FixityAlgorithms, constants.AlgSha256) tagFilesToParse := make([]string, 0) for pathToFile, filespec := range bagValidationConfig.FileSpecs { if filespec.ParseAsTagFile { tagFilesToParse = append(tagFilesToParse, pathToFile) } } bagValidator := &BagValidator{ PathToBag: pathToBag, BagValidationConfig: bagValidationConfig, virtualBag: models.NewVirtualBag(pathToBag, tagFilesToParse, calculateMd5, calculateSha256), } return bagValidator, nil }
func TestVirtualBagRead_FromTarFile(t *testing.T) { tarFilePath := testhelper.VbagGetPath("example.edu.tagsample_good.tar") files := []string{"bagit.txt", "bag-info.txt", "aptrust-info.txt"} vbag := models.NewVirtualBag(tarFilePath, files, true, true) assert.NotNil(t, vbag) obj, summary := vbag.Read() runAssertions(t, obj, summary, "TestVirtualBagRead_FromTarFile") }
func TestVirtualBagRead_FromDirectory(t *testing.T) { tempDir, bagPath, err := testhelper.UntarTestBag("example.edu.tagsample_good.tar") if err != nil { assert.Fail(t, err.Error()) } if tempDir != "" { defer os.RemoveAll(tempDir) } files := []string{"bagit.txt", "bag-info.txt", "aptrust-info.txt"} vbag := models.NewVirtualBag(bagPath, files, true, true) assert.NotNil(t, vbag) obj, summary := vbag.Read() runAssertions(t, obj, summary, "TestVirtualBagRead_FromDirectory") }
func TestVirtualBagReadReportsMissingFiles(t *testing.T) { // This bad bag has a number of problems. // Here, we're specifically testing to see if a missing file is reported. tarFilePath := testhelper.VbagGetPath("example.edu.tagsample_bad.tar") files := []string{"bagit.txt", "bag-info.txt", "aptrust-info.txt"} vbag := models.NewVirtualBag(tarFilePath, files, true, true) assert.NotNil(t, vbag) obj, summary := vbag.Read() // custom_tags/tag_file_xyz.pdf appears twice in missing files // list because it's mentioned in manifest-md5 and manifest-sha256 assert.True(t, summary.HasErrors()) require.Equal(t, 3, len(obj.IngestMissingFiles)) assert.Equal(t, "data/file-not-in-bag", obj.IngestMissingFiles[0].FilePath) assert.Equal(t, "custom_tags/tag_file_xyz.pdf", obj.IngestMissingFiles[1].FilePath) assert.Equal(t, "custom_tags/tag_file_xyz.pdf", obj.IngestMissingFiles[2].FilePath) }
func TestNewVirtualBag(t *testing.T) { tarFilePath := testhelper.VbagGetPath("example.edu.tagsample_good.tar") vbag := models.NewVirtualBag(tarFilePath, nil, false, false) assert.NotNil(t, vbag) }