Split encode command tests to separate commands

This commit is contained in:
Daniil Fedotov 2017-07-03 12:11:34 +01:00
parent c40368fe02
commit 58304ffc95
5 changed files with 195 additions and 62 deletions

View File

@ -32,7 +32,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListHashesCommand do
def formatter(), do: RabbitMQ.CLI.Formatters.Erlang
def usage, do: "list_ciphers"
def usage, do: "list_hashes"
def banner(_, _), do: "Listing supported hash algorithms ..."

View File

@ -0,0 +1,113 @@
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at http://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
defmodule DecodeCommandTest do
use ExUnit.Case, async: false
@command RabbitMQ.CLI.Ctl.Commands.DecodeCommand
test "validate: not providing exactly 2 arguments for decoding is reported as invalid", _context do
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate([], %{})
)
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value"], %{})
)
assert match?(
{:validation_failure, :too_many_args},
@command.validate(["value", "secret", "incorrect"], %{})
)
end
test "validate: hash and cipher must be supported", _context do
opts = %{cipher: :rabbit_pbe.default_cipher,
hash: :rabbit_pbe.default_hash,
iterations: :rabbit_pbe.default_iterations}
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value", "secret"], Map.merge(opts, %{cipher: :funny_cipher}))
)
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value", "secret"], Map.merge(opts, %{hash: :funny_hash}))
)
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value", "secret"], Map.merge(opts, %{cipher: :funny_cipher, hash: :funny_hash}))
)
assert match?(
:ok,
@command.validate(["value", "secret"], opts)
)
end
test "validate: number of iterations must greather than 0", _context do
opts = %{cipher: :rabbit_pbe.default_cipher,
hash: :rabbit_pbe.default_hash,
iterations: :rabbit_pbe.default_iterations}
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value", "secret"], Map.merge(opts, %{iterations: 0}))
)
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value", "secret"], Map.merge(opts, %{iterations: -1}))
)
assert match?(
:ok,
@command.validate(["value", "secret"], opts)
)
end
test "run: encrypt/decrypt", _context do
# erlang list/string
encrypt_decrypt(to_charlist("foobar"))
# binary
encrypt_decrypt("foobar")
# tuple
encrypt_decrypt({:password, "secret"})
end
defp encrypt_decrypt(secret) do
secret_as_erlang_term = format_as_erlang_term(secret)
passphrase = "passphrase"
cipher = :rabbit_pbe.default_cipher()
hash = :rabbit_pbe.default_hash()
iterations = :rabbit_pbe.default_iterations()
encrypted = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, secret)
output = {:encrypted, encrypted}
opts = %{cipher: cipher,
hash: hash,
iterations: iterations
}
# decode plain value
assert {:ok, secret} === @command.run([format_as_erlang_term(encrypted), passphrase], Map.merge(opts, %{}))
# decode {encrypted, ...} tuple form
assert {:ok, secret} === @command.run([format_as_erlang_term(output), passphrase], Map.merge(opts, %{}))
# wrong passphrase
assert match?(
{:error, _},
@command.run([format_as_erlang_term(encrypted), "wrong passphrase"], Map.merge(opts, %{}))
)
assert match?(
{:error, _},
@command.run([format_as_erlang_term(output), "wrong passphrase"], Map.merge(opts, %{}))
)
end
defp format_as_erlang_term(value), do: to_string(:lists.flatten(:io_lib.format("~p", [value])))
end

View File

