Skip to content

Commit

Permalink
Add test case to check that SecureKeyBlock is serializable.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgonzal committed Jun 5, 2024
1 parent 232da2c commit bd6c0f3
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions jpos/src/test/java/org/jpos/security/SecureKeyBlockTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2023 jPOS Software SRL
* Copyright (C) 2000-2024 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand All @@ -18,20 +18,23 @@

package org.jpos.security;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.LinkedHashMap;
import java.util.Map;

import org.bouncycastle.util.Arrays;
import org.jpos.iso.ISOUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.*;

/**
*
*/
public class SecureKeyBlockTest {

private static final String NL = System.getProperty("line.separator");
Expand Down Expand Up @@ -227,4 +230,26 @@ public void testDumpWithNameAndOptHeader() {
assertEquals(sb.toString(), os.toString());
}

@Test
public void testSerialization() throws IOException, ClassNotFoundException {
String tr31 = "C0088P0TN00E000054CF02CF89CCC36897E3D4AC22D7BBECA1CBD08296A315A47228274A2FAE03EA699AF35F";
SecureKeyBlock key = SecureKeyBlockBuilder.newBuilder().build(tr31);
key.dump(System.out, " ");
// Serialize instance.lkajsdf
Object obj = key;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject(obj); // This call errors out if 'obj' is not serializable.
byte[] img = baos.toByteArray();
// Deserialize.
ByteArrayInputStream bais = new ByteArrayInputStream(img);
ObjectInputStream is = new ObjectInputStream(bais);
SecureKeyBlock key2 = (SecureKeyBlock) is.readObject();
key2.dump(System.out, " ");
assertEquals(key.getKeyName(), key2.getKeyName());
assertEquals(key.getModeOfUse(), key2.getModeOfUse());
assertEquals(key.getKeyUsage().getCode(), key2.getKeyUsage().getCode());
assertEquals(key.getAlgorithm().getCode(), key2.getAlgorithm().getCode());
assertTrue(Arrays.areEqual(key.getKeyBytes(), key2.getKeyBytes()));
}
}

0 comments on commit bd6c0f3

Please sign in to comment.