Skip to content

Commit f019d63

Browse files
committed
Added some re-read tests
1 parent 57e200c commit f019d63

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

tests/tests.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ def read_file(self, filename, stream=False):
9292
with open(found_file, 'rb') as filep:
9393
return filep.read()
9494

95+
def _try_marshalling(self, original_stream, original_object):
96+
"""
97+
Tries to marshall an object and compares it to the original stream
98+
"""
99+
marshalled_stream = javaobj.dumps(original_object)
100+
# Reloading the new dump allows to compare the decoding sequence
101+
try:
102+
javaobj.loads(marshalled_stream)
103+
self.assertEqual(original_stream, marshalled_stream)
104+
except:
105+
print("-" * 80)
106+
print("=" * 30, "Original", "=" * 30)
107+
print(javaobj.JavaObjectUnmarshaller._create_hexdump(
108+
original_stream))
109+
print("*" * 30, "Marshalled", "*" * 30)
110+
print(javaobj.JavaObjectUnmarshaller._create_hexdump(
111+
marshalled_stream))
112+
print("-" * 80)
113+
raise
114+
95115
def test_char_rw(self):
96116
"""
97117
Reads testChar.ser and checks the serialization process
@@ -100,8 +120,7 @@ def test_char_rw(self):
100120
pobj = javaobj.loads(jobj)
101121
_logger.debug("Read char object: %s", pobj)
102122
self.assertEqual(pobj, '\x00C')
103-
jobj_ = javaobj.dumps(pobj)
104-
self.assertEqual(jobj, jobj_)
123+
self._try_marshalling(jobj, pobj)
105124

106125
def test_double_rw(self):
107126
"""
@@ -112,9 +131,7 @@ def test_double_rw(self):
112131
_logger.debug("Read double object: %s", pobj)
113132

114133
self.assertEqual(pobj, '\x7f\xef\xff\xff\xff\xff\xff\xff')
115-
116-
jobj_ = javaobj.dumps(pobj)
117-
self.assertEqual(jobj, jobj_)
134+
self._try_marshalling(jobj, pobj)
118135

119136
def test_bytes_rw(self):
120137
"""
@@ -125,19 +142,14 @@ def test_bytes_rw(self):
125142
_logger.debug("Read bytes: %s", pobj)
126143

127144
self.assertEqual(pobj, 'HelloWorld')
128-
129-
jobj_ = javaobj.dumps(pobj)
130-
self.assertEqual(jobj, jobj_)
145+
self._try_marshalling(jobj, pobj)
131146

132147
def test_class_with_byte_array_rw(self):
133148
jobj = self.read_file("testClassWithByteArray.ser")
134149
pobj = javaobj.loads(jobj)
135150

136151
self.assertEqual(pobj.myArray, [1,3,7,11])
137-
138-
jobj_ = javaobj.dumps(pobj)
139-
javaobj.loads(jobj_)
140-
self.assertEqual(jobj, jobj_)
152+
self._try_marshalling(jobj, pobj)
141153

142154
def test_boolean(self):
143155
"""
@@ -148,9 +160,7 @@ def test_boolean(self):
148160
_logger.debug("Read boolean object: %s", pobj)
149161

150162
self.assertEqual(pobj, chr(0))
151-
152-
jobj_ = javaobj.dumps(pobj)
153-
self.assertEqual(jobj, jobj_)
163+
self._try_marshalling(jobj, pobj)
154164

155165
def test_byte(self):
156166
"""
@@ -163,9 +173,7 @@ def test_byte(self):
163173
_logger.debug("Read Byte: %r", pobj)
164174

165175
self.assertEqual(pobj, chr(127))
166-
167-
jobj_ = javaobj.dumps(pobj)
168-
self.assertEqual(jobj, jobj_)
176+
self._try_marshalling(jobj, pobj)
169177

170178
def test_fields(self):
171179
"""
@@ -189,11 +197,7 @@ def test_fields(self):
189197
_logger.debug(".. Fields Types: %s", classdesc.fields_types)
190198

191199
self.assertEqual(len(classdesc.fields_names), 3)
192-
193-
jobj_ = javaobj.dumps(pobj)
194-
# Reloading the new dump allows to compare the decoding sequence
195-
javaobj.loads(jobj_)
196-
self.assertEqual(jobj, jobj_)
200+
self._try_marshalling(jobj, pobj)
197201

198202
def test_class(self):
199203
"""
@@ -203,9 +207,7 @@ def test_class(self):
203207
pobj = javaobj.loads(jobj)
204208
_logger.debug("Read object: %s", pobj)
205209
self.assertEqual(pobj.name, 'java.lang.String')
206-
207-
jobj_ = javaobj.dumps(pobj)
208-
self.assertEqual(jobj, jobj_)
210+
self._try_marshalling(jobj, pobj)
209211

210212
# def test_swing_object(self):
211213
# """
@@ -235,6 +237,8 @@ def test_super(self):
235237
self.assertEqual(pobj.integer, -1)
236238
self.assertEqual(pobj.superString, "Super!!")
237239

240+
self._try_marshalling(jobj, pobj)
241+
238242
def test_arrays(self):
239243
jobj = self.read_file("objArrays.ser")
240244
pobj = javaobj.loads(jobj)
@@ -256,6 +260,8 @@ def test_arrays(self):
256260
_logger.debug(pobj.boolArr)
257261
_logger.debug(pobj.concreteArr)
258262

263+
self._try_marshalling(jobj, pobj)
264+
259265
def test_enums(self):
260266
jobj = self.read_file("objEnums.ser")
261267
pobj = javaobj.loads(jobj)
@@ -274,6 +280,8 @@ def test_enums(self):
274280
self.assertEqual(color.classdesc.name, "Color")
275281
self.assertEqual(color.constant, intended)
276282

283+
# self._try_marshalling(jobj, pobj)
284+
277285
# def test_exception(self):
278286
# jobj = self.read_file("objException.ser")
279287
# pobj = javaobj.loads(jobj)
@@ -312,10 +320,14 @@ def test_collections(self):
312320
_logger.debug("linkedList: %s", pobj.linkedList)
313321
self.assertTrue(isinstance(pobj.linkedList, list))
314322

323+
# FIXME: referencing problems with the collection class
324+
# self._try_marshalling(jobj, pobj)
325+
315326
def test_jceks_issue_5(self):
316327
jobj = self.read_file("jceks_issue_5.ser")
317328
pobj = javaobj.loads(jobj)
318329
_logger.info(pobj)
330+
# self._try_marshalling(jobj, pobj)
319331

320332
# ------------------------------------------------------------------------------
321333

0 commit comments

Comments
 (0)