blob: ff5646b4e11331cb3f6a26baaf0606a9ea11c278 [file] [log] [blame]
Andrew Sculld0361042022-05-16 10:41:40 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2022 Google, Inc.
4 * Written by Andrew Scull <ascull@google.com>
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <virtio_types.h>
10#include <virtio.h>
11#include <virtio_ring.h>
12#include <dm/device-internal.h>
13#include <dm/test.h>
14#include <rng.h>
15#include <test/test.h>
16#include <test/ut.h>
17
18/* This is a brittle means of getting access to the virtqueue */
19struct virtio_rng_priv {
20 struct virtqueue *rng_vq;
21};
22
23/* Test the virtio-rng driver validates the used size */
24static int dm_test_virtio_rng_check_len(struct unit_test_state *uts)
25{
26 struct udevice *bus, *dev;
27 struct virtio_rng_priv *priv;
28 u8 buffer[16];
29
30 /* check probe success */
31 ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus));
32 ut_assertnonnull(bus);
33
34 /* check the child virtio-rng device is bound */
35 ut_assertok(device_find_first_child(bus, &dev));
36 ut_assertnonnull(dev);
37
38 /* probe the virtio-rng driver */
39 ut_assertok(device_probe(dev));
40
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;
46
47 /* check the driver gracefully handles the error */
48 ut_asserteq(-EIO, dm_rng_read(dev, buffer, sizeof(buffer)));
49
50 return 0;
51}
52DM_TEST(dm_test_virtio_rng_check_len, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);