forked from janmojzis/tinyssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetlntest.c
71 lines (61 loc) · 2.01 KB
/
getlntest.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
/*
20140323
Jan Mojzis
Public domain.
*/
#include <unistd.h>
#include <stdio.h>
#include "fail.h"
#include "savesync.h"
#include "str.h"
#include "byte.h"
#include "open.h"
#include "getln.h"
int main(void) {
char buf[6];
int fd;
int r;
/* create test file */
if (savesync("getlntest.txt", "ab\ncd", 5) == -1) fail("savesync() failure");
/* ok test */
fd = open_read("getlntest.txt");
if (fd == -1) fail("open_read() failure");
r = getln(fd, buf, sizeof buf);
if (r != 1) fail("getln() failure");
if (str_len(buf) != 3) fail("getln() failure");
if (!byte_isequal(buf, 3, "ab\n")) fail("getln() failure");
r = getln(fd, buf, sizeof buf);
if (r != 0) fail("getln() failure");
if (str_len(buf) != 2) fail("getln() failure");
if (!byte_isequal(buf, 2, "cd")) fail("getln() failure");
close(fd);
/* overflow test */
fd = open_read("getlntest.txt");
if (fd == -1) fail("open_read() failure");
r = getln(fd, buf, 3);
if (r != -1) fail("getln() failure");
close(fd);
if (unlink("getlntest.txt") == -1) fail("unlink() failure");
/* empty test */
if (savesync("getlntest.txt", "", 0) == -1) fail("savesync() failure");
fd = open_read("getlntest.txt");
if (fd == -1) fail("open_read() failure");
r = getln(fd, buf, sizeof buf);
if (r != 0) fail("getln() failure");
if (str_len(buf) != 0) fail("getln() failure");
close(fd);
if (unlink("getlntest.txt") == -1) fail("unlink() failure");
/* empty line test */
if (savesync("getlntest.txt", "\n", 1) == -1) fail("savesync() failure");
fd = open_read("getlntest.txt");
if (fd == -1) fail("open_read() failure");
r = getln(fd, buf, sizeof buf);
if (r != 1) fail("getln() failure");
if (str_len(buf) != 1) fail("getln() failure");
r = getln(fd, buf, sizeof buf);
if (r != 0) fail("getln() failure");
if (str_len(buf) != 0) fail("getln() failure");
close(fd);
if (unlink("getlntest.txt") == -1) fail("unlink() failure");
_exit(0);
}