import os, signal, sys, time from random import randrange from cStringIO import StringIO from twisted.internet import reactor, defer, threads from twisted.python import failure from twisted.trial import unittest from allmydata.util import fileutil, log from ..util.assertutil import precondition from allmydata.util.encodingutil import unicode_platform, get_filesystem_encoding from ..scripts import runner def skip_if_cannot_represent_filename(u): precondition(isinstance(u, unicode)) enc = get_filesystem_encoding() if not unicode_platform(): try: u.encode(enc) except UnicodeEncodeError: raise unittest.SkipTest("A non-ASCII filename could not be encoded on this platform.") def run_cli(verb, *args, **kwargs): precondition(not [True for arg in args if not isinstance(arg, str)], "arguments to do_cli must be strs -- convert using unicode_to_argv", args=args) nodeargs = kwargs.get("nodeargs", []) argv = nodeargs + [verb] + list(args) stdin = kwargs.get("stdin", "") stdout, stderr = StringIO(), StringIO() d = threads.deferToThread(runner.runner, argv, run_by_human=False, stdin=StringIO(stdin), stdout=stdout, stderr=stderr) def _done(rc): return rc, stdout.getvalue(), stderr.getvalue() d.addCallback(_done) return d class DevNullDictionary(dict): def __setitem__(self, key, value): return def insecurerandstr(n): return ''.join(map(chr, map(randrange, [0]*n, [256]*n))) def flip_bit(good, which): # flip the low-order bit of good[which] if which == -1: pieces = good[:which], good[-1:], "" else: pieces = good[:which], good[which:which+1], good[which+1:] return pieces[0] + chr(ord(pieces[1]) ^ 0x01) + pieces[2] def flip_one_bit(s, offset=0, size=None): """ flip one random bit of the string s, in a byte greater than or equal to offset and less than offset+size. """ if size is None: size=len(s)-offset i = randrange(offset, offset+size) result = s[:i] + chr(ord(s[i])^(0x01<