From f2d208c464a1f157014127bfa97dbb7b84158f95 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 5 Jun 2016 12:53:10 +0200 Subject: configfile: use map[flagIota] for feature flags This should make things saner and more extensible. It prepares the infrastructure for "required feature flags" that will be used to deprecate old gocryptfs version. --- internal/configfile/config_file.go | 45 ++++-------------------------------- internal/configfile/config_test.go | 23 ++++++++++++------ internal/configfile/feature_flags.go | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 internal/configfile/feature_flags.go (limited to 'internal') diff --git a/internal/configfile/config_file.go b/internal/configfile/config_file.go index d91ce3e..6ed49d9 100644 --- a/internal/configfile/config_file.go +++ b/internal/configfile/config_file.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "github.com/rfjakob/gocryptfs/internal/contentenc" "github.com/rfjakob/gocryptfs/internal/cryptocore" @@ -55,13 +54,13 @@ func CreateConfFile(filename string, password string, plaintextNames bool, logN cf.EncryptKey(key, password, logN) // Set feature flags - cf.FeatureFlags = append(cf.FeatureFlags, FlagGCMIV128) + cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagGCMIV128]) if plaintextNames { - cf.FeatureFlags = append(cf.FeatureFlags, FlagPlaintextNames) + cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagPlaintextNames]) } else { - cf.FeatureFlags = append(cf.FeatureFlags, FlagDirIV) - cf.FeatureFlags = append(cf.FeatureFlags, FlagEMENames) - cf.FeatureFlags = append(cf.FeatureFlags, FlagLongNames) + cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagDirIV]) + cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagEMENames]) + cf.FeatureFlags = append(cf.FeatureFlags, knownFlags[FlagLongNames]) } // Write file to disk @@ -166,37 +165,3 @@ func (cf *ConfFile) WriteFile() error { return nil } - -const ( - // Understood Feature Flags. - // Also teach isFeatureFlagKnown() about any additions and - // add it to CreateConfFile() if you want to have it enabled by default. - FlagPlaintextNames = "PlaintextNames" - FlagDirIV = "DirIV" - FlagEMENames = "EMENames" - FlagGCMIV128 = "GCMIV128" - FlagLongNames = "LongNames" -) - -// Verify that we understand a feature flag -func (cf *ConfFile) isFeatureFlagKnown(flag string) bool { - switch flag { - case FlagPlaintextNames, FlagDirIV, FlagEMENames, FlagGCMIV128, FlagLongNames: - return true - default: - return false - } -} - -// isFeatureFlagSet - is the feature flag "flagWant" enabled? -func (cf *ConfFile) IsFeatureFlagSet(flagWant string) bool { - if !cf.isFeatureFlagKnown(flagWant) { - log.Panicf("BUG: Tried to use unsupported feature flag %s", flagWant) - } - for _, flag := range cf.FeatureFlags { - if flag == flagWant { - return true - } - } - return false -} diff --git a/internal/configfile/config_test.go b/internal/configfile/config_test.go index c83c4bd..d0540c3 100644 --- a/internal/configfile/config_test.go +++ b/internal/configfile/config_test.go @@ -72,14 +72,23 @@ func TestCreateConfFile(t *testing.T) { } func TestIsFeatureFlagKnown(t *testing.T) { - var cf ConfFile - if !cf.isFeatureFlagKnown(FlagDirIV) { - t.Errorf("This flag should be known") + // Test a few hardcoded values + testKnownFlags := []string{"DirIV", "PlaintextNames", "EMENames", "GCMIV128", "LongNames"} + // And also everything in knownFlags (yes, it is likely that we end up with + // some duplicates. Does not matter.) + for _, f := range knownFlags { + testKnownFlags = append(testKnownFlags, f) } - if !cf.isFeatureFlagKnown(FlagPlaintextNames) { - t.Errorf("This flag should be known") + + var cf ConfFile + for _, f := range testKnownFlags { + if !cf.isFeatureFlagKnown(f) { + t.Errorf("flag %q should be known", f) + } } - if cf.isFeatureFlagKnown("StrangeFeatureFlag") { - t.Errorf("This flag should be NOT known") + + f := "StrangeFeatureFlag" + if cf.isFeatureFlagKnown(f) { + t.Errorf("flag %q should be NOT known", f) } } diff --git a/internal/configfile/feature_flags.go b/internal/configfile/feature_flags.go new file mode 100644 index 0000000..d439363 --- /dev/null +++ b/internal/configfile/feature_flags.go @@ -0,0 +1,41 @@ +package configfile + +type flagIota int + +const ( + FlagPlaintextNames flagIota = iota + FlagDirIV + FlagEMENames + FlagGCMIV128 + FlagLongNames +) + +// knownFlags stores the known feature flags and their string representation +var knownFlags map[flagIota]string = map[flagIota]string{ + FlagPlaintextNames: "PlaintextNames", + FlagDirIV: "DirIV", + FlagEMENames: "EMENames", + FlagGCMIV128: "GCMIV128", + FlagLongNames: "LongNames", +} + +// isFeatureFlagKnown verifies that we understand a feature flag +func (cf *ConfFile) isFeatureFlagKnown(flag string) bool { + for _, knownFlag := range knownFlags { + if knownFlag == flag { + return true + } + } + return false +} + +// isFeatureFlagSet - is the feature flag "flagWant" enabled? +func (cf *ConfFile) IsFeatureFlagSet(flagWant flagIota) bool { + flagString := knownFlags[flagWant] + for _, flag := range cf.FeatureFlags { + if flag == flagString { + return true + } + } + return false +} -- cgit v1.2.3