def get_orders(self): ''' Get orders from tables with Customers that are ready to order. ''' self.send_cmd(self.CMD_GET_ORDERS) if DEBUG: print "cmd: get orders -----------" orders = self.state['e'].get_orders(self.magic_page) # recv order count self.read(length=1, expect=sp.pack_single_uint8(len(orders))) if DEBUG: print " {0} orders: {1}".format(len(orders), orders) for o in orders: [(p_tid, p_cid, p_ft, p_fi) ] = self.state['e'].pack_orders_tuples([o]) # recv orders f = Variable('food{0}'.format(o.cid)) f.set_slice(0) i = Variable('id{0}'.format(o.cid)) i.set_slice(0) self.read(length=len(p_tid), expect=p_tid) self.read(length=len(p_cid), assign=i) self.read(length=len(p_ft), expect=p_ft) self.read(length=len(p_fi), assign=f) self.state['food_item'][o.cid] = f self.state['c_ids'][o.cid] = i self.recv_status(self.STATUS_OK) return 0
def get_orders(self): ''' Get orders from tables with Customers that are ready to order. ''' self.send_cmd(self.CMD_GET_ORDERS) if DEBUG: print "cmd: get orders -----------" orders = self.state['e'].get_orders(self.magic_page) # recv order count self.read(length=1, expect=sp.pack_single_uint8(len(orders))) if DEBUG: print " {0} orders: {1}".format(len(orders), orders) for o in orders: [(p_tid, p_cid, p_ft, p_fi)] = self.state['e'].pack_orders_tuples([o]) # recv orders f = Variable('food{0}'.format(o.cid)) f.set_slice(0) i = Variable('id{0}'.format(o.cid)) i.set_slice(0) self.read(length=len(p_tid), expect=p_tid) self.read(length=len(p_cid), assign=i) self.read(length=len(p_ft), expect=p_ft) self.read(length=len(p_fi), assign=f) self.state['food_item'][o.cid] = f self.state['c_ids'][o.cid] = i self.recv_status(self.STATUS_OK) return 0
def rpacket(self, pkt_type, op_code, status): self.read(length=8) # we can't easily check these tid = Variable('trans' + str(self.state['counter'])) tid.set_slice(0) self.state['counter'] += 1 self.read(length=3, expect=struct.pack('<BBB', pkt_type, op_code, status)) self.read(length=4, assign=tid) return tid
def start(self): """ Intialize state. """ # The CB will first request a randomly-named index file. This is a # unique case because this filename will be a Variable and its value, # therefore, is inacccessible during poll generation. idx_path = Variable("idxpath") idx_path.set_slice(begin=0, end=-1) self.read(delim=self.STRING_TERMINATOR, assign=idx_path) self.state['idx_path'] = idx_path
def _create_election(self): ''' Create the election manager profile and setup the election parameters ''' self._process_menu() self.state['e'].authd_user = None cmd = 0x11 self._send_str(cmd) if True == self._is_menu_id_valid_op(cmd): # create election mgr self._receive_by_len(self.CREATE_E_MGR, term=CONFIG['TERM']) mgr = self.state['e'].make_random_e_mgr() self._send_first_last_name(mgr.f_name, mgr.l_name) self._receive_by_len(self.NEW_UID) self._receive_by_len(mgr.id, delim=CONFIG['DELIM']) self._receive_by_len(self.NEW_AUTH_KEY) auth_key = Variable('authkey') auth_key.set_slice(0) self.read(delim=CONFIG['DELIM'], assign=auth_key) self.state['e'].set_e_mgr_auth_key(auth_key) # send election name self._receive_prompt_send_answer(self.ELECTION_NAME, self.state['e'].name) # set election conf num_winners = randint(1, 4) max_candidates = randint(5, 20) write_in_ok = choice(['Y', 'N']) self._receive_prompt_send_answer(self.NUM_WINNERS_Q, num_winners) self._receive_prompt_send_answer(self.MAX_CANDIDATES_Q, max_candidates) self._receive_prompt_send_answer(self.WRITE_IN_OK_Q, write_in_ok) self.state['e'].num_winners = num_winners self.state['e'].max_candidates = max_candidates self.state['e'].write_in_ok = 1 if 'Y' == write_in_ok else 0 self.state['e'].is_configured = True # set state to NEW self.state['e'].set_state('NEW') # recv ELECTION_CREATED self._receive_by_len(self.ELECTION_CREATED, term=CONFIG['TERM']) return 0 else: self._receive_fail() return -1
def start(self): """ Intialize state. """ # The CB will first request a randomly-named index file. This is a # unique case because this filename will be a Variable and its value, # therefore, is inacccessible during poll generation. idx_path = Variable("idxpath") idx_path.set_slice(begin=0, end=-1) self.read( delim=self.STRING_TERMINATOR, assign=idx_path) self.state['idx_path'] = idx_path
def _vote(self): ''' Place a vote. ''' self._process_menu() cmd = 0x24 self._send_str(cmd) if True == self._is_menu_id_valid_op(cmd): # check to see if there are any candidates to vote for if 0 == self.state['e'].get_candidate_count(): self._receive_by_len(self.EMPTY_CANDIDATES, term=CONFIG['TERM']) else: # read candidate list self._receive_by_len(self.SELECT_CANDIDATE, term=CONFIG['TERM']) fmt = "\t{0}: {1} {2}\n" for c in self.state['e'].candidates: line = fmt.format(c.id, c.f_name, c.l_name) self._receive_by_len(line) self._receive_by_len(self.CHOOSE, term=CONFIG['TERM']) # make selection c = self.state['e'].get_random_candidate() self._send_str(c.id) # receive voting receipt p1 = "Confirmation #" p2 = " Vote recorded for candidate: {0} {1}.\n" self._receive_by_len(p1) vote_id = Variable('voteid{0}'.format( self.state['e'].authd_user.id)) vote_id.set_slice(0) self.read(delim='.', assign=vote_id) self._receive_by_len(p2.format(c.f_name, c.l_name)) # record vote v = Vote(vote_id, self.state['e'].authd_user, c) self.state['e'].add_vote(v) self.state['e'].authd_user = None return 0 else: self._receive_fail() return -1
def _vote(self): ''' Place a vote. ''' self._process_menu() cmd = 0x24 self._send_str(cmd) if True == self._is_menu_id_valid_op(cmd): # check to see if there are any candidates to vote for if 0 == self.state['e'].get_candidate_count(): self._receive_by_len(self.EMPTY_CANDIDATES, term=CONFIG['TERM']) else: # read candidate list self._receive_by_len(self.SELECT_CANDIDATE, term=CONFIG['TERM']) fmt = "\t{0}: {1} {2}\n" for c in self.state['e'].candidates: line = fmt.format(c.id, c.f_name, c.l_name) self._receive_by_len(line) self._receive_by_len(self.CHOOSE, term=CONFIG['TERM']) # make selection c = self.state['e'].get_random_candidate() self._send_str(c.id) # receive voting receipt p1 = "Confirmation #" p2 = " Vote recorded for candidate: {0} {1}.\n" self._receive_by_len(p1) vote_id = Variable('voteid{0}'.format(self.state['e'].authd_user.id)) vote_id.set_slice(0) self.read(delim='.', assign=vote_id) self._receive_by_len(p2.format(c.f_name, c.l_name)) # record vote v = Vote(vote_id, self.state['e'].authd_user, c) self.state['e'].add_vote(v) self.state['e'].authd_user = None return 0 else: self._receive_fail() return -1
def start(self): n = random.randint(0, 32 * 1024) data = random_bytes(n) self.write('compress\n') self.write(data + '\n') compressed = Variable('compressed') compressed.set_slice(0) self.read(delim=end_marker, assign=compressed) self.write('decompress\n') self.write(compressed) self.write('\n') self.read(expect=data, length=len(data)) self.write('quit\n')
def _process_nonce(self): nonce = Variable('nonce') nonce.set_slice(0, None) self.read(length=8, assign=nonce) if self.chance(0.001): cmd = self.state['m'].pack_single_uint32(0xAAAA) cmd += self.state['m'].pack_single_uint32(0xBBBB) self.write(cmd) self._recv_error('ERR_INVALID_NONCE') if self.DEBUG_ERR: print('exiting due to incorrect nonce.') return -1 else: self.write(nonce) return 0
def buy_postage(self): ''' Sell postage to customer. ''' self.send_cmd(self.CMD_BUY_POSTAGE) if DEBUG: print "cmd: buy postage" stamp = Variable('stamp{0}'.format(self.state['e'].stamp_id())) stamp.set_slice(0,3) self.read(length=3, assign=stamp) self.state['e'].add_stamp(stamp) self.recv_status(self.STATUS_OK) return 0
def test_variable(self): with self.assertRaises(TypeError): Variable() with self.assertRaises(AssertionError): Variable('!') x = Variable('foo') # can't 'set' without defining the value to set with self.assertRaises(AssertionError): x.get_set() # can't 'read' without defining what to read with self.assertRaises(AssertionError): x.get_read() # can't 'write' until value has been set with self.assertRaises(AssertionError): x.get_write() with self.assertRaises(AssertionError): x.set_re(None) x.set_re('(.*)') self.assertEquals( x.get_read(), '<assign> <var>foo</var> ' '<pcre>(.*)</pcre> </assign>') x.set_value('hi') with self.assertRaises(AssertionError): x.get_read() self.assertEquals( x.get_set(), '<decl> <var>foo</var> ' '<value> <data>hi</data> </value> </decl>') with self.assertRaises(AssertionError): x.set_slice("1") with self.assertRaises(AssertionError): x.set_slice(0, "1") x.set_slice(0) self.assertEquals( x.get_read(), '<assign> <var>foo</var> <slice ' 'begin="0" /> </assign>') x.set_slice(1, 2) self.assertEquals( x.get_read(), '<assign> <var>foo</var> <slice ' 'begin="1" end="2" /> </assign>') self.assertEquals(x.get_write(), '<var>foo</var>')
class PalindromMaker(Actions): def start(self) : self.string = "".join([chr(random.randint(0,255)) for _ in xrange(random.randint(1, (BUF_SIZE / 2) -1))]) self.nonce = Variable('nonce') self.nonce.set_slice(TOKEN_START_PAD_LEN, TOKEN_START_PAD_LEN + TOKEN_SIZE) def genpalindrome(self) : self.read(length=TOKEN_BUF_SIZE, assign=self.nonce) self.write(self.nonce) self.write(self.string) self.read(length=len(self.string)*2, expect=self.string + "".join([_ for _ in reversed(self.string)])) def end(self): pass
def _read_and_match_calculated_dbl64(self, val): ''' This function will read the least significant byte of a double precision float with a wildcard, and then read the other 7 bytes with an exact match. Exact comparison with computed double precision floating point numbers is non-deterministic when the numbers are computed in 2 different programming languages. There are too many cases where the least significant digit or two of a calculated float differ when calculated in C vs Python. ''' exp_result = self.state['m'].pack_single(val)[1:] # read 1 byte with wildcard least_sig_byte = Variable('LSB') least_sig_byte.set_slice(0, 1) self.read(length=1, assign=least_sig_byte) # read 7 bytes for exact match self.read(length=len(exp_result), expect=exp_result)
def _read_and_match_calculated_dbl64(self, val): ''' This function will read the least significant byte of a double precision float with a wildcard, and then read the other 7 bytes with an exact match. Exact comparison with computed double precision floating point numbers is non-deterministic when the numbers are computed in 2 different programming languages. There are too many cases where the least significant digit or two of a calculated float differ when calculated in C vs Python. ''' exp_result = self.state['m'].pack_single(val)[1:] # read 1 byte with wildcard least_sig_byte = Variable('LSB') least_sig_byte.set_slice(0,1) self.read(length=1, assign=least_sig_byte) # read 7 bytes for exact match self.read(length=len(exp_result), expect=exp_result)
def test_variable(self): with self.assertRaises(TypeError): Variable() with self.assertRaises(AssertionError): Variable('!') x = Variable('foo') # can't 'set' without defining the value to set with self.assertRaises(AssertionError): x.get_set() # can't 'read' without defining what to read with self.assertRaises(AssertionError): x.get_read() # can't 'write' until value has been set with self.assertRaises(AssertionError): x.get_write() with self.assertRaises(AssertionError): x.set_re(None) x.set_re('(.*)') self.assertEquals(x.get_read(), '<assign> <var>foo</var> ' '<pcre>(.*)</pcre> </assign>') x.set_value('hi') with self.assertRaises(AssertionError): x.get_read() self.assertEquals(x.get_set(), '<decl> <var>foo</var> ' '<value> <data>hi</data> </value> </decl>') with self.assertRaises(AssertionError): x.set_slice("1") with self.assertRaises(AssertionError): x.set_slice(0, "1") x.set_slice(0) self.assertEquals(x.get_read(), '<assign> <var>foo</var> <slice ' 'begin="0" /> </assign>') x.set_slice(1, 2) self.assertEquals(x.get_read(), '<assign> <var>foo</var> <slice ' 'begin="1" end="2" /> </assign>') self.assertEquals(x.get_write(), '<var>foo</var>')
def issue(self): value = r_uint32() self.packet(0, 0, self.INIT, self.ISSUE, 0, 0) self.write(struct.pack('<I', value)) card_id = Variable('card' + str(self.state['counter'])) card_id.set_slice(0) self.state['counter'] += 1 auth_code = Variable('auth' + str(self.state['counter'])) auth_code.set_slice(0) self.state['counter'] += 1 tid = Variable('trans' + str(self.state['counter'])) tid.set_slice(0) self.state['counter'] += 1 self.read(length=4, assign=card_id) self.read(length=4, assign=auth_code) self.read(length=3, expect=struct.pack('<BBB', self.INIT, self.ISSUE, self.OK)) self.read(length=4, assign=tid) self.packet(card_id, auth_code, self.FIN, self.ISSUE, self.OK, tid) self.rpacket(self.FIN, self.ISSUE, self.OK) self.state['cards'][(card_id, auth_code)] = value