Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Bech32 character mutation test. #332

Merged
merged 1 commit into from
May 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions lib/bech32/test/Codec/Binary/Bech32Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,17 @@ spec = do
Right (humanReadablePart v, unencodedDataPart v)

describe "Decoding a corrupted string should fail" $ do
let chooseWithinDataPart originalString = do
let sepIx = maybe
(error "couldn't find separator in valid bech32 string")
(\ix -> T.length originalString - ix - 1)
(T.findIndex (== '1') (T.reverse originalString))
choose (sepIx + 1, T.length originalString - 2)

it "Decoding fails when an adjacent pair of characters is swapped." $
property $ withMaxSuccess 10000 $ \s -> do
let originalString = getValidBech32String s
index <- choose (0, T.length originalString - 2)
index <- chooseWithinDataPart originalString
let prefix = T.take index originalString
let suffix = T.drop (index + 2) originalString
let char1 = T.singleton (T.index originalString index)
Expand All @@ -150,7 +156,7 @@ spec = do
it "Decoding fails when a character is omitted." $
property $ withMaxSuccess 10000 $ \s -> do
let originalString = getValidBech32String s
index <- choose (0, T.length originalString - 1)
index <- chooseWithinDataPart originalString
let char = T.index originalString index
let prefix = T.take index originalString
let suffix = T.drop (index + 1) originalString
Expand Down Expand Up @@ -179,7 +185,7 @@ spec = do
property $ withMaxSuccess 10000 $ \s c -> do
let originalString = getValidBech32String s
let char = getDataChar c
index <- choose (0, T.length originalString - 1)
index <- chooseWithinDataPart originalString
let prefix = T.take index originalString
let suffix = T.drop index originalString
let corruptedString = prefix <> T.singleton char <> suffix
Expand All @@ -203,9 +209,9 @@ spec = do
&& T.all (== 'q') (T.dropEnd 1 suffix))

it "Decoding fails when a single character is mutated." $
property $ \s c -> do
withMaxSuccess 10000 $ property $ \s c -> do
let originalString = getValidBech32String s
index <- choose (0, T.length originalString - 1)
index <- chooseWithinDataPart originalString
let originalChar = T.index originalString index
let replacementChar = getDataChar c
let prefix = T.take index originalString
Expand Down Expand Up @@ -235,9 +241,9 @@ spec = do

it "Decoding fails for an upper-case string with a lower-case \
\character." $
property $ \s -> do
withMaxSuccess 10000 $ property $ \s -> do
let originalString = T.map toUpper $ getValidBech32String s
index <- choose (0, T.length originalString - 1)
index <- chooseWithinDataPart originalString
let prefix = T.take index originalString
let suffix = T.drop (index + 1) originalString
let char = toLower $ T.index originalString index
Expand All @@ -255,9 +261,9 @@ spec = do

it "Decoding fails for a lower-case string with an upper-case \
\character." $
property $ \s -> do
withMaxSuccess 10000 $ property $ \s -> do
let originalString = T.map toLower $ getValidBech32String s
index <- choose (0, T.length originalString - 1)
index <- chooseWithinDataPart originalString
let prefix = T.take index originalString
let suffix = T.drop (index + 1) originalString
let char = toUpper $ T.index originalString index
Expand Down