This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
flush-new.c
162 lines (140 loc) · 3.85 KB
/
flush-new.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020-2021, Intel Corporation */
/*
* flush-new.c -- unit tests of the flush module
*
* APIs covered:
* - rpma_flush_new
* - rpma_flush_delete
*/
#include "cmocka_headers.h"
#include "flush.h"
#include "flush-common.h"
#include "mocks-stdlib.h"
#include "test-common.h"
#include <sys/mman.h>
/*
* new__malloc_ENOMEM -- malloc() fail with ENOMEM
*/
static void
new__malloc_ENOMEM(void **unused)
{
/* configure mocks */
will_return(__wrap__test_malloc, ENOMEM);
/* run test */
struct rpma_flush *flush = NULL;
int ret = rpma_flush_new(MOCK_PEER, &flush);
/* verify the results */
assert_int_equal(ret, RPMA_E_NOMEM);
assert_null(flush);
}
/*
* new__apm_sysconf_EINVAL -- sysconf() fail with EINVAL
*/
static void
new__apm_sysconf_EINVAL(void **unused)
{
/* configure mocks */
will_return_always(__wrap__test_malloc, MOCK_OK);
will_return(__wrap_sysconf, EINVAL);
/* run test */
struct rpma_flush *flush = NULL;
int ret = rpma_flush_new(MOCK_PEER, &flush);
/* verify the results */
assert_int_equal(ret, RPMA_E_PROVIDER);
assert_null(flush);
}
/*
* new__apm_posix_memalign_ENOMEM -- malloc() fail with ENOMEM
*/
static void
new__apm_posix_memalign_ENOMEM(void **unused)
{
/* configure mocks */
will_return_always(__wrap__test_malloc, MOCK_OK);
will_return(__wrap_sysconf, MOCK_OK);
will_return(__wrap_mmap, MAP_FAILED);
/* run test */
struct rpma_flush *flush = NULL;
int ret = rpma_flush_new(MOCK_PEER, &flush);
/* verify the results */
assert_int_equal(ret, RPMA_E_NOMEM);
assert_null(flush);
}
/*
* new__apm_mr_reg_RPMA_E_NOMEM -- malloc() fail with ENOMEM
*/
static void
new__apm_mr_reg_RPMA_E_NOMEM(void **unused)
{
/* configure mocks */
will_return_always(__wrap__test_malloc, MOCK_OK);
will_return(__wrap_sysconf, MOCK_OK);
will_return(__wrap_mmap, MOCK_OK);
struct mmap_args allocated_raw = {0};
will_return(__wrap_mmap, &allocated_raw);
expect_value(rpma_mr_reg, peer, MOCK_PEER);
expect_value(rpma_mr_reg, size, 8);
expect_value(rpma_mr_reg, usage, RPMA_MR_USAGE_READ_DST);
will_return(rpma_mr_reg, &allocated_raw.ptr);
will_return(rpma_mr_reg, NULL);
will_return(rpma_mr_reg, RPMA_E_NOMEM);
/* run test */
struct rpma_flush *flush = NULL;
int ret = rpma_flush_new(MOCK_PEER, &flush);
/* verify the results */
assert_int_equal(ret, RPMA_E_NOMEM);
assert_null(flush);
}
/*
* new__apm_malloc_ENOMEM -- malloc() fail with ENOMEM
*/
static void
new__apm_malloc_ENOMEM(void **unused)
{
/* configure mocks */
will_return(__wrap__test_malloc, MOCK_OK);
will_return(__wrap_sysconf, MOCK_OK);
will_return(__wrap_mmap, MOCK_OK);
struct mmap_args allocated_raw = {0};
will_return(__wrap_mmap, &allocated_raw);
expect_value(rpma_mr_reg, peer, MOCK_PEER);
expect_value(rpma_mr_reg, size, 8);
expect_value(rpma_mr_reg, usage, RPMA_MR_USAGE_READ_DST);
will_return(rpma_mr_reg, &allocated_raw.ptr);
will_return(rpma_mr_reg, MOCK_RPMA_MR_LOCAL);
will_return(__wrap__test_malloc, ENOMEM);
expect_value(rpma_mr_dereg, *mr_ptr, MOCK_RPMA_MR_LOCAL);
/* run test */
struct rpma_flush *flush = NULL;
int ret = rpma_flush_new(MOCK_PEER, &flush);
/* verify the results */
assert_int_equal(ret, RPMA_E_NOMEM);
assert_null(flush);
}
/*
* new__apm_success -- happy day scenario
*/
static void
new__apm_success(void **unused)
{
/*
* The thing is done by setup__flush_new()
* and teardown__flush_delete().
*/
}
int
main(int argc, char *argv[])
{
const struct CMUnitTest tests[] = {
/* rpma__new() unit tests */
cmocka_unit_test(new__malloc_ENOMEM),
cmocka_unit_test(new__apm_sysconf_EINVAL),
cmocka_unit_test(new__apm_posix_memalign_ENOMEM),
cmocka_unit_test(new__apm_mr_reg_RPMA_E_NOMEM),
cmocka_unit_test(new__apm_malloc_ENOMEM),
cmocka_unit_test_setup_teardown(new__apm_success,
setup__flush_new, teardown__flush_delete),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}