Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2020, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. 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 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : : #include <stddef.h>
31 : : #include <stdarg.h>
32 : : #include <setjmp.h>
33 : : #include <cmocka.h>
34 : : #include <errno.h>
35 : : #include <wrappers.h>
36 : :
37 : : #include "test-suites.h"
38 : : #include "libvhd.h"
39 : : #include "vhd-wrappers.h"
40 : :
41 : : #define RAW_PRT_ARGS_SIZE 9
42 : :
43 : : char* raw_prt_args[RAW_PRT_ARGS_SIZE] = {
44 : : "--debug",
45 : : "-n",
46 : : "test1.vhdcache",
47 : : "-p",
48 : : "test2.vhdcache",
49 : : "-S",
50 : : "71680",
51 : : "-e",
52 : : "-m"};
53 : :
54 : : #define ARGS_SIZE 8
55 : :
56 : : char* args[ARGS_SIZE] = {
57 : : "--debug",
58 : : "-n",
59 : : "test1.vhdcache",
60 : : "-p",
61 : : "test2.vhdcache",
62 : : "-S",
63 : : "71680",
64 : : "-e"};
65 : :
66 : : /*
67 : : * Tests to ensure errors are propagated by vhd-util-snapshot.
68 : : */
69 : 1 : void test_vhd_util_snapshot_enospc_from_vhd_snapshot(void **state)
70 : : {
71 : 1 : will_return(__wrap_canonpath, "testing");
72 : 1 : will_return(__wrap_vhd_snapshot, ENOSPC);
73 : 1 : expect_any(__wrap_free, in);
74 : 1 : int res = vhd_util_snapshot(RAW_PRT_ARGS_SIZE, raw_prt_args);
75 : 1 : assert_int_equal(get_close_count(), 0);
76 : 1 : assert_int_equal(res, ENOSPC);
77 : 1 : }
78 : :
79 : 1 : void test_vhd_util_snapshot_einval_from_vhd_open(void **state)
80 : : {
81 : 1 : will_return(__wrap_canonpath, "testing");
82 : 1 : will_return(__wrap_vhd_snapshot, 0);
83 : 1 : will_return(__wrap_vhd_open, EINVAL);
84 : 1 : expect_any(__wrap_free, in);
85 : 1 : int res = vhd_util_snapshot(ARGS_SIZE, args);
86 : 1 : assert_int_equal(get_close_count(), 0);
87 : 1 : assert_int_equal(res, EINVAL);
88 : 1 : }
89 : :
90 : 1 : void test_vhd_util_snapshot_einval_from_vhd_get_keyhash(void **state)
91 : : {
92 : 1 : will_return(__wrap_canonpath, "testing");
93 : 1 : will_return(__wrap_vhd_snapshot, 0);
94 : 1 : will_return(__wrap_vhd_open, 0);
95 : 1 : will_return(__wrap_vhd_get_keyhash, EINVAL);
96 : 1 : will_return(__wrap_vhd_close, 0);
97 : 1 : expect_any(__wrap_vhd_close, ctx);
98 : 1 : expect_any(__wrap_free, in);
99 : 1 : int res = vhd_util_snapshot(ARGS_SIZE, args);
100 : 1 : assert_int_equal(get_close_count(), 1);
101 : 1 : assert_int_equal(res, EINVAL);
102 : 1 : }
103 : :
104 : 1 : void test_vhd_util_snapshot_einval_from_vhd_open_cookie(void **state)
105 : : {
106 : 1 : will_return(__wrap_canonpath, "testing");
107 : 1 : will_return(__wrap_vhd_snapshot, 0);
108 : 1 : set_cookie();
109 : 1 : will_return(__wrap_vhd_get_keyhash, 0);
110 : 1 : will_return(__wrap_vhd_close, 0);
111 : 1 : expect_any(__wrap_vhd_close, ctx);
112 : 1 : int err[] = {0, EINVAL};
113 : 1 : set_open_errors(2, err);
114 : 1 : expect_any(__wrap_free, in);
115 : 1 : int res = vhd_util_snapshot(ARGS_SIZE, args);
116 : 1 : assert_int_equal(get_close_count(), 1);
117 : 1 : assert_int_equal(res, EINVAL);
118 : 1 : }
119 : :
120 : 1 : void test_vhd_util_snapshot_einval_from_vhd_set_keyhash(void **state)
121 : : {
122 : 1 : will_return(__wrap_canonpath, "testing");
123 : 1 : will_return(__wrap_vhd_snapshot, 0);
124 : 1 : will_return_always(__wrap_vhd_open, 0);
125 : 1 : set_cookie();
126 : 1 : will_return(__wrap_vhd_get_keyhash, 0);
127 : 1 : will_return_always(__wrap_vhd_close, 0);
128 : 1 : will_return(__wrap_vhd_set_keyhash, EINVAL);
129 : 1 : expect_any_count(__wrap_vhd_close, ctx, 2);
130 : 1 : expect_any(__wrap_free, in);
131 : 1 : int res = vhd_util_snapshot(ARGS_SIZE, args);
132 : 1 : assert_int_equal(get_close_count(), 2);
133 : 1 : assert_int_equal(res, EINVAL);
134 : 1 : }
|