1 /***************************************************************************
2  *            test-userdata-dir-invalid-home.c
3  *
4  *  Thu Sep 29 22:48:57 2005
5  *  Copyright  2005  GnuCash team
6  ****************************************************************************/
7 /*
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  *  02110-1301, USA.
22  */
23 
24 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include <glib.h>
29 #include "test-stuff.h"
30 #include "gnc-filepath-utils.h"
31 
32 struct usr_confpath_strings_struct
33 {
34     int func_num;
35     char *funcname;
36     char *output;
37 };
38 
39 typedef struct usr_confpath_strings_struct usr_confpath_strings;
40 
41 usr_confpath_strings strs2[] =
42 {
43     {
44         0, "gnc_build_userdata_path",
45         PROJECT_NAME
46     },
47     {
48         1, "gnc_build_book_path",
49         PROJECT_NAME G_DIR_SEPARATOR_S "books"
50     },
51     {
52         2, "gnc_build_translog_path",
53         PROJECT_NAME G_DIR_SEPARATOR_S "translog"
54     },
55     {
56         3, "gnc_build_data_path",
57         PROJECT_NAME G_DIR_SEPARATOR_S "data"
58     },
59     { 0, NULL, NULL },
60 };
61 
62 int
main(G_GNUC_UNUSED int argc,G_GNUC_UNUSED char ** argv)63 main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
64 {
65 /* Don't run this test on Windows or OS X. This
66    test attempts to fool the code into using a non-existent
67    home directory, but the way this is done only works on linux
68    */
69 #ifndef MAC_INTEGRATION
70 #ifndef G_OS_WIN32
71     int i;
72     const char *tmp_dir = g_get_tmp_dir();
73     const char *builddir = g_getenv ("GNC_BUILDDIR");
74     char *homedir = g_build_filename (builddir, "notexist", NULL);
75 
76     /* Assume we're not in a build environment to test
77      * the function's actual behaviour in a real world use case, using
78      * a non-existent homedir. */
79     g_unsetenv("GNC_BUILDDIR");
80     g_unsetenv("GNC_UNINSTALLED");
81 
82     /* Run usr conf dir tests with an invalid homedir
83      * The code should fall back to using the temporary
84      * directory in that case. */
85     g_setenv("HOME", homedir, TRUE);
86     for (i = 0; strs2[i].funcname != NULL; i++)
87     {
88         char *daout;
89         char *wantout;
90 
91         if (strs2[i].func_num == 0)
92         {
93             wantout = g_build_filename(tmp_dir, g_get_user_name (), strs2[i].output, "foo",
94                                        (gchar *)NULL);
95             daout = gnc_build_userdata_path("foo");
96         }
97         else if (strs2[i].func_num == 1)
98         {
99             wantout = g_build_filename(tmp_dir, g_get_user_name (), strs2[i].output, "foo",
100                                        (gchar *)NULL);
101             daout = gnc_build_book_path("foo");
102         }
103         else if (strs2[i].func_num == 2)
104         {
105             wantout = g_build_filename(tmp_dir, g_get_user_name (), strs2[i].output, "foo",
106                                        (gchar *)NULL);
107             daout = gnc_build_translog_path("foo");
108         }
109         else // if (strs2[i].prefix_home == 3)
110         {
111             wantout = g_build_filename(tmp_dir, g_get_user_name (), strs2[i].output, "foo",
112                                        (gchar *)NULL);
113             daout = gnc_build_data_path("foo");
114         }
115 
116         do_test_args(g_strcmp0(daout, wantout) == 0,
117                      "gnc_build_x_path",
118                      __FILE__, __LINE__,
119                      "%s (%s) vs %s", daout, strs2[i].funcname, wantout);
120         g_free(wantout);
121         g_free(daout);
122     }
123 
124     print_test_results();
125     return get_rv();
126 #endif
127 #endif
128 }
129