Skip to content

Commit

Permalink
fix C_WaitForSlotEvent: it should be a Library- instead of a Slot-method
Browse files Browse the repository at this point in the history
add simple test for C_WaitForSlotEvent
  • Loading branch information
larskanis committed Jan 24, 2012
1 parent 7630290 commit b7eaab5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
14 changes: 14 additions & 0 deletions lib/pkcs11/library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ def all_slots
slots(false)
end

alias unwrapped_C_WaitForSlotEvent C_WaitForSlotEvent

# Waits for a slot event, such as token insertion or token removal, to occur.
#
# @param [Integer] flags determines whether or not the C_WaitForSlotEvent call blocks (i.e., waits
# for a slot event to occur);
# At present, the only flag defined for use in the flags argument is PKCS11::CKF_DONT_BLOCK
# @return [Slot, nil] the slot that the event occurred in; nil if no event occured (CKR_NO_EVENT)
def C_WaitForSlotEvent(flags=0)
slot = unwrapped_C_WaitForSlotEvent(flags)
slot ? Slot.new(self, slot) : nil
end
alias wait_for_slot_event C_WaitForSlotEvent

# Finalize and unload the library. If not called explicit, the library is freed by the GC.
def close
self.C_Finalize
Expand Down
15 changes: 3 additions & 12 deletions lib/pkcs11/slot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,13 @@ def C_GetSlotInfo
@pk.C_GetSlotInfo(@slot)
end
alias info C_GetSlotInfo

# Obtains information about a particular token in the system.
# @return [PKCS11::CK_TOKEN_INFO]
def C_GetTokenInfo
@pk.C_GetTokenInfo(@slot)
end
alias token_info C_GetTokenInfo

# Waits for a slot event, such as token insertion or token removal, to
# occur.
# @param flags determines whether or not the C_WaitForSlotEvent call blocks (i.e., waits
# for a slot event to occur);
def C_WaitForSlotEvent(flags)
@pk.C_WaitForSlotEvent(@slot, flags)
end
alias wait_for_event C_WaitForSlotEvent

# C_GetMechanismList is used to obtain a list of mechanism types supported by a token.
# @return [Array<PKCS11::CKM_*>]
Expand Down Expand Up @@ -75,7 +66,7 @@ def C_InitToken(pin, label)
self
end
alias init_token C_InitToken

# Opens a Session between an application and a token in a particular slot.
#
# @param [Integer] flags indicates the type of session. Default is read-only,
Expand All @@ -99,7 +90,7 @@ def C_OpenSession(flags=CKF_SERIAL_SESSION)
end
end
alias open C_OpenSession

# Closes all sessions an application has with a token.
# @return [PKCS11::Slot]
def C_CloseAllSessions
Expand Down
11 changes: 11 additions & 0 deletions test/test_pkcs11.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ def test_C_Initialize_with_Hash
pk.info
pk.close
end

def test_wait_for_slot_event
open
# Softokn's C_WaitForSlotEvent() currently raises PKCS11::CKR_FUNCTION_NOT_SUPPORTED.
# So just check, that the call goes to softokn at all.
begin
pk.wait_for_slot_event
rescue PKCS11::Error
end
close
end
end

0 comments on commit b7eaab5

Please sign in to comment.