-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.c
101 lines (83 loc) · 3.17 KB
/
tester.c
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
/*
tester.c
Copyright (C) 2020 jotaro-sama
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "guestbook.h"
#include <sys/ioctl.h>
#define BUF_SIZE 512
int write_mess(int dev, char *mess) {
size_t len = strlen(mess);
int status = write(dev, mess, len);
return status;
}
int main() {
int dev;
dev = open("/dev/guestbook", O_RDWR);
// Write test
char *mess = "I want to know!";
int status = write_mess(dev, mess);
if (status == -1) {
printf("Writing the message failed. Error code: %s\n", strerror(errno));
return 1;
}
// Read test
char buf[BUF_SIZE];
if (read(dev, &buf, BUF_SIZE) == -1) {
printf("Reading the message failed. Error code: %s\n", strerror(errno));
return 1;
}
printf("Message read: %s\n", buf);
// ioctl test
// first we write another couple messages
mess = "These aren't the droids you're looking for...";
status = write_mess(dev, mess);
if (status == -1) {
printf("Writing the message failed. Error code: %s\n", strerror(errno));
return 1;
}
mess = "These aren't either.";
status = write_mess(dev, mess);
// then invoke the ioctl to read the 1st message again
status = ioctl(dev, IOCTL_CHANGE_MESS_TO_READ, 1);
if (status < 0) {
printf("IOCTL_CHANGE_MESS_TO_READ failed. Error code: %s\n", strerror(errno));
return 1;
}
// read again to check if it worked. We should get "I want to know!"
if (read(dev, &buf, BUF_SIZE) == -1) {
printf("Reading the message failed. Error code: %s\n", strerror(errno));
return 1;
}
printf("Message read after IOCTL_CHANGE_MESS_TO_READ: %s\n", buf);
// now we test the other ioctl, we clear the guestbook and should get the
// number of cleared elements
size_t elems_se;
ssize_t elems_ret = ioctl(dev, IOCTL_CLEAR_GUESTBOOK, &elems_se);
if (elems_ret < 1) {
printf("IOCTL_CLEAR_GUESTBOOK failed. Error code: %s\n", strerror(errno));
return 1;
}
printf("Returned number of elements is: %lu\n", elems_ret);
printf("Side-effect number of elements is: %lu\n", elems_se);
// Let's also read again to check if the guestbook is really empty. The read should fail.
if (read(dev, &buf, BUF_SIZE) == -1) {
printf("Reading the message failed. Error code: %s\n", strerror(errno));
}
close(dev);
}