mqi 0.3.0

Idiomatic IBM® MQ Interface (MQI) and MQ Administration Interface (MQAI) APIs
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
use std::{borrow::Cow, marker::PhantomData, num::NonZero, ptr};

use libmqm_default as default;
use libmqm_sys::{self as mq, Mqi};

use super::option;
use crate::{
    Connection, Library, MqFunctions,
    connection::AsConnection,
    constants,
    handle::{ConnectionHandle, MessageHandle},
    prelude::*,
    result::{Completion, Error, ResultComp, ResultCompErr, ResultErr},
    string::EncodedString,
    structs,
    traits::{Buffer, WriteRaw},
    types::{MQBMHO, MQBYTE, MQCHAR, MQCMHO, MQDMPO, MQIMPO, MQMHBO, MQSMPO, MQTYPE, MessageFormat},
    verb::MqInqError,
};

/// Manage message properties
///
/// This is equivalent of a [`MQHMSG`](mq::MQHMSG), with an associated connection.
#[derive(Debug)]
#[must_use]
pub struct Properties<C: AsConnection> {
    handle: MessageHandle,
    connection: C,
}

enum InqBuffer<'a, T> {
    Slice(&'a mut [T]),
    Owned(Vec<T>),
}

impl<T> InqBuffer<'_, T> {
    #[must_use]
    pub fn truncate(self, len: usize) -> Self {
        match self {
            Self::Slice(s) => {
                let buf_len = s.len();
                Self::Slice(&mut s[..std::cmp::min(len, buf_len)])
            }
            Self::Owned(mut v) => {
                v.truncate(len);
                Self::Owned(v)
            }
        }
    }
}

impl<T> AsRef<[T]> for InqBuffer<'_, T> {
    fn as_ref(&self) -> &[T] {
        match self {
            InqBuffer::Slice(s) => s,
            InqBuffer::Owned(o) => o,
        }
    }
}

impl<T> AsMut<[T]> for InqBuffer<'_, T> {
    fn as_mut(&mut self) -> &mut [T] {
        match self {
            InqBuffer::Slice(s) => s,
            InqBuffer::Owned(o) => o,
        }
    }
}

impl<'a, T: Clone> From<InqBuffer<'a, T>> for Cow<'a, [T]>
where
    [T]: ToOwned,
{
    fn from(value: InqBuffer<'a, T>) -> Self {
        match value {
            InqBuffer::Slice(s) => Cow::Borrowed(&*s),
            InqBuffer::Owned(o) => o.into(),
        }
    }
}

impl<'a, T: Clone> Buffer<'a, T> for InqBuffer<'a, T> {
    fn truncate(self, size: usize) -> Self {
        Self::truncate(self, size)
    }

    fn into_cow(self) -> Cow<'a, [T]> {
        self.into()
    }

    fn len(&self) -> usize {
        self.as_ref().len()
    }

    fn split_at(self, at: usize) -> (Self, Self) {
        match self {
            Self::Slice(s) => {
                let (head, tail) = s.split_at(at);
                (Self::Slice(head), Self::Slice(tail))
            }
            Self::Owned(v) => {
                let (head, tail) = v.split_at(at);
                (Self::Owned(head), Self::Owned(tail))
            }
        }
    }
}

impl<C: AsConnection> Drop for Properties<C> {
    fn drop(&mut self) {
        let mqdmho = default::MQDMHO_DEFAULT;

        if self.handle.is_deleteable() {
            let Connection { mq, handle, .. } = self.connection.as_connection();
            let _ = mq.mqdltmh(Some(*handle), &mut self.handle, &mqdmho);
        }
    }
}

