Skip to content

Commit c68a800

Browse files
committed
Bug 36572422 - Validate serialization of 4-byte UTF-8 sequences in C++ client
- pass the bytes from C++ for the string to test instead of having to hard code it in Java [git-p4: depot-paths = "//dev/main.cpp/": change = 110900]
1 parent a0cf4da commit c68a800

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

tests/common/include/common/TestClasses.hpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,24 +1252,40 @@ class TestUtf8Processor
12521252
* Default constructor.
12531253
*/
12541254
TestUtf8Processor()
1255+
: m_vabStringInBytes(self())
12551256
{ }
12561257

1257-
// ----- PortableObject interface -------------------------------------------
1258+
TestUtf8Processor(Array<octet_t>::View vab)
1259+
: m_vabStringInBytes(self())
1260+
{
1261+
m_vabStringInBytes = cast<Array<octet_t>::View>(vab->clone());
1262+
}
1263+
1264+
// ----- PortableObject interface ---------------------------------------
12581265

12591266
public:
12601267
/**
12611268
* @inheritDoc
12621269
*/
1263-
void readExternal(PofReader::Handle /* hIn */)
1270+
void readExternal(PofReader::Handle hIn)
12641271
{
1272+
m_vabStringInBytes = hIn->readOctetArray(0);
12651273
}
12661274

12671275
/**
12681276
* @inheritDoc
12691277
*/
1270-
void writeExternal(PofWriter::Handle /* hOut */) const
1278+
void writeExternal(PofWriter::Handle hOut) const
12711279
{
1280+
hOut->writeOctetArray(0, m_vabStringInBytes);
12721281
}
1282+
1283+
// ----- data members ---------------------------------------------------
1284+
1285+
/**
1286+
* The raw bytes of the UTF-8 string to validate.
1287+
*/
1288+
MemberView<Array<octet_t> > m_vabStringInBytes;
12731289
};
12741290
COH_REGISTER_PORTABLE_CLASS(1513, TestUtf8Processor);
12751291

tests/common/src/java/coherence/tests/TestUtf8Processor.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ public TestUtf8Processor()
4646
@Override
4747
public Object process(InvocableMap.Entry entry)
4848
{
49-
// string initialization copied from Java ExternalizableHelperTest
50-
String sExpected = new String(toBytes(new int[] {0xf0938080, 0xf09f8ebf, 0xf09f8f80, 0xf09f8e89, 0xf09f9294}),
51-
StandardCharsets.UTF_8);
49+
String sExpected = new String(m_abStringInBytes, StandardCharsets.UTF_8);
5250
BinaryEntry binEntry = (BinaryEntry) entry;
5351
Binary binValue = ExternalizableHelper.getUndecorated(binEntry.getBinaryValue());
5452
Binary binExpected = ExternalizableHelper.getUndecorated((Binary) binEntry.getContext()
@@ -75,27 +73,17 @@ public Object process(InvocableMap.Entry entry)
7573
public void readExternal(PofReader in)
7674
throws IOException
7775
{
76+
m_abStringInBytes = in.readByteArray(0);
7877
}
7978

8079
@Override
8180
public void writeExternal(PofWriter out)
8281
throws IOException
8382
{
84-
}
85-
86-
// ----- helper methods -------------------------------------------------
87-
88-
private static byte[] toBytes(int[] ai)
89-
{
90-
ByteBuffer buf = ByteBuffer.allocate(4 * ai.length);
91-
for (int n : ai)
92-
{
93-
buf.putInt(n);
94-
}
95-
return buf.array();
83+
out.writeByteArray(0, m_abStringInBytes);
9684
}
9785

9886
// ----- data members ---------------------------------------------------
9987

100-
private int m_nId;
88+
private byte[] m_abStringInBytes;
10189
}

tests/functional/coherence/net/cache/Utf8Test.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ class Utf8Test : public CxxTest::TestSuite
2929
*/
3030
void test4ByteUtf8()
3131
{
32-
NamedCache::Handle hCache = ensureCleanCache("dist-cache");
32+
NamedCache::Handle hCache = ensureCleanCache("dist-cache");
33+
const int NUM_BYTES = 20; // number of bytes in the byte array to be converted to string
3334

3435
// create UTF-8 4-byte characters: 0xf0938080, 0xf09f8ebf, 0xf09f8f80, 0xf09f8e89, 0xf09f9294
35-
char five[21];
36+
char five[NUM_BYTES + 1]; // add byte for null
3637
// 0xf0938080
3738
five[0] = (char) 0xf0;
3839
five[1] = (char) 0x93;
@@ -66,8 +67,16 @@ class Utf8Test : public CxxTest::TestSuite
6667

6768
hCache->put(vNid, vsTest);
6869

70+
// do not include the null byte as it will be added as another character on Java
71+
// when converting to a String
72+
Array<octet_t>::Handle hab = Array<octet_t>::create(NUM_BYTES);
73+
for (int i = 0; i < NUM_BYTES; ++i)
74+
{
75+
hab[i] = five[i];
76+
}
77+
6978
// compare the Java serialized value to the C++ serialized value
70-
Object::View vResult = hCache->invoke(vNid, TestUtf8Processor::create());
79+
Object::View vResult = hCache->invoke(vNid, TestUtf8Processor::create(hab));
7180

7281
TS_ASSERT(vResult->equals(Boolean::valueOf(true)));
7382
}

0 commit comments

Comments
 (0)