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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

//! Test utilities.

use super::FfiResult;
use repr_c::ReprC;
use std::fmt::Debug;
use std::os::raw::c_void;
use std::ptr;
use std::slice;
use std::sync::mpsc::{self, Sender};

/// User data wrapper.
pub struct UserData {
    /// Common field, used by standard callbacks.
    pub common: *mut c_void,
    /// Custom field, used by additional callbacks.
    pub custom: *mut c_void,
}

impl Default for UserData {
    fn default() -> Self {
        let common: *const c_void = ptr::null();
        let custom: *const c_void = ptr::null();

        UserData {
            common: common as *mut c_void,
            custom: custom as *mut c_void,
        }
    }
}

/// Convert a `UserData` to a void pointer which can be passed to ffi functions.
pub fn user_data_as_void(ud: &UserData) -> *mut c_void {
    let ptr: *const _ = ud;
    ptr as *mut c_void
}

/// Convert a `mpsc::Sender<T>` to a void ptr which is then stored in the `UserData` struct and
/// passed to ffi functions.
pub fn sender_as_user_data<T>(tx: &Sender<T>, ud: &mut UserData) -> *mut c_void {
    let ptr: *const _ = tx;
    ud.common = ptr as *mut c_void;
    user_data_as_void(ud)
}

/// Send through a `mpsc::Sender` pointed to by the user data's common pointer.
pub unsafe fn send_via_user_data<T>(user_data: *mut c_void, value: T)
where
    T: Send,
{
    let ud = user_data as *mut UserData;
    let tx = (*ud).common as *mut Sender<T>;
    unwrap!((*tx).send(value));
}

/// Send through a `mpsc::Sender` pointed to by the user data's custom pointer.
pub unsafe fn send_via_user_data_custom<T>(user_data: *mut c_void, value: T)
where
    T: Send,
{
    let ud = user_data as *mut UserData;
    let tx = (*ud).custom as *mut Sender<T>;
    unwrap!((*tx).send(value));
}

/// Call a FFI function and block until its callback gets called.
/// Use this if the callback accepts no arguments in addition to `user_data`
/// and `error_code`.
pub fn call_0<F>(f: F) -> Result<(), i32>
where
    F: FnOnce(*mut c_void, extern "C" fn(user_data: *mut c_void, result: *const FfiResult)),
{
    let mut ud = Default::default();
    call_0_with_custom(&mut ud, f)
}

/// Call a FFI function and block until its callback gets called.
/// Use this if the callback accepts no arguments in addition to `user_data`
/// and `error_code`.
/// This version of the function takes a `UserData` with custom inner data.
pub fn call_0_with_custom<F>(ud: &mut UserData, f: F) -> Result<(), i32>
where
    F: FnOnce(*mut c_void, extern "C" fn(user_data: *mut c_void, result: *const FfiResult)),
{
    let (tx, rx) = mpsc::channel::<i32>();
    f(sender_as_user_data(&tx, ud), callback_0);

    let error = unwrap!(rx.recv());
    if error == 0 {
        Ok(())
    } else {
        Err(error)
    }
}

/// Call an FFI function and block until its callback gets called, then return
/// the argument which were passed to that callback.
/// Use this if the callback accepts one argument in addition to `user_data`
/// and `error_code`.
pub unsafe fn call_1<F, E: Debug, T>(f: F) -> Result<T, i32>
where
    F: FnOnce(*mut c_void, extern "C" fn(user_data: *mut c_void, result: *const FfiResult, T::C)),
    T: ReprC<Error = E>,
{
    let mut ud = Default::default();
    call_1_with_custom(&mut ud, f)
}

/// Call an FFI function and block until its callback gets called, then return
/// the argument which were passed to that callback.
/// Use this if the callback accepts one argument in addition to `user_data`
/// and `error_code`.
/// This version of the function takes a `UserData` with custom inner data.
pub fn call_1_with_custom<F, E: Debug, T>(ud: &mut UserData, f: F) -> Result<T, i32>
where
    F: FnOnce(*mut c_void, extern "C" fn(user_data: *mut c_void, result: *const FfiResult, T::C)),
    T: ReprC<Error = E>,
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<T, i32>>>();
    f(sender_as_user_data(&tx, ud), callback_1::<E, T>);
    unwrap!(rx.recv()).0
}

/// Call a FFI function and block until its callback gets called, then return
/// the argument which were passed to that callback.
/// Use this if the callback accepts two arguments in addition to `user_data`
/// and `error_code`.
pub unsafe fn call_2<F, E0, E1, T0, T1>(f: F) -> Result<(T0, T1), i32>
where
    F: FnOnce(
        *mut c_void,
        extern "C" fn(user_data: *mut c_void, result: *const FfiResult, T0::C, T1::C),
    ),
    E0: Debug,
    E1: Debug,
    T0: ReprC<Error = E0>,
    T1: ReprC<Error = E1>,
{
    let mut ud = Default::default();
    call_2_with_custom(&mut ud, f)
}

/// Call a FFI function and block until its callback gets called, then return
/// the argument which were passed to that callback.
/// Use this if the callback accepts two arguments in addition to `user_data`
/// and `error_code`.
/// This version of the function takes a `UserData` with custom inner data.
pub unsafe fn call_2_with_custom<F, E0, E1, T0, T1>(
    ud: &mut UserData,
    f: F,
) -> Result<(T0, T1), i32>
where
    F: FnOnce(
        *mut c_void,
        extern "C" fn(user_data: *mut c_void, result: *const FfiResult, T0::C, T1::C),
    ),
    E0: Debug,
    E1: Debug,
    T0: ReprC<Error = E0>,
    T1: ReprC<Error = E1>,
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<(T0, T1), i32>>>();
    f(sender_as_user_data(&tx, ud), callback_2::<E0, E1, T0, T1>);
    unwrap!(rx.recv()).0
}

