1%%
2%% Licensed to the Apache Software Foundation (ASF) under one
3%% or more contributor license agreements. See the NOTICE file
4%% distributed with this work for additional information
5%% regarding copyright ownership. The ASF licenses this file
6%% to you under the Apache License, Version 2.0 (the
7%% "License"); you may not use this file except in compliance
8%% with the License. You may obtain a copy of the License at
9%%
10%%   http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing,
13%% software distributed under the License is distributed on an
14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15%% KIND, either express or implied. See the License for the
16%% specific language governing permissions and limitations
17%% under the License.
18%%
19
20-module(test_thrift_file_transport).
21-include_lib("eunit/include/eunit.hrl").
22
23
24new(File) -> thrift_file_transport:new(File).
25new(File, Opts) -> thrift_file_transport:new(File, Opts).
26
27new_test_() ->
28  [
29    {"new file", ?_assertMatch(
30      {ok, {_, thrift_file_transport, {t_file, a_fake_file, true, write}}},
31      new(a_fake_file)
32    )},
33    {"new file in read mode", ?_assertMatch(
34      {ok, {_, thrift_file_transport, {t_file, a_fake_file, true, read}}},
35      new(a_fake_file, [{mode, read}])
36    )},
37    {"new file in write mode", ?_assertMatch(
38      {ok, {_, thrift_file_transport, {t_file, a_fake_file, true, write}}},
39      new(a_fake_file, [{mode, write}])
40    )},
41    {"new file in should_close true mode", ?_assertMatch(
42      {ok, {_, thrift_file_transport, {t_file, a_fake_file, true, write}}},
43      new(a_fake_file, [{should_close, true}])
44    )},
45    {"new file in should_close false mode", ?_assertMatch(
46      {ok, {_, thrift_file_transport, {t_file, a_fake_file, false, write}}},
47      new(a_fake_file, [{should_close, false}])
48    )}
49  ].
50
51
52read(File, Bytes) -> thrift_file_transport:read(File, Bytes).
53
54read_test_() ->
55  {setup,
56    fun() ->
57      meck:new(file, [unstick, passthrough]),
58      meck:expect(file, read, fun(Bin, N) ->
59        {Result, _} = split_binary(Bin, min(iolist_size(Bin), N)),
60        {ok, Result}
61      end)
62    end,
63    fun(_) -> meck:unload(file) end,
64    [
65      {"read zero bytes from empty file", ?_assertMatch(
66        {_, {ok, <<>>}},
67        read({t_file, <<>>, true, read}, 0)
68      )},
69      {"read 1 byte from empty file", ?_assertMatch(
70        {_, {ok, <<>>}},
71        read({t_file, <<>>, true, read}, 1)
72      )},
73      {"read zero bytes from nonempty file", ?_assertMatch(
74        {_, {ok, <<>>}},
75        read({t_file, <<"hallo world">>, true, read}, 0)
76      )},
77      {"read 1 byte from nonempty file", ?_assertMatch(
78        {_, {ok, <<"h">>}},
79        read({t_file, <<"hallo world">>, true, read}, 1)
80      )},
81      {"read a zillion bytes from nonempty file", ?_assertMatch(
82        {_, {ok, <<"hallo world">>}},
83        read({t_file, <<"hallo world">>, true, read}, 65536)
84      )},
85      {"read 0 byte from file in write mode", ?_assertMatch(
86        {_, {error, write_mode}},
87        read({t_file, <<>>, true, write}, 0)
88      )},
89      {"read 1 byte from file in write mode", ?_assertMatch(
90        {_, {error, write_mode}},
91        read({t_file, <<>>, true, write}, 1)
92      )}
93    ]
94  }.
95
96
97read_exact(File, Bytes) -> thrift_file_transport:read_exact(File, Bytes).
98
99read_exact_test_() ->
100  {setup,
101    fun() ->
102      meck:new(file, [unstick, passthrough]),
103      meck:expect(file, read, fun(Bin, N) ->
104        {Result, _} = split_binary(Bin, min(iolist_size(Bin), N)),
105        {ok, Result}
106      end)
107    end,
108    fun(_) -> meck:unload(file) end,
109    [
110      {"read exactly zero bytes from empty file", ?_assertMatch(
111        {_, {ok, <<>>}},
112        read_exact({t_file, <<>>, true, read}, 0)
113      )},
114      {"read exactly 1 byte from empty file", ?_assertMatch(
115        {_, {error, eof}},
116        read_exact({t_file, <<>>, true, read}, 1)
117      )},
118      {"read exactly zero bytes from nonempty file", ?_assertMatch(
119        {_, {ok, <<>>}},
120        read_exact({t_file, <<"hallo world">>, true, read}, 0)
121      )},
122      {"read exactly 1 byte from nonempty file", ?_assertMatch(
123        {_, {ok, <<"h">>}},
124        read_exact({t_file, <<"hallo world">>, true, read}, 1)
125      )},
126      {"read exactly a zillion bytes from nonempty file", ?_assertMatch(
127        {_, {error, eof}},
128        read_exact({t_file, <<"hallo world">>, true, read}, 65536)
129      )},
130      {"read exactly 0 byte from file in write mode", ?_assertMatch(
131        {_, {error, write_mode}},
132        read_exact({t_file, <<>>, true, write}, 0)
133      )},
134      {"read exactly 1 byte from file in write mode", ?_assertMatch(
135        {_, {error, write_mode}},
136        read_exact({t_file, <<>>, true, write}, 1)
137      )}
138    ]
139  }.
140
141
142write(File, Data) -> thrift_file_transport:write(File, Data).
143
144write_test_() ->
145  {setup,
146    fun() ->
147      meck:new(file, [unstick, passthrough]),
148      meck:expect(file, write, fun(_, _) -> ok end)
149    end,
150    fun(_) -> meck:unload(file) end,
151    [
152      {"write empty list to file", ?_assertMatch(
153        {{t_file, a_fake_file, true, write}, ok},
154        write({t_file, a_fake_file, true, write}, [])
155      )},
156      {"write empty binary to file", ?_assertMatch(
157        {{t_file, a_fake_file, true, write}, ok},
158        write({t_file, a_fake_file, true, write}, <<>>)
159      )},
160      {"write a list to file", ?_assertMatch(
161        {{t_file, a_fake_file, true, write}, ok},
162        write({t_file, a_fake_file, true, write}, "hallo world")
163      )},
164      {"write a binary to file", ?_assertMatch(
165        {{t_file, a_fake_file, true, write}, ok},
166        write({t_file, a_fake_file, true, write}, <<"hallo world">>)
167      )},
168      {"write a binary to file in read mode", ?_assertMatch(
169        {_, {error, read_mode}},
170        write({t_file, a_fake_file, true, read}, <<"hallo world">>)
171      )},
172      {"write a list to file in read mode", ?_assertMatch(
173        {_, {error, read_mode}},
174        write({t_file, a_fake_file, true, read}, "hallo world")
175      )}
176    ]
177  }.
178
179
180flush(Transport) -> thrift_file_transport:flush(Transport).
181
182flush_test_() ->
183  {setup,
184    fun() ->
185      meck:new(file, [unstick, passthrough]),
186      meck:expect(file, sync, fun(_File) -> ok end)
187    end,
188    fun(_) -> meck:unload(file) end,
189    [
190      {"flush file", ?_assertMatch(
191        {{t_file, a_fake_file, true, write}, ok},
192        flush({t_file, a_fake_file, true, write})
193      )}
194    ]
195  }.
196
197
198close(Transport) -> thrift_file_transport:close(Transport).
199
200close_test_() ->
201  {setup,
202    fun() ->
203      meck:new(file, [unstick, passthrough]),
204      meck:expect(file, close, fun(_) -> ok end)
205    end,
206    fun(_) -> meck:unload(file) end,
207    [
208      {"close file", ?_assertMatch(
209        {{t_file, a_fake_file, true, write}, ok},
210        close({t_file, a_fake_file, true, write})
211      )}
212    ]
213  }.