Skip to content

Commit

Permalink
tests for APDU::Request
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Chechel committed Oct 3, 2014
1 parent 1d25ebd commit d38be33
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
source 'https://rubygems.org'

gem 'ffi'
gem 'minitest'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ GEM
remote: https://rubygems.org/
specs:
ffi (1.9.3)
minitest (5.4.2)

PLATFORMS
ruby

DEPENDENCIES
ffi
minitest
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs << 'test'
t.pattern = "test/*_test.rb"
end
2 changes: 2 additions & 0 deletions lib/ruby-nfc/apdu/apdu.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module APDU
class Error < ::Exception; end

class Errno < ::Exception
STATUS_STRINGS = {
#0x6XXX => "Transmission protocol related codes
Expand Down
9 changes: 0 additions & 9 deletions lib/ruby-nfc/apdu/apdu_request.rb

This file was deleted.

42 changes: 42 additions & 0 deletions lib/ruby-nfc/apdu/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative './apdu'

module APDU
class Request
attr_accessor :cla, :ins, :p1, :p2, :lc, :data, :le

def self.from_string(apdu)
raise APDU::Error, "APDU is too short: #{apdu.size}" if apdu.size < 5

apdu_8bit = apdu.dup
apdu_8bit.force_encoding('ASCII-8BIT')

req = self.new
req.cla, req.ins, req.p1, req.p2, req.lc, req.data = apdu.unpack('CCCCCA*')

if req.data.size == req.lc
req.le = 0
elsif req.data.size == req.lc + 1
req.le = req.data[-1,1].ord
req.data = req.data[0...-1]
else
raise APDU::Error, "Wrong Lc or wrong command data length"
end

req
end

def self.from_hex_string(apdu)
raise APDU::Error, "Wrong format" if apdu !~ /^([a-fA-F0-9]{2}){5,128}$/
from_string([apdu].pack('H*'))
end

# Public: Build APDU command
def build
[self.to_s].pack('H*')
end

def to_s
[cla, ins, p1, p2, lc, data, le].pack('CCCCCA*C').unpack('H*').pop.upcase
end
end
end
1 change: 1 addition & 0 deletions ruby-nfc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.requirements << 'libfreefare'

s.add_runtime_dependency 'ffi'
s.add_development_dependency 'minitest'

s.post_install_message = [
"Don't forget to install libnfc and libfreefare",
Expand Down
51 changes: 51 additions & 0 deletions test/apdu_request_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'test_helper'
require 'ruby-nfc/apdu/request'

class PostTest < MiniTest::Unit::TestCase
def test_raise_apdu_error_if_length_too_small
e = assert_raises(APDU::Error) {APDU::Request.from_string("123")}
assert_match e.message, /too short/
end

def test_raise_apdu_error_if_wrong_command_data_length
apdu = "\x01\x02\x03\x04\x05\xDE\xAD\xBE\xEF"
e = assert_raises(APDU::Error) {APDU::Request.from_string(apdu)}
assert_match e.message, /Wrong Lc/

apdu << "\xAA\xBB\xCC"
e = assert_raises(APDU::Error) {APDU::Request.from_string(apdu)}
assert_match e.message, /Wrong Lc/
end

def test_from_string_method
apdu = "\x01\x02\x03\x04\x05\xDE\xAD\xBE\xEF\xAA\x06"
request = APDU::Request.from_string(apdu)
request_helper(request)
end

def test_from_hex_string_method_wrong_format
apdu = "0102030405DEADBEEFAA0"
e = assert_raises(APDU::Error) {APDU::Request.from_hex_string(apdu)}
assert_match e.message, /Wrong format/

apdu = "0102030405DEADBEEFAA0Z"
e = assert_raises(APDU::Error) {APDU::Request.from_hex_string(apdu)}
assert_match e.message, /Wrong format/
end

def test_from_hex_string_method
apdu = "0102030405DEADBEEFAA06"
request = APDU::Request.from_hex_string(apdu)
request_helper(request)
end

def request_helper(request)
assert_equal 1, request.cla
assert_equal 2, request.ins
assert_equal 3, request.p1
assert_equal 4, request.p2
assert_equal 5, request.lc
assert_equal 6, request.le
assert_equal "\xDE\xAD\xBE\xEF\xAA", request.data
end
end
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'ruby-nfc'

require 'minitest/unit'
require 'minitest/autorun'
require 'minitest/pride'

0 comments on commit d38be33

Please sign in to comment.