alsa: Fix mixer path when running from build tree
[platform/upstream/pulseaudio.git] / src / tests / alsa-mixer-path-test.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include <check.h>
6 #include <dirent.h>
7 #include <stdbool.h>
8 #include <stdio.h>
9
10 #include <pulse/pulseaudio.h>
11 #include <pulsecore/log.h>
12 #include <pulsecore/core-util.h>
13 #include <pulsecore/strlist.h>
14 #include <modules/alsa/alsa-mixer.h>
15
16 /* This function was copied from alsa-mixer.c */
17 static const char *get_default_paths_dir(void) {
18     if (pa_run_from_build_tree())
19         return PA_SRCDIR "/modules/alsa/mixer/paths/";
20     else
21         return PA_ALSA_PATHS_DIR;
22 }
23
24 static pa_strlist *load_makefile() {
25     FILE *f;
26     bool lookforfiles = false;
27     char buf[2048];
28     pa_strlist *result = NULL;
29     const char *Makefile = PA_BUILDDIR "/Makefile";
30
31     f = pa_fopen_cloexec(Makefile, "r");
32     fail_unless(f != NULL); /* Consider skipping this test instead of failing if Makefile not found? */
33     while (!feof(f)) {
34         if (!fgets(buf, sizeof(buf), f)) {
35             fail_unless(feof(f));
36             break;
37         }
38         if (strstr(buf, "dist_alsapaths_DATA = \\") != NULL) {
39            lookforfiles = true;
40            continue;
41         }
42         if (!lookforfiles)
43            continue;
44         if (!strstr(buf, "\\"))
45            lookforfiles = false;
46         else
47            strstr(buf, "\\")[0] = '\0';
48         pa_strip(buf);
49         pa_log_debug("Shipping file '%s'", pa_path_get_filename(buf));
50         result = pa_strlist_prepend(result, pa_path_get_filename(buf));
51     }
52     fclose(f);
53     return result;
54 }
55
56
57 START_TEST (mixer_path_test) {
58     DIR *dir;
59     struct dirent *ent;
60     pa_strlist *ship = load_makefile();
61     const char *pathsdir = get_default_paths_dir();
62     pa_log_debug("Analyzing directory: '%s'", pathsdir);
63
64     dir = opendir(pathsdir);
65     fail_unless(dir != NULL);
66     while ((ent = readdir(dir)) != NULL) {
67         pa_alsa_path *path;
68         if (pa_streq(ent->d_name, ".") || pa_streq(ent->d_name, ".."))
69             continue;
70         pa_log_debug("Analyzing file: '%s'", ent->d_name);
71
72         /* Can the file be parsed? */
73         path = pa_alsa_path_new(pathsdir, ent->d_name, PA_ALSA_DIRECTION_ANY);
74         fail_unless(path != NULL);
75
76         /* Is the file shipped? */
77         if (ship) {
78             pa_strlist *n;
79             bool found = false;
80             for (n = ship; n; n = pa_strlist_next(n))
81                 found |= pa_streq(ent->d_name, pa_strlist_data(n));
82             fail_unless(found);
83         }
84     }
85     closedir(dir);
86     pa_strlist_free(ship);
87 }
88 END_TEST
89
90 int main(int argc, char *argv[]) {
91     int failed = 0;
92     Suite *s;
93     TCase *tc;
94     SRunner *sr;
95
96     if (!getenv("MAKE_CHECK"))
97         pa_log_set_level(PA_LOG_DEBUG);
98
99     s = suite_create("Alsa-mixer-path");
100     tc = tcase_create("alsa-mixer-path");
101     tcase_add_test(tc, mixer_path_test);
102     tcase_set_timeout(tc, 30);
103     suite_add_tcase(s, tc);
104
105     sr = srunner_create(s);
106     srunner_run_all(sr, CK_NORMAL);
107     failed = srunner_ntests_failed(sr);
108     srunner_free(sr);
109
110     return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
111 }