#[expect(clippy::too_many_arguments)]
unsafe fn inqmp<'a, 'b, A: Library<MQ: Mqi>>(
    mq: &MqFunctions<A>,
    connection_handle: Option<ConnectionHandle>,
    message_handle: &MessageHandle,
    mqimpo: &mut structs::MQIMPO,
    name: &structs::MQCHARV,
    mqpd: &mut structs::MQPD,
    value_type: &mut MQTYPE,
    mut value: InqBuffer<'a, u8>,
    max_value_size: Option<NonZero<usize>>,
    mut returned_name: Option<InqBuffer<'b, MQCHAR>>,
    max_name_size: Option<NonZero<usize>>,
) -> ResultCompErr<(InqBuffer<'a, u8>, Option<InqBuffer<'b, MQCHAR>>), MqInqError> {
    if let Some(rn) = returned_name.as_mut() {
        let rn_ref = rn.as_mut();
        mqimpo.ReturnedName.VSPtr = rn_ref.as_mut_ptr().cast();
        mqimpo.ReturnedName.VSBufSize = rn_ref.len().try_into().expect("length should convert to usize");
    } else {
        mqimpo.ReturnedName.VSPtr = ptr::null_mut();
    }

    match (
        unsafe {
            mq.mqinqmp(
                connection_handle,
                message_handle,
                mqimpo,
                name,
                mqpd,
                value_type,
                Some(value.as_mut()),
            )
        },
        returned_name,
    ) {
        (Err(MqInqError::Length(length, Error(.., constants::MQRC_PROPERTY_VALUE_TOO_BIG))), rn)
            if max_value_size.is_none_or(|max_len| Into::<usize>::into(max_len) > value.len()) =>
        {
            let len = length.try_into().expect("length should convert to usize");
            let value_vec = InqBuffer::Owned(vec![0; len]);
            unsafe {
                inqmp(
                    mq,
                    connection_handle,
                    message_handle,
                    mqimpo,
                    name,
                    mqpd,
                    value_type,
                    value_vec,
                    max_value_size,
                    rn,
                    max_name_size,
                )
            }
        }
        (Err(MqInqError::Length(length, Error(.., constants::MQRC_PROPERTY_NAME_TOO_BIG))), Some(rn))
            if max_name_size.is_none_or(|max_len| Into::<usize>::into(max_len) > rn.len()) =>
        {
            let len = length.try_into().expect("length should convert to usize");
            let name_vec = InqBuffer::Owned(vec![0; len]);
            unsafe {
                inqmp(
                    mq,
                    connection_handle,
                    message_handle,
                    mqimpo,
                    name,
                    mqpd,
                    value_type,
                    value,
                    max_value_size,
                    Some(name_vec),
                    max_name_size,
                )
            }
        }
        (other, rn) => other.map_completion(|length| {
            (
                value.truncate(length.try_into().expect("length should convert to usize")),
                rn.map(|name| {
                    name.truncate(
                        mqimpo
                            .ReturnedName
                            .VSLength
                            .try_into()
                            .expect("length should convert to usize"),
                    )
                }),
            )
        }),
    }
}

pub struct MsgPropIter<'name, 'message, P, N: EncodedString + ?Sized, C: AsConnection> {
    name: &'name N,
    message: &'message Properties<C>,
    options: MQIMPO,
    _marker: PhantomData<P>,
}

impl<P: option::PropertyValue, N: EncodedString + ?Sized, C: AsConnection> Iterator for MsgPropIter<'_, '_, P, N, C> {
    type Item = ResultCompErr<P, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        let result = match self.message.property::<P>(self.name, self.options) {
            Ok(Completion(Some(value), warning)) => Some(Ok(Completion(value, warning))),
            Ok(Completion(None, _)) => None,
            Err(e) => Some(Err(e)),
        };

        self.options.insert(constants::MQIMPO_INQ_NEXT);

        result
    }
}

impl<C: AsConnection> Properties<C> {
    pub const fn handle(&self) -> &MessageHandle {
        &self.handle
    }

    /// This function uses the [`MQCRTMH`](libmqm_sys::MQCRTMH) MQ API function.
    pub fn new(connection: C, options: MQCMHO) -> ResultErr<Self> {
        let mqcmho = mq::MQCMHO {
            Options: options.0,
            ..default::MQCMHO_DEFAULT
        };
        let conn = connection.as_connection();
        conn.mq
            .mqcrtmh(Some(conn.handle), &mqcmho)
            .map(|handle| Self { handle, connection })
    }

