/usr/lib/python2.7/dist-packages/rekall/plugins/tools/json_test.py is in python-rekall-core 1.6.0+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | # Rekall Memory Forensics
# Copyright 2014 Google Inc. All Rights Reserved.
#
# Author: Michael Cohen scudette@google.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""Tests for json encoding/decoding."""
import json
import logging
import StringIO
from rekall import testlib
from rekall import utils
from rekall.ui import json_renderer
from rekall.plugins.renderers import data_export
class JsonTest(testlib.RekallBaseUnitTestCase):
"""Test the Json encode/decoder."""
PLUGIN = "json_render"
def setUp(self):
self.session = self.MakeUserSession()
self.renderer = json_renderer.JsonRenderer(session=self.session)
self.encoder = self.renderer.encoder
self.decoder = self.renderer.decoder
def testEncoderCache(self):
# Make the string long enough so that parts of it are garbage
# collected. If the encoded uses id() to deduplicate it will fail since
# id() might reuse across GCed objects.
test_string = ("this_is_a_very_long_sentence" * 10)
parts = [test_string[x:x+16] for x in xrange(
0, len(test_string), 16)]
with data_export.DataExportRenderer(
session=self.session,
output=StringIO.StringIO()).start() as renderer:
utils.WriteHexdump(renderer, test_string)
rows = []
for row in renderer.data:
if row[0] == "r":
rows.append(row[1]["data"])
self.assertEqual(rows, parts)
def testObjectRenderer(self):
cases = [
('\xff\xff\x00\x00', {'mro': u'str:basestring:object',
'b64': u'//8AAA=='}),
("hello", u'hello'), # A string is converted into unicode if
# possible.
(1, 1), # Ints are already JSON serializable.
(dict(foo=2), {'foo': 2}),
(set([1, 2, 3]), {'mro': u'set:object', 'data': [1, 2, 3]}),
([1, 2, 3], [1, 2, 3]),
([1, "\xff\xff\x00\x00", 3], [1, {'mro': u'str:basestring:object',
'b64': u'//8AAA=='}, 3]),
]
for case in cases:
encoded = self.encoder.Encode(case[0])
self.assertEqual(encoded, case[1])
def testProperSerialization(self):
"""Test that serializing simple python objects with json works.
NOTE: Json is not intrinsically a fully functional serialization format
- it is unable to serialize many common python primitives (e.g. strings,
dicts with numeric keys etc). This tests that our wrapping around the
json format allows the correct serialization of python primitives.
"""
for case in [
[1, 2],
[1, "hello"],
["1", "2"],
["hello", u'Gr\xfcetzi'],
"hello",
u'Gr\xfcetzi',
dict(a="hello"),
dict(b=dict(a="hello")), # Nested dict.
]:
data = self.encoder.Encode(case)
logging.debug("%s->%s" % (case, data))
# Make sure the data is JSON serializable.
self.assertEqual(data, json.loads(json.dumps(data)))
self.assertEqual(case, self.decoder.Decode(data))
def testObjectSerization(self):
"""Serialize _EPROCESS objects.
We check that the deserialized object is an exact replica of the
original - this includes the same address spaces, profile and offset.
Having the objects identical allows us to dereference object members
seamlessly.
"""
for task in self.session.plugins.pslist().filter_processes():
data = self.encoder.Encode(task)
logging.debug("%r->%s" % (task, data))
# Make sure the data is JSON serializable.
self.assertEqual(data, json.loads(json.dumps(data)))
decoded_task = self.decoder.Decode(data)
self.assertEqual(task.obj_offset, decoded_task.obj_offset)
self.assertEqual(task.obj_name, decoded_task.obj_name)
self.assertEqual(task.obj_vm.name, decoded_task.obj_vm.name)
# Check the process name is the same - this tests subfield
# dereferencing.
self.assertEqual(task.name, decoded_task.name)
self.assertEqual(task.pid, decoded_task.pid)
def testAllObjectSerialization(self):
for vtype in self.session.profile.vtypes:
obj = self.session.profile.Object(vtype)
self.CheckObjectSerization(obj)
if self.session.profile != None:
self.CheckObjectSerization(self.session.profile)
self.CheckObjectSerization(self.session.kernel_address_space)
self.CheckObjectSerization(self.session.physical_address_space)
# Some native types.
self.CheckObjectSerization(set([1, 2, 3]))
self.CheckObjectSerization(dict(a=1, b=dict(a=1)))
def CheckObjectSerization(self, obj):
json_renderer_obj = json_renderer.JsonRenderer(session=self.session)
data_export_renderer_obj = data_export.DataExportRenderer(
session=self.session)
# First test json encodings.
encoded = json_renderer_obj.encode(obj)
# Make sure it is json safe.
json.dumps(encoded)
# Now decode it.
decoded = json_renderer_obj.decode(encoded)
self.assertEqual(decoded, obj)
# Now check the DataExportRenderer.
encoded = data_export_renderer_obj.encode(obj)
# Make sure it is json safe.
json.dumps(encoded)
# Data Export is not decodable.
|