auth: Rework afsconf_UpToDate to use CellServDB
[openafs.git] / tests / auth / common.c
1 /*
2  * Copyright (c) 2010 Your File System Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 /*!
26  * Common functions for building a configuration directory
27  */
28
29 #include <afsconfig.h>
30 #include <afs/param.h>
31 #include <roken.h>
32
33 #include <afs/afsutil.h>
34
35 #include "common.h"
36
37 static FILE *
38 openConfigFile(char *dirname, char *filename) {
39     char *path = NULL;
40     FILE *file;
41
42     if (asprintf(&path, "%s/%s", dirname, filename) == -1)
43         return NULL;
44
45     file = fopen(path, "w");
46     free(path);
47     return file;
48 }
49
50 static void
51 unlinkConfigFile(char *dirname, char *filename) {
52     char *path;
53
54     if (asprintf(&path, "%s/%s", dirname, filename) != -1) {
55         unlink(path);
56         free(path);
57     }
58 }
59
60 /*!
61  * Build a test configuration directory, containing a CellServDB and ThisCell
62  * file for the "example.org" cell
63  *
64  * @return
65  * The path to the configuration directory. This should be freed by the caller
66  * using free()
67  *
68  */
69
70 char *
71 buildTestConfig(void) {
72     char *dir = NULL;
73     FILE *file;
74
75     if (asprintf(&dir, "%s/afs_XXXXXX", gettmpdir()) == -1)
76         goto fail;
77
78     if (mkdtemp(dir) == NULL)
79         goto fail;
80
81     /* Create a CellServDB */
82     file = openConfigFile(dir, "CellServDB");
83     fprintf(file, ">example.org # An example cell\n");
84     fprintf(file, "127.0.0.1 #test.example.org\n");
85     fclose(file);
86
87     /* Create a ThisCell file */
88     file = openConfigFile(dir, "ThisCell");
89     fprintf(file, "example.org");
90     fclose(file);
91
92     return dir;
93
94 fail:
95     if (dir)
96         free(dir);
97     return NULL;
98 }
99
100 /*!
101  * Delete at test configuration directory
102  */
103
104 void
105 unlinkTestConfig(char *dir)
106 {
107     unlinkConfigFile(dir, "KeyFile");
108     unlinkConfigFile(dir, "CellServDB");
109     unlinkConfigFile(dir, "ThisCell");
110     unlinkConfigFile(dir, "UserList");
111     rmdir(dir);
112 }