test/dm: check if devices exist

Running 'ut dm' on the sandbox without -D or -d results in segmentation
faults due to NULL pointer dereferences.

Check that device pointers are non-NULL before using them.

Use ut_assertnonnull() for pointers instead of ut_assert().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Tested-by: Philippe Reynes <philippe.reynes@softathome.com>
This commit is contained in:
Heinrich Schuchardt
2020-07-17 00:20:14 +02:00
committed by Simon Glass
parent 469f04e882
commit 105da6251a
5 changed files with 18 additions and 5 deletions

View File

@@ -22,9 +22,11 @@ static int dm_test_virtio_base(struct unit_test_state *uts)
/* check probe success */
ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
ut_assertnonnull(bus);
/* check the child virtio-blk device is bound */
ut_assertok(device_find_first_child(bus, &dev));
ut_assertnonnull(dev);
ut_assertok(strcmp(dev->name, "virtio-blk#0"));
/* check driver status */
@@ -49,15 +51,18 @@ static int dm_test_virtio_all_ops(struct unit_test_state *uts)
/* check probe success */
ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
ut_assertnonnull(bus);
/* check the child virtio-blk device is bound */
ut_assertok(device_find_first_child(bus, &dev));
ut_assertnonnull(dev);
/*
* fake the virtio device probe by filling in uc_priv->vdev
* which is used by virtio_find_vqs/virtio_del_vqs.
*/
uc_priv = dev_get_uclass_priv(bus);
ut_assertnonnull(uc_priv);
uc_priv->vdev = dev;
/* test virtio_xxx APIs */
@@ -106,9 +111,11 @@ static int dm_test_virtio_remove(struct unit_test_state *uts)
/* check probe success */
ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
ut_assertnonnull(bus);
/* check the child virtio-blk device is bound */
ut_assertok(device_find_first_child(bus, &dev));
ut_assertnonnull(dev);
/* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */
ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK));