-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maxim Chechel
committed
Oct 3, 2014
1 parent
1d25ebd
commit d38be33
Showing
9 changed files
with
110 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'ffi' | ||
gem 'minitest' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ GEM | |
remote: https://rubygems.org/ | ||
specs: | ||
ffi (1.9.3) | ||
minitest (5.4.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
ffi | ||
minitest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |