1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2022 Google, Inc.
4 * Written by Andrew Scull <ascull@google.com>
9 #include <virtio_types.h>
11 #include <virtio_ring.h>
12 #include <dm/device-internal.h>
15 #include <test/test.h>
18 /* This is a brittle means of getting access to the virtqueue */
19 struct virtio_rng_priv {
20 struct virtqueue *rng_vq;
23 /* Test the virtio-rng driver validates the used size */
24 static int dm_test_virtio_rng_check_len(struct unit_test_state *uts)
26 struct udevice *bus, *dev;
27 struct virtio_rng_priv *priv;
30 /* check probe success */
31 ut_assertok(uclass_first_device_err(UCLASS_VIRTIO, &bus));
32 ut_assertnonnull(bus);
34 /* check the child virtio-rng device is bound */
35 ut_assertok(device_find_first_child(bus, &dev));
36 ut_assertnonnull(dev);
38 /* probe the virtio-rng driver */
39 ut_assertok(device_probe(dev));
41 /* simulate the device returning the buffer with too much data */
42 priv = dev_get_priv(dev);
43 priv->rng_vq->vring.used->idx = 1;
44 priv->rng_vq->vring.used->ring[0].id = 0;
45 priv->rng_vq->vring.used->ring[0].len = U32_MAX;
47 /* check the driver gracefully handles the error */
48 ut_asserteq(-EIO, dm_rng_read(dev, buffer, sizeof(buffer)));
52 DM_TEST(dm_test_virtio_rng_check_len, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);