Skip to content

Commit

Permalink
ISOUtil.protect - better handling of track1 data
Browse files Browse the repository at this point in the history
relates to #579
  • Loading branch information
ar committed Jan 10, 2024
1 parent 2b5e9fc commit f66f287
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 105 deletions.
44 changes: 40 additions & 4 deletions jpos/src/main/java/org/jpos/iso/ISOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ public static String normalize (String s) {
* @param mask char used to protect the string
* @return 'protected' String
*/
public static String protect (String s, char mask) {
public static String protect0 (String s, char mask) {
StringBuilder sb = new StringBuilder();
int len = s.length();
int clear = len > 6 ? 6 : 0;
Expand Down Expand Up @@ -932,15 +932,51 @@ else if (i == lastFourIndex)
try {
//Addresses Track1 Truncation
int charCount = s.replaceAll("[^\\^]", "").length();
if (charCount == 2 ) {
s = s.substring(0, s.lastIndexOf("^")+1);
s = ISOUtil.padright(s, len, mask);
if (charCount >= 2) {
int firstCaret = s.indexOf("^");
int secondCaret = s.indexOf("^", firstCaret + 1);

s = s.substring(0, firstCaret + 1);
s = padright(s, secondCaret, mask);
s = s.concat("^");
s = padright(s, len, mask);
}
} catch (ISOException e){
//cannot PAD - should never get here
}
return s;
}


public static String protect(String s, char mask) {
// Validation for minimum length
if (s.length() < 10) { // 6 (BIN) + 4 (last digits) = 10
return s; // nothing to do
}
StringBuilder protectedTrack = new StringBuilder(s);

// Identify the positions of separators (^ and =)
String separator = s.contains("^") ? "^" : "=";
int firstSeparatorIndex = protectedTrack.indexOf(separator);
if (firstSeparatorIndex < 6) {
return s; // nothing to do
}
int lastDigitIndex = firstSeparatorIndex - 4;

// Replace characters with underscore except BIN, last four digits and separators
for (int i = 6; i < lastDigitIndex; i++) {
protectedTrack.setCharAt(i, mask);
}
for (int i = firstSeparatorIndex + 1; i < protectedTrack.length(); i++) {
char c = protectedTrack.charAt(i);
if ((c != '=' && c != '^')) {
protectedTrack.setCharAt(i, mask);
}
}
return protectedTrack.toString();
}


public static String protect(String s) {
return protect(s, '_');
}
Expand Down
112 changes: 11 additions & 101 deletions jpos/src/test/java/org/jpos/iso/ISOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4170,58 +4170,31 @@ public void testProtect() throws Throwable {
}

@Test
public void testProtect1() throws Throwable {
String result = ISOUtil.protect("=WaW=4V0");
assertEquals("=___=___", result, "result");
public void testProtectTrack1() throws Throwable {
String result = ISOUtil.protect("B4000340000000504^John/Doe ^22251110000123000");
assertEquals("B40003_______0504^__________________________^_________________", result, "result");
}

@Test
public void testProtect10() throws Throwable {
String result = ISOUtil.protect("=====^===========^====^===");
assertEquals("=====^===========^====^===", result, "result");
public void testProtectTrack2() throws Throwable {
String result = ISOUtil.protect("4000340000000504=23052211985878901234");
assertEquals("400034______0504=____________________", result, "result");
}

@Test
public void testProtect11() throws Throwable {
String result = ISOUtil.protect("testISOUtils");
assertEquals("testIS__tils", result, "result");
}

@Test
public void testProtect12() throws Throwable {
String result = ISOUtil.protect("=HNb^D4uZfz0@|\")61b:~dSS`[.2!!qlL4Z0");
assertEquals("=___^D4uZfz0@|\")61b:~dSS`[.2!!qlL4Z0", result, "result");
public void testProtect10() throws Throwable {
String result = ISOUtil.protect("=====^===========^====^===");
assertEquals("=====^===========^====^===", result, "result");
}

@Test
public void testProtect13() throws Throwable {
String result = ISOUtil.protect("^58*(=@");
assertEquals("^58*(=_", result, "result");
}

@Test
public void testProtect14() throws Throwable {
String result = ISOUtil.protect("===\u3455w");
assertEquals("===__", result, "result");
}

@Test
public void testProtect15() throws Throwable {
String result = ISOUtil.protect("=\u0AC4\uC024\uF29B=~2A)~5aCgl\"lLU*lm_cJ1M/!KFnA");
assertEquals("=___=________________________KFnA", result, "result");
}

@Test
public void testProtect16() throws Throwable {
String result = ISOUtil.protect("\u30C5\uE09B\u6028\uB54E\u2094\uFA25\uAD56\u3A1F\uE55C\u31AA\u5FE0=$");
assertEquals("\u30C5\uE09B\u6028\uB54E\u2094\uFA25_\u3A1F\uE55C\u31AA\u5FE0=_", result, "result");
}

@Test
public void testProtect17() throws Throwable {
String result = ISOUtil.protect("+6+[=I?");
assertEquals("+6+[=__", result, "result");
}

@Test
public void testProtect18() throws Throwable {
Expand All @@ -4241,23 +4214,7 @@ public void testProtect2() throws Throwable {
assertEquals("===^===", result, "result");
}

@Test
public void testProtect20() throws Throwable {
String result = ISOUtil.protect("\u6D1D^KI");
assertEquals("_^KI", result, "result");
}

@Test
public void testProtect21() throws Throwable {
String result = ISOUtil.protect("=7G^=^");
assertEquals("=__^=^", result, "result");
}

@Test
public void testProtect3() throws Throwable {
String result = ISOUtil.protect("^D==N^=r=\u0002^g)==");
assertEquals("^D==_^=_=_^g)==", result, "result");
}

@Test
public void testProtect4() throws Throwable {
Expand All @@ -4271,58 +4228,11 @@ public void testProtect5() throws Throwable {
assertEquals("", result, "result");
}

@Test
public void testProtect6() throws Throwable {
String result = ISOUtil.protect("VqM_'");
assertEquals("_____", result, "result");
}

@Test
public void testProtect7() throws Throwable {
String result = ISOUtil.protect("\\7.=^6C3");
assertEquals("\\7.=^6C3", result, "result");
}

@Test
public void testProtect8() throws Throwable {
String result = ISOUtil.protect("#<gF=uG!");
assertEquals("#<gF=___", result, "result");
}

@Test
public void testProtect9() throws Throwable {
String result = ISOUtil.protect("^9a{=o;G");
assertEquals("^9a{=___", result, "result");
}

@Test
public void testProtectT2D1() throws Throwable {
String result = ISOUtil.protect("#<gFDuG!");
assertEquals("#<gFD___", result, "result");
}

@Test
public void testProtectT2D2() throws Throwable {
String result = ISOUtil.protect("9a{#<gFuG!53Do;G");
assertEquals("9a{#<g__G!53D___", result, "result");
}

@Test
public void testProtectT1D1() throws Throwable {
String result = ISOUtil.protect("a{#<gFuG!53o;G609^FOO/BAR COM^67890o;G");
assertEquals("a{#<gF_______G609^FOO/BAR COM^________", result, "result");
}

@Test
public void testProtectT1D2() throws Throwable {
String result = ISOUtil.protect("9a{#<gFuG!^FOO/BAR COM^67890o;G");
assertEquals("9a{#<gFuG!^FOO/BAR COM^________", result, "result");
}


@Test
public void testProtectT1D3() throws Throwable {
String result = ISOUtil.protect("9a{D<gFuG!^FOO/BAR COM^67890o;G");
assertEquals("9a{D<gFuG!^FOO/BAR COM^________", result, "result");
assertEquals("9a{D<gFuG!^___________^________", result, "result");
}

@Test
Expand Down

0 comments on commit f66f287

Please sign in to comment.