    pub fn property_iter<'message, 'name, P, N>(
        &'message self,
        name: &'name N,
        options: MQIMPO,
    ) -> MsgPropIter<'name, 'message, P, N, C>
    where
        P: option::PropertyValue,
        N: EncodedString + ?Sized,
    {
        MsgPropIter {
            name,
            message: self,
            options: options | constants::MQIMPO_INQ_NEXT,
            _marker: PhantomData,
        }
    }

    /// This function uses the [`MQINQMP`](libmqm_sys::MQINQMP) MQ API function.
    pub fn property<P>(&self, name: &(impl EncodedString + ?Sized), options: MQIMPO) -> ResultCompErr<Option<P>, Error>
    where
        P: option::PropertyValue,
    {
        const DEFAULT_BUF_SIZE: usize = 1024;
        let mut val_return_buffer = [0; DEFAULT_BUF_SIZE]; // Returned value buffer
        let mut name_return_buffer = [0; DEFAULT_BUF_SIZE]; // Returned name buffer

        let mut property_not_available = false;

        let mut param = option::PropertyParam {
            impo: structs::MQIMPO::new(mq::MQIMPO {
                Options: options.0,
                ..default::MQIMPO_DEFAULT
            }),
            value_type: MQTYPE::default(),
            mqpd: structs::MQPD::new(default::MQPD_DEFAULT),
            name_required: option::NameUsage::default(),
        };

        let mut inq_value_buffer = InqBuffer::Slice(val_return_buffer.as_mut_slice());
        inq_value_buffer = match P::max_value_size() {
            Some(max_size) => inq_value_buffer.truncate(max_size.into()),
            None => inq_value_buffer,
        };
        let name = structs::MQCHARV::from_encoded_str(name);

        let result = P::property_consume(&mut param, |param| {
            let mut inq_name_buffer = match param.name_required {
                option::NameUsage::Ignored => None,
                used => {
                    let buf = InqBuffer::Slice(name_return_buffer.as_mut_slice());
                    Some(match used {
                        option::NameUsage::MaxLength(length) => buf.truncate(length.into()),
                        _ => buf,
                    })
                }
            };
            param.impo.ReturnedName = inq_name_buffer.as_mut().map_or(default::MQCHARV_DEFAULT, |name| mq::MQCHARV {
                VSPtr: (&raw mut *name).cast(),
                VSBufSize: name
                    .as_ref()
                    .len()
                    .try_into()
                    .expect("length of buffer should fit within MQLONG range"),
                ..default::MQCHARV_DEFAULT
            });

            let conn = self.connection.as_connection();
            let mqi_inqmp = unsafe {
                inqmp(
                    &conn.mq,
                    Some(conn.handle),
                    &self.handle,
                    &mut param.impo,
                    &name,
                    &mut param.mqpd,
                    &mut param.value_type,
                    inq_value_buffer,
                    P::max_value_size(),
                    inq_name_buffer,
                    param.name_required.into(),
                )
                .map_err(Into::into) // Convert the error into an ordinary MQ error
                .map_completion(|(value, name)| option::PropertyState {
                    name: name.map(Into::into),
                    value: value.into(),
                })
            };

            property_not_available = mqi_inqmp
                .as_ref()
                .is_err_and(|e| matches!(e, &Error(constants::MQCC_FAILED, .., constants::MQRC_PROPERTY_NOT_AVAILABLE)));

            mqi_inqmp
        });

        if property_not_available {
            Ok(Completion::new(None))
        } else {
            result.map_completion(Some).map_err(Into::into)
        }
    }

