1#!/usr/bin/env python3 2# 3# Copyright 2019 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17from datetime import datetime, timedelta 18import logging 19from threading import Timer 20import time 21import traceback 22 23from mobly import asserts 24 25from acts import signals 26from acts.base_test import BaseTestClass 27 28from bluetooth_packets_python3 import hci_packets 29from bluetooth_packets_python3 import l2cap_packets 30from cert.event_stream import EventStream, FilteringEventStream 31from cert.truth import assertThat 32from cert.metadata import metadata 33from cert.behavior import when, wait_until 34from cert.behavior import IHasBehaviors 35from cert.behavior import anything 36from cert.behavior import SingleArgumentBehavior 37from cert.behavior import ReplyStage 38from cert.cert_self_test_lib import * 39 40 41class CertSelfTest(BaseTestClass): 42 43 def setup_test(self): 44 return True 45 46 def teardown_test(self): 47 return True 48 49 def test_assert_occurs_at_least_passes(self): 50 test_assert_occurs_at_least_passes_core() 51 52 def test_assert_occurs_passes(self): 53 test_assert_occurs_passes_core() 54 55 def test_assert_occurs_fails(self): 56 test_assert_occurs_fails_core() 57 58 def test_assert_occurs_at_most_passes(self): 59 test_assert_occurs_at_most_passes_core() 60 61 def test_assert_occurs_at_most_fails(self): 62 test_assert_occurs_at_most_fails_core() 63 64 def test_skip_a_test(self): 65 test_skip_a_test_core() 66 67 def test_nested_packets(self): 68 test_nested_packets_core() 69 70 def test_l2cap_config_options(self): 71 test_l2cap_config_options_core() 72 73 def test_assertThat_boolean_success(self): 74 test_assertThat_boolean_success_core() 75 76 def test_assertThat_boolean_falseIsTrue(self): 77 test_assertThat_boolean_falseIsTrue_core() 78 79 def test_assertThat_boolean_trueIsFalse(self): 80 test_assertThat_boolean_trueIsFalse_core() 81 82 def test_assertThat_object_success(self): 83 test_assertThat_object_success_core() 84 85 def test_assertThat_object_isEqualToFails(self): 86 test_assertThat_object_isEqualToFails_core() 87 88 def test_assertThat_object_isNotEqualToFails(self): 89 test_assertThat_object_isNotEqualToFails_core() 90 91 def test_assertThat_object_isNoneFails(self): 92 test_assertThat_object_isNoneFails_core() 93 94 def test_assertThat_object_isNotNoneFails(self): 95 test_assertThat_object_isNotNoneFails_core() 96 97 def test_assertThat_eventStream_emits_passes(self): 98 test_assertThat_eventStream_emits_passes_core() 99 100 def test_assertThat_eventStream_emits_then_passes(self): 101 test_assertThat_eventStream_emits_then_passes_core() 102 103 def test_assertThat_eventStream_emits_fails(self): 104 test_assertThat_eventStream_emits_fails_core() 105 106 def test_assertThat_eventStream_emits_then_fails(self): 107 test_assertThat_eventStream_emits_then_fails_core() 108 109 def test_assertThat_eventStream_emitsInOrder_passes(self): 110 test_assertThat_eventStream_emitsInOrder_passes_core() 111 112 def test_assertThat_eventStream_emitsInAnyOrder_passes(self): 113 test_assertThat_eventStream_emitsInAnyOrder_passes_core() 114 115 def test_assertThat_eventStream_emitsInOrder_fails(self): 116 test_assertThat_eventStream_emitsInOrder_fails_core() 117 118 def test_assertThat_eventStream_emitsInAnyOrder_fails(self): 119 test_assertThat_eventStream_emitsInAnyOrder_fails_core() 120 121 def test_assertThat_emitsNone_passes(self): 122 test_assertThat_emitsNone_passes_core() 123 124 def test_assertThat_emitsNone_passes_after_1_second(self): 125 test_assertThat_emitsNone_passes_after_1_second_core() 126 127 def test_assertThat_emitsNone_fails(self): 128 test_assertThat_emitsNone_fails_core() 129 130 def test_assertThat_emitsNone_zero_passes(self): 131 test_assertThat_emitsNone_zero_passes_core() 132 133 def test_assertThat_emitsNone_zero_passes_after_one_second(self): 134 test_assertThat_emitsNone_zero_passes_after_one_second_core() 135 136 def test_assertThat_emitsNone_zero_fails(self): 137 test_assertThat_emitsNone_zero_fails_core() 138 139 def test_filtering_event_stream_none_filter_function(self): 140 test_filtering_event_stream_none_filter_function_core() 141 142 def test_metadata_empty(self): 143 144 @metadata() 145 def simple_pass_test(arg): 146 pass 147 148 try: 149 simple_pass_test(1) 150 except signals.TestFailure: 151 pass 152 except Exception as e: 153 asserts.fail("@metadata() should only raise signals.TestFailure, " 154 "but raised %s with msg %s instead" % (e.__class__.__name__, str(e))) 155 else: 156 asserts.fail("@metadata() should not work") 157 158 def test_metadata_empty_no_function_call(self): 159 160 @metadata 161 def simple_pass_test(arg): 162 pass 163 164 try: 165 simple_pass_test(1) 166 except signals.TestFailure: 167 pass 168 except Exception as e: 169 asserts.fail("@metadata should only raise signals.TestFailure, " 170 "but raised %s with msg %s instead" % (e.__class__.__name__, str(e))) 171 else: 172 asserts.fail("@metadata should not work") 173 174 def test_metadata_pts_missing_id(self): 175 176 @metadata(pts_test_name="Hello world") 177 def simple_pass_test(arg): 178 pass 179 180 try: 181 simple_pass_test(1) 182 except signals.TestFailure: 183 pass 184 except Exception as e: 185 asserts.fail("should only raise signals.TestFailure, " 186 "but raised %s with msg %s instead" % (e.__class__.__name__, str(e))) 187 else: 188 asserts.fail("missing pts_test_id should not work") 189 190 def test_metadata_pts_missing_name(self): 191 192 @metadata(pts_test_id="A/B/C") 193 def simple_pass_test(arg): 194 pass 195 196 try: 197 simple_pass_test(1) 198 except signals.TestFailure: 199 pass 200 except Exception as e: 201 asserts.fail("should only raise signals.TestFailure, " 202 "but raised %s with msg %s instead" % (e.__class__.__name__, str(e))) 203 else: 204 asserts.fail("missing pts_test_name should not work") 205 206 def test_metadata_pts_test_id_and_description(self): 207 208 @metadata(pts_test_id="A/B/C", pts_test_name="Hello world") 209 def simple_pass_test(arg): 210 pass 211 212 try: 213 simple_pass_test(1) 214 except signals.TestPass as e: 215 asserts.assert_true("pts_test_id" in e.extras, msg=("pts_test_id not in extra: %s" % str(e.extras))) 216 asserts.assert_equal(e.extras["pts_test_id"], "A/B/C") 217 asserts.assert_true("pts_test_name" in e.extras, msg=("pts_test_name not in extra: %s" % str(e.extras))) 218 asserts.assert_equal(e.extras["pts_test_name"], "Hello world") 219 else: 220 asserts.fail("Must throw an exception using @metadata decorator") 221 222 def test_metadata_test_with_exception_stacktrace(self): 223 224 @metadata(pts_test_id="A/B/C", pts_test_name="Hello world") 225 def simple_fail_test(failure_argument): 226 raise ValueError(failure_argument) 227 228 try: 229 simple_fail_test("BEEFBEEF") 230 except signals.TestError as e: 231 asserts.assert_true("pts_test_id" in e.extras, msg=("pts_test_id not in extra: %s" % str(e.extras))) 232 asserts.assert_equal(e.extras["pts_test_id"], "A/B/C") 233 asserts.assert_true("pts_test_name" in e.extras, msg=("pts_test_name not in extra: %s" % str(e.extras))) 234 asserts.assert_equal(e.extras["pts_test_name"], "Hello world") 235 trace_str = traceback.format_exc() 236 asserts.assert_true( 237 "raise ValueError(failure_argument)" in trace_str, 238 msg="Failed test method not in error stack trace: %s" % trace_str) 239 else: 240 asserts.fail("Must throw an exception using @metadata decorator") 241 242 def test_fluent_behavior_simple(self): 243 test_fluent_behavior_simple_core() 244 245 def test_fluent_behavior__then_single__captures_one(self): 246 test_fluent_behavior__then_single__captures_one_core() 247 248 def test_fluent_behavior__then_times__captures_all(self): 249 test_fluent_behavior__then_times__captures_all_core() 250 251 def test_fluent_behavior__always__captures_all(self): 252 test_fluent_behavior__always__captures_all_core() 253 254 def test_fluent_behavior__matcher__captures_relevant(self): 255 test_fluent_behavior__matcher__captures_relevant_core() 256 257 def test_fluent_behavior__then_repeated__captures_relevant(self): 258 test_fluent_behavior__then_repeated__captures_relevant_core() 259 260 def test_fluent_behavior__fallback__captures_relevant(self): 261 test_fluent_behavior__fallback__captures_relevant_core() 262 263 def test_fluent_behavior__default_unhandled_crash(self): 264 test_fluent_behavior__default_unhandled_crash_core() 265 266 def test_fluent_behavior__set_default_works(self): 267 test_fluent_behavior__set_default_works_core() 268 269 def test_fluent_behavior__wait_until_done(self): 270 test_fluent_behavior__wait_until_done_core() 271 272 def test_fluent_behavior__wait_until_done_different_lambda(self): 273 test_fluent_behavior__wait_until_done_different_lambda_core() 274 275 def test_fluent_behavior__wait_until_done_anything(self): 276 test_fluent_behavior__wait_until_done_anything_core() 277 278 def test_fluent_behavior__wait_until_done_not_happened(self): 279 test_fluent_behavior__wait_until_done_not_happened_core() 280 281 def test_fluent_behavior__wait_until_done_with_default(self): 282 test_fluent_behavior__wait_until_done_with_default_core() 283 284 def test_fluent_behavior__wait_until_done_two_events_AA(self): 285 test_fluent_behavior__wait_until_done_two_events_AA_core() 286 287 def test_fluent_behavior__wait_until_done_two_events_AB(self): 288 test_fluent_behavior__wait_until_done_two_events_AB_core() 289 290 def test_fluent_behavior__wait_until_done_only_one_event_is_done(self): 291 test_fluent_behavior__wait_until_done_only_one_event_is_done_core() 292