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

Handle corner case of values that look like numbers but should be treated as strings #51

Merged
merged 3 commits into from
Dec 4, 2023
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
4 changes: 4 additions & 0 deletions lib/arkana/models/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def self.new(string_value:)
when "true", "false"
BOOLEAN
when /^\d+$/
# Handles cases like "0001" which should be interpreted as strings
return STRING if string_value.to_i.to_s != string_value
# Handle int overflow
return STRING if string_value.to_i > (2**31) - 1
INTEGER
else
STRING
Expand Down
40 changes: 40 additions & 0 deletions lib/arkana/templates/arkana_tests.swift.erb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,46 @@ final class <%= @namespace %>Tests: XCTestCase {
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), 42)
}

func test_decodeIntValueWithLeadingZeroes_shouldDecodeAsString() {
<% int_with_leading_zeroes_key = "0001" %>
<% secret = generate_test_secret(key: int_with_leading_zeroes_key) %>
let encoded: [UInt8] = [
<%= secret.encoded_value %>

]
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), "0001")
}

func test_decodeMassiveIntValue_shouldDecodeAsString() {
<% int_with_massive_number_key = "92233720368547758079223372036854775807" %>
<% secret = generate_test_secret(key: int_with_massive_number_key) %>
let encoded: [UInt8] = [
<%= secret.encoded_value %>

]
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), "92233720368547758079223372036854775807")
}

func test_decodeNegativeIntValue_shouldDecodeAsString() {
<% negative_int_key = "-42" %>
<% secret = generate_test_secret(key: negative_int_key) %>
let encoded: [UInt8] = [
<%= secret.encoded_value %>

]
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), "-42")
}

func test_decodeFloatingPointValue_shouldDecodeAsString() {
<% float_key = "3.14" %>
<% secret = generate_test_secret(key: float_key) %>
let encoded: [UInt8] = [
<%= secret.encoded_value %>

]
XCTAssertEqual(<%= @namespace %>.decode(encoded: encoded, cipher: salt), "3.14")
}

func test_encodeAndDecodeValueWithDollarSign_shouldDecode() {
<% dollar_sign_key = "real_$lim_shady" %>
<% secret = generate_test_secret(key: dollar_sign_key) %>
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/.env.fruitloops
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ BoolAsBoolTrueKey = true
BoolAsBoolFalseKey = false
IntAsStringKey = "42"
IntAsNumberKey = 42
IntWithLeadingZeroesAsStringKey = "0001"
IntWithLeadingZeroesAsNumberKey = 0001
MassiveIntAsStringKey = "92233720368547758079223372036854775807"
MassiveIntAsNumberKey = 92233720368547758079223372036854775807
NegativeIntAsStringKey = "-42"
NegativeIntAsNumberKey = -42
FloatAsStringKey = "3.14"
FloatAsNumberKey = 3.14
SecretWithDollarSignEscapedAndAndNoQuotesKey = real_\$lim_shady
SecretWithDollarSignEscapedAndDoubleQuoteKey = "real_\$lim_shady"
SecretWithDollarSignNotEscapedAndSingleQuoteKey = 'real_$lim_shady'
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/swift-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ global_secrets:
- BoolAsBoolFalseKey
- IntAsStringKey
- IntAsNumberKey
- IntWithLeadingZeroesAsStringKey
- IntWithLeadingZeroesAsNumberKey
- MassiveIntAsStringKey
- MassiveIntAsNumberKey
- NegativeIntAsStringKey
- NegativeIntAsNumberKey
- FloatAsStringKey
- FloatAsNumberKey
- SecretWithDollarSignEscapedAndAndNoQuotesKey
- SecretWithDollarSignEscapedAndDoubleQuoteKey
- SecretWithDollarSignNotEscapedAndSingleQuoteKey
Expand Down
50 changes: 50 additions & 0 deletions spec/models/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,55 @@
expect(subject).to eq :string
end
end

context "when passing something that looks like a number" do
context "when it contains leading zeros" do
subject { described_class.new(string_value: "0001") }

it "returns :string" do
expect(subject).to eq :string
end
end

context "when its string representation is the same as its integer representation" do
subject { described_class.new(string_value: "1234567890") }

it "returns :integer" do
expect(subject).to eq :integer
end
end

context "when its string representation is different from its integer representation" do
subject { described_class.new(string_value: "1234567890.0") }

it "returns :string" do
expect(subject).to eq :string
end
end

context "when it contains a decimal point" do
subject { described_class.new(string_value: "3.14") }

it "returns :string" do
expect(subject).to eq :string
end
end

context "when it contains a comma" do
subject { described_class.new(string_value: "1,000") }

it "returns :string" do
expect(subject).to eq :string
end
end

context "when it contains a massive number" do
subject { described_class.new(string_value: "123456789012345678901234567890") }

it "returns :string" do
expect(subject).to eq :string
end
end
end
end
end
Loading