    /// This function uses the [`MQDLTMP`](libmqm_sys::MQDLTMP) MQ API function.
    pub fn delete_property(&self, name: &(impl EncodedString + ?Sized), options: MQDMPO) -> ResultComp<()> {
        let mut mqdmpo = structs::MQDMPO::new(default::MQDMPO_DEFAULT);
        *mqdmpo.Options.as_mut() = options;

        let name_mqcharv = structs::MQCHARV::from_encoded_str(name);

        let conn = self.connection.as_connection();
        conn.mq.mqdltmp(Some(conn.handle), &self.handle, &mqdmpo, &name_mqcharv)
    }

    /// This function uses the [`MQSETMP`](libmqm_sys::MQSETMP) MQ API function.
    pub fn set_property(
        &self,
        name: &(impl EncodedString + ?Sized),
        value: &(impl option::SetProperty + ?Sized),
        location: MQSMPO,
    ) -> ResultComp<()> {
        let mut mqpd = structs::MQPD::new(default::MQPD_DEFAULT);
        let mut mqsmpo = structs::MQSMPO::new(default::MQSMPO_DEFAULT);
        *mqsmpo.Options.as_mut() = location;
        let (data, value_type) = value.apply_mqsetmp(&mut mqpd, &mut mqsmpo);
        assert!(mqpd.Version <= mq::MQPD_CURRENT_VERSION);
        assert!(mqsmpo.Version <= mq::MQSMPO_CURRENT_VERSION);

        let name_mqcharv = structs::MQCHARV::from_encoded_str(name);
        let conn = self.connection.as_connection();
        // SAFETY: The name MQCHARV formed from reference
        unsafe {
            conn.mq.mqsetmp(
                Some(conn.handle),
                &self.handle,
                &mqsmpo,
                &name_mqcharv,
                &mut mqpd,
                value_type,
                data,
            )
        }
    }

    /// This function uses the [`MQDLTMH`](libmqm_sys::MQDLTMH) MQ API function.
    pub fn close(self) -> ResultErr<()> {
        let mut s = self;
        let mqdmho = default::MQDMHO_DEFAULT;
        let conn = s.connection.as_connection();
        conn.mq.mqdltmh(Some(conn.handle), &mut s.handle, &mqdmho)
    }

    /// This function uses the [`MQMHBUF`](libmqm_sys::MQMHBUF) MQ API function.
    pub fn to_buffer<'a, A: Buffer<'a, impl WriteRaw<MQBYTE>>>(
        &self,
        name: &(impl EncodedString + ?Sized),
        options: MQMHBO,
        buffer: A,
    ) -> ResultCompErr<(MessageFormat, A), MqInqError> {
        let read_only_options = options - constants::MQMHBO_DELETE_PROPERTIES;
        let mut buf = buffer;
        let mut mhbo = structs::MQMHBO::new(default::MQMHBO_DEFAULT);
        *mhbo.Options.as_mut() = read_only_options;
        let mut mqmd = structs::MQMD::new(default::MQMD_DEFAULT);
        let name_mqcharv = structs::MQCHARV::from_encoded_str(name);

        let conn = self.connection.as_connection();
        // SAFETY: The name MQCHARV formed from references
        unsafe {
            conn.mq
                .mqmhbuf(
                    Some(conn.handle),
                    self.handle(),
                    &mhbo,
                    &name_mqcharv,
                    &mut *mqmd,
                    buf.as_mut(),
                )
                .map_completion(|len| {
                    (
                        MessageFormat::from_mqmd(&mqmd),
                        buf.truncate(len.try_into().expect("length should convert to usize")),
                    )
                })
        }
    }

