1*1c9681d1Schristos /* $NetBSD: test-readenv.c,v 1.2 2017/01/28 21:31:50 christos Exp $ */
2f59d82ffSelric
3f59d82ffSelric /*
4f59d82ffSelric * Copyright (c) 2005 Kungliga Tekniska Högskolan
5f59d82ffSelric * (Royal Institute of Technology, Stockholm, Sweden).
6f59d82ffSelric * All rights reserved.
7f59d82ffSelric *
8f59d82ffSelric * Redistribution and use in source and binary forms, with or without
9f59d82ffSelric * modification, are permitted provided that the following conditions
10f59d82ffSelric * are met:
11f59d82ffSelric *
12f59d82ffSelric * 1. Redistributions of source code must retain the above copyright
13f59d82ffSelric * notice, this list of conditions and the following disclaimer.
14f59d82ffSelric *
15f59d82ffSelric * 2. Redistributions in binary form must reproduce the above copyright
16f59d82ffSelric * notice, this list of conditions and the following disclaimer in the
17f59d82ffSelric * documentation and/or other materials provided with the distribution.
18f59d82ffSelric *
19f59d82ffSelric * 3. Neither the name of the Institute nor the names of its contributors
20f59d82ffSelric * may be used to endorse or promote products derived from this software
21f59d82ffSelric * without specific prior written permission.
22f59d82ffSelric *
23f59d82ffSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24f59d82ffSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f59d82ffSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f59d82ffSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27f59d82ffSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f59d82ffSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f59d82ffSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f59d82ffSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f59d82ffSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f59d82ffSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f59d82ffSelric * SUCH DAMAGE.
34f59d82ffSelric */
35f59d82ffSelric
36f59d82ffSelric #include <config.h>
37f59d82ffSelric
38f59d82ffSelric #include <krb5/roken.h>
39f59d82ffSelric #include "test-mem.h"
40f59d82ffSelric
41f59d82ffSelric char *s1 = "VAR1=VAL1#comment\n\
42f59d82ffSelric VAR2=VAL2 VAL2 #comment\n\
43f59d82ffSelric #this another comment\n\
44f59d82ffSelric \n\
45f59d82ffSelric VAR3=FOO";
46f59d82ffSelric
47f59d82ffSelric char *s2 = "VAR1=ENV2\n\
48f59d82ffSelric ";
49f59d82ffSelric
50f59d82ffSelric static void
make_file(char * tmpl,size_t l)51f59d82ffSelric make_file(char *tmpl, size_t l)
52f59d82ffSelric {
53f59d82ffSelric int fd;
54f59d82ffSelric strlcpy(tmpl, "env.XXXXXX", l);
55f59d82ffSelric fd = mkstemp(tmpl);
56f59d82ffSelric if(fd < 0)
57f59d82ffSelric err(1, "mkstemp");
58f59d82ffSelric close(fd);
59f59d82ffSelric }
60f59d82ffSelric
61f59d82ffSelric static void
write_file(const char * fn,const char * s)62f59d82ffSelric write_file(const char *fn, const char *s)
63f59d82ffSelric {
64f59d82ffSelric FILE *f;
65f59d82ffSelric f = fopen(fn, "w");
66f59d82ffSelric if(f == NULL) {
67f59d82ffSelric unlink(fn);
68f59d82ffSelric err(1, "fopen");
69f59d82ffSelric }
70f59d82ffSelric if(fwrite(s, 1, strlen(s), f) != strlen(s))
71f59d82ffSelric err(1, "short write");
72f59d82ffSelric if(fclose(f) != 0) {
73f59d82ffSelric unlink(fn);
74f59d82ffSelric err(1, "fclose");
75f59d82ffSelric }
76f59d82ffSelric }
77f59d82ffSelric
78f59d82ffSelric int
main(int argc,char ** argv)79f59d82ffSelric main(int argc, char **argv)
80f59d82ffSelric {
81f59d82ffSelric char **env = NULL;
82f59d82ffSelric int count = 0;
83f59d82ffSelric char fn[MAXPATHLEN];
84f59d82ffSelric int error = 0;
85f59d82ffSelric
86f59d82ffSelric make_file(fn, sizeof(fn));
87f59d82ffSelric
88f59d82ffSelric write_file(fn, s1);
89f59d82ffSelric count = read_environment(fn, &env);
90f59d82ffSelric if(count != 3) {
91f59d82ffSelric warnx("test 1: variable count %d != 3", count);
92f59d82ffSelric error++;
93f59d82ffSelric }
94f59d82ffSelric
95f59d82ffSelric write_file(fn, s2);
96f59d82ffSelric count = read_environment(fn, &env);
97f59d82ffSelric if(count != 1) {
98f59d82ffSelric warnx("test 2: variable count %d != 1", count);
99f59d82ffSelric error++;
100f59d82ffSelric }
101f59d82ffSelric
102f59d82ffSelric unlink(fn);
103f59d82ffSelric count = read_environment(fn, &env);
104f59d82ffSelric if(count != 0) {
105f59d82ffSelric warnx("test 3: variable count %d != 0", count);
106f59d82ffSelric error++;
107f59d82ffSelric }
108f59d82ffSelric for(count = 0; env && env[count]; count++);
109f59d82ffSelric if(count != 3) {
110f59d82ffSelric warnx("total variable count %d != 3", count);
111f59d82ffSelric error++;
112f59d82ffSelric }
113f59d82ffSelric free_environment(env);
114f59d82ffSelric
115f59d82ffSelric
116f59d82ffSelric return error;
117f59d82ffSelric }
118