@ -18,45 +18,25 @@ defmodule EncodeCommandTest do
@command RabbitMQ.CLI.Ctl.Commands.EncodeCommand
test "validate: specifying both --list-ciphers and --list-hashes is reported as invalid", _context do
test "validate: not providing exactly 2 arguments for encoding is reported as invalid", _context do
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate([], %{list_ciphers: true, list_hashes: true})
)
end
test "validate: providing arguments when listing ciphers or hashes is reported as invalid", _context do
assert match?(
{:validation_failure, {:bad_argument, :too_many_args}},
@command.validate(["value"], %{list_ciphers: true, list_hashes: false})
)
assert match?(
{:validation_failure, {:bad_argument, :too_many_args}},
@command.validate(["value"], %{list_ciphers: false, list_hashes: true})
)
end
test "validate: not providing exactly 2 arguments for encoding/decoding is reported as invalid", _context do
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate([], %{list_ciphers: false, list_hashes: false})
@command.validate([], %{})
)
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value"], %{list_ciphers: false, list_hashes: false})
@command.validate(["value"], %{})
)
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value", "secret", "incorrect"], %{list_ciphers: false, list_hashes: false})
{:validation_failure, :too_many_args},
@command.validate(["value", "secret", "incorrect"], %{})
)
end
test "validate: hash and cipher must be supported", _context do
opts = %{cipher: :rabbit_pbe.default_cipher,
hash: :rabbit_pbe.default_hash,
iterations: :rabbit_pbe.default_iterations,
list_ciphers: false,
list_hashes: false}
iterations: :rabbit_pbe.default_iterations}
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value", "secret"], Map.merge(opts, %{cipher: :funny_cipher}))
@ -78,9 +58,7 @@ defmodule EncodeCommandTest do
test "validate: number of iterations must greather than 0", _context do
opts = %{cipher: :rabbit_pbe.default_cipher,
hash: :rabbit_pbe.default_hash,
iterations: :rabbit_pbe.default_iterations,
list_ciphers: false,
list_hashes: false}
iterations: :rabbit_pbe.default_iterations}
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["value", "secret"], Map.merge(opts, %{iterations: 0}))
@ -95,20 +73,6 @@ defmodule EncodeCommandTest do
)
end
test "run: list ciphers", _context do
assert match?(
{:ok, _},
@command.run([], %{list_ciphers: true})
)
end
test "run: list hashes", _context do
assert match?(
{:ok, _},
@command.run([], %{list_hashes: true})
)
end
test "run: encrypt/decrypt", _context do
# erlang list/string
encrypt_decrypt(to_charlist("foobar"))
@ -121,29 +85,19 @@ defmodule EncodeCommandTest do
defp encrypt_decrypt(secret) do
secret_as_erlang_term = format_as_erlang_term(secret)
passphrase = "passphrase"
opts = %{list_ciphers: false,
list_hashes: false,
cipher: :rabbit_pbe.default_cipher(),
hash: :rabbit_pbe.default_hash(),
iterations: :rabbit_pbe.default_iterations(),
decode: false
cipher = :rabbit_pbe.default_cipher()
hash = :rabbit_pbe.default_hash()
iterations = :rabbit_pbe.default_iterations()
opts = %{cipher: cipher,
hash: hash,
iterations: iterations
}
{:ok, output} = @command.run([secret_as_erlang_term, passphrase], opts)
{:encrypted, encrypted} = output
# decode plain value
assert {:ok, secret} === @command.run([format_as_erlang_term(encrypted), passphrase], Map.merge(opts, %{decode: true}))
assert secret === :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, encrypted)
# decode {encrypted, ...} tuple form
assert {:ok, secret} === @command.run([format_as_erlang_term(output), passphrase], Map.merge(opts, %{decode: true}))
# wrong passphrase
assert match?(
{:error, _},
@command.run([format_as_erlang_term(encrypted), "wrong passphrase"], Map.merge(opts, %{decode: true}))
)
assert match?(
{:error, _},
@command.run([format_as_erlang_term(output), "wrong passphrase"], Map.merge(opts, %{decode: true}))
)
assert secret === :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, encrypted)
end
defp format_as_erlang_term(value), do: to_string(:lists.flatten(:io_lib.format("~p", [value])))

View File

@ -0,0 +1,33 @@
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at http://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
defmodule ListCiphersCommandTest do
use ExUnit.Case, async: false
@command RabbitMQ.CLI.Ctl.Commands.ListCiphersCommand
test "validate: providing arguments when listing ciphers is reported as invalid", _context do
assert match?(
{:validation_failure, {:bad_argument, :too_many_args}},
@command.validate(["value"], %{})
)
end
test "run: list ciphers", _context do
assert match?(
{:ok, _},
@command.run([], %{})
)
end
end

View File

@ -0,0 +1,33 @@
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at http://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
defmodule ListHashesCommandTest do
use ExUnit.Case, async: false
@command RabbitMQ.CLI.Ctl.Commands.ListHashesCommand
test "validate: providing arguments when listing hashes is reported as invalid", _context do
assert match?(
{:validation_failure, {:bad_argument, :too_many_args}},
@command.validate(["value"], %{})
)
end
test "run: list hashes", _context do
assert match?(
{:ok, _},
@command.run([], %{})
)
end
end