Commit graph android_packages_modules_Co.../staticlibs/testutils/hostdevice/com/android
Message SHA1
Fix typo
Still not sure whether the log message is 100% correct. Technically, it
means no callback was received between now and the last successful poll
operation that incremented the track record position.

Test: TH
Flag: EXEMPT trivial
Change-Id: I57e9600321cb4a79d035d64d488af09b17a50706

Patrick Rohr   commited 2025-06-24 08:33:51 -07:00
a36b89c5b0
Migrate CSAgentWrapper to TestableCallback.
This makes it much more powerful as well as consistent with
other use cases of this paradigm.

Test: TH
Flag: EXEMPT test-only
Change-Id: I4f5b86bfdd16d8bfe21b79235444817a13bc3c49

Chalard Jean   authored and   Jean Chalard   commited 2025-06-20 01:08:25 -07:00
6fe5d13b3a
Have TestableNetworkAgent implement Expectable
This will make these agents testing a lot easier.

Also fix a small bug where waiting for callbacks that
are not expected would take a lot longer than it should.

Also trivial improvements to logging that I needed to stabilize
this.

Test: TH
Flag: EXEMPT test-only
Change-Id: I654d63eb226990ad1c5087cade7cf0a82d8526a1

Chalard Jean   commited 2025-06-16 17:28:57 +09:00
7bff7b2455
Make a base class for testable callbacks
Have TestableNetworkCallback adopt it, as a first client. It's also by
far the most complex.

Test: TH
Flag: EXEMPT test-only
Change-Id: Ie58d051b4478c901b7f0f54e89a67d22f1ee2ce2

Chalard Jean   commited 2025-06-10 17:22:01 +09:00
2db96977db
Revert "Make a base class for testable callbacks"
This reverts commit a77d5ac126.

Reason for revert: Droidmonitor created revert due to b/422782308. Will be verifying through ABTD before submission.

Fix: 422782308
Change-Id: I00825e0f17a9a9e0d152ac8ebe767da3f267fbb1

Priyanka Advani (xWF)   authored and   Android (Google) Code Review   commited 2025-06-05 11:35:49 -07:00
827aa58224
Make a base class for testable callbacks
Have TestableNetworkCallback adopt it, as a first client. It's also by
far the most complex.

Test: TH
Flag: EXEMPT test-only
Change-Id: Ifd636a09d0695f04b17330c4987f2b7b742508fa

Chalard Jean   commited 2025-06-05 16:13:34 +09:00
a77d5ac126
Fix a bug in poll(from)
Test: new test in this patch
Flag: EXEMPT test-only
Change-Id: I00d367ce9db47e3fd30d27816b68280ab418f988

Chalard Jean   commited 2025-05-19 16:35:54 +09:00
f0252b1cf4
Move failWithErrorReason to a common place
This is a shared public utility. Its main utility is for methods
that can fail, have an error message of their own, and also want
to take an error message from a caller and want to combine them.
Example useĀ :
fun <T> assertEquals(expected: T, actual: T, msg: String? = null) {
  if (expected != null) {
    failWithErrorReason(msg, "Expected $expected, got $actual")
  }
}
...which allows someone to say
assertEquals(a, b, "Checking a vs b")

Test: TH
Flag: EXEMPT test-only change
Change-Id: Id3eaa8384b4177f38f13576d9b4d742c52027ae4

Chalard Jean   commited 2025-04-27 15:54:35 +09:00
c96d1ae0e6
Add a way to poll with a position for ReadHead
It was thought originally that it would not make a lot of sense to
offer such a call, but with usage it became clear that there is a
simple, obvious and useful behavior.
In particular this is very useful to mix eventuallyExpect and
expect calls. Interestingly, before this patch, calling
callback.eventuallyExpect<...>()
callback.expect<...>()
does not actually behave like you would intuitively think, because
eventuallyExpect does not advance the read head. When this
utility was made we thought this was the better behavior, but
usage has shown that advancing is both obvious and more useful.

Astonishingly, there seem to be no caller who needs to
eventuallyExpect() and later expect(), meaning that this
behavior change is apparently fine.

Test: ConnectivityCoverageTest and treehugger
Flag: EXEMPT test-only change
Change-Id: I2078330bd913cf0f75240bf3d4852e487e3e0163

Chalard Jean   commited 2025-04-27 15:54:33 +09:00
a14fb7cf79
Expose backtrace() in PollPacketReader
Figuring out what packets were received is useful for debugging.

Test: TH
Flag: EXEMPT mainline
Bug: 1000001
Change-Id: I3184845b76e33da186f39a2344548c2d16715173

