1 | /*
|
---|
2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <errno.h>
|
---|
30 | #include <pcut/pcut.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <str.h>
|
---|
33 |
|
---|
34 | #include "../volume.h"
|
---|
35 |
|
---|
36 | PCUT_INIT;
|
---|
37 |
|
---|
38 | PCUT_TEST_SUITE(volume);
|
---|
39 |
|
---|
40 | /** Volumes list creation and destruction. */
|
---|
41 | PCUT_TEST(volumes_basic)
|
---|
42 | {
|
---|
43 | vol_volumes_t *volumes;
|
---|
44 | char *namebuf;
|
---|
45 | char *fname;
|
---|
46 | errno_t rc;
|
---|
47 | int rv;
|
---|
48 |
|
---|
49 | namebuf = malloc(L_tmpnam);
|
---|
50 | PCUT_ASSERT_NOT_NULL(namebuf);
|
---|
51 |
|
---|
52 | fname = tmpnam(namebuf);
|
---|
53 | PCUT_ASSERT_NOT_NULL(fname);
|
---|
54 |
|
---|
55 | rc = vol_volumes_create(fname, &volumes);
|
---|
56 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
57 |
|
---|
58 | vol_volumes_destroy(volumes);
|
---|
59 | rv = remove(fname);
|
---|
60 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
---|
61 | free(fname);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Two references to the same volume, reference to a different volume. */
|
---|
65 | PCUT_TEST(two_same_different)
|
---|
66 | {
|
---|
67 | vol_volumes_t *volumes;
|
---|
68 | vol_volume_t *va, *vb, *va1;
|
---|
69 | char *namebuf;
|
---|
70 | char *fname;
|
---|
71 | errno_t rc;
|
---|
72 | int rv;
|
---|
73 |
|
---|
74 | namebuf = malloc(L_tmpnam);
|
---|
75 | PCUT_ASSERT_NOT_NULL(namebuf);
|
---|
76 |
|
---|
77 | fname = tmpnam(namebuf);
|
---|
78 | PCUT_ASSERT_NOT_NULL(fname);
|
---|
79 |
|
---|
80 | rc = vol_volumes_create(fname, &volumes);
|
---|
81 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
82 |
|
---|
83 | rc = vol_volume_lookup_ref(volumes, "foo", &va);
|
---|
84 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
85 |
|
---|
86 | rc = vol_volume_lookup_ref(volumes, "bar", &vb);
|
---|
87 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
88 |
|
---|
89 | rc = vol_volume_lookup_ref(volumes, "foo", &va1);
|
---|
90 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
91 |
|
---|
92 | PCUT_ASSERT_TRUE(va1 == va);
|
---|
93 |
|
---|
94 | vol_volume_del_ref(va);
|
---|
95 | vol_volume_del_ref(vb);
|
---|
96 | vol_volume_del_ref(va1);
|
---|
97 |
|
---|
98 | vol_volumes_destroy(volumes);
|
---|
99 | rv = remove(fname);
|
---|
100 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
---|
101 | free(fname);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Reference the same volume twice, making sure it persists. */
|
---|
105 | PCUT_TEST(same_twice)
|
---|
106 | {
|
---|
107 | vol_volumes_t *volumes;
|
---|
108 | vol_volume_t *va;
|
---|
109 | char *namebuf;
|
---|
110 | char *fname;
|
---|
111 | errno_t rc;
|
---|
112 | int rv;
|
---|
113 |
|
---|
114 | namebuf = malloc(L_tmpnam);
|
---|
115 | PCUT_ASSERT_NOT_NULL(namebuf);
|
---|
116 |
|
---|
117 | fname = tmpnam(namebuf);
|
---|
118 | PCUT_ASSERT_NOT_NULL(fname);
|
---|
119 |
|
---|
120 | rc = vol_volumes_create(fname, &volumes);
|
---|
121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
122 |
|
---|
123 | /* Look up a volume */
|
---|
124 | rc = vol_volume_lookup_ref(volumes, "foo", &va);
|
---|
125 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
126 |
|
---|
127 | /* Setting mount point forces it to persist after dropping the ref. */
|
---|
128 | rc = vol_volume_set_mountp(va, "/xyz");
|
---|
129 |
|
---|
130 | /* Drop the reference */
|
---|
131 | vol_volume_del_ref(va);
|
---|
132 |
|
---|
133 | /* Look up volume again */
|
---|
134 | rc = vol_volume_lookup_ref(volumes, "foo", &va);
|
---|
135 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
136 |
|
---|
137 | /* The mount point should still be set */
|
---|
138 | PCUT_ASSERT_NOT_NULL(va->mountp);
|
---|
139 | PCUT_ASSERT_TRUE(str_cmp(va->mountp, "/xyz") == 0);
|
---|
140 |
|
---|
141 | vol_volume_del_ref(va);
|
---|
142 |
|
---|
143 | vol_volumes_destroy(volumes);
|
---|
144 | rv = remove(fname);
|
---|
145 | PCUT_ASSERT_INT_EQUALS(0, rv);
|
---|
146 | free(fname);
|
---|
147 | }
|
---|
148 |
|
---|
149 | PCUT_EXPORT(volume);
|
---|