mysql_protocol_tests.erl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. %% MySQL/OTP – MySQL client library for Erlang/OTP
  2. %% Copyright (C) 2014 Viktor Söderqvist
  3. %%
  4. %% This file is part of MySQL/OTP.
  5. %%
  6. %% MySQL/OTP is free software: you can redistribute it and/or modify it under
  7. %% the terms of the GNU Lesser General Public License as published by the Free
  8. %% Software Foundation, either version 3 of the License, or (at your option)
  9. %% any later version.
  10. %%
  11. %% This program is distributed in the hope that it will be useful, but WITHOUT
  12. %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. %% more details.
  15. %%
  16. %% You should have received a copy of the GNU Lesser General Public License
  17. %% along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. %% @doc Eunit test cases for the mysql_protocol module.
  19. -module(mysql_protocol_tests).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include("protocol.hrl").
  22. -include("records.hrl").
  23. resultset_test() ->
  24. %% A query that returns a result set in the text protocol.
  25. Query = <<"SELECT @@version_comment">>,
  26. ExpectedReq = <<(size(Query) + 1):24/little, 0, ?COM_QUERY, Query/binary>>,
  27. ExpectedResponse = hexdump_to_bin(
  28. "01 00 00 01 01|27 00 00 02 03 64 65 66 00 00 00 .....'....def..."
  29. "11 40 40 76 65 72 73 69 6f 6e 5f 63 6f 6d 6d 65 .@@version_comme"
  30. "6e 74 00 0c 08 00 1c 00 00 00 fd 00 00 1f 00 00| nt.............."
  31. "05 00 00 03 fe 00 00 02 00|1d 00 00 04 1c 4d 79 ..............My"
  32. "53 51 4c 20 43 6f 6d 6d 75 6e 69 74 79 20 53 65 SQL Community Se"
  33. "72 76 65 72 20 28 47 50 4c 29|05 00 00 05 fe 00 rver (GPL)......"
  34. "00 02 00 ..."),
  35. ExpectedCommunication = [{send, ExpectedReq},
  36. {recv, ExpectedResponse}],
  37. FakeSock = fakesocket_create(ExpectedCommunication),
  38. SendFun = fun (Data) -> fakesocket_send(FakeSock, Data) end,
  39. RecvFun = fun (Size) -> fakesocket_recv(FakeSock, Size) end,
  40. ResultSet = mysql_protocol:query(Query, SendFun, RecvFun),
  41. fakesocket_close(FakeSock),
  42. ?assertMatch(#resultset{column_definitions =
  43. [#column_definition{
  44. name = <<"@@version_comment">>}],
  45. rows = [[<<"MySQL Community Server (GPL)">>]]},
  46. ResultSet),
  47. ok.
  48. resultset_error_test() ->
  49. %% A query that returns a response starting as a result set but then
  50. %% interupts itself and decides that it is an error.
  51. Query = <<"EXPLAIN SELECT * FROM dual;">>,
  52. ExpectedReq = <<(size(Query) + 1):24/little, 0, ?COM_QUERY, Query/binary>>,
  53. ExpectedResponse = hexdump_to_bin(
  54. "01 00 00 01 0a 18 00 00 02 03 64 65 66 00 00 00 ..........def..."
  55. "02 69 64 00 0c 3f 00 03 00 00 00 08 a1 00 00 00 .id..?.........."
  56. "00 21 00 00 03 03 64 65 66 00 00 00 0b 73 65 6c .!....def....sel"
  57. "65 63 74 5f 74 79 70 65 00 0c 08 00 13 00 00 00 ect_type........"
  58. "fd 01 00 1f 00 00 1b 00 00 04 03 64 65 66 00 00 ...........def.."
  59. "00 05 74 61 62 6c 65 00 0c 08 00 40 00 00 00 fd ..table....@...."
  60. "00 00 1f 00 00 1a 00 00 05 03 64 65 66 00 00 00 ..........def..."
  61. "04 74 79 70 65 00 0c 08 00 0a 00 00 00 fd 00 00 .type..........."
  62. "1f 00 00 23 00 00 06 03 64 65 66 00 00 00 0d 70 ...#....def....p"
  63. "6f 73 73 69 62 6c 65 5f 6b 65 79 73 00 0c 08 00 ossible_keys...."
  64. "00 10 00 00 fd 00 00 1f 00 00 19 00 00 07 03 64 ...............d"
  65. "65 66 00 00 00 03 6b 65 79 00 0c 08 00 40 00 00 ef....key....@.."
  66. "00 fd 00 00 1f 00 00 1d 00 00 08 03 64 65 66 00 ............def."
  67. "00 00 07 6b 65 79 5f 6c 65 6e 00 0c 08 00 00 10 ...key_len......"
  68. "00 00 fd 00 00 1f 00 00 19 00 00 09 03 64 65 66 .............def"
  69. "00 00 00 03 72 65 66 00 0c 08 00 00 04 00 00 fd ....ref........."
  70. "00 00 1f 00 00 1a 00 00 0a 03 64 65 66 00 00 00 ..........def..."
  71. "04 72 6f 77 73 00 0c 3f 00 0a 00 00 00 08 a0 00 .rows..?........"
  72. "00 00 00 1b 00 00 0b 03 64 65 66 00 00 00 05 45 ........def....E"
  73. "78 74 72 61 00 0c 08 00 ff 00 00 00 fd 01 00 1f xtra............"
  74. "00 00 05 00 00 0c fe 00 00 02 00 17 00 00 0d ff ................"
  75. "48 04 23 48 59 30 30 30 4e 6f 20 74 61 62 6c 65 H.#HY000No table"
  76. "73 20 75 73 65 64 s used"),
  77. Sock = fakesocket_create([{send, ExpectedReq}, {recv, ExpectedResponse}]),
  78. SendFun = fun (Data) -> fakesocket_send(Sock, Data) end,
  79. RecvFun = fun (Size) -> fakesocket_recv(Sock, Size) end,
  80. Result = mysql_protocol:query(Query, SendFun, RecvFun),
  81. ?assertMatch(#error{}, Result),
  82. fakesocket_close(Sock),
  83. ok.
  84. prepare_test() ->
  85. %% Prepared statement. The example from "14.7.4 COM_STMT_PREPARE" in the
  86. %% "MySQL Internals" guide.
  87. Query = <<"SELECT CONCAT(?, ?) AS col1">>,
  88. ExpectedReq = hexdump_to_bin(
  89. "1c 00 00 00 16 53 45 4c 45 43 54 20 43 4f 4e 43 .....SELECT CONC"
  90. "41 54 28 3f 2c 20 3f 29 20 41 53 20 63 6f 6c 31 AT(?, ?) AS col1"
  91. ),
  92. ExpectedResp = hexdump_to_bin(
  93. "0c 00 00 01 00 01 00 00 00 01 00 02 00 00 00 00| ................"
  94. "17 00 00 02 03 64 65 66 00 00 00 01 3f 00 0c 3f .....def....?..?"
  95. "00 00 00 00 00 fd 80 00 00 00 00|17 00 00 03 03 ................"
  96. "64 65 66 00 00 00 01 3f 00 0c 3f 00 00 00 00 00 def....?..?....."
  97. "fd 80 00 00 00 00|05 00 00 04 fe 00 00 02 00|1a ................"
  98. "00 00 05 03 64 65 66 00 00 00 04 63 6f 6c 31 00 ....def....col1."
  99. "0c 3f 00 00 00 00 00 fd 80 00 1f 00 00|05 00 00 .?.............."
  100. "06 fe 00 00 02 00 ......"),
  101. Sock = fakesocket_create([{send, ExpectedReq}, {recv, ExpectedResp}]),
  102. SendFun = fun (Data) -> fakesocket_send(Sock, Data) end,
  103. RecvFun = fun (Size) -> fakesocket_recv(Sock, Size) end,
  104. Result = mysql_protocol:prepare(Query, SendFun, RecvFun),
  105. fakesocket_close(Sock),
  106. ?assertMatch(#prepared{statement_id = StmtId,
  107. param_count = 2,
  108. warning_count = 0} when is_integer(StmtId),
  109. Result),
  110. ok.
  111. %% --- Helper functions for the above tests ---
  112. %% Convert hex dumps to binaries. This is a helper function for the tests.
  113. %% This function is also tested below.
  114. hexdump_to_bin(HexDump) ->
  115. hexdump_to_bin(iolist_to_binary(HexDump), <<>>).
  116. hexdump_to_bin(<<Line:50/binary, _Junk:20/binary, Rest/binary>>, Acc) ->
  117. hexdump_to_bin(Line, Rest, Acc);
  118. hexdump_to_bin(<<Line:50/binary, _Junk/binary>>, Acc) ->
  119. %% last line (shorter than 70)
  120. hexdump_to_bin(Line, <<>>, Acc);
  121. hexdump_to_bin(<<>>, Acc) ->
  122. Acc.
  123. hexdump_to_bin(Line, Rest, Acc) ->
  124. HexNums = re:split(Line, <<"[ |]+">>, [{return, list}, trim]),
  125. Acc1 = lists:foldl(fun (HexNum, Acc0) ->
  126. {ok, [Byte], []} = io_lib:fread("~16u", HexNum),
  127. <<Acc0/binary, Byte:8>>
  128. end,
  129. Acc,
  130. HexNums),
  131. hexdump_to_bin(Rest, Acc1).
  132. hexdump_to_bin_test() ->
  133. HexDump =
  134. "0e 00 00 00 03 73 65 6c 65 63 74 20 55 53 45 52 .....select USER"
  135. "28 29 ()",
  136. Expect = <<16#0e, 16#00, 16#00, 16#00, 16#03, 16#73, 16#65, 16#6c,
  137. 16#65, 16#63, 16#74, 16#20, 16#55, 16#53, 16#45, 16#52,
  138. 16#28, 16#29>>,
  139. ?assertEqual(Expect, hexdump_to_bin(HexDump)).
  140. %% --- Fake socket ---
  141. %%
  142. %% A "fake socket" is used in test where we need to mock socket communication.
  143. %% It is a pid maintaining a list of expected send and recv events.
  144. %% @doc Creates a fakesocket process with a buffer of expected recv and send
  145. %% calls. The pid of the fakesocket process is returned.
  146. -spec fakesocket_create([{recv, binary()} | {send, binary()}]) -> pid().
  147. fakesocket_create(ExpectedEvents) ->
  148. spawn_link(fun () -> fakesocket_loop(ExpectedEvents) end).
  149. %% @doc Receives NumBytes bytes from fakesocket Pid. This function can be used
  150. %% as a replacement for gen_tcp:recv/2 in unit tests. If there not enough data
  151. %% in the fakesocket's buffer, an error is raised.
  152. fakesocket_recv(Pid, NumBytes) ->
  153. Pid ! {recv, NumBytes, self()},
  154. receive
  155. {ok, Data} -> {ok, Data};
  156. error -> error({unexpected_recv, NumBytes})
  157. after 100 ->
  158. error(noreply)
  159. end.
  160. %% @doc Sends data to fa fakesocket. This can be used as replacement for
  161. %% gen_tcp:send/2 in unit tests. If the data sent is not what the fakesocket
  162. %% expected, an error is raised.
  163. fakesocket_send(Pid, Data) ->
  164. Pid ! {send, iolist_to_binary(Data), self()},
  165. receive
  166. ok -> ok;
  167. error -> error({unexpected_send, Data})
  168. after 100 ->
  169. error(noreply)
  170. end.
  171. %% Stops the fakesocket process. If the fakesocket's buffer is not empty,
  172. %% an error is raised.
  173. fakesocket_close(Pid) ->
  174. Pid ! {done, self()},
  175. receive
  176. ok -> ok;
  177. {remains, Remains} -> error({unexpected_close, Remains})
  178. after 100 ->
  179. error(noreply)
  180. end.
  181. %% Used by fakesocket_create/1.
  182. fakesocket_loop(AllEvents = [{Func, Data} | Events]) ->
  183. receive
  184. {recv, NumBytes, FromPid} when Func == recv, NumBytes == size(Data) ->
  185. FromPid ! {ok, Data},
  186. fakesocket_loop(Events);
  187. {recv, NumBytes, FromPid} when Func == recv, NumBytes < size(Data) ->
  188. <<Data1:NumBytes/binary, Rest/binary>> = Data,
  189. FromPid ! {ok, Data1},
  190. fakesocket_loop([{recv, Rest} | Events]);
  191. {send, Bytes, FromPid} when Func == send, Bytes == Data ->
  192. FromPid ! ok,
  193. fakesocket_loop(Events);
  194. {send, Bytes, FromPid} when Func == send, size(Bytes) < size(Data) ->
  195. Size = size(Bytes),
  196. case Data of
  197. <<Bytes:Size/binary, Rest/binary>> ->
  198. FromPid ! ok,
  199. fakesocket_loop([{send, Rest} | Events]);
  200. _ ->
  201. FromPid ! error
  202. end;
  203. {_, _, FromPid} ->
  204. FromPid ! error;
  205. {done, FromPid} ->
  206. FromPid ! {remains, AllEvents}
  207. end;
  208. fakesocket_loop([]) ->
  209. receive
  210. {done, FromPid} -> FromPid ! ok;
  211. {_, _, FromPid} -> FromPid ! error
  212. end.
  213. %% Tests for the fakesocket functions.
  214. fakesocket_bad_recv_test() ->
  215. Pid = fakesocket_create([{recv, <<"foobar">>}]),
  216. ?assertError(_, fakesocket_recv(Pid, 10)).
  217. fakesocket_success_test() ->
  218. Pid = fakesocket_create([{recv, <<"foobar">>}, {send, <<"baz">>}]),
  219. %?assertError({unexpected_close, _}, fakesocket_close(Pid)),
  220. ?assertEqual({ok, <<"foo">>}, fakesocket_recv(Pid, 3)),
  221. ?assertEqual({ok, <<"bar">>}, fakesocket_recv(Pid, 3)),
  222. ?assertEqual(ok, fakesocket_send(Pid, <<"baz">>)),
  223. ?assertEqual(ok, fakesocket_close(Pid)),
  224. %% The process will exit after close. Another recv will raise noreply.
  225. ?assertError(noreply, fakesocket_recv(Pid, 3)).