Patrick Rohr   commited 2025-04-11 15:59:37 -07:00
f39b748cbf
Merge "Only report tryTest errors when not caught" into main
Treehugger Robot   authored and   Gerrit Code Review   commited 2024-12-02 01:48:40 +00:00
739a6c4320
Only report tryTest errors when not caught
Instead of reporting tryTest errors when the tryTest block throws,
report them in the cleanup block if no catch block has caught it.

This fixes a bug where tests that use tryTest/catch would collect
diagnostics in expected cases.

Test: atest
Change-Id: Ic53ae54f1945a2784becd50e5890fad8e7461474

Remi NGUYEN VAN   commited 2024-12-02 09:26:53 +09:00
fc724ab59a
Fix assertEventuallyTrue timeout
A nanosecond is 1E6 milliseconds, not 1000.

Test: atest
Change-Id: Iaa5fc15b4dc5a735d1a7bdbf7642c1376d2f41d9

Remi NGUYEN VAN   commited 2024-11-29 16:28:16 +09:00
2229796976
Fix flakes due to cell reconnecting on wifi toggle
On some networks, telephony may teardown its network
(unregisterAfterReplacement) when wifi is toggled to change the MMS
capability.

Update testRejectUnvalidated_TearDownNetwork to take that into account:
right after toggling wifi to make it unvalidated, wifi may still be
default if mobile data is being reconnected. Wait for mobile data to
reconnect and validate in that case.

Bug: 296403855
Test: atest
Change-Id: Ief46bed41ecb23d75c9a1f58e7a272e7f3de7caf

Remi NGUYEN VAN   commited 2024-10-24 11:22:56 +09:00
c19e8a00cc
Add connectivity diagnostics on test failure
When CtsNetTestCases tests fail, or when a tryTest statement fails,
collect connectivity diagnostics and output them as a test artifact.

tryTest must be followed by cleanup blocks, which often change the
configuration, so having diagnostics when the tryTest fails in addition
to after the test failure is useful.

The collected diagnostics will be available as individual test
artifacts. They should make it easier to investigate test failures, and
could be collected across test runs in an automated way.

Output files are currently limited at 20 per run, to avoid collecting
too much data in case everything is failing in the run.

Test: atest
Change-Id: Iaa48e519028dc61c03d387beeade7d41fca2ef48

Remi NGUYEN VAN   commited 2024-10-16 10:05:35 +09:00
43730b3f0f
Quit NetworkMonitor threads in integration tests
Similarly to NetworkMonitorTest, terminate NetworkMonitor threads and
ExecutorServices after each test case in integration tests.

This should fix flakes where the HTTP probe from testValidation could
finish after mock probe responses are cleared in tearDown.

The utils used in NetworkMonitorTest are factored out to ConcurrentUtils
to achieve that.

Fixes: 292481238
Test: atest --rerun-until-failure 50
Change-Id: I5cdeee8d4f06ea40e338dd2cb1fdcc2fe0907a7d

Remi NGUYEN VAN   commited 2024-03-14 13:18:44 +09:00
b3fcdacfae
Fix replying to queries via unicast
Instead of (wrongly) looking at the DNS message flags for the Query
Unicast bit, look at the flag in the question record.

The previous bug means that all queries would be replied multicast
instead of sometimes unicast. The fix is flagged off as this is a
behavior change that may affect performance and latency.

Bug: 289482497
Test: atest
Change-Id: I08e30c4ffa747c9073d631e8addca1278ea40648

Remi NGUYEN VAN   commited 2024-01-15 20:26:28 +09:00
0ca094b577
TrackRecord: add a method to return callbacks since last poll
This change adds a backtrace() method which returns a list of events
that were received since the last time a user called poll() on a
ReadHead. This is particularly useful for logging observed events while
a poll was in progress that did not end up matching an event.

Test: atest EthernetManagerTest
Change-Id: If019cf9eb5e3e9268c5e6b74edbd8f49959cc71c

Patrick Rohr   commited 2023-08-09 05:26:36 -07:00
533af6d72d
Merge "Add an annotation for @SkipMainlinePresubmit" into main
Remi NGUYEN VAN   authored and   Gerrit Code Review   commited 2023-07-27 02:13:22 +00:00
7035b4418d
Add an annotation for @SkipMainlinePresubmit
This will typically be used in mainline-presubmit TEST_MAPPING
configuration to exclude some tests.

This is necessary as some tests are flaky in mainline configurations
(when running on older devices), and they need to be disabled to meet
SLO requirements, but they are not flaky on presubmit using the latest
platform builds.

