Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix Tag Bytesize Calculation #13

Merged
merged 4 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/zatca/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def to_tlv
# TLV should be concatenated together without any separator in the following
# format: character_value_of_id character_value_of_value_length value_itself
# All of this should be in 8-bit ASCII.
tlv = @id.chr + @value.length.chr + value
tlv = @id.chr + @value.bytesize.chr + value

# We need to use force_encoding because encode will raise errors when
# trying to encode a string with utf-8 characters.
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
vat_total: "15",
invoice_total: "115"
}.freeze

FIXTURES_UNICODE_TAGS = {
seller_name: "مرسول",
vat_registration_number: "310228833400003",
timestamp: "2021-10-20T19:29:32+03:00",
vat_total: "15",
invoice_total: "115"
}.freeze
2 changes: 1 addition & 1 deletion spec/lib/zatca/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
end

it "generates a valid TLV when passed unicode input" do
expected_output = "\x01\x05\xD9\x85\xD8\xB1\xD8\xB3\xD9\x88\xD9\x84".force_encoding("ASCII-8BIT")
expected_output = "\x01\n\xD9\x85\xD8\xB1\xD8\xB3\xD9\x88\xD9\x84".force_encoding("ASCII-8BIT")

expect(unicode_tag.to_tlv).to eq(expected_output)
end
Expand Down
7 changes: 6 additions & 1 deletion spec/lib/zatca/tags_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
describe ZATCA::Tags do
let(:tags) { ZATCA::Tags.new(FIXTURES_TAGS) }
let(:unicode_tags) { ZATCA::Tags.new(FIXTURES_UNICODE_TAGS) }

describe "#to_base64" do
it "generates valid Base64" do
it "generates valid Base64 with ASCII tags" do
expect(tags.to_base64).to eq("AQZNcnNvb2wCDzMxMDIyODgzMzQwMDAwMwMZMjAyMS0xMC0yMFQxOToyOTozMiswMzowMAQDMTE1BQIxNQ==")
end

it "generates valid Base64 with unicode tags" do
expect(unicode_tags.to_base64).to eq("AQrZhdix2LPZiNmEAg8zMTAyMjg4MzM0MDAwMDMDGTIwMjEtMTAtMjBUMTk6Mjk6MzIrMDM6MDAEAzExNQUCMTU=")
end
end
end