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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* Samsung Exynos SoC series NPU driver
*
* Copyright (c) 2017 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
/*
* IDIOT - Intutive Device driver integrated Operation Testing
*
*/
#include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include "idiot-config.h"
#define STRINGFY(v) #v
/* Buffers to store the result */
char IDIOT_result_buf[IDIOT_RESULT_BUF_LEN + 4] = {0};
size_t IDIOT_result_len;
/* type definition of testcase function table */
struct IDIOT_tc_list {
int (*tc_func)(struct file*, char*, size_t);
char *tc_name;
};
/* Check whether the idiot file name is available */
#ifndef IDIOT_ALL_TESTCASE_INCLUDE
#error "Please define IDIOT_ALL_TESTCASE_INCLUDE as an header file includes all the .idiot files."
#endif
/* Do not include declaration section of testcase here */
#ifdef IDIOT_DECL_SECTION
#undef IDIOT_DECL_SECTION
#endif
/* Insert prototype of test functions */
#ifdef TESTDEF
#undef TESTDEF
#endif
#define TESTDEF(fname, ...) int TESTFUNCNAME(fname) (struct file*, char*, size_t);
#include IDIOT_ALL_TESTCASE_INCLUDE
#undef TESTDEF
/* Insert test function table */
#define TESTDEF(fname, ...) {TESTFUNCNAME(fname), #fname},
static struct IDIOT_tc_list IDIOT_testcase_table[] = {
#include IDIOT_ALL_TESTCASE_INCLUDE
};
#undef TESTDEF
/* Save list of testcases registered into result buffer */
static ssize_t IDIOT_list_tests(void)
{
unsigned int i;
int ret;
const size_t tcnum = ARRAY_SIZE(IDIOT_testcase_table);
const size_t max_line_len = TC_NAME_LEN + TC_NUM_DIGIT + 10;
size_t writeLen = 0;
char *pt;
for (i = 0, pt = IDIOT_result_buf; (i < tcnum) && ((writeLen + 1) < (IDIOT_RESULT_BUF_LEN - max_line_len)); i++) {
ret = scnprintf(pt, max_line_len, "[%08d] : %s\n",
i + 1, IDIOT_testcase_table[i].tc_name); /* TC id is 1-based */
writeLen += ret;
pt += ret;
}
*pt = '\0';
IDIOT_result_len = writeLen + 1; /* Add one more character for NULL termination */
return writeLen;
}
/* Test harness functions - write and read */
static int IDIOT_open(struct inode *inode, struct file *file)
{
/* No op */
return 0;
}
static ssize_t IDIOT_read(struct file *file, char __user *userbuf, size_t count, loff_t *offp)
{
size_t copyLen;
unsigned long ret;
if (IDIOT_result_len == 0) {
return 0; /* No result */
}
copyLen = min(count, IDIOT_result_len);
ret = copy_to_user(userbuf, IDIOT_result_buf, copyLen);
if (ret != 0) {
printk(KERN_ERR "IDIOT: Result write error. Result message may be truncated\n");
}
IDIOT_result_len -= copyLen;
return copyLen;
}
static ssize_t IDIOT_write(struct file *file, const char *__user userbuf, size_t count, loff_t *offp)
{
unsigned int i;
size_t writeLen;
unsigned int tcidx = 0;
const size_t tcnum = ARRAY_SIZE(IDIOT_testcase_table);
int test_result;
char *pt;
char ch;
/* Skip leading non-digit characters */
for (i = 0; i < count && i < TC_NUM_DIGIT && get_user(ch, userbuf + i) == 0; i++) {
if ('0' <= ch && ch <= '9') {
break;
}
}
/* Convert string to integer, up to TC_NUM_DIGIT */
for (; i < count && i < TC_NUM_DIGIT && get_user(ch, userbuf + i) == 0; i++) {
if ('0' <= ch && ch <= '9') {
tcidx *= 10;
tcidx += ch - '0';
} else {
break;
}
}
writeLen = i + 1;
/* execute the specified test */
IDIOT_result_buf[0] = '\0';
IDIOT_result_len = 0;
if (0 < tcidx && tcidx <= tcnum) {
/* Invoke testcase function here */
test_result = IDIOT_testcase_table[tcidx - 1] /* tcidx is 1-based index */
.tc_func(file, IDIOT_result_buf, IDIOT_RESULT_BUF_LEN);
for (pt = IDIOT_result_buf; *pt != '\0'; pt++); /* Search end of buffer */
if (pt > IDIOT_result_buf + IDIOT_RESULT_BUF_LEN - IDIOT_FINAL_RESULT_LEN) {
/* Check whether there are enough space to store final result line */
IDIOT_result_buf[0] = '!';
pt = IDIOT_result_buf + 1;
}
/* Write result */
scnprintf(pt, IDIOT_FINAL_RESULT_LEN, "@@%c TC# [%08u/%lu] %s : %s\n",
(test_result == 0) ? 'P' : 'F',
tcidx, tcnum, IDIOT_testcase_table[tcidx - 1].tc_name,
(test_result == 0) ? "PASS" : "FAIL");
/* Adjust result message length */
for (; *pt != '\0'; pt++);
IDIOT_result_len = (pt + 1) - IDIOT_result_buf; /* Includes null termination */
/* Successful return */
return writeLen;
} else if (tcidx == 0) {
/* Get TC list */
IDIOT_list_tests();
return writeLen;
} else {
return -1;
}
}
#if 0
int main(void)
{
size_t i;
const size_t tcnum = ARRAY_SIZE(IDIOT_testcase_table);
unsigned int pass_cnt = 0;
unsigned int fail_cnt = 0;
printf("Number of TC is %lu\n", tcnum);
for (i = 0; i < tcnum; ++i) {
if (IDIOT_testcase_table[i]() == 0) {
printf("@@ TC# [%08lu/%lu] : PASS\n", i+1, tcnum);
pass_cnt++;
} else {
printf("@@ TC# [%08lu/%lu] : FAIL\n", i+1, tcnum);
fail_cnt++;
}
}
printf("@@ Among %lu testcases, %u tests have run, PASS = %u, FAIL = %u.\n",
tcnum, pass_cnt + fail_cnt, pass_cnt, fail_cnt);
return 0;
}
#endif
|