Test: m
Change-Id: Ia532d6d3f9833ddec613d79c0eb517d20a1c529a

Remi NGUYEN VAN   commited 2023-07-26 13:37:47 +09:00
5e4900dbbe
Add DnsResolverModuleTest annotation
This annotation can be used to mark a test case that requires
the latest resolv module. Tests that don't run with the latest
resolv module (for example CtsNetTestCasesLatestSdk) can exclude
the testcases being flaky due to some known issues in old resolv
module.

Bug: 279846955
Test: TreeHugger
Change-Id: Ie19eed1c4aa17645c4eec45493e7999027a01205

Mike Yu   commited 2023-06-26 07:40:18 +00:00
8f4debc3f9
Add NetworkStackModuleTest annotation.
This annotation can be used to exclude the testcases which requires the
latest NetworkStack module in the CTS/MTS suite.

Bug: 283200648
Test: atest CtsTetheringTestLatestSdk
Change-Id: Idaffed93af077c2998081142af7b3bfa311dcd90

Xiao Ma   commited 2023-06-01 13:34:01 +09:00
cff263aa10
Add packet filters for IPv6 UDP
This is useful to test mDNS.

Bug: 266151066
Test: atest NsdManagerTest (with related change)
Change-Id: I790da3f3be5277f2480600cfbbaeac86c306f77d

Remi NGUYEN VAN   commited 2023-01-23 18:20:44 +09:00
bcdc972c86
Add Function3
...and rename ExceptionUtils to FunctionalUtils as the old name
would no longer be appropriate.

Test: FrameworksNetTests
Change-Id: I2affd69fb84d7f250b4a45497eec6c052bf6ec50

Chalard Jean   commited 2022-08-16 18:53:17 +09:00
a19f85124e
Fix handling of Throwable in tryTest/catch
Throwable could be used as a type argument to catch(), but it would
possibly never be caught, because the method only catches Exception.

Notably, junit assertions fail with AssertionError, which is not a
subclass of Exception. Due to that, tests or utilities like
DeviceConfigRule that have failing assertions in catch() would not
run the subsequent cleanup steps.

Bug: 210377950
Test: atest CleanupTest
Change-Id: I54e2922cb466f077ba4d219f8c3c6f885316296c

Remi NGUYEN VAN   commited 2022-06-21 12:52:10 +09:00
a79814aade
Support multiple cleanup blocks in tryTest{}
This is useful for some tests that need to make sure a few
things all happen, but some of them may throw an exception.

In vanilla Java you'd say

try {
  ...
} finally {
  try {
    ...
  } finally {
    try {
      ...
    } finally {
      ...
    }
  }
}

With this patch, you can pass a list of blocks to clean.
Kotlin :

tryTest {
  ...
} cleanupStep {
  ...
} cleanupStep {
  ...
} cleanup {
  ...
}

Java :
tryAndCleanup(() -> {
  ...
  }, () -> {
  ...
  }, () -> {
  ...
  })

This keeps the semantics of tryTest{} of throwing any
exception that was thrown in tryTest{} and adding the
exception in the cleanup steps as suppressed.

Test: new tests for this
Change-Id: Ie26b802cb4893e13a70646aa0b7887701dee1ec6

Chalard Jean   commited 2021-12-06 17:57:29 +09:00
997300b9d1
Correct a comment
Test: TH
Change-Id: I08ac8f0f40b9e3732564da73057cd3d22c06c79b

Chalard Jean   commited 2021-11-25 19:55:59 +09:00
fee2cab5d6
Have tryTest{} work with throwables
Test: NetworkStaticLibTests
Change-Id: I9ee52e7d34866c82e169bbecbca3f928bfd79993

Chalard Jean   commited 2021-11-25 16:26:42 +09:00
28348e43ae
Rename ExceptionCleanupBlock into TryExpr
This is a lot more inline with standard compiling theory

Test: NetworkStaticLibTests
Change-Id: I32c8926cb08a30b4a87bc6867ce194483a7f0c3a

Chalard Jean   commited 2021-11-25 16:26:42 +09:00
2918ada958
Add catch{} to tryTest{}
Usage is
tryTest {
  ...
}.catch<Exception> {
  it
} cleanup {
  ...
}

Test: New tests for this
Change-Id: Idb7d9bd33034921c8f212288179b34a175e866f4

Chalard Jean   commited 2021-11-25 16:26:42 +09:00
8f0a33054b
tryTest to return its last evaluated expression
This is the same thing try{} does, and allows to lift
return out of tryTest.

This allows syntaxes likeĀ :
fun foo() = try {
    "Foo";
  } cleanup {
    doSomeCleanup()
  }
}

