1/*
2 * Automated Testing Framework (atf)
3 *
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <fcntl.h>
31#include <stdio.h>
32#include <unistd.h>
33
34#include <atf-c.h>
35
36#include "dynstr.h"
37#include "test_helpers.h"
38
39/* ---------------------------------------------------------------------
40 * Test cases for the free functions.
41 * --------------------------------------------------------------------- */
42
43/* TODO: Add checks for build_check_c_o and the macros defined in the
44 * header file. */
45
46ATF_TC(grep_string);
47ATF_TC_HEAD(grep_string, tc)
48{
49    atf_tc_set_md_var(tc, "descr", "Tests the grep_string helper "
50                      "function");
51}
52ATF_TC_BODY(grep_string, tc)
53{
54    atf_dynstr_t str;
55
56    atf_dynstr_init_fmt(&str, "a string - aaaabbbb");
57    ATF_CHECK(grep_string(&str, "a string"));
58    ATF_CHECK(grep_string(&str, "^a string"));
59    ATF_CHECK(grep_string(&str, "aaaabbbb$"));
60    ATF_CHECK(grep_string(&str, "aa.*bb"));
61    ATF_CHECK(!grep_string(&str, "foo"));
62    ATF_CHECK(!grep_string(&str, "bar"));
63    ATF_CHECK(!grep_string(&str, "aaaaa"));
64
65    atf_dynstr_fini(&str);
66}
67
68
69ATF_TC(grep_file);
70ATF_TC_HEAD(grep_file, tc)
71{
72    atf_tc_set_md_var(tc, "descr", "Tests the grep_file helper function");
73}
74ATF_TC_BODY(grep_file, tc)
75{
76    FILE *f;
77
78    f = fopen("test.txt", "w");
79    ATF_CHECK(f != NULL);
80    fprintf(f, "line1\n");
81    fprintf(f, "the second line\n");
82    fprintf(f, "aaaabbbb\n");
83    fclose(f);
84
85    ATF_CHECK(grep_file("test.txt", "line1"));
86    ATF_CHECK(grep_file("test.txt", "line%d", 1));
87    ATF_CHECK(grep_file("test.txt", "second line"));
88    ATF_CHECK(grep_file("test.txt", "aa.*bb"));
89    ATF_CHECK(!grep_file("test.txt", "foo"));
90    ATF_CHECK(!grep_file("test.txt", "bar"));
91    ATF_CHECK(!grep_file("test.txt", "aaaaa"));
92}
93
94ATF_TC(read_line);
95ATF_TC_HEAD(read_line, tc)
96{
97    atf_tc_set_md_var(tc, "descr", "Tests the read_line function");
98}
99ATF_TC_BODY(read_line, tc)
100{
101    const char *l1 = "First line with % formatting % characters %";
102    const char *l2 = "Second line; much longer than the first one";
103    const char *l3 = "Last line, without terminator";
104
105    {
106        FILE *f;
107
108        f = fopen("test", "w");
109        ATF_REQUIRE(f != NULL);
110        fclose(f);
111    }
112
113    {
114        int fd;
115        atf_dynstr_t dest;
116        bool eof;
117
118        fd = open("test", O_RDONLY);
119        ATF_REQUIRE(fd != -1);
120
121        RE(atf_dynstr_init(&dest));
122        eof = read_line(fd, &dest);
123        ATF_REQUIRE(eof);
124        atf_dynstr_fini(&dest);
125    }
126
127    {
128        FILE *f;
129
130        f = fopen("test", "w");
131        ATF_REQUIRE(f != NULL);
132
133        fprintf(f, "%s\n", l1);
134        fprintf(f, "%s\n", l2);
135        fprintf(f, "%s", l3);
136
137        fclose(f);
138    }
139
140    {
141        int fd;
142        atf_dynstr_t dest;
143        bool eof;
144
145        fd = open("test", O_RDONLY);
146        ATF_REQUIRE(fd != -1);
147
148        RE(atf_dynstr_init(&dest));
149        eof = read_line(fd, &dest);
150        ATF_REQUIRE(!eof);
151        printf("1st line: >%s<\n", atf_dynstr_cstring(&dest));
152        ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l1));
153        atf_dynstr_fini(&dest);
154
155        RE(atf_dynstr_init(&dest));
156        eof = read_line(fd, &dest);
157        ATF_REQUIRE(!eof);
158        printf("2nd line: >%s<\n", atf_dynstr_cstring(&dest));
159        ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l2));
160        atf_dynstr_fini(&dest);
161
162        RE(atf_dynstr_init(&dest));
163        eof = read_line(fd, &dest);
164        ATF_REQUIRE(eof);
165        printf("3rd line: >%s<\n", atf_dynstr_cstring(&dest));
166        ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l3));
167        atf_dynstr_fini(&dest);
168
169        close(fd);
170    }
171}
172
173/* ---------------------------------------------------------------------
174 * Main.
175 * --------------------------------------------------------------------- */
176
177ATF_TP_ADD_TCS(tp)
178{
179    /* Add the tests for the free functions. */
180    ATF_TP_ADD_TC(tp, grep_string);
181    ATF_TP_ADD_TC(tp, grep_file);
182    ATF_TP_ADD_TC(tp, read_line);
183
184    return atf_no_error();
185}
186