Skip to content

Commit

Permalink
add nonce constant and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mogest committed Jun 9, 2012
1 parent a54391a commit d056936
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions ext/nacl/nacl.c
Expand Up @@ -167,6 +167,7 @@ void Init_nacl() {
rb_define_module_function(NaCl, "crypto_box_keypair", method_crypto_box_keypair, 0);
rb_define_module_function(NaCl, "crypto_box", method_crypto_box, 4);
rb_define_module_function(NaCl, "crypto_box_open", method_crypto_box_open, 4);
rb_define_const(NaCl, "BOX_NONCE_LENGTH", INT2FIX(crypto_box_NONCEBYTES));

rb_define_module_function(NaCl, "crypto_sign_keypair", method_crypto_sign_keypair, 0);
rb_define_module_function(NaCl, "crypto_sign", method_crypto_sign, 2);
Expand Down
6 changes: 5 additions & 1 deletion test/test_nacl_crypto_box.rb
Expand Up @@ -56,7 +56,11 @@ def test_open_with_changed_ciphertext
mangled = "#{crypted[0..13]}#{char}#{crypted[15..-1]}"
assert_not_equal crypted, mangled
assert_raise(NaCl::OpenError) do
p NaCl.crypto_box_open(mangled, @nonce, @pub_a, @sec_b)
NaCl.crypto_box_open(mangled, @nonce, @pub_a, @sec_b)
end
end

def test_constants
assert_equal 24, NaCl::BOX_NONCE_LENGTH
end
end
30 changes: 30 additions & 0 deletions test/test_nacl_crypto_sign.rb
Expand Up @@ -18,5 +18,35 @@ def test_with_normal_message
crypted = NaCl.crypto_sign(message, @sec)
assert_equal message, NaCl.crypto_sign_open(crypted, @pub)
end

def test_with_empty_message
message = ""
crypted = NaCl.crypto_sign(message, @sec)
assert_equal message, NaCl.crypto_sign_open(crypted, @pub)
end

def test_with_invalid_arguments
assert_raise(ArgumentError) do
NaCl.crypto_sign("message", "invalid seckey")
end
assert_raise(ArgumentError) do
NaCl.crypto_sign_open("message", "invalid pubkey")
end
end

def test_open_with_empty_message
assert_raise(NaCl::OpenError) do
NaCl.crypto_sign_open("", @pub)
end
end

def test_open_with_changed_signedtext
signed = NaCl.crypto_sign("This is a test message", @sec)
mangled = signed.gsub("test message", "best message")
assert_not_equal signed, mangled
assert_raise(NaCl::OpenError) do
NaCl.crypto_sign_open(mangled, @pub)
end
end
end

0 comments on commit d056936

Please sign in to comment.