val network = try {
    registerNetworkCallback
    callback.getNetwork()
  } cleanup {
    unregisterNetworkCallback
  }
}

Note: bypassing ktlint because of b/185077240

Test: FrameworksNetTests
Change-Id: Ib8f6fde7ccfd62fdcb3c1e3b7b03909ed94d4b23

Chalard Jean   commited 2021-11-25 16:26:42 +09:00
b6fdf63095
Disallow non-local returns in tryTest{}
Non-local returns will prevent the execution of the cleanup{}
block, which is too much of a footgun to allow. See the bug
for details of how it happens.

There doesn't seem to be a way to keep the nice syntax, allow
non-local returns and still guarantee execution of the cleanup
block in all cases. Thus, forbid non-local returns. Users can
still use return@tryTest to accomplish almost the same thing,
and the next patch will also let tryTest{} return its last
evaluated value, fixing remaining cases.

E.g.
tryTest {
  foo()
  if (condition) return result
  bar()
} cleanup {
  doCleanup()
}

can always be written

return tryTest {
  foo()
  if (condition) return@tryTest result
  bar()
} cleanup {
  doCleanup()
}

...and it's a rare case, so the additional syntax is acceptable.

Test: NetworkStaticLibTests
Bug: 207358921
Change-Id: I40443acf7e4d86813641adc877e27fb2334d0daf

Chalard Jean   commited 2021-11-24 17:08:54 +09:00
7df6f89c37
Make sure no suppressed expression is present when they shouldn't
In fact the test already passes, because addSuppressed contains
an explicit test that if this === argument, then it doesn't add
it. But otherwise that's a bug in tryCleanup

Test: NetworkStaticLibTests
Change-Id: I202790bbe8d82445c5affdd9076561c2c6ea9b59

Chalard Jean   commited 2021-11-24 17:07:28 +09:00
bfbcacef6d
Add a new utility to improve stack traces from automatic testing
A common pattern in tests is to use try-finally to make sure
cleanup is executed. This is necessary in CTS in particular to
make sure the test does not leave the device in a bad state.

The problem with using try-finally in this manner is that any
exception thrown in the finally{} block will override any thrown
in the try{} block. If the exception in finally{} is caused by
the one from try{}, then the stack trace reported by the tools
is the consequence and not the cause, making it difficult to
interpret the stack trace of automated tests.

So today, code has to make sure that either the code in
finally{} can't throw, or can't be affected by any code in
try{}, and both of these are really difficult to ensure in
presence in finally{} of code the tester does not control.

What we'd want ideally is a structure like try-finally, that
guaratees the code in finally{} is executed in all cases,
but that bubbles up the exception from try{} if any, and
will still bubble up any exception from finally{} if the
try{} block hasn't thrown.

That's what this new tool does.

Usage from Kotlin is like try-finally :
tryTest {
  testing code
} cleanup {
  cleanup code
}

Usage from Java can't be made as nice, but it's relatively okay :
testAndCleanup(() -> {
  testing code
}, () -> {
  cleanup code
});

Bugs listed below are some tests that have been affected by
this issue and have unhelpful traces.

Test: new test for this code
Bug: 198586720
Bug: 198998862
Change-Id: I54b30a7d53772feeade99274b6120a79707ad1c9

Chalard Jean   commited 2021-09-17 12:21:09 +09:00
5872cb8dbd
Add the ConnectivityModuleTest annotation
The annotation will be used to mark tests cases that are part of MTS
modules used by multiple modules, to indicate that the test case should
only be run if the MTS suite should cover an updated Connectivity
(tethering) module.

This allows tests to verify new behavior in the Connectivity module,
without failing if that module is not updated.

Bug: 196755836
Test: mts-tradefed run mts-network with exclude-annotation in the config
Change-Id: I05d2200c7ab68042e4e48029e045f164c26b8037

Remi NGUYEN VAN   commited 2021-08-16 18:09:41 +09:00
843740592c
[FUI26.1] Generalize assertSameElements
Test: atest NetworkStaticLibTests
Bug: 174123988
Change-Id: I72a33a3c6ba849666e9f892745be3175aeb34b0e

junyulai   commited 2021-05-14 11:06:18 +08:00
4a73a9f08f
Move net-test-utils to its own directory
Having the test targets in a different directory allows setting
visibility rules for tests only, which is necessary for access to
targets that should not be used for device builds.

Bug: 182859030
Test: m
Change-Id: Iaf426cf339a97833acf80c941db692329c6e2dcb

Remi NGUYEN VAN   commited 2021-03-30 16:52:09 +09:00
be2a07fce3