1240116Smarcel/*
2240116Smarcel * Automated Testing Framework (atf)
3240116Smarcel *
4240116Smarcel * Copyright (c) 2009 The NetBSD Foundation, Inc.
5240116Smarcel * All rights reserved.
6240116Smarcel *
7240116Smarcel * Redistribution and use in source and binary forms, with or without
8240116Smarcel * modification, are permitted provided that the following conditions
9240116Smarcel * are met:
10240116Smarcel * 1. Redistributions of source code must retain the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer.
12240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel *    notice, this list of conditions and the following disclaimer in the
14240116Smarcel *    documentation and/or other materials provided with the distribution.
15240116Smarcel *
16240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel */
29240116Smarcel
30240116Smarcel#include <fcntl.h>
31240116Smarcel#include <stdio.h>
32240116Smarcel#include <unistd.h>
33240116Smarcel
34240116Smarcel#include <atf-c.h>
35240116Smarcel
36240116Smarcel#include "dynstr.h"
37240116Smarcel#include "test_helpers.h"
38240116Smarcel
39240116Smarcel/* ---------------------------------------------------------------------
40240116Smarcel * Test cases for the free functions.
41240116Smarcel * --------------------------------------------------------------------- */
42240116Smarcel
43240116Smarcel/* TODO: Add checks for build_check_c_o and the macros defined in the
44240116Smarcel * header file. */
45240116Smarcel
46240116SmarcelATF_TC(grep_string);
47240116SmarcelATF_TC_HEAD(grep_string, tc)
48240116Smarcel{
49240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the grep_string helper "
50240116Smarcel                      "function");
51240116Smarcel}
52240116SmarcelATF_TC_BODY(grep_string, tc)
53240116Smarcel{
54240116Smarcel    atf_dynstr_t str;
55240116Smarcel
56240116Smarcel    atf_dynstr_init_fmt(&str, "a string - aaaabbbb");
57240116Smarcel    ATF_CHECK(grep_string(&str, "a string"));
58240116Smarcel    ATF_CHECK(grep_string(&str, "^a string"));
59240116Smarcel    ATF_CHECK(grep_string(&str, "aaaabbbb$"));
60240116Smarcel    ATF_CHECK(grep_string(&str, "aa.*bb"));
61240116Smarcel    ATF_CHECK(!grep_string(&str, "foo"));
62240116Smarcel    ATF_CHECK(!grep_string(&str, "bar"));
63240116Smarcel    ATF_CHECK(!grep_string(&str, "aaaaa"));
64240116Smarcel
65240116Smarcel    atf_dynstr_fini(&str);
66240116Smarcel}
67240116Smarcel
68240116Smarcel
69240116SmarcelATF_TC(grep_file);
70240116SmarcelATF_TC_HEAD(grep_file, tc)
71240116Smarcel{
72240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the grep_file helper function");
73240116Smarcel}
74240116SmarcelATF_TC_BODY(grep_file, tc)
75240116Smarcel{
76240116Smarcel    FILE *f;
77240116Smarcel
78240116Smarcel    f = fopen("test.txt", "w");
79240116Smarcel    ATF_CHECK(f != NULL);
80240116Smarcel    fprintf(f, "line1\n");
81240116Smarcel    fprintf(f, "the second line\n");
82240116Smarcel    fprintf(f, "aaaabbbb\n");
83240116Smarcel    fclose(f);
84240116Smarcel
85240116Smarcel    ATF_CHECK(grep_file("test.txt", "line1"));
86240116Smarcel    ATF_CHECK(grep_file("test.txt", "line%d", 1));
87240116Smarcel    ATF_CHECK(grep_file("test.txt", "second line"));
88240116Smarcel    ATF_CHECK(grep_file("test.txt", "aa.*bb"));
89240116Smarcel    ATF_CHECK(!grep_file("test.txt", "foo"));
90240116Smarcel    ATF_CHECK(!grep_file("test.txt", "bar"));
91240116Smarcel    ATF_CHECK(!grep_file("test.txt", "aaaaa"));
92240116Smarcel}
93240116Smarcel
94240116SmarcelATF_TC(read_line);
95240116SmarcelATF_TC_HEAD(read_line, tc)
96240116Smarcel{
97240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the read_line function");
98240116Smarcel}
99240116SmarcelATF_TC_BODY(read_line, tc)
100240116Smarcel{
101240116Smarcel    const char *l1 = "First line with % formatting % characters %";
102240116Smarcel    const char *l2 = "Second line; much longer than the first one";
103240116Smarcel    const char *l3 = "Last line, without terminator";
104240116Smarcel
105240116Smarcel    {
106240116Smarcel        FILE *f;
107240116Smarcel
108240116Smarcel        f = fopen("test", "w");
109240116Smarcel        ATF_REQUIRE(f != NULL);
110240116Smarcel        fclose(f);
111240116Smarcel    }
112240116Smarcel
113240116Smarcel    {
114240116Smarcel        int fd;
115240116Smarcel        atf_dynstr_t dest;
116240116Smarcel        bool eof;
117240116Smarcel
118240116Smarcel        fd = open("test", O_RDONLY);
119240116Smarcel        ATF_REQUIRE(fd != -1);
120240116Smarcel
121240116Smarcel        RE(atf_dynstr_init(&dest));
122240116Smarcel        eof = read_line(fd, &dest);
123240116Smarcel        ATF_REQUIRE(eof);
124240116Smarcel        atf_dynstr_fini(&dest);
125240116Smarcel    }
126240116Smarcel
127240116Smarcel    {
128240116Smarcel        FILE *f;
129240116Smarcel
130240116Smarcel        f = fopen("test", "w");
131240116Smarcel        ATF_REQUIRE(f != NULL);
132240116Smarcel
133240116Smarcel        fprintf(f, "%s\n", l1);
134240116Smarcel        fprintf(f, "%s\n", l2);
135240116Smarcel        fprintf(f, "%s", l3);
136240116Smarcel
137240116Smarcel        fclose(f);
138240116Smarcel    }
139240116Smarcel
140240116Smarcel    {
141240116Smarcel        int fd;
142240116Smarcel        atf_dynstr_t dest;
143240116Smarcel        bool eof;
144240116Smarcel
145240116Smarcel        fd = open("test", O_RDONLY);
146240116Smarcel        ATF_REQUIRE(fd != -1);
147240116Smarcel
148240116Smarcel        RE(atf_dynstr_init(&dest));
149240116Smarcel        eof = read_line(fd, &dest);
150240116Smarcel        ATF_REQUIRE(!eof);
151240116Smarcel        printf("1st line: >%s<\n", atf_dynstr_cstring(&dest));
152240116Smarcel        ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l1));
153240116Smarcel        atf_dynstr_fini(&dest);
154240116Smarcel
155240116Smarcel        RE(atf_dynstr_init(&dest));
156240116Smarcel        eof = read_line(fd, &dest);
157240116Smarcel        ATF_REQUIRE(!eof);
158240116Smarcel        printf("2nd line: >%s<\n", atf_dynstr_cstring(&dest));
159240116Smarcel        ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l2));
160240116Smarcel        atf_dynstr_fini(&dest);
161240116Smarcel
162240116Smarcel        RE(atf_dynstr_init(&dest));
163240116Smarcel        eof = read_line(fd, &dest);
164240116Smarcel        ATF_REQUIRE(eof);
165240116Smarcel        printf("3rd line: >%s<\n", atf_dynstr_cstring(&dest));
166240116Smarcel        ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l3));
167240116Smarcel        atf_dynstr_fini(&dest);
168240116Smarcel
169240116Smarcel        close(fd);
170240116Smarcel    }
171240116Smarcel}
172240116Smarcel
173240116Smarcel/* ---------------------------------------------------------------------
174240116Smarcel * Main.
175240116Smarcel * --------------------------------------------------------------------- */
176240116Smarcel
177240116SmarcelATF_TP_ADD_TCS(tp)
178240116Smarcel{
179240116Smarcel    /* Add the tests for the free functions. */
180240116Smarcel    ATF_TP_ADD_TC(tp, grep_string);
181240116Smarcel    ATF_TP_ADD_TC(tp, grep_file);
182240116Smarcel    ATF_TP_ADD_TC(tp, read_line);
183240116Smarcel
184240116Smarcel    return atf_no_error();
185240116Smarcel}
186