Skip to content

Commit

Permalink
Merge pull request #9 from staticfloat/sf/coveragebaby
Browse files Browse the repository at this point in the history
Increase coverage
  • Loading branch information
staticfloat committed Aug 19, 2015
2 parents 9fb9433 + dc31f63 commit 98f4e99
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/base_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ sigma0_512(x) = (S64( 1, @compat(UInt64(x))) $ S64( 8, @compat(UInt64(x))) $ R
sigma1_512(x) = (S64(19, @compat(UInt64(x))) $ S64(61, @compat(UInt64(x))) $ R( 6, @compat(UInt64(x))))

# Let's be able to bswap arrays of these types as well
bswap!(x::Vector{UInt32}) = map!(bswap, x)
bswap!(x::Vector{UInt64}) = map!(bswap, x)
bswap!(x::Vector{UInt128}) = map!(bswap, x)
bswap!{T<:Integer}(x::Vector{T}) = map!(bswap, x)
95 changes: 69 additions & 26 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using SHA
using Compat

# Define some data we will run our tests on
lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
so_many_as = repmat([0x61], 1000000)
data = Any["", "test", lorem, so_many_as]

# Descriptions of the data, the SHA functions we'll run on the data, etc...
data_desc = ["the empty string", "the string \"test\"", "lorem ipsum", "one million a's"]
sha_types = [SHA.SHA1_CTX, SHA.SHA224_CTX, SHA.SHA256_CTX, SHA.SHA384_CTX, SHA.SHA512_CTX]
sha_funcs = [sha1, sha224, sha256, sha384, sha512]

answers = @compat Dict(
Expand Down Expand Up @@ -40,52 +44,91 @@ sha512 => [
]
)

# First, test one-shots on everything
# Our code coverage reaches all the way to the show() methods for the SHA types, and gives us
# an excuse to give readers of this code a little mental parsing workout.
println("Loaded hash types: $(join([split(string(t()))[1] for t in sha_types], ", ", " and "))")

# First, test processing the data in one go
nerrors = 0
for idx in 1:length(data)
println("Testing on $(data_desc[idx]):")
desc = data_desc[idx]
print("Testing on $desc$(join(["." for z in 1:(34-length(desc))]))")
nerrors_old = nerrors
for sha_func in sha_funcs
hash = sha_func(data[idx])
print(" $("$(sha_func)"[1:min(6,end)]): ")
if hash != answers[sha_func][idx]
print("\n")
warn(
"""
Expected:
For $("$(sha_func)"[1:min(6,end)]) expected:
$(answers[sha_func][idx])
Calculated:
$(hash)
""")
nerrors += 1
else
println("OK")
print(".")
end
end
println("Done! [$(nerrors - nerrors_old) errors]")
end

# Do a final test on the "so many a's" data where we chunk up the data into
# two chunks (sized appropriately to avoid padding) to test multiple update!() calls
println("Testing on one million a's (chunked):")
for (ctx_func, sha_func) in [(SHA.SHA1_CTX, sha1),
(SHA.SHA224_CTX, sha224),
(SHA.SHA256_CTX, sha256),
(SHA.SHA384_CTX, sha384),
(SHA.SHA512_CTX, sha512)]
ctx = ctx_func()
# Do another test on the "so many a's" data where we chunk up the data into
# two chunks, (sized appropriately to AVOID overflow from one update to another)
# in order to test multiple update!() calls
print("Testing on one million a's (chunked properly)")
nerrors_old = nerrors
for idx in 1:length(sha_funcs)
ctx = sha_types[idx]()
SHA.update!(ctx, so_many_as[1:2*SHA.blocklen(typeof(ctx))])
SHA.update!(ctx, so_many_as[2*SHA.blocklen(typeof(ctx))+1:end])
hash = bytes2hex(SHA.digest!(ctx))
print(" $("$(sha_func)"[1:min(6,end)]): ")
if hash != answers[sha_func][end]
if hash != answers[sha_funcs[idx]][end]
print("\n")
warn(
"""
Expected:
$(answers[sha_func][end-1])
Calculated:
$(hash)
""")
nerrors += 1
else
println("OK")
end
"""
For $("$(sha_funcs[idx])"[1:min(6,end)]) expected:
$(answers[sha_funcs[idx]][end-1])
Calculated:
$(hash)
""")
nerrors += 1
else
print(".")
end
end
println("Done! [$(nerrors - nerrors_old) errors]")

# Do another test on the "so many a's" data where we chunk up the data into
# three chunks, (sized appropriately to CAUSE overflow from one update to another)
# in order to test multiple update!() calls as well as the overflow codepaths
print("Testing on one million a's (chunked clumsily)")
nerrors_old = nerrors
for idx in 1:length(sha_funcs)
ctx = sha_types[idx]()
SHA.update!(ctx, so_many_as[1:int(1.3*SHA.blocklen(typeof(ctx)))])
SHA.update!(ctx, so_many_as[int(1.3*SHA.blocklen(typeof(ctx)))+1:int(1.7*SHA.blocklen(typeof(ctx)))])
SHA.update!(ctx, so_many_as[int(1.7*SHA.blocklen(typeof(ctx)))+1:end])
hash = bytes2hex(SHA.digest!(ctx))
if hash != answers[sha_funcs[idx]][end]
print("\n")
warn(
"""
For $("$(sha_funcs[idx])"[1:min(6,end)]) expected:
$(answers[sha_funcs[idx]][end-1])
Calculated:
$(hash)
""")
nerrors += 1
else
print(".")
end
end
println("Done! [$(nerrors - nerrors_old) errors]")

if nerrors == 0
println("ALL OK")
else
println("Failed with $nerrors failures")
end
exit(nerrors)

0 comments on commit 98f4e99

Please sign in to comment.