summaryrefstree
path: root/kernel/drivers/kunit/test-stream.c
blob: bc029ae819e699fce78293811b29107517b7c4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: GPL-2.0
/*
 * C++ stream style string formatter and printer used in KUnit for outputting
 * KUnit messages.
 *
 * Copyright (C) 2018, Google LLC.
 * Author: Brendan Higgins <brendanhiggins@google.com>
 */

#include <kunit/test.h>
#include <kunit/test-stream.h>
#include <kunit/string-stream.h>

static void test_stream_set_level(struct test_stream *this,
				  const char *level)
{
	this->level = level;
}

static void test_stream_add(struct test_stream *this, const char *fmt, ...)
{
	va_list args;
	struct string_stream *stream = this->internal_stream;

	va_start(args, fmt);
	if (stream->vadd(stream, fmt, args) < 0)
		test_err(this->test, "Failed to allocate fragment: %s", fmt);

	va_end(args);
}

static void test_stream_append(struct test_stream *this,
			       struct test_stream *other)
{
	struct string_stream *other_stream = other->internal_stream;
	const char *other_content;

	other_content = other_stream->get_string(other_stream);

	if (!other_content) {
		test_err(this->test,
			 "Failed to get string from second argument for appending.");
		return;
	}

	this->add(this, other_content);
	kfree(other_content);
}

static void test_stream_clear(struct test_stream *this)
{
	this->internal_stream->clear(this->internal_stream);
}

static void test_stream_commit(struct test_stream *this)
{
	char *buf;
	struct string_stream_fragment *fragment;
	struct string_stream *stream = this->internal_stream;

	if (!this->level) {
		test_err(this->test,
			 "Stream was committed without a specified log level.");
		this->set_level(this, KERN_ERR);
	}

	buf = stream->get_string(stream);
	if (!buf) {
		test_err(this->test,
			 "Could not allocate buffer, dumping stream:");
		list_for_each_entry(fragment, &stream->fragments, node) {
			test_err(this->test, fragment->fragment);
		}
		goto cleanup;
	}

	test_printk(this->level, this->test, buf);
	kfree(buf);

cleanup:
	this->clear(this);
}

static int test_stream_init(struct test_resource *res, void *context)
{
	struct test_stream *stream;
	struct test *test = context;

	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
	if (!stream)
		return -ENOMEM;
	res->allocation = stream;
	stream->test = test;
	stream->level = NULL;
	stream->internal_stream = new_string_stream();

	if (!stream->internal_stream)
		return -ENOMEM;

	stream->set_level = test_stream_set_level;
	stream->add = test_stream_add;
	stream->append = test_stream_append;
	stream->commit = test_stream_commit;
	stream->clear = test_stream_clear;
	return 0;
}

static void test_stream_free(struct test_resource *res)
{
	struct test_stream *stream = res->allocation;

	if (!stream->internal_stream->is_empty(stream->internal_stream)) {
		test_err(stream->test,
			 "End of test case reached with uncommitted stream entries.");
		stream->commit(stream);
	}

	destroy_string_stream(stream->internal_stream);
	kfree(stream);
}

struct test_stream *test_new_stream(struct test *test)
{
	struct test_resource *res;

	res = test_alloc_resource(test,
				  test_stream_init,
				  test_stream_free,
				  test);

	if (res)
		return res->allocation;
	else
		return NULL;
}