This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
trailing_hash_read_device_test.exs
186 lines (137 loc) · 4.8 KB
/
trailing_hash_read_device_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
defmodule Xgit.Util.TrailingHashReadDeviceTest do
use ExUnit.Case, async: true
alias Xgit.Util.TrailingHashReadDevice, as: THR
import ExUnit.CaptureLog
describe "test infrastructure" do
test "string_with_trailing_hash/1" do
s = string_with_trailing_hash("hello")
assert byte_size(s) == 25
assert s ==
"hello" <>
<<170, 244, 198, 29, 220, 197, 232, 162, 218, 190, 222, 15, 59, 72, 44, 217, 174,
169, 67, 77>>
end
end
describe "open_file/1" do
test "simple file" do
assert {:ok, device} = open_file_with_trailing_hash("hello")
assert THR.valid?(device)
assert "hello" = IO.binread(device, 5)
assert :eof = IO.binread(device, 5)
assert THR.valid_hash?(device)
end
test "multiple reads" do
assert {:ok, device} = open_file_with_trailing_hash("hello, goodbye")
assert "hello" = IO.binread(device, 5)
assert ", " = IO.binread(device, 2)
assert "goodbye" = IO.binread(device, 7)
assert :eof = IO.binread(device, 1)
assert THR.valid_hash?(device)
end
test "immediate EOF if file is <= 20 bytes long" do
Temp.track!()
path = Temp.path!()
File.write!(path, "hello")
assert {:ok, device} = THR.open_file(path)
assert is_pid(device)
assert :eof = IO.binread(device, 5)
refute THR.valid_hash?(device)
end
test "hash is invalid" do
Temp.track!()
path = Temp.path!()
File.write!(path, "hellobogusbogusbogusbogus")
assert {:ok, device} = THR.open_file(path)
assert is_pid(device)
assert "hello" = IO.binread(device, 5)
assert :eof = IO.binread(device, 5)
refute THR.valid_hash?(device)
end
test "Posix error" do
Temp.track!()
path = Temp.path!()
File.mkdir_p!(path)
assert {:error, :eisdir} = THR.open_file(path)
end
test "error :too_soon" do
assert {:ok, device} = open_file_with_trailing_hash("hello, goodbye")
assert "hello" = IO.binread(device, 5)
assert THR.valid_hash?(device) == :too_soon
end
test "error :already_called" do
assert {:ok, device} = open_file_with_trailing_hash("hello")
assert "hello" = IO.binread(device, 5)
assert :eof = IO.binread(device, 5)
assert THR.valid_hash?(device)
assert THR.valid_hash?(device) == :already_called
end
test "error: unexpected io request" do
assert {:ok, device} = open_file_with_trailing_hash("hello")
assert capture_log(fn ->
assert {:error, :request} = IO.binwrite(device, "hello")
end) =~
~s(TrailingHashReadDevice received unexpected iorequest {:put_chars, :latin1, "hello"})
end
test "warn: unexpected message" do
assert {:ok, device} = open_file_with_trailing_hash("hello, goodbye")
assert capture_log(fn ->
send(device, :random_unknown_message)
Process.sleep(10)
# Give time for message to land.
end) =~ "TrailingHashReadDevice received unexpected message :random_unknown_message"
end
test "warn: unexpected call" do
assert {:ok, device} = open_file_with_trailing_hash("hello, goodbye")
assert capture_log(fn ->
assert :unknown_message = GenServer.call(device, :random_unknown_call)
end) =~ "TrailingHashReadDevice received unexpected call :random_unknown_call"
end
end
describe "open_string/1" do
test "simple file" do
assert {:ok, device} = open_string_with_trailing_hash("hello")
assert THR.valid?(device)
assert "hello" = IO.binread(device, 5)
assert :eof = IO.binread(device, 5)
assert THR.valid_hash?(device)
end
test "FunctionClauseError if string is <= 20 bytes long" do
assert_raise FunctionClauseError, fn ->
THR.open_string("hello")
end
end
test "hash is invalid" do
assert {:ok, device} = THR.open_string("hellobogusbogusbogusbogus")
assert "hello" = IO.binread(device, 5)
assert :eof = IO.binread(device, 5)
refute THR.valid_hash?(device)
end
end
describe "valid?/1" do
test "other process" do
{:ok, pid} = GenServer.start_link(NotValid, nil)
refute THR.valid?(pid)
end
test "not a PID" do
refute THR.valid?("blah")
end
end
defp open_file_with_trailing_hash(s) do
Temp.track!()
path = Temp.path!()
File.write!(path, string_with_trailing_hash(s))
THR.open_file(path)
end
defp open_string_with_trailing_hash(s) do
s
|> string_with_trailing_hash()
|> THR.open_string()
end
defp string_with_trailing_hash(s), do: s <> hash_for_string(s)
defp hash_for_string(s) do
:sha
|> :crypto.hash_init()
|> :crypto.hash_update(s)
|> :crypto.hash_final()
end
end