    /// This function uses the [`MQMHBUF`](libmqm_sys::MQMHBUF) MQ API function.
    pub fn to_buffer_mut<'a, A: Buffer<'a, impl WriteRaw<MQBYTE>>>(
        &mut self,
        name: &(impl EncodedString + ?Sized),
        options: MQMHBO,
        buffer: A,
    ) -> ResultCompErr<(MessageFormat, A), MqInqError> {
        let mut buf = buffer;
        let mhbo = structs::MQMHBO::new(mq::MQMHBO {
            Options: options.0,
            ..default::MQMHBO_DEFAULT
        });
        let mut mqmd = structs::MQMD::new(default::MQMD_DEFAULT);
        let name_mqcharv = structs::MQCHARV::from_encoded_str(name);

        let conn = self.connection.as_connection();
        // SAFETY: The name MQCHARV is formed from references
        unsafe {
            conn.mq
                .mqmhbuf(
                    Some(conn.handle),
                    self.handle(),
                    &mhbo,
                    &name_mqcharv,
                    &mut *mqmd,
                    buf.as_mut(),
                )
                .map_completion(|len| {
                    (
                        MessageFormat::from_mqmd(&mqmd),
                        buf.truncate(len.try_into().expect("length should convert to usize")),
                    )
                })
        }
    }

    /// This function uses the [`MQBUFMH`](libmqm_sys::MQBUFMH) MQ API function.
    pub fn from_buffer(&mut self, options: MQBMHO, format: &MessageFormat, buffer: &[MQBYTE]) -> ResultComp<()> {
        // Drop the delete properties option as this fn does not modify the buffer
        let options_read_only = options - constants::MQBMHO_DELETE_PROPERTIES;
        let mut mqmd = format.into_mqmd2();
        let bmho = structs::MQBMHO::new(mq::MQBMHO {
            Options: options_read_only.0,
            ..default::MQBMHO_DEFAULT
        });

        let conn = self.connection.as_connection();
        conn.mq
            .mqbufmh(Some(conn.handle), &self.handle, &bmho, &mut *mqmd, buffer)
            .map_completion(|_| {})
    }

    /// This function uses the [`MQBUFMH`](libmqm_sys::MQBUFMH) MQ API function.
    pub fn from_buffer_mut<'a>(
        &mut self,
        options: MQBMHO,
        format: &MessageFormat,
        buffer: &'a mut [MQBYTE],
    ) -> ResultComp<(MessageFormat, &'a [MQBYTE])> {
        let mut mqmd = format.into_mqmd2();
        let bmho = structs::MQBMHO::new(mq::MQBMHO {
            Options: options.0,
            ..default::MQBMHO_DEFAULT
        });

        let conn = self.connection.as_connection();
        conn.mq
            .mqbufmh(Some(conn.handle), &self.handle, &bmho, &mut *mqmd, buffer)
            .map_completion(|len| {
                (
                    MessageFormat::from_mqmd(&mqmd),
                    &buffer[..len.try_into().expect("length should convert to usize")],
                )
            })
    }
}

#[cfg(test)]
#[cfg(feature = "mock")]
#[cfg_attr(coverage_nightly, coverage(off))]
mod test {
    use std::{error::Error, rc::Rc};

    use libmqm_sys::mock::MockMq;

    use super::*;
    use crate::{
        Connection,
        connection::ThreadNone,
        constants::{
            self, MQCC_FAILED, MQCC_OK, MQRC_CALL_IN_PROGRESS, MQRC_NONE, MQRC_PROPERTY_NAME_TOO_BIG,
            MQRC_PROPERTY_NOT_AVAILABLE, MQRC_PROPERTY_VALUE_TOO_BIG, MQTYPE_BYTE_STRING,
        },
        header::{TextEnc, fmt::MQFMT_NONE},
        properties::Name,
        result::ResultErr,
        string::CCSID,
        test::mock,
        types::{MQCC, MQRC, MessageFormat},
    };

    /// Set up mocks for the MQINQMP function
    fn mqinqmp(mock_list: &[(&str, MQTYPE, &[u8], MQCC, MQRC)]) -> impl Fn(&mut MockMq) {
        |mock_library| {
            let mut seq = mockall::Sequence::new();
            mock::properties_ok(mock_library, 0xf0f0, 1, &mut seq);

            for (name, data_typ, data, cc, rc) in mock_list.iter().copied() {
                let data = data.to_owned();
                let name = name.to_owned();
                mock_library.expect_MQINQMP().once().in_sequence(&mut seq).returning(
                    move |_, _, mqimpo, _, _, typ, value_length, value, real_length, comp_code, reason| {
                        let mut_real_length = &mut *real_length;
                        let maybe_name_mqcharv: Option<&mut mq::MQCHARV> =
                            unsafe { mqimpo.ReturnedName.VSPtr.as_mut().map(|_| &mut mqimpo.ReturnedName) };

                        // Set the returned real length
                        *mut_real_length = data.len().try_into().expect("i32 in range of usize");

                        // Copy the supplied name
                        if let Some(mut_name_mqcharv) = maybe_name_mqcharv {
                            let name_copied_length = std::cmp::min(
                                mut_name_mqcharv.VSBufSize.try_into().expect("i32 in range of usize"),
                                name.len(),
                            );
                            let mut_name: &mut [u8] =
                                unsafe { std::slice::from_raw_parts_mut(mut_name_mqcharv.VSPtr.cast(), name_copied_length) };
                            mut_name.copy_from_slice(&name.as_bytes()[..name_copied_length]);
                            mut_name_mqcharv.VSCCSID = 1208;
                            mut_name_mqcharv.VSLength = name_copied_length.try_into().expect("i32 in range of usize");
                        }

                        // Copy the supplied data into the returned value
                        let copied_length = std::cmp::min(*mut_real_length, value_length)
                            .try_into()
                            .expect("i32 in range of usize");
                        let mut_value: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(value.cast(), copied_length) };
                        mut_value.copy_from_slice(&data[..copied_length]);

                        // Set the data type
                        *typ.as_mut() = data_typ;

                        mock::mqi_outcome(comp_code, reason, cc, rc);
                    },
                );
            }
        }
    }

    fn mqmhbuf(mock_data: &'static [u8]) -> impl Fn(&mut MockMq) {
        |mock_library| {
            let mut seq = mockall::Sequence::new();
            mock::properties_ok(mock_library, 0xf0f0, 1, &mut seq);
            mock_library
                .expect_MQMHBUF()
                .returning(|_, _, _, _, _, buf_len, buf_target, data_len, cc, rc| {
                    unsafe { mock::copy_to_mq_data(mock_data, buf_len, buf_target, data_len) };
                    mock::mqi_outcome_ok(cc, rc);
                })
                .once()
                .in_sequence(&mut seq);
        }
    }

    fn mqbufmh(mock_library: &mut MockMq) {
        let mut seq = mockall::Sequence::new();

        mock::properties_ok(mock_library, 0xf0f0, 1, &mut seq);
        mock_library
            .expect_MQBUFMH()
            .returning(|_, _, _, _, buffer_len, _, data_len, cc, rc| {
                *data_len = buffer_len;
                mock::mqi_outcome_ok(cc, rc);
            })
            .once()
            .in_sequence(&mut seq);
    }

    fn properties_mocked(m: impl Fn(&mut MockMq)) -> ResultErr<Properties<Connection<Rc<MockMq>, ThreadNone>>> {
        Properties::new(mock::connect_ok(m), constants::MQCMHO_NONE)
    }

    #[test]
    fn property() -> Result<(), Box<dyn Error>> {
        // Test basic succesful retrieval of a byte property
        let value: Option<Vec<u8>> = properties_mocked(mqinqmp(&[("name", MQTYPE_BYTE_STRING, b"data", MQCC_OK, MQRC_NONE)]))?
            .property("_", MQIMPO::default())
            .warn_as_error()?;
        assert_ne!(value, None);

        // Property does not exist
        let value: Option<Vec<u8>> =
            properties_mocked(mqinqmp(&[("", MQTYPE(0), b"", MQCC_FAILED, MQRC_PROPERTY_NOT_AVAILABLE)]))?
                .property("_", MQIMPO::default())
                .warn_as_error()?;
        assert_eq!(value, None);

        // Failure response
        let result: ResultErr<Option<Vec<u8>>> =
            properties_mocked(mqinqmp(&[("", MQTYPE(0), b"", MQCC_FAILED, MQRC_CALL_IN_PROGRESS)]))?
                .property("_", MQIMPO::default())
                .warn_as_error();
        assert!(result.is_err_and(|err| matches!(err, Error(MQCC_FAILED, _, MQRC_CALL_IN_PROGRESS))));

        // Value too big for initial buffer - mqinqmp should be called twice
        let value: Option<Vec<u8>> = properties_mocked(mqinqmp(&[
            ("name", MQTYPE_BYTE_STRING, b"data", MQCC_FAILED, MQRC_PROPERTY_VALUE_TOO_BIG),
            ("name", MQTYPE_BYTE_STRING, b"data", MQCC_OK, MQRC_NONE),
        ]))?
        .property("_", MQIMPO::default())
        .warn_as_error()?;
        assert_ne!(value, None);

        // Value too big for initial buffer - mqinqmp should be called twice
        let value: Option<(Vec<u8>, Name<String>)> = properties_mocked(mqinqmp(&[
            ("name", MQTYPE_BYTE_STRING, b"data", MQCC_FAILED, MQRC_PROPERTY_NAME_TOO_BIG),
            ("name", MQTYPE_BYTE_STRING, b"data", MQCC_OK, MQRC_NONE),
        ]))?
        .property("_", MQIMPO::default())
        .warn_as_error()?;
        assert_ne!(value, None);

        // Name and value too big for initial buffer - mqinqmp should be called three times
        let value: Option<(Vec<u8>, Name<String>)> = properties_mocked(mqinqmp(&[
            ("name", MQTYPE_BYTE_STRING, b"data", MQCC_FAILED, MQRC_PROPERTY_VALUE_TOO_BIG),
            ("name", MQTYPE_BYTE_STRING, b"data", MQCC_FAILED, MQRC_PROPERTY_NAME_TOO_BIG),
            ("name", MQTYPE_BYTE_STRING, b"data", MQCC_OK, MQRC_NONE),
        ]))?
        .property("_", MQIMPO::default())
        .warn_as_error()?;
        assert_ne!(value, None);

        Ok(())
    }

    #[test]
    fn to_buffer() -> Result<(), Box<dyn Error>> {
        const MOCK_DATA: &[u8] = b"MOCK";

        let mut prop = properties_mocked(mqmhbuf(MOCK_DATA))?;
        let buffer = vec![0u8; usize::pow(2, 16)]; // 64k
        let (_, prop_buffer) = prop.to_buffer_mut("%", constants::MQMHBO_NONE, buffer).warn_as_error()?;
        assert_eq!(MOCK_DATA, prop_buffer);

        let prop = properties_mocked(mqmhbuf(MOCK_DATA))?;
        let buffer = vec![0u8; usize::pow(2, 16)]; // 64k
        let (_, prop_buffer) = prop.to_buffer("%", constants::MQMHBO_NONE, buffer).warn_as_error()?;
        assert_eq!(MOCK_DATA, prop_buffer);

        Ok(())
    }

    #[test]
    fn from_buffer() -> Result<(), Box<dyn Error>> {
        const MOCK_DATA: &[u8] = b"MOCK_FROM_BUFFER";
        const MOCK_MF: &MessageFormat = &MessageFormat {
            ccsid: CCSID(1208),
            encoding: constants::MQENC_NATIVE,
            fmt: TextEnc::Ascii(MQFMT_NONE),
        };

        let mut prop = properties_mocked(mqbufmh)?;
        let mut buffer_clone = MOCK_DATA.to_owned();
        let (same_format, same_buffer) = prop
            .from_buffer_mut(constants::MQBMHO_NONE, MOCK_MF, &mut buffer_clone)
            .warn_as_error()?;
        assert_eq!(same_buffer, MOCK_DATA);
        assert_eq!(&same_format, MOCK_MF);

        let mut prop = properties_mocked(mqbufmh)?;
        prop.from_buffer(constants::MQBMHO_NONE, MOCK_MF, MOCK_DATA).warn_as_error()?;

        Ok(())
    }
}