mysql_protocol_tests.erl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. Sock = mock_tcp:create(ExpectedCommunication),
  38. ResultSet = mysql_protocol:query(Query, mock_tcp, Sock, infinity),
  39. mock_tcp:close(Sock),
  40. ?assertMatch(#resultset{cols = [#col{name = <<"@@version_comment">>}],
  41. rows = [[<<"MySQL Community Server (GPL)">>]]},
  42. ResultSet),
  43. ok.
  44. resultset_error_test() ->
  45. %% A query that returns a response starting as a result set but then
  46. %% interupts itself and decides that it is an error.
  47. Query = <<"EXPLAIN SELECT * FROM dual;">>,
  48. ExpectedReq = <<(size(Query) + 1):24/little, 0, ?COM_QUERY, Query/binary>>,
  49. ExpectedResponse = hexdump_to_bin(
  50. "01 00 00 01 0a 18 00 00 02 03 64 65 66 00 00 00 ..........def..."
  51. "02 69 64 00 0c 3f 00 03 00 00 00 08 a1 00 00 00 .id..?.........."
  52. "00 21 00 00 03 03 64 65 66 00 00 00 0b 73 65 6c .!....def....sel"
  53. "65 63 74 5f 74 79 70 65 00 0c 08 00 13 00 00 00 ect_type........"
  54. "fd 01 00 1f 00 00 1b 00 00 04 03 64 65 66 00 00 ...........def.."
  55. "00 05 74 61 62 6c 65 00 0c 08 00 40 00 00 00 fd ..table....@...."
  56. "00 00 1f 00 00 1a 00 00 05 03 64 65 66 00 00 00 ..........def..."
  57. "04 74 79 70 65 00 0c 08 00 0a 00 00 00 fd 00 00 .type..........."
  58. "1f 00 00 23 00 00 06 03 64 65 66 00 00 00 0d 70 ...#....def....p"
  59. "6f 73 73 69 62 6c 65 5f 6b 65 79 73 00 0c 08 00 ossible_keys...."
  60. "00 10 00 00 fd 00 00 1f 00 00 19 00 00 07 03 64 ...............d"
  61. "65 66 00 00 00 03 6b 65 79 00 0c 08 00 40 00 00 ef....key....@.."
  62. "00 fd 00 00 1f 00 00 1d 00 00 08 03 64 65 66 00 ............def."
  63. "00 00 07 6b 65 79 5f 6c 65 6e 00 0c 08 00 00 10 ...key_len......"
  64. "00 00 fd 00 00 1f 00 00 19 00 00 09 03 64 65 66 .............def"
  65. "00 00 00 03 72 65 66 00 0c 08 00 00 04 00 00 fd ....ref........."
  66. "00 00 1f 00 00 1a 00 00 0a 03 64 65 66 00 00 00 ..........def..."
  67. "04 72 6f 77 73 00 0c 3f 00 0a 00 00 00 08 a0 00 .rows..?........"
  68. "00 00 00 1b 00 00 0b 03 64 65 66 00 00 00 05 45 ........def....E"
  69. "78 74 72 61 00 0c 08 00 ff 00 00 00 fd 01 00 1f xtra............"
  70. "00 00 05 00 00 0c fe 00 00 02 00 17 00 00 0d ff ................"
  71. "48 04 23 48 59 30 30 30 4e 6f 20 74 61 62 6c 65 H.#HY000No table"
  72. "73 20 75 73 65 64 s used"),
  73. Sock = mock_tcp:create([{send, ExpectedReq}, {recv, ExpectedResponse}]),
  74. Result = mysql_protocol:query(Query, mock_tcp, Sock, infinity),
  75. ?assertMatch(#error{}, Result),
  76. mock_tcp:close(Sock),
  77. ok.
  78. prepare_test() ->
  79. %% Prepared statement. The example from "14.7.4 COM_STMT_PREPARE" in the
  80. %% "MySQL Internals" guide.
  81. Query = <<"SELECT CONCAT(?, ?) AS col1">>,
  82. ExpectedReq = hexdump_to_bin(
  83. "1c 00 00 00 16 53 45 4c 45 43 54 20 43 4f 4e 43 .....SELECT CONC"
  84. "41 54 28 3f 2c 20 3f 29 20 41 53 20 63 6f 6c 31 AT(?, ?) AS col1"
  85. ),
  86. ExpectedResp = hexdump_to_bin(
  87. "0c 00 00 01 00 01 00 00 00 01 00 02 00 00 00 00| ................"
  88. "17 00 00 02 03 64 65 66 00 00 00 01 3f 00 0c 3f .....def....?..?"
  89. "00 00 00 00 00 fd 80 00 00 00 00|17 00 00 03 03 ................"
  90. "64 65 66 00 00 00 01 3f 00 0c 3f 00 00 00 00 00 def....?..?....."
  91. "fd 80 00 00 00 00|05 00 00 04 fe 00 00 02 00|1a ................"
  92. "00 00 05 03 64 65 66 00 00 00 04 63 6f 6c 31 00 ....def....col1."
  93. "0c 3f 00 00 00 00 00 fd 80 00 1f 00 00|05 00 00 .?.............."
  94. "06 fe 00 00 02 00 ......"),
  95. Sock = mock_tcp:create([{send, ExpectedReq}, {recv, ExpectedResp}]),
  96. Result = mysql_protocol:prepare(Query, mock_tcp, Sock),
  97. mock_tcp:close(Sock),
  98. ?assertMatch(#prepared{statement_id = StmtId,
  99. param_count = 2,
  100. warning_count = 0} when is_integer(StmtId),
  101. Result),
  102. ok.
  103. %% --- Helper functions for the above tests ---
  104. %% Convert hex dumps to binaries. This is a helper function for the tests.
  105. %% This function is also tested below.
  106. hexdump_to_bin(HexDump) ->
  107. hexdump_to_bin(iolist_to_binary(HexDump), <<>>).
  108. hexdump_to_bin(<<Line:50/binary, _Junk:20/binary, Rest/binary>>, Acc) ->
  109. hexdump_to_bin(Line, Rest, Acc);
  110. hexdump_to_bin(<<Line:50/binary, _Junk/binary>>, Acc) ->
  111. %% last line (shorter than 70)
  112. hexdump_to_bin(Line, <<>>, Acc);
  113. hexdump_to_bin(<<>>, Acc) ->
  114. Acc.
  115. hexdump_to_bin(Line, Rest, Acc) ->
  116. HexNums = re:split(Line, <<"[ |]+">>, [{return, list}, trim]),
  117. Acc1 = lists:foldl(fun (HexNum, Acc0) ->
  118. {ok, [Byte], []} = io_lib:fread("~16u", HexNum),
  119. <<Acc0/binary, Byte:8>>
  120. end,
  121. Acc,
  122. HexNums),
  123. hexdump_to_bin(Rest, Acc1).
  124. hexdump_to_bin_test() ->
  125. HexDump =
  126. "0e 00 00 00 03 73 65 6c 65 63 74 20 55 53 45 52 .....select USER"
  127. "28 29 ()",
  128. Expect = <<16#0e, 16#00, 16#00, 16#00, 16#03, 16#73, 16#65, 16#6c,
  129. 16#65, 16#63, 16#74, 16#20, 16#55, 16#53, 16#45, 16#52,
  130. 16#28, 16#29>>,
  131. ?assertEqual(Expect, hexdump_to_bin(HexDump)).
  132. %% --- Fake socket ---
  133. %%