/// Call a FFI function and block until its callback gets called, then copy
/// the array argument which was passed to `Vec<T>` and return the result.
/// Use this if the callback accepts `*const T` and `usize` (length) arguments in addition
/// to `user_data` and `error_code`.
pub unsafe fn call_vec<F, E, T, U>(f: F) -> Result<Vec<T>, i32>
where
    F: FnOnce(
        *mut c_void,
        extern "C" fn(user_data: *mut c_void, result: *const FfiResult, T::C, usize),
    ),
    E: Debug,
    T: ReprC<C = *const U, Error = E>,
{
    let mut ud = Default::default();
    call_vec_with_custom(&mut ud, f)
}

/// Call a FFI function and block until its callback gets called, then copy
/// the array argument which was passed to `Vec<T>` and return the result.
/// Use this if the callback accepts `*const T` and `usize` (length) arguments in addition
/// to `user_data` and `error_code`.
/// This version of the function takes a `UserData` with custom inner data.
pub unsafe fn call_vec_with_custom<F, E, T, U>(ud: &mut UserData, f: F) -> Result<Vec<T>, i32>
where
    F: FnOnce(
        *mut c_void,
        extern "C" fn(user_data: *mut c_void, result: *const FfiResult, T::C, usize),
    ),
    E: Debug,
    T: ReprC<C = *const U, Error = E>,
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<Vec<T>, i32>>>();
    f(sender_as_user_data(&tx, ud), callback_vec::<E, T, U>);
    unwrap!(rx.recv()).0
}

/// Call a FFI function and block until its callback gets called, then copy
/// the byte array argument which was passed to `Vec<u8>` and return the result.
pub unsafe fn call_vec_u8<F>(f: F) -> Result<Vec<u8>, i32>
where
    F: FnOnce(
        *mut c_void,
        extern "C" fn(user_data: *mut c_void, result: *const FfiResult, *const u8, usize),
    ),
{
    let mut ud = Default::default();
    call_vec_u8_with_custom(&mut ud, f)
}

/// Call a FFI function and block until its callback gets called, then copy
/// the byte array argument which was passed to `Vec<u8>` and return the result.
/// This version of the function takes a `UserData` with custom inner data.
/// This version of the function takes a `UserData` with custom inner data.
pub unsafe fn call_vec_u8_with_custom<F>(ud: &mut UserData, f: F) -> Result<Vec<u8>, i32>
where
    F: FnOnce(
        *mut c_void,
        extern "C" fn(user_data: *mut c_void, result: *const FfiResult, *const u8, usize),
    ),
{
    let (tx, rx) = mpsc::channel::<Result<Vec<u8>, i32>>();
    f(sender_as_user_data(&tx, ud), callback_vec_u8);
    unwrap!(rx.recv())
}

extern "C" fn callback_0(user_data: *mut c_void, res: *const FfiResult) {
    unsafe { send_via_user_data(user_data, (*res).error_code) }
}

extern "C" fn callback_1<E, T>(user_data: *mut c_void, res: *const FfiResult, arg: T::C)
where
    E: Debug,
    T: ReprC<Error = E>,
{
    unsafe {
        let result: Result<T, i32> = if (*res).error_code == 0 {
            Ok(unwrap!(T::clone_from_repr_c(arg)))
        } else {
            Err((*res).error_code)
        };
        send_via_user_data(user_data, SendWrapper(result));
    }
}

extern "C" fn callback_2<E0, E1, T0, T1>(
    user_data: *mut c_void,
    res: *const FfiResult,
    arg0: T0::C,
    arg1: T1::C,
) where
    E0: Debug,
    E1: Debug,
    T0: ReprC<Error = E0>,
    T1: ReprC<Error = E1>,
{
    unsafe {
        let result: Result<(T0, T1), i32> = if (*res).error_code == 0 {
            Ok((
                unwrap!(T0::clone_from_repr_c(arg0)),
                unwrap!(T1::clone_from_repr_c(arg1)),
            ))
        } else {
            Err((*res).error_code)
        };
        send_via_user_data(user_data, SendWrapper(result))
    }
}

extern "C" fn callback_vec<E, T, U>(
    user_data: *mut c_void,
    res: *const FfiResult,
    array: *const U,
    size: usize,
) where
    E: Debug,
    T: ReprC<C = *const U, Error = E>,
{
    unsafe {
        let result: Result<Vec<T>, i32> = if (*res).error_code == 0 {
            let slice_ffi = slice::from_raw_parts(array, size);
            let mut vec = Vec::with_capacity(slice_ffi.len());
            for elt in slice_ffi {
                vec.push(unwrap!(T::clone_from_repr_c(elt)));
            }
            Ok(vec)
        } else {
            Err((*res).error_code)
        };

        send_via_user_data(user_data, SendWrapper(result))
    }
}

extern "C" fn callback_vec_u8(
    user_data: *mut c_void,
    res: *const FfiResult,
    ptr: *const u8,
    len: usize,
) {
    unsafe {
        let result = if (*res).error_code == 0 {
            Ok(slice::from_raw_parts(ptr, len).to_vec())
        } else {
            Err((*res).error_code)
        };

        send_via_user_data(user_data, result)
    }
}

/// Unsafe wrapper for passing non-Send types through mpsc channels.
/// Use with caution!
pub struct SendWrapper<T>(T);
unsafe impl<T> Send for SendWrapper<T> {}