def verify_discovery_session_term(self, dut, disc_id, config, is_publish, term_ind_on): """Utility to verify that the specified discovery session has terminated (by waiting for the TTL and then attempting to reconfigure). Args: dut: device under test disc_id: discovery id for the existing session config: configuration of the existing session is_publish: True if the configuration was publish, False if subscribe term_ind_on: True if a termination indication is expected, False otherwise """ # Wait for session termination if term_ind_on: autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_SESSION_TERMINATED, disc_id)) else: # can't defer wait to end since in any case have to wait for session to # expire autils.fail_on_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_SESSION_TERMINATED, disc_id)) # Validate that session expired by trying to configure it (expect failure) config[aconsts.DISCOVERY_KEY_SSI] = "something else" if is_publish: dut.droid.wifiAwareUpdatePublish(disc_id, config) else: dut.droid.wifiAwareUpdateSubscribe(disc_id, config) # The response to update discovery session is: # term_ind_on=True: session was cleaned-up so won't get an explicit failure, but won't get a # success either. Can check for no SESSION_CB_ON_SESSION_CONFIG_UPDATED but # will defer to the end of the test (no events on queue). # term_ind_on=False: session was not cleaned-up (yet). So expect # SESSION_CB_ON_SESSION_CONFIG_FAILED. if not term_ind_on: autils.wait_for_event( dut, autils.decorate_event( aconsts.SESSION_CB_ON_SESSION_CONFIG_FAILED, disc_id))
def test_attach_multiple_sessions(self): """Functional test case / Attach test cases / multiple attach sessions Validates that when creating multiple attach sessions each can be configured independently as to whether or not to receive an identity callback. """ dut = self.android_devices[0] # Create 3 attach sessions: 2 without identity callback, 1 with id1 = dut.droid.wifiAwareAttach(False, None, True) time.sleep(10) # to make sure all calls and callbacks are done id2 = dut.droid.wifiAwareAttach(True, None, True) time.sleep(10) # to make sure all calls and callbacks are done id3 = dut.droid.wifiAwareAttach(False, None, True) dut.log.info('id1=%d, id2=%d, id3=%d', id1, id2, id3) # Attach session 1: wait for attach, should not get identity autils.wait_for_event( dut, autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED, id1)) autils.fail_on_event( dut, autils.decorate_event(aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id1)) # Attach session 2: wait for attach and for identity callback autils.wait_for_event( dut, autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED, id2)) autils.wait_for_event( dut, autils.decorate_event(aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id2)) # Attach session 3: wait for attach, should not get identity autils.wait_for_event( dut, autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED, id3)) autils.fail_on_event( dut, autils.decorate_event(aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id3))
def positive_ttl_test_utility(self, is_publish, ptype, stype, term_ind_on): """Utility which runs a positive discovery session TTL configuration test Iteration 1: Verify session started with TTL Iteration 2: Verify session started without TTL and reconfigured with TTL Iteration 3: Verify session started with (long) TTL and reconfigured with (short) TTL Args: is_publish: True if testing publish, False if testing subscribe ptype: Publish discovery type (used if is_publish is True) stype: Subscribe discovery type (used if is_publish is False) term_ind_on: Configuration of termination indication """ SHORT_TTL = 5 # 5 seconds LONG_TTL = 100 # 100 seconds dut = self.android_devices[0] # Attach and wait for confirmation id = dut.droid.wifiAwareAttach(False) autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED) # Iteration 1: Start discovery session with TTL config = self.create_base_config(dut.aware_capabilities, is_publish, ptype, stype, self.PAYLOAD_SIZE_TYPICAL, SHORT_TTL, term_ind_on, False) if is_publish: disc_id = dut.droid.wifiAwarePublish(id, config, True) autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED, disc_id)) else: disc_id = dut.droid.wifiAwareSubscribe(id, config, True) autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED, disc_id)) # Wait for session termination & verify self.verify_discovery_session_term(dut, disc_id, config, is_publish, term_ind_on) # Iteration 2: Start a discovery session without TTL config = self.create_base_config(dut.aware_capabilities, is_publish, ptype, stype, self.PAYLOAD_SIZE_TYPICAL, 0, term_ind_on, False) if is_publish: disc_id = dut.droid.wifiAwarePublish(id, config, True) autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED, disc_id)) else: disc_id = dut.droid.wifiAwareSubscribe(id, config, True) autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED, disc_id)) # Update with a TTL config = self.create_base_config(dut.aware_capabilities, is_publish, ptype, stype, self.PAYLOAD_SIZE_TYPICAL, SHORT_TTL, term_ind_on, False) if is_publish: dut.droid.wifiAwareUpdatePublish(disc_id, config) else: dut.droid.wifiAwareUpdateSubscribe(disc_id, config) autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_SESSION_CONFIG_UPDATED, disc_id)) # Wait for session termination & verify self.verify_discovery_session_term(dut, disc_id, config, is_publish, term_ind_on) # Iteration 3: Start a discovery session with (long) TTL config = self.create_base_config(dut.aware_capabilities, is_publish, ptype, stype, self.PAYLOAD_SIZE_TYPICAL, LONG_TTL, term_ind_on, False) if is_publish: disc_id = dut.droid.wifiAwarePublish(id, config, True) autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_PUBLISH_STARTED, disc_id)) else: disc_id = dut.droid.wifiAwareSubscribe(id, config, True) autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED, disc_id)) # Update with a TTL config = self.create_base_config(dut.aware_capabilities, is_publish, ptype, stype, self.PAYLOAD_SIZE_TYPICAL, SHORT_TTL, term_ind_on, False) if is_publish: dut.droid.wifiAwareUpdatePublish(disc_id, config) else: dut.droid.wifiAwareUpdateSubscribe(disc_id, config) autils.wait_for_event( dut, autils.decorate_event(aconsts.SESSION_CB_ON_SESSION_CONFIG_UPDATED, disc_id)) # Wait for session termination & verify self.verify_discovery_session_term(dut, disc_id, config, is_publish, term_ind_on) # verify that there were no other events autils.verify_no_more_events(dut) # verify that forbidden callbacks aren't called if not term_ind_on: autils.validate_forbidden_callbacks( dut, { aconsts.CB_EV_PUBLISH_TERMINATED: 0, aconsts.CB_EV_SUBSCRIBE_TERMINATED: 0 })
def prep_message_exchange(self, extra_diff=None): """Creates a discovery session (publish and subscribe), and waits for service discovery - at that point the sessions are ready for message exchange. Args: extra_diff: String to add to service name: allows differentiating discovery sessions. """ p_dut = self.android_devices[0] p_dut.pretty_name = "Publisher" s_dut = self.android_devices[1] s_dut.pretty_name = "Subscriber" # if differentiating (multiple) sessions then should decorate events with id use_id = extra_diff is not None # Publisher+Subscriber: attach and wait for confirmation p_id = p_dut.droid.wifiAwareAttach(False, None, use_id) autils.wait_for_event( p_dut, aconsts.EVENT_CB_ON_ATTACHED if not use_id else autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED, p_id)) time.sleep(self.device_startup_offset) s_id = s_dut.droid.wifiAwareAttach(False, None, use_id) autils.wait_for_event( s_dut, aconsts.EVENT_CB_ON_ATTACHED if not use_id else autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED, s_id)) # Publisher: start publish and wait for confirmation p_disc_id = p_dut.droid.wifiAwarePublish( p_id, self.create_config(True, extra_diff=extra_diff), use_id) autils.wait_for_event( p_dut, aconsts.SESSION_CB_ON_PUBLISH_STARTED if not use_id else autils.decorate_event( aconsts.SESSION_CB_ON_PUBLISH_STARTED, p_disc_id)) # Subscriber: start subscribe and wait for confirmation s_disc_id = s_dut.droid.wifiAwareSubscribe( s_id, self.create_config(False, extra_diff=extra_diff), use_id) autils.wait_for_event( s_dut, aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED if not use_id else autils.decorate_event( aconsts.SESSION_CB_ON_SUBSCRIBE_STARTED, s_disc_id)) # Subscriber: wait for service discovery discovery_event = autils.wait_for_event( s_dut, aconsts.SESSION_CB_ON_SERVICE_DISCOVERED if not use_id else autils.decorate_event( aconsts.SESSION_CB_ON_SERVICE_DISCOVERED, s_disc_id)) peer_id_on_sub = discovery_event["data"][ aconsts.SESSION_CB_KEY_PEER_ID] return { "p_dut": p_dut, "s_dut": s_dut, "p_id": p_id, "s_id": s_id, "p_disc_id": p_disc_id, "s_disc_id": s_disc_id, "peer_id_on_sub": peer_id_on_sub }
def wait_for_messages(self, tx_msgs, tx_msg_ids, tx_disc_id, rx_disc_id, tx_dut, rx_dut, are_msgs_empty=False): """Validate that all expected messages are transmitted correctly and received as expected. Method is called after the messages are sent into the transmission queue. Note: that message can be transmitted and received out-of-order (which is acceptable and the method handles that correctly). Args: tx_msgs: dictionary of transmitted messages tx_msg_ids: dictionary of transmitted message ids tx_disc_id: transmitter discovery session id (None for no decoration) rx_disc_id: receiver discovery session id (None for no decoration) tx_dut: transmitter device rx_dut: receiver device are_msgs_empty: True if the messages are None or empty (changes dup detection) Returns: the peer ID from any of the received messages """ # peer id on receiver peer_id_on_rx = None # wait for all messages to be transmitted still_to_be_tx = len(tx_msg_ids) while still_to_be_tx != 0: tx_event = autils.wait_for_event( tx_dut, aconsts.SESSION_CB_ON_MESSAGE_SENT if tx_disc_id is None else autils.decorate_event( aconsts.SESSION_CB_ON_MESSAGE_SENT, tx_disc_id)) tx_msg_id = tx_event["data"][aconsts.SESSION_CB_KEY_MESSAGE_ID] tx_msg_ids[tx_msg_id] = tx_msg_ids[tx_msg_id] + 1 if tx_msg_ids[tx_msg_id] == 1: still_to_be_tx = still_to_be_tx - 1 # check for any duplicate transmit notifications asserts.assert_equal(len(tx_msg_ids), sum(tx_msg_ids.values()), "Duplicate transmit message IDs: %s" % tx_msg_ids) # wait for all messages to be received still_to_be_rx = len(tx_msg_ids) while still_to_be_rx != 0: rx_event = autils.wait_for_event( rx_dut, aconsts.SESSION_CB_ON_MESSAGE_RECEIVED if rx_disc_id is None else autils.decorate_event( aconsts.SESSION_CB_ON_MESSAGE_RECEIVED, rx_disc_id)) peer_id_on_rx = rx_event["data"][aconsts.SESSION_CB_KEY_PEER_ID] if are_msgs_empty: still_to_be_rx = still_to_be_rx - 1 else: rx_msg = rx_event["data"][ aconsts.SESSION_CB_KEY_MESSAGE_AS_STRING] asserts.assert_true( rx_msg in tx_msgs, "Received a message we did not send!? -- '%s'" % rx_msg) tx_msgs[rx_msg] = tx_msgs[rx_msg] + 1 if tx_msgs[rx_msg] == 1: still_to_be_rx = still_to_be_rx - 1 # check for any duplicate received messages if not are_msgs_empty: asserts.assert_equal(len(tx_msgs), sum(tx_msgs.values()), "Duplicate transmit messages: %s" % tx_msgs) return peer_id_on_rx