Skip to content

Commit 2baecd7

Browse files
committed
fixing bugs exposed by dialyzer
1 parent 09efe85 commit 2baecd7

File tree

6 files changed

+31
-42
lines changed

6 files changed

+31
-42
lines changed

lib/scenic/cache/file.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ defmodule Scenic.Cache.File do
8181
defp do_unzip(data, opts) do
8282
case opts[:decompress] do
8383
true ->
84-
case :zlib.gunzip(data) do
85-
bin when is_binary(bin) -> {:ok, bin}
86-
_ -> {:error, :gunzip}
87-
end
84+
{:ok, :zlib.gunzip(data)}
8885

8986
_ ->
9087
# not decompressing

lib/scenic/cache/hash.ex

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,16 @@ defmodule Scenic.Cache.Hash do
3434

3535
# --------------------------------------------------------
3636
def binary(data, hash_type) do
37-
valid_hash_type!(hash_type)
38-
|> :crypto.hash(data)
39-
|> Base.url_encode64(padding: false)
37+
case valid_hash_type?(hash_type) do
38+
true -> {:ok, :crypto.hash(hash_type, data) |> Base.url_encode64(padding: false)}
39+
false -> {:error, :invalid_hash_type}
40+
end
4041
end
4142

4243
def binary!(data, hash_type) do
43-
{:ok, hash} =
44-
valid_hash_type!(hash_type)
45-
|> :crypto.hash(data)
46-
|> Base.url_encode64(padding: false)
47-
48-
hash
44+
valid_hash_type!(hash_type)
45+
|> :crypto.hash(data)
46+
|> Base.url_encode64(padding: false)
4947
end
5048

5149
# --------------------------------------------------------
@@ -99,15 +97,15 @@ defmodule Scenic.Cache.Hash do
9997

10098
# --------------------------------------------------------
10199
def verify(data, hash, hash_type) do
102-
case binary(data, hash_type) == hash do
103-
true -> {:ok, data}
104-
false -> {:error, :hash_failure}
100+
case binary(data, hash_type) do
101+
{:ok, ^hash} -> {:ok, data}
102+
_ -> {:error, :hash_failure}
105103
end
106104
end
107105

108106
# --------------------------------------------------------
109107
def verify!(data, hash, hash_type) do
110-
case binary(data, hash_type) == hash do
108+
case binary!(data, hash_type) == hash do
111109
true -> data
112110
false -> raise Error
113111
end

lib/scenic/cache/term.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ defmodule Scenic.Cache.Term do
1919
# hashes. Is also slower because it has to load the file and compute the hash
2020
# to use as a key even it is is already loaded into the cache.
2121
def load(path, :insecure, opts) do
22-
with {:ok, data} <- Cache.File.read(path, :insecure, opts) do
23-
hash = Hash.binary(data, opts[:hash] || :sha)
24-
22+
with {:ok, data} <- Cache.File.read(path, :insecure, opts),
23+
{:ok, hash} <- Hash.binary(data, opts[:hash] || :sha) do
2524
case Cache.claim(hash, opts[:scope]) do
2625
true ->
2726
{:ok, hash}

lib/utilities.ex

Lines changed: 0 additions & 17 deletions
This file was deleted.

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule Scenic.Mixfile do
2020
make_targets: ["all"],
2121
make_clean: ["clean"],
2222
make_env: make_env(),
23-
dialyzer: [plt_add_deps: :transitive, plt_add_apps: [:mix, :iex, :scenic_math]],
23+
dialyzer: [plt_add_deps: :transitive, plt_add_apps: [:mix, :iex]],
2424
test_coverage: [tool: ExCoveralls],
2525
preferred_cli_env: [
2626
coveralls: :test,

test/scenic/cache/hash_test.exs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,25 @@ defmodule Scenic.Cache.HashTest do
2929
test "binary computes a hash for some binary data" do
3030
data = "some data. af98hwu4lhrliw4uhtliuhet;giojres;ihg;usdhg"
3131
expected_hash = :crypto.hash(:sha, data) |> Base.url_encode64(padding: false)
32-
assert Hash.binary(data, :sha) == expected_hash
32+
assert Hash.binary(data, :sha) == {:ok, expected_hash}
33+
end
34+
35+
test "binary rejects invalid hash types" do
36+
data = "some data. af98hwu4lhrliw4uhtliuhet;giojres;ihg;usdhg"
37+
assert Hash.binary(data, :invalid) == {:error, :invalid_hash_type}
3338
end
3439

3540
test "binary! computes a hash for some binary data" do
3641
data = "some data. af98hwu4lhrliw4uhtliuhet;giojres;ihg;usdhg"
3742
expected_hash = :crypto.hash(:sha, data) |> Base.url_encode64(padding: false)
38-
assert Hash.binary(data, :sha) == expected_hash
43+
assert Hash.binary!(data, :sha) == expected_hash
44+
end
45+
46+
test "binary! raises on an invalid hash type" do
47+
data = "some data. af98hwu4lhrliw4uhtliuhet;giojres;ihg;usdhg"
48+
assert_raise Scenic.Cache.Hash.Error, fn ->
49+
Hash.binary!(data, :invalid)
50+
end
3951
end
4052

4153
# ============================================================================
@@ -73,7 +85,7 @@ defmodule Scenic.Cache.HashTest do
7385

7486
test "verify returns {:ok, data} when the hash checks out ok" do
7587
data = "This is some data to hash - awleiufhoq34htuwehtljwuh5toihu"
76-
expected = Hash.binary(data, :sha)
88+
expected = Hash.binary!(data, :sha)
7789
assert Hash.verify(data, expected, :sha) == {:ok, data}
7890
end
7991

@@ -87,7 +99,7 @@ defmodule Scenic.Cache.HashTest do
8799

88100
test "verify! returns data when the hash checks out ok" do
89101
data = "This is some data to hash - awleiufhoq34htuwehtljwuh5toihu"
90-
expected = Hash.binary(data, :sha)
102+
expected = Hash.binary!(data, :sha)
91103
assert Hash.verify!(data, expected, :sha) == data
92104
end
93105

0 commit comments

Comments
 (0)