def test_basic_validation(self): self.assertRaises(TypeError, is_valid, 100) self.assertRaises(TypeError, is_valid, u"test") self.assertRaises(TypeError, is_valid, 10.4) self.assertInvalid(b"test") # the simplest valid BSON document self.assertTrue(is_valid(b"\x05\x00\x00\x00\x00")) self.assertTrue(is_valid(BSON(b"\x05\x00\x00\x00\x00"))) # failure cases self.assertInvalid(b"\x04\x00\x00\x00\x00") self.assertInvalid(b"\x05\x00\x00\x00\x01") self.assertInvalid(b"\x05\x00\x00\x00") self.assertInvalid(b"\x05\x00\x00\x00\x00\x00") self.assertInvalid(b"\x07\x00\x00\x00\x02a\x00\x78\x56\x34\x12") self.assertInvalid(b"\x09\x00\x00\x00\x10a\x00\x05\x00") self.assertInvalid(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") self.assertInvalid(b"\x13\x00\x00\x00\x02foo\x00" b"\x04\x00\x00\x00bar\x00\x00") self.assertInvalid(b"\x18\x00\x00\x00\x03foo\x00\x0f\x00\x00" b"\x00\x10bar\x00\xff\xff\xff\x7f\x00\x00") self.assertInvalid(b"\x15\x00\x00\x00\x03foo\x00\x0c" b"\x00\x00\x00\x08bar\x00\x01\x00\x00") self.assertInvalid(b"\x1c\x00\x00\x00\x03foo\x00" b"\x12\x00\x00\x00\x02bar\x00" b"\x05\x00\x00\x00baz\x00\x00\x00") self.assertInvalid(b"\x10\x00\x00\x00\x02a\x00" b"\x04\x00\x00\x00abc\xff\x00")
def test_basic_validation(self): self.assertRaises(TypeError, is_valid, 100) self.assertRaises(TypeError, is_valid, u("test")) self.assertRaises(TypeError, is_valid, 10.4) self.assertInvalid(b"test") # the simplest valid BSON document self.assertTrue(is_valid(b"\x05\x00\x00\x00\x00")) self.assertTrue(is_valid(BSON(b"\x05\x00\x00\x00\x00"))) # failure cases self.assertInvalid(b"\x04\x00\x00\x00\x00") self.assertInvalid(b"\x05\x00\x00\x00\x01") self.assertInvalid(b"\x05\x00\x00\x00") self.assertInvalid(b"\x05\x00\x00\x00\x00\x00") self.assertInvalid(b"\x07\x00\x00\x00\x02a\x00\x78\x56\x34\x12") self.assertInvalid(b"\x09\x00\x00\x00\x10a\x00\x05\x00") self.assertInvalid(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") self.assertInvalid(b"\x13\x00\x00\x00\x02foo\x00" b"\x04\x00\x00\x00bar\x00\x00") self.assertInvalid(b"\x18\x00\x00\x00\x03foo\x00\x0f\x00\x00" b"\x00\x10bar\x00\xff\xff\xff\x7f\x00\x00") self.assertInvalid(b"\x15\x00\x00\x00\x03foo\x00\x0c" b"\x00\x00\x00\x08bar\x00\x01\x00\x00") self.assertInvalid(b"\x1c\x00\x00\x00\x03foo\x00" b"\x12\x00\x00\x00\x02bar\x00" b"\x05\x00\x00\x00baz\x00\x00\x00") self.assertInvalid(b"\x10\x00\x00\x00\x02a\x00" b"\x04\x00\x00\x00abc\xff\x00")
def submit(_pool, environ, system_token): counters_fam = pycassa.ColumnFamily(_pool, 'Counters', retry_counter_mutations=True) proposed_counters_fam = pycassa.ColumnFamily(_pool, 'CountersForProposed', retry_counter_mutations=True) systemoopshashes_cf = pycassa.ColumnFamily(_pool, 'SystemOOPSHashes') try: data = environ['wsgi.input'].read() except IOError as e: if e.message == 'request data read error': # The client disconnected while sending the report. metrics.meter('invalid.connection_dropped') return (False, 'Connection dropped.') else: raise try: if not bson.is_valid(data): metrics.meter('invalid.invalid_bson') return (False, 'Invalid BSON.') data = bson.BSON(data).decode() except bson.errors.InvalidBSON, TypeError: metrics.meter('invalid.invalid_bson') return (False, 'Invalid BSON.')
def test_basic_validation(self): self.assertRaises(TypeError, is_valid, 100) self.assertRaises(TypeError, is_valid, u"test") self.assertRaises(TypeError, is_valid, 10.4) self.assertFalse(is_valid(b("test"))) # the simplest valid BSON document self.assertTrue(is_valid(b("\x05\x00\x00\x00\x00"))) self.assertTrue(is_valid(BSON(b("\x05\x00\x00\x00\x00")))) self.assertFalse(is_valid(b("\x04\x00\x00\x00\x00"))) self.assertFalse(is_valid(b("\x05\x00\x00\x00\x01"))) self.assertFalse(is_valid(b("\x05\x00\x00\x00"))) self.assertFalse(is_valid(b("\x05\x00\x00\x00\x00\x00"))) self.assertFalse(is_valid(b("\x07\x00\x00\x00\x02a\x00\x78\x56\x34\x12"))) self.assertFalse(is_valid(b("\x09\x00\x00\x00\x10a\x00\x05\x00"))) self.assertFalse(is_valid(b("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")))
def __parse_message_to_bson(self, message_body): try: if not bson.is_valid(self.message_body): raise message = bson.decode_all(message_body) if not (isinstance(message, list) or len(message) == 1): raise ValueError("Received message must be an object, not " "a list.") return message[0] except: raise errors.InvalidBSON(message_body)
def unpack(self): if len(self.buf) < 4: raise StopIteration('stop') framelen = struct.unpack('<i', self.buf[:4])[0] if len(self.buf) < framelen: raise StopIteration('stop') rest, self.buf = self.buf[:framelen], self.buf[framelen:] if not bson.is_valid(rest): logging.critical('Invalid BSON in frame from remote! content: {0}'.format(repr(rest))) return self.unpack() dec = bson.BSON(rest).decode() return dec['data']
def commit_step(self, parent_id, data): """ Commit step to backend. parent_id may be None Args: parent_id - int | None data: BSON serializable data Returns: step_id - int """ assert bson.is_valid(data) self._commit_step(parent_id, data) step = Step(parent_id, data) return hash(step)
def test_basic_validation(self): self.assertRaises(TypeError, is_valid, 100) self.assertRaises(TypeError, is_valid, u"test") self.assertRaises(TypeError, is_valid, 10.4) self.assertFalse(is_valid("test")) # the simplest valid BSON document self.assert_(is_valid("\x05\x00\x00\x00\x00")) self.assert_(is_valid(BSON("\x05\x00\x00\x00\x00"))) self.assertFalse(is_valid("\x04\x00\x00\x00\x00")) self.assertFalse(is_valid("\x05\x00\x00\x00\x01")) self.assertFalse(is_valid("\x05\x00\x00\x00")) self.assertFalse(is_valid("\x05\x00\x00\x00\x00\x00"))
def test_basic_validation(self): self.assertRaises(TypeError, is_valid, 100) self.assertRaises(TypeError, is_valid, "test") self.assertRaises(TypeError, is_valid, 10.4) self.assertFalse(is_valid(b"test")) # the simplest valid BSON document self.assert_(is_valid(b"\x05\x00\x00\x00\x00")) self.assert_(is_valid(BSON(b"\x05\x00\x00\x00\x00"))) self.assertFalse(is_valid(b"\x04\x00\x00\x00\x00")) self.assertFalse(is_valid(b"\x05\x00\x00\x00\x01")) self.assertFalse(is_valid(b"\x05\x00\x00\x00")) self.assertFalse(is_valid(b"\x05\x00\x00\x00\x00\x00"))
def __init__(self, val): """Initialize BSONObjPrinter.""" self.val = val self.ptr = self.val['_objdata'].cast(gdb.lookup_type('void').pointer()) self.is_valid = False # Handle the endianness of the BSON object size, which is represented as a 32-bit integer # in little-endian format. inferior = gdb.selected_inferior() if self.ptr.is_optimized_out: # If the value has been optimized out, we cannot decode it. self.size = -1 self.raw_memory = None else: self.size = struct.unpack('<I', inferior.read_memory(self.ptr, 4))[0] self.raw_memory = bytes( memoryview(inferior.read_memory(self.ptr, self.size))) if bson: self.is_valid = bson.is_valid(self.raw_memory)
def BSONObjPrinter(valobj, *_args): # pylint: disable=invalid-name """Print a BSONObj in a JSON format.""" ptr = valobj.GetChildMemberWithName("_objdata").GetValueAsUnsigned() size = struct.unpack( "<I", valobj.GetProcess().ReadMemory(ptr, 4, lldb.SBError()))[0] if size < 5 or size > 17 * 1024 * 1024: return None mem = bytes( memoryview(valobj.GetProcess().ReadMemory(ptr, size, lldb.SBError()))) if not bson.is_valid(mem): return None buf_str = bson.decode(mem) obj = json_util.dumps(buf_str, indent=4) # If the object is huge then just dump it as one line if obj.count("\n") > 1000: return json_util.dumps(buf_str) # Otherwise try to be nice and pretty print the JSON return obj
def open(self, uri, options=None): """Open `uri`. The `options` argument provies additional input to the driver. Which options that are supported varies between the plugins. It should be a valid URL query string of the form: key1=value1;key2=value2... An ampersand (&) may be used instead of the semicolon (;). Typical options supported by most drivers include: - mode : append (default) | r | w Valid values are: - append Append to existing file or create new file - r Open existing file for read-only - w Truncate existing file or create new file After the options are passed, this method may set attribute `writable` to True if it is writable and to False otherwise. If `writable` is not set, it is assumed to be True. The BSON data is translated to JSON. """ self.options = Options(options, defaults='mode=append') self.mode = dict(r='rb', w='wb', append='rb+')[self.options.mode] if self.mode == 'rb' and not os.path.exists(uri): raise FileNotFoundError(f"Did not find URI '{uri}'") self.writable = False if 'rb' in self.mode else True self.uri = uri self.d = {} if self.mode in ('rb', 'rb+'): with open(uri, self.mode) as f: bson_data = f.read() if pybson.is_valid(bson_data): self.d = pybson.decode(bson_data) if not self.d: raise EOFError(f"Failed to read BSON data from '{uri}'") else: raise EOFError(f"Invalid BSON data in source '{uri}'")
def load_state_file(path): if not os.path.exists(path): return False d = open(path, 'rb').read() if not bson.is_valid(d): return False return bson.BSON(d).decode()