mysql_protocol_tests.erl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. %% Most of the hexdump tests are from examples in the protocol documentation.
  20. %%
  21. %% TODO: Use ngrep -x -q -d lo '' 'port 3306' to dump traffic using various
  22. %% server versions.
  23. -module(mysql_protocol_tests).
  24. -include_lib("eunit/include/eunit.hrl").
  25. -include("protocol.hrl").
  26. -include("records.hrl").
  27. resultset_test() ->
  28. %% A query that returns a result set in the text protocol.
  29. Query = <<"SELECT @@version_comment">>,
  30. ExpectedReq = <<(size(Query) + 1):24/little, 0, ?COM_QUERY, Query/binary>>,
  31. ExpectedResponse = hexdump_to_bin(
  32. "01 00 00 01 01|27 00 00 02 03 64 65 66 00 00 00 .....'....def..."
  33. "11 40 40 76 65 72 73 69 6f 6e 5f 63 6f 6d 6d 65 .@@version_comme"
  34. "6e 74 00 0c 08 00 1c 00 00 00 fd 00 00 1f 00 00| nt.............."
  35. "05 00 00 03 fe 00 00 02 00|1d 00 00 04 1c 4d 79 ..............My"
  36. "53 51 4c 20 43 6f 6d 6d 75 6e 69 74 79 20 53 65 SQL Community Se"
  37. "72 76 65 72 20 28 47 50 4c 29|05 00 00 05 fe 00 rver (GPL)......"
  38. "00 02 00 ..."),
  39. ExpectedCommunication = [{send, ExpectedReq},
  40. {recv, ExpectedResponse}],
  41. Sock = mock_tcp:create(ExpectedCommunication),
  42. {ok, [ResultSet]} = mysql_protocol:query(Query, mock_tcp, Sock, infinity),
  43. mock_tcp:close(Sock),
  44. ?assertMatch(#resultset{cols = [#col{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 = mock_tcp:create([{send, ExpectedReq}, {recv, ExpectedResponse}]),
  78. {ok, [Result]} = mysql_protocol:query(Query, mock_tcp, Sock, infinity),
  79. ?assertMatch(#error{}, Result),
  80. mock_tcp:close(Sock),
  81. ok.
  82. prepare_test() ->
  83. %% Prepared statement. The example from "14.7.4 COM_STMT_PREPARE" in the
  84. %% "MySQL Internals" guide.
  85. Query = <<"SELECT CONCAT(?, ?) AS col1">>,
  86. ExpectedReq = hexdump_to_bin(
  87. "1c 00 00 00 16 53 45 4c 45 43 54 20 43 4f 4e 43 .....SELECT CONC"
  88. "41 54 28 3f 2c 20 3f 29 20 41 53 20 63 6f 6c 31 AT(?, ?) AS col1"
  89. ),
  90. ExpectedResp = hexdump_to_bin(
  91. "0c 00 00 01 00 01 00 00 00 01 00 02 00 00 00 00| ................"
  92. "17 00 00 02 03 64 65 66 00 00 00 01 3f 00 0c 3f .....def....?..?"
  93. "00 00 00 00 00 fd 80 00 00 00 00|17 00 00 03 03 ................"
  94. "64 65 66 00 00 00 01 3f 00 0c 3f 00 00 00 00 00 def....?..?....."
  95. "fd 80 00 00 00 00|05 00 00 04 fe 00 00 02 00|1a ................"
  96. "00 00 05 03 64 65 66 00 00 00 04 63 6f 6c 31 00 ....def....col1."
  97. "0c 3f 00 00 00 00 00 fd 80 00 1f 00 00|05 00 00 .?.............."
  98. "06 fe 00 00 02 00 ......"),
  99. Sock = mock_tcp:create([{send, ExpectedReq}, {recv, ExpectedResp}]),
  100. Result = mysql_protocol:prepare(Query, mock_tcp, Sock),
  101. mock_tcp:close(Sock),
  102. ?assertMatch(#prepared{statement_id = StmtId,
  103. param_count = 2,
  104. warning_count = 0} when is_integer(StmtId),
  105. Result),
  106. ok.
  107. bad_protocol_version_test() ->
  108. Sock = mock_tcp:create([{recv, <<2, 0, 0, 0, 9, 0>>}]),
  109. ?assertError(unknown_protocol,
  110. mysql_protocol:handshake("foo", "bar", "db", mock_tcp, Sock)),
  111. mock_tcp:close(Sock).
  112. %% --- Helper functions for the above tests ---
  113. %% Convert hex dumps to binaries. This is a helper function for the tests.
  114. %% This function is also tested below.
  115. hexdump_to_bin(HexDump) ->
  116. hexdump_to_bin(iolist_to_binary(HexDump), <<>>).
  117. hexdump_to_bin(<<Line:50/binary, _Junk:20/binary, Rest/binary>>, Acc) ->
  118. hexdump_to_bin(Line, Rest, Acc);
  119. hexdump_to_bin(<<Line:50/binary, _Junk/binary>>, Acc) ->
  120. %% last line (shorter than 70)
  121. hexdump_to_bin(Line, <<>>, Acc);
  122. hexdump_to_bin(<<>>, Acc) ->
  123. Acc.
  124. hexdump_to_bin(Line, Rest, Acc) ->
  125. HexNums = re:split(Line, <<"[ |]+">>, [{return, list}, trim]),
  126. Acc1 = lists:foldl(fun (HexNum, Acc0) ->
  127. {ok, [Byte], []} = io_lib:fread("~16u", HexNum),
  128. <<Acc0/binary, Byte:8>>
  129. end,
  130. Acc,
  131. HexNums),
  132. hexdump_to_bin(Rest, Acc1).
  133. hexdump_to_bin_test() ->
  134. HexDump =
  135. "0e 00 00 00 03 73 65 6c 65 63 74 20 55 53 45 52 .....select USER"
  136. "28 29 ()",
  137. Expect = <<16#0e, 16#00, 16#00, 16#00, 16#03, 16#73, 16#65, 16#6c,
  138. 16#65, 16#63, 16#74, 16#20, 16#55, 16#53, 16#45, 16#52,
  139. 16#28, 16#29>>,
  140. ?assertEqual(Expect, hexdump_to_bin(HexDump)).