tighten file2key to not produce invalid keys with no keyName

A file named "foo-" or "foo-bar" was taken as a key's file, with a backend
of "foo", and an empty keyName. This led to various problems, especially
because converting that key back to a file did not yeild the same filename.
This commit is contained in:
Joey Hess 2013-10-16 12:46:24 -04:00
parent 15597e5fff
commit 396e47b07e
4 changed files with 21 additions and 3 deletions

View file

@ -14,7 +14,8 @@ module Types.Key (
key2file,
file2key,
prop_idempotent_key_encode
prop_idempotent_key_encode,
prop_idempotent_key_decode
) where
import System.Posix.Types
@ -59,7 +60,7 @@ key2file Key { keyBackendName = b, keySize = s, keyMtime = m, keyName = n } =
_ ?: _ = ""
file2key :: FilePath -> Maybe Key
file2key s = if key == Just stubKey then Nothing else key
file2key s = if key == Just stubKey || (keyName <$> key) == Just "" then Nothing else key
where
key = startbackend stubKey s
@ -88,3 +89,8 @@ instance Arbitrary Key where
prop_idempotent_key_encode :: Key -> Bool
prop_idempotent_key_encode k = Just k == (file2key . key2file) k
prop_idempotent_key_decode :: FilePath -> Bool
prop_idempotent_key_decode f
| null f = True -- skip illegal empty filename
| otherwise = maybe True (\k -> key2file k == f) (file2key f)