Skip to content

Commit

Permalink
Merge pull request #139 from TeachersPayTeachers/mysql-handshake
Browse files Browse the repository at this point in the history
Update parsing of auth_plugin_data2 in MySQL handshake
  • Loading branch information
liveforeverx committed Oct 10, 2016
2 parents a0aa98b + fdd6669 commit f21f987
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/mariaex/coder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ defmodule Mariaex.Coder do
end
end

defp gen_body({key, _, [:auth_plugin_data2]}, _) do
quote do: {unquote(Macro.var(key, nil)), next} = auth_plugin_data2(next)
end

defp gen_body({key, _, [:length_encoded_integer]}, _) do
quote do: {unquote(Macro.var(key, nil)), next} = length_encoded_integer(next)
end
Expand Down Expand Up @@ -165,6 +169,9 @@ defmodule Mariaex.Coder do
defp match({key, _, [:length_encoded_integer]}, :encode) do
[(quote do: unquote(Macro.var(key, nil)) :: integer)]
end
defp match({key, _, [:auth_plugin_data2]}, :encode) do
[(quote do: unquote(Macro.var(key, nil)) :: binary)]
end
defp match({key, _, [:length_encoded_string | _]}, :encode) do
[(quote do: unquote(Macro.var(key, nil)) :: binary)]
end
Expand All @@ -176,6 +183,17 @@ defmodule Mariaex.Coder do
{string, next}
end

def auth_plugin_data2(<< length_auth_plugin_data :: 8, _ :: 80, next :: binary >>) do
length = max(13, length_auth_plugin_data - 8)
length_nul_terminated = length - 1
<< null_terminated? :: size(length)-binary, next :: binary >> = next
auth_plugin_data2 = case null_terminated? do
<< contents :: size(length_nul_terminated)-binary, 0 :: 8 >> -> contents
contents -> contents
end
{String.strip(auth_plugin_data2, 0), next}
end

def length_encoded_string_eof(bin, acc \\ []) do
case length_encoded_string(bin) do
{value, ""} ->
Expand Down
5 changes: 2 additions & 3 deletions lib/mariaex/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ defmodule Mariaex.Messages do
character_set 1
status_flags 2
capability_flags_2 2
length_auth_plugin_data 1
_ 10
auth_plugin_data2 :string #max(13, length_auth_plugin_data - 8), :string
# length_auth_plugin_data and the following ten bytes in the spec are rolled into the following field
auth_plugin_data2 :auth_plugin_data2 #max(13, length_auth_plugin_data - 8), :string
plugin :string_eof
end

Expand Down
46 changes: 46 additions & 0 deletions test/coder_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule CoderTest do
use ExUnit.Case, async: true
import Mariaex.Coder.Utils

test "auth_plugin_data2 understands null-terminated strings longer than 12 bytes" do
ten_bytes = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>
auth_plugin_data2 = "12345678901234"
obs = auth_plugin_data2(<<23>> <> ten_bytes <> auth_plugin_data2 <> <<0>>)
assert({auth_plugin_data2, <<>>} == obs)
end

test "auth_plugin_data2 understands null-terminated strings equal to 12 bytes" do
ten_bytes = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>
auth_plugin_data2 = "123456789012"
obs = auth_plugin_data2(<<20>> <> ten_bytes <> auth_plugin_data2 <> <<0>>)
assert({auth_plugin_data2, <<>>} == obs)
end

test "auth_plugin_data2 understands null-terminated strings shorter than 12 bytes" do
ten_bytes = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>
auth_plugin_data2 = "1234567890"
obs = auth_plugin_data2(<<18>> <> ten_bytes <> auth_plugin_data2 <> <<0, 0, 0>>)
assert({auth_plugin_data2, <<>>} == obs)
end

test "auth_plugin_data2 understands fixlen strings longer than 13 bytes" do
ten_bytes = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>
auth_plugin_data2 = "123456789012345"
obs = auth_plugin_data2(<<23>> <> ten_bytes <> auth_plugin_data2)
assert({auth_plugin_data2, <<>>} == obs)
end

test "auth_plugin_data2 understands fixlen strings equal to 13 bytes" do
ten_bytes = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>
auth_plugin_data2 = "1234567890123"
obs = auth_plugin_data2(<<20>> <> ten_bytes <> auth_plugin_data2)
assert({auth_plugin_data2, <<>>} == obs)
end

test "auth_plugin_data2 understands fixlen strings shorter than 13 bytes" do
ten_bytes = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>
auth_plugin_data2 = "1234567890"
obs = auth_plugin_data2(<<18>> <> ten_bytes <> auth_plugin_data2 <> <<0, 0, 0>>)
assert({auth_plugin_data2, <<>>} == obs)
end
end

0 comments on commit f21f987

Please sign in to comment.