-
Notifications
You must be signed in to change notification settings - Fork 108
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
Return error with invalid length #180
Changes from 3 commits
f4ae3ee
03f402f
03cf22d
4735ea4
c23e866
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -296,7 +296,7 @@ func TestCompositePacking(t *testing.T) { | |
require.EqualError(t, err, "failed to pack subfield 1: failed to encode length: field length: 4 should be fixed: 2") | ||
}) | ||
|
||
t.Run("Pack returns error on failure to encode length", func(t *testing.T) { | ||
t.Run("Pack returns error when encoded data length is larger than specified fixed max length", func(t *testing.T) { | ||
invalidSpec := &Spec{ | ||
// Base field length < summation of lengths of subfields | ||
// This will throw an error when encoding the field's length. | ||
|
@@ -330,7 +330,7 @@ func TestCompositePacking(t *testing.T) { | |
} | ||
|
||
composite := NewComposite(invalidSpec) | ||
err := composite.SetData(data) | ||
err := composite.Marshal(data) | ||
require.NoError(t, err) | ||
|
||
_, err = composite.Pack() | ||
|
@@ -450,11 +450,8 @@ func TestCompositePacking(t *testing.T) { | |
}), | ||
}, | ||
} | ||
data := &CompositeTestData{} | ||
|
||
composite := NewComposite(invalidSpec) | ||
err := composite.SetData(data) | ||
require.NoError(t, err) | ||
|
||
// Length of input too long, causing failure to decode length. | ||
read, err := composite.Unpack([]byte("ABCD123")) | ||
|
@@ -496,7 +493,7 @@ func TestCompositePacking(t *testing.T) { | |
} | ||
|
||
func TestCompositePackingWithTags(t *testing.T) { | ||
t.Run("Pack returns error on failure to encode length", func(t *testing.T) { | ||
t.Run("Pack returns error when encoded data length is larger than specified fixed max length", func(t *testing.T) { | ||
// Base field length < summation of (lengths of subfields + IDs). | ||
// This will throw an error when encoding the field's length. | ||
invalidSpec := &Spec{ | ||
|
@@ -542,6 +539,49 @@ func TestCompositePackingWithTags(t *testing.T) { | |
require.EqualError(t, err, "failed to encode length: field length: 12 should be fixed: 6") | ||
}) | ||
|
||
t.Run("Pack returns error when encoded data length is larger than specified variable max length", func(t *testing.T) { | ||
invalidSpec := &Spec{ | ||
Length: 8, | ||
Pref: prefix.ASCII.LL, | ||
Tag: &TagSpec{ | ||
Length: 2, | ||
Enc: encoding.ASCII, | ||
Pad: padding.Left('0'), | ||
Sort: sort.StringsByInt, | ||
}, | ||
Subfields: map[string]Field{ | ||
"1": NewString(&Spec{ | ||
Length: 2, | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"2": NewString(&Spec{ | ||
Length: 2, | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"3": NewNumeric(&Spec{ | ||
Length: 2, | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
}, | ||
} | ||
data := &CompositeTestData{ | ||
F1: NewStringValue("AB"), | ||
F2: NewStringValue("CD"), | ||
F3: NewNumericValue(12), | ||
} | ||
|
||
composite := NewComposite(invalidSpec) | ||
err := composite.Marshal(data) | ||
require.NoError(t, err) | ||
|
||
b, err := composite.Pack() | ||
require.Nil(t, b) | ||
require.EqualError(t, err, "failed to encode length: field length: 12 is larger than maximum: 8") | ||
}) | ||
|
||
t.Run("Pack correctly serializes fully populated data to bytes", func(t *testing.T) { | ||
data := &CompositeTestData{ | ||
F1: NewStringValue("AB"), | ||
|
@@ -595,142 +635,6 @@ func TestCompositePackingWithTags(t *testing.T) { | |
require.Equal(t, "120102AB0202CD", string(packed)) | ||
}) | ||
|
||
t.Run("Pack correctly ignores excess subfields in excess of the length described by the prefix", func(t *testing.T) { | ||
type ExcessSubfieldsTestData struct { | ||
F1 *String | ||
F2 *Numeric | ||
F3 *String | ||
F4 *String | ||
F5 *String | ||
F6 *String | ||
F7 *String | ||
F8 *String | ||
F9 *Numeric | ||
F10 *String | ||
F11 *String | ||
F12 *String | ||
F13 *String | ||
F14 *String | ||
} | ||
|
||
excessSubfieldsSpec := &Spec{ | ||
Length: 26, | ||
Description: "POS Data", | ||
Pref: prefix.ASCII.LLL, | ||
Tag: &TagSpec{ | ||
Sort: sort.StringsByInt, | ||
}, | ||
Subfields: map[string]Field{ | ||
"1": NewString(&Spec{ | ||
Length: 1, | ||
Description: "POS Terminal Attendance", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"2": NewNumeric(&Spec{ | ||
Length: 1, | ||
Description: "Reserved for Future Use", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"3": NewString(&Spec{ | ||
Length: 1, | ||
Description: "POS Terminal Location", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"4": NewString(&Spec{ | ||
Length: 1, | ||
Description: "POS Cardholder Presence", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"5": NewString(&Spec{ | ||
Length: 1, | ||
Description: "POS Card Presence", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"6": NewString(&Spec{ | ||
Length: 1, | ||
Description: "POS Card Capture Capabilities", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"7": NewString(&Spec{ | ||
Length: 1, | ||
Description: "POS Transaction Status", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"8": NewString(&Spec{ | ||
Length: 1, | ||
Description: "POS Transaction Security", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"9": NewNumeric(&Spec{ | ||
Length: 1, | ||
Description: "Reserved for Future Use", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
Pad: padding.Left('0'), | ||
}), | ||
"10": NewString(&Spec{ | ||
Length: 1, | ||
Description: "Cardholder-Activated Terminal Level", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"11": NewString(&Spec{ | ||
Length: 1, | ||
Description: "POS Card Data Terminal Input Capability Indicator", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"12": NewString(&Spec{ | ||
Length: 2, | ||
Description: "POS Authorization Life Cycle", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"13": NewString(&Spec{ | ||
Length: 3, | ||
Description: "POS Country Code", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
"14": NewString(&Spec{ | ||
Length: 10, | ||
Description: "POS Postal Code", | ||
Enc: encoding.ASCII, | ||
Pref: prefix.ASCII.Fixed, | ||
}), | ||
}, | ||
} | ||
|
||
data := &ExcessSubfieldsTestData{} | ||
|
||
composite := NewComposite(excessSubfieldsSpec) | ||
err := composite.SetData(data) | ||
require.NoError(t, err) | ||
|
||
// Subfield 12, 13 & 14 fall outside of the bounds of the | ||
// 11 byte limit imposed by the prefix. [011 | 10000100012] | ||
// Therefore, it won't be included in the packed bytes. | ||
|
||
packed := []byte("01110000100012") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is proper packed data for the
|
||
|
||
read, err := composite.Unpack(packed) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, 14, read) | ||
|
||
packedBytes, err := composite.Pack() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we just pack it back. Unset fields will not be included in the packed value. It's just expected behavior. So, test is removed as we don't test here anything. Or I'm missing something. |
||
require.NoError(t, err) | ||
require.Equal(t, packedBytes, packed) | ||
}) | ||
|
||
t.Run("Unpack returns an error on failure of subfield to unpack bytes", func(t *testing.T) { | ||
data := &CompositeTestData{} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there was a test for fixed length, I added one for variable length.