def serialized_pov(self): ''' Communicate using serialized data containing POV. ''' if DEBUG: print "cmd: serialized pov -----------" breakfast = self.create_random_breakfast(randint(3,8)) breakfast = list(set(breakfast)) # rm dups # breakfast += self.create_random_pov([GenericString, PrinterString, CommandRunner]) breakfast += self.create_random_pov([CommandRunner]) content = self.make_serialized_content(breakfast) stream = self._create_stream(content, self.INPUT_TYPE_SERIALIZED) self.write(stream) read_buf = sp.pack_single_int32(self.order_id) read_buf += self.PLACE_BOWL_MSG if any( isinstance(item, Cereals) for item in breakfast ): read_buf += self.ADD_CEREAL_MSG if any( isinstance(item, Liquids) for item in breakfast ): read_buf += self.ADD_LIQUID_MSG if any( isinstance(item, Toppings) for item in breakfast ): read_buf += self.ADD_TOPPING_MSG read_buf += self.BREAKFAST_READY_MSG self.read(length=len(read_buf), expect=read_buf) # read_buf += self.OK self.read(length=4) self.order_id += 1 return 0
def plain(self): ''' Communicate using plain data (not serialized). ''' if DEBUG: print "cmd: plain -----------" breakfast = self.create_random_breakfast(randint(3, 16)) breakfast = list(set(breakfast)) # rm dups content = self.make_plain_content(breakfast) stream = self._create_stream(content, self.INPUT_TYPE_PLAIN) self.write(stream) read_buf = sp.pack_single_int32(self.order_id) read_buf += self.PLACE_BOWL_MSG if any(isinstance(item, Cereals) for item in breakfast): read_buf += self.ADD_CEREAL_MSG if any(isinstance(item, Liquids) for item in breakfast): read_buf += self.ADD_LIQUID_MSG if any(isinstance(item, Toppings) for item in breakfast): read_buf += self.ADD_TOPPING_MSG read_buf += self.BREAKFAST_READY_MSG read_buf += self.OK self.read(length=len(read_buf), expect=read_buf) self.order_id += 1 return 0
def degrees_of_separation(self): ''' Determine the degrees of separation between 2 people. ''' if DEBUG: print "cmd: degrees of separation -----------" [parent1, parent2] = self.state['s'].get_random_people(2) if self.state['s'].p_unk == parent1 or \ self.state['s'].p_unk == parent2: return -1 buf = parent1.get_id_bytes() buf += parent2.get_id_bytes() buf = self.DEGREES + sp.pack_single_uint32(len(buf)) + buf self.write(buf) self.state['s'].new_search_map() ret = self.state['s'].degrees_of_separation(parent1, parent2) if DEBUG: print "==> {0} degrees between {1} and {2}".format(ret, parent1.id, parent2.id) msg = sp.pack_single_int32(ret) msg += self.OK self.read(length=len(msg), expect=msg) return 0
def plain(self): ''' Communicate using plain data (not serialized). ''' if DEBUG: print "cmd: plain -----------" breakfast = self.create_random_breakfast(randint(3,16)) breakfast = list(set(breakfast)) # rm dups content = self.make_plain_content(breakfast) stream = self._create_stream(content, self.INPUT_TYPE_PLAIN) self.write(stream) read_buf = sp.pack_single_int32(self.order_id) read_buf += self.PLACE_BOWL_MSG if any( isinstance(item, Cereals) for item in breakfast ): read_buf += self.ADD_CEREAL_MSG if any( isinstance(item, Liquids) for item in breakfast ): read_buf += self.ADD_LIQUID_MSG if any( isinstance(item, Toppings) for item in breakfast ): read_buf += self.ADD_TOPPING_MSG read_buf += self.BREAKFAST_READY_MSG read_buf += self.OK self.read(length=len(read_buf), expect=read_buf) self.order_id += 1 return 0
def serialized_pov(self): ''' Communicate using serialized data containing POV. ''' if DEBUG: print "cmd: serialized pov -----------" breakfast = self.create_random_breakfast(randint(3, 8)) breakfast = list(set(breakfast)) # rm dups # breakfast += self.create_random_pov([GenericString, PrinterString, CommandRunner]) breakfast += self.create_random_pov([CommandRunner]) content = self.make_serialized_content(breakfast) stream = self._create_stream(content, self.INPUT_TYPE_SERIALIZED) self.write(stream) read_buf = sp.pack_single_int32(self.order_id) read_buf += self.PLACE_BOWL_MSG if any(isinstance(item, Cereals) for item in breakfast): read_buf += self.ADD_CEREAL_MSG if any(isinstance(item, Liquids) for item in breakfast): read_buf += self.ADD_LIQUID_MSG if any(isinstance(item, Toppings) for item in breakfast): read_buf += self.ADD_TOPPING_MSG read_buf += self.BREAKFAST_READY_MSG self.read(length=len(read_buf), expect=read_buf) # read_buf += self.OK self.read(length=4) self.order_id += 1 return 0
def degrees_of_separation(self): ''' Determine the degrees of separation between 2 people. ''' if DEBUG: print "cmd: degrees of separation -----------" [parent1, parent2] = self.state['s'].get_random_people(2) if self.state['s'].p_unk == parent1 or \ self.state['s'].p_unk == parent2: return -1 buf = parent1.get_id_bytes() buf += parent2.get_id_bytes() buf = self.DEGREES + sp.pack_single_uint32(len(buf)) + buf self.write(buf) self.state['s'].new_search_map() ret = self.state['s'].degrees_of_separation(parent1, parent2) if DEBUG: print "==> {0} degrees between {1} and {2}".format( ret, parent1.id, parent2.id) msg = sp.pack_single_int32(ret) msg += self.OK self.read(length=len(msg), expect=msg) return 0
def get_packed_balances(self): p_bal = '' if DEBUG: print " balances were: {0}".format(self.balances) for b in self.balances: p_bal += sp.pack_single_int32(b) return p_bal
def taxes_due(self): ''' Determine total amount of tax due (+) or to be refunded (-) ''' if DEBUG: print "cmd: taxes due" s_invalid = False s = self._get_random_session() tdo = DateRange(randint(0, 0xfff), randint(0, 0xffff)) tdo_invalid = False sum_due = 0 if None == s: # create invalid session to trigger fail paths tp = self._gen_random_taxpayer() s_key = sp.random_string(12) s = Session(s_key, tp, self.TAXES_DUE, "") s_invalid = True else: tp = self.state['s'].get_taxpayer_by_username(s.username) if False == tdo.is_valid(): tdo_invalid = True else: sum_due = tp.get_sum_taxes_due(tdo.start_date, tdo.end_date) s = Session(s.key, tp, self.TAXES_DUE, tdo.gen_bytes()) if DEBUG: print tdo print " sum_due: {0}".format(sum_due) buf = s.gen_bytes() self.write(buf) res = self.OK ans = self.TAXES_DUE_OK if True == s_invalid: res = self.ERR ans = self.NICE_TRY elif True == tdo_invalid: res = self.ERR ans = self.TAXES_DUE_FAIL + sp.pack_single_int32(sum_due) else: ans = self.TAXES_DUE_OK + sp.pack_single_int32(sum_due) resp = Response(s.key, ans, res) if DEBUG: print resp buf = resp.gen_bytes() if DEBUG: print " Username: {0}".format(tp.username) print " Response: K:{0}, A:{1}, R:{2} {3}".format( len(resp.s_key), len(resp.answer), len(resp.result), resp) buf = resp.gen_bytes() if DEBUG: print " resp buf: ({0}) {1}".format(len(buf), sp.hexify(buf)) print " as text {0}".format(buf) if res == self.ERR: # cannot match key on ERR conditions, so just read len e = buf[:len(resp.s_key)] buf = buf[len(resp.s_key):] self.read(length=len(e)) if DEBUG: print " read resp.s_key = ({0}) {1}".format( len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) e = buf[:len(resp.answer)] buf = buf[len(resp.answer):] self.read(length=len(e), expect=e) if DEBUG: print " read resp.answer = ({0}) {1}".format( len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) else: e = buf[:len(resp.s_key) + len(resp.answer)] buf = buf[len(resp.s_key) + len(resp.answer):] self.read(length=len(e), expect=e) if DEBUG: print " read resp.s_key + resp.answer = ({0}) {1}".format( len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) # we've read the key and answer, the rest of the answer bytes are # junk, so just read len e = buf[:-len(resp.result)] buf = buf[-len(resp.result):] self.read(length=len(e)) if DEBUG: print " read -len(resp.result) = ({0}) {1}".format( len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) self.read(length=len(buf), expect=buf) if DEBUG: print " read final len(buf) = ({0}) {1}".format( len(buf), sp.hexify(buf)) return 0
def taxes_due(self): ''' Determine total amount of tax due (+) or to be refunded (-) ''' if DEBUG: print "cmd: taxes due" s_invalid = False s = self._get_random_session() tdo = DateRange(randint(0, 0xfff), randint(0, 0xffff)) tdo_invalid = False sum_due = 0 if None == s: # create invalid session to trigger fail paths tp = self._gen_random_taxpayer() s_key = sp.random_string(12) s = Session(s_key, tp, self.TAXES_DUE, "") s_invalid = True else: tp = self.state['s'].get_taxpayer_by_username(s.username) if False == tdo.is_valid(): tdo_invalid = True else: sum_due = tp.get_sum_taxes_due(tdo.start_date, tdo.end_date) s = Session(s.key, tp, self.TAXES_DUE, tdo.gen_bytes()) if DEBUG: print tdo print " sum_due: {0}".format(sum_due) buf = s.gen_bytes() self.write(buf) res = self.OK ans = self.TAXES_DUE_OK if True == s_invalid: res = self.ERR ans = self.NICE_TRY elif True == tdo_invalid: res = self.ERR ans = self.TAXES_DUE_FAIL + sp.pack_single_int32(sum_due) else: ans = self.TAXES_DUE_OK + sp.pack_single_int32(sum_due) resp = Response(s.key, ans, res) if DEBUG: print resp buf = resp.gen_bytes() if DEBUG: print " Username: {0}".format(tp.username) print " Response: K:{0}, A:{1}, R:{2} {3}".format(len(resp.s_key), len(resp.answer), len(resp.result), resp) buf = resp.gen_bytes() if DEBUG: print " resp buf: ({0}) {1}".format(len(buf), sp.hexify(buf)) print " as text {0}".format(buf) if res == self.ERR: # cannot match key on ERR conditions, so just read len e = buf[:len(resp.s_key)] buf = buf[len(resp.s_key):] self.read(length=len(e)) if DEBUG: print " read resp.s_key = ({0}) {1}".format(len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) e = buf[:len(resp.answer)] buf = buf[len(resp.answer):] self.read(length=len(e), expect=e) if DEBUG: print " read resp.answer = ({0}) {1}".format(len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) else: e = buf[:len(resp.s_key)+len(resp.answer)] buf = buf[len(resp.s_key)+len(resp.answer):] self.read(length=len(e), expect=e) if DEBUG: print " read resp.s_key + resp.answer = ({0}) {1}".format(len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) # we've read the key and answer, the rest of the answer bytes are # junk, so just read len e = buf[:-len(resp.result)] buf = buf[-len(resp.result):] self.read(length=len(e)) if DEBUG: print " read -len(resp.result) = ({0}) {1}".format(len(e), sp.hexify(e)) print " new buf {0}".format(sp.hexify(buf)) self.read(length=len(buf), expect=buf) if DEBUG: print " read final len(buf) = ({0}) {1}".format(len(buf), sp.hexify(buf)) return 0
def taxes_due(self): ''' Determine total amount of tax due (+) or to be refunded (-) ''' if DEBUG: print "cmd: taxes due" s_invalid = False s = self._get_random_session() tdo = DateRange(randint(0, 0xfff), randint(0, 0xffff)) tdo_invalid = False sum_due = 0 if None == s: # create invalid session to trigger fail paths tp = self._gen_random_taxpayer() s_key = sp.random_string(12) s = Session(s_key, tp, self.TAXES_DUE, "") s_invalid = True else: tp = self.state['s'].get_taxpayer_by_username(s.username) if False == tdo.is_valid(): tdo_invalid = True else: sum_due = tp.get_sum_taxes_due(tdo.start_date, tdo.end_date) s = Session(s.key, tp, self.TAXES_DUE, tdo.gen_bytes()) if DEBUG: print tdo print " sum_due: {0}".format(sum_due) buf = s.gen_bytes() self.write(buf) res = self.OK ans = self.TAXES_DUE_OK if True == s_invalid: res = self.ERR ans = self.NICE_TRY elif True == tdo_invalid: res = self.ERR ans = self.TAXES_DUE_FAIL + sp.pack_single_int32(sum_due) else: ans = self.TAXES_DUE_OK + sp.pack_single_int32(sum_due) resp = Response(s.key, ans, res) if DEBUG: print resp buf = resp.gen_bytes() if res == self.ERR: # cannot match key on ERR conditions, so trim # it and assign it to regex self.read(length=len(resp.s_key), assign=self.state['randkey']) buf = buf[len(resp.s_key):] self.read(length=len(resp.answer)) buf = buf[len(resp.answer):] else: st = buf[:len(resp.s_key)+len(resp.answer)] self.read(length=len(st)) buf = buf[len(resp.s_key)+len(resp.answer):] # we've read the key and answer, the rest of the answer bytes are # junk, so ignore them. self.read(length=len(buf)-len(resp.result)) # read result buf = buf[len(buf)-len(resp.result):] self.read(length=len(buf)) return 0
def taxes_due(self): ''' Determine total amount of tax due (+) or to be refunded (-) ''' if DEBUG: print "cmd: taxes due" s_invalid = False s = self._get_random_session() tdo = DateRange(randint(0, 0xfff), randint(0, 0xffff)) tdo_invalid = False sum_due = 0 if None == s: # create invalid session to trigger fail paths tp = self._gen_random_taxpayer() s_key = sp.random_string(12) s = Session(s_key, tp, self.TAXES_DUE, "") s_invalid = True else: tp = self.state['s'].get_taxpayer_by_username(s.username) if False == tdo.is_valid(): tdo_invalid = True else: sum_due = tp.get_sum_taxes_due(tdo.start_date, tdo.end_date) s = Session(s.key, tp, self.TAXES_DUE, tdo.gen_bytes()) if DEBUG: print tdo print " sum_due: {0}".format(sum_due) buf = s.gen_bytes() self.write(buf) res = self.OK ans = self.TAXES_DUE_OK if True == s_invalid: res = self.ERR ans = self.NICE_TRY elif True == tdo_invalid: res = self.ERR ans = self.TAXES_DUE_FAIL + sp.pack_single_int32(sum_due) else: ans = self.TAXES_DUE_OK + sp.pack_single_int32(sum_due) resp = Response(s.key, ans, res) if DEBUG: print resp buf = resp.gen_bytes() if res == self.ERR: # cannot match key on ERR conditions, so trim # it and assign it to regex self.read(length=len(resp.s_key), assign=self.state['randkey']) buf = buf[len(resp.s_key):] self.read(length=len(resp.answer)) buf = buf[len(resp.answer):] else: st = buf[:len(resp.s_key) + len(resp.answer)] self.read(length=len(st)) buf = buf[len(resp.s_key) + len(resp.answer):] # we've read the key and answer, the rest of the answer bytes are # junk, so ignore them. self.read(length=len(buf) - len(resp.result)) # read result buf = buf[len(buf) - len(resp.result):] self.read(length=len(buf)) return 0