Ejemplo n.º 1
0
// Make sure we get an error and not a panic.
func TestLoadBagValidationConfigBadFiles(t *testing.T) {
	// Missing file
	configFilePath := path.Join("testdata", "json_objects", "file_does_not_exist.json")
	_, err := validation.LoadBagValidationConfig(configFilePath)
	assert.NotNil(t, err)
	// Unparseable JSON
	configFilePath = path.Join("testdata", "json_objects", "virginia.edu.uva-lib_2278801.tar")
	_, err = validation.LoadBagValidationConfig(configFilePath)
	assert.NotNil(t, err)
}
Ejemplo n.º 2
0
func TestLoadBagValidationConfig(t *testing.T) {
	configFilePath := path.Join("testdata", "json_objects", "bag_validation_config.json")
	conf, errors := validation.LoadBagValidationConfig(configFilePath)
	if errors != nil && len(errors) > 0 {
		assert.Fail(t, errors[0].Error())
	}
	assert.True(t, conf.AllowMiscTopLevelFiles)
	assert.True(t, conf.AllowMiscDirectories)
	assert.True(t, conf.TopLevelDirMustMatchBagName)
	assert.Equal(t, 7, len(conf.FileSpecs))
	assert.Equal(t, 3, len(conf.TagSpecs))
	assert.Equal(t, 2, len(conf.FixityAlgorithms))

	// Spot checks
	if _, ok := conf.FileSpecs["manifest-md5.txt"]; !ok {
		assert.Fail(t, "FileSpec for manifest-md5.txt is missing")
	}
	if _, ok := conf.FileSpecs["manifest-sha256.txt"]; !ok {
		assert.Fail(t, "FileSpec for manifest-sha256.txt is missing")
	}
	if _, ok := conf.TagSpecs["Title"]; !ok {
		assert.Fail(t, "TagSpec for Title is missing")
	}
	if len(conf.FixityAlgorithms) > 1 {
		assert.Equal(t, "md5", conf.FixityAlgorithms[0])
		assert.Equal(t, "sha256", conf.FixityAlgorithms[1])
	}
	assert.Equal(t, validation.REQUIRED, conf.FileSpecs["manifest-md5.txt"].Presence)
	assert.Equal(t, validation.OPTIONAL, conf.FileSpecs["manifest-sha256.txt"].Presence)
	assert.Equal(t, "aptrust-info.txt", conf.TagSpecs["Title"].FilePath)
	assert.Equal(t, validation.REQUIRED, conf.TagSpecs["Title"].Presence)
	assert.False(t, conf.TagSpecs["Title"].EmptyOK)
	assert.Equal(t, 3, len(conf.TagSpecs["Access"].AllowedValues))
}
Ejemplo n.º 3
0
func TestCompileFileNameRegex(t *testing.T) {
	configFilePath := path.Join("testdata", "json_objects", "bag_validation_config.json")
	conf, errors := validation.LoadBagValidationConfig(configFilePath)
	if errors != nil && len(errors) > 0 {
		assert.Fail(t, errors[0].Error())
	}
	err := conf.CompileFileNameRegex()
	assert.Nil(t, err)

	conf.FileNamePattern = "ThisPatternIsInvalid[-"
	err = conf.CompileFileNameRegex()
	require.NotNil(t, err)
	assert.True(t, strings.HasPrefix(err.Error(), "Cannot compile regex"))

	conf.FileNamePattern = "aptrust"
	err = conf.CompileFileNameRegex()
	assert.Nil(t, err)
	assert.Equal(t, constants.APTrustFileNamePattern, conf.FileNameRegex)

	conf.FileNamePattern = "APTRUST"
	err = conf.CompileFileNameRegex()
	assert.Nil(t, err)
	assert.Equal(t, constants.APTrustFileNamePattern, conf.FileNameRegex)

	conf.FileNamePattern = "posix"
	err = conf.CompileFileNameRegex()
	assert.Nil(t, err)
	assert.Equal(t, constants.PosixFileNamePattern, conf.FileNameRegex)

	conf.FileNamePattern = "POSIX"
	err = conf.CompileFileNameRegex()
	assert.Nil(t, err)
	assert.Equal(t, constants.PosixFileNamePattern, conf.FileNameRegex)

}
Ejemplo n.º 4
0
func getValidationConfig() (*validation.BagValidationConfig, error) {
	configFilePath := path.Join("testdata", "json_objects", "bag_validation_config.json")
	conf, errors := validation.LoadBagValidationConfig(configFilePath)
	if errors != nil && len(errors) > 0 {
		return nil, errors[0]
	}
	return conf, nil
}
Ejemplo n.º 5
0
// Loads the bag validation config file specified in the general config
// options. This will die if the bag validation config cannot be loaded
// or is invalid.
func (fetcher *APTFetcher) loadBagValidationConfig() {
	bagValidationConfig, errors := validation.LoadBagValidationConfig(
		fetcher.Context.Config.BagValidationConfigFile)
	if errors != nil && len(errors) > 0 {
		msg := fmt.Sprintf("Could not load bag validation config from %s",
			fetcher.Context.Config.BagValidationConfigFile)
		for _, err := range errors {
			msg += fmt.Sprintf("%s ... ", err.Error())
		}
		fmt.Fprintln(os.Stderr, msg)
		fetcher.Context.MessageLog.Fatal(msg)
	} else {
		fetcher.Context.MessageLog.Info("Loaded bag validation config file %s",
			fetcher.Context.Config.BagValidationConfigFile)
	}
	fetcher.BagValidationConfig = bagValidationConfig
}
Ejemplo n.º 6
0
func TestValidateConfig(t *testing.T) {
	configFilePath := path.Join("testdata", "json_objects", "bag_validation_config.json")
	conf, errors := validation.LoadBagValidationConfig(configFilePath)
	if errors != nil && len(errors) > 0 {
		assert.Fail(t, errors[0].Error())
	}
	errors = conf.ValidateConfig()
	assert.Empty(t, errors)

	badPathSpec := validation.TagSpec{
		FilePath: "",
		Presence: "REQUIRED",
		EmptyOK:  true,
	}
	badPresenceSpec := validation.TagSpec{
		FilePath: "orangina",
		Presence: "orangina",
		EmptyOK:  true,
	}
	conf.TagSpecs["bad_path_spec"] = badPathSpec
	conf.TagSpecs["bad_presence"] = badPresenceSpec
	errors = conf.ValidateConfig()
	assert.Equal(t, 2, len(errors))
}