tester = SpecialVariables(testdict, prng)
eq(tester['foo'], 'bar')
- eq(tester['random 10-15 printable'], '[/pNI$;92@') #FIXME: how should I test pseudorandom content?
+ eq(tester['random 10-15 printable'], '[/pNI$;92@')
+
+def test_SpecialVariables_binary():
+ prng = random.Random(1)
+ tester = SpecialVariables({}, prng)
+
+ eq(tester['random 10-15 binary'], '\xdfj\xf1\xd80>a\xcd\xc4\xbb')
def test_assemble_decision():
graph = build_graph()
eq(request['key1'], 'value1')
eq(request['indirect_key1'], 'value1')
eq(request['path'], '/my-readable-bucket')
- eq(request['randkey'], 'value-NI$;92@H/0I') #FIXME: again, how to handle the pseudorandom content?
+ eq(request['randkey'], 'value-NI$;92@H/0I')
assert_raises(KeyError, lambda x: decision[x], 'key3')
def test_weighted_choices():
import itertools
import random
import string
+import struct
import yaml
import sys
class SpecialVariables(dict):
charsets = {
+ 'binary': 'binary',
'printable': string.printable,
'punctuation': string.punctuation,
'whitespace': string.whitespace
charset = self.charsets['printable']
length = self.prng.randint(size_min, size_max)
- return ''.join([self.prng.choice(charset) for _ in xrange(length)]) # Won't scale nicely
+ if charset is 'binary':
+ num_bytes = length + 8
+ tmplist = [self.prng.getrandbits(64) for _ in xrange(num_bytes / 8)]
+ tmpstring = struct.pack((num_bytes / 8) * 'Q', *tmplist)
+ return tmpstring[0:length]
+ else:
+ return ''.join([self.prng.choice(charset) for _ in xrange(length)]) # Won't scale nicely; won't do binary