xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/send_xdr_encoding/send_xdr_encoding.kshlib (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1#!/bin/ksh
2# SPDX-License-Identifier: CDDL-1.0
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# https://opensource.org/license/CDDL-1.0.
12#
13
14#
15# Copyright (c) 2026 by Garth Snyder. All rights reserved.
16#
17
18. $STF_SUITE/include/libtest.shlib
19. $STF_SUITE/tests/functional/send_xdr_encoding/send_xdr_encoding.cfg
20
21#
22# Verify that the DRR_BEGIN records in the given send stream encode their
23# nvlist payloads with NV_ENCODE_XDR (and not NV_ENCODE_NATIVE).
24#
25# DRR_BEGIN records that carry an nvlist payload (raw sends, redacted sends,
26# resumed sends, and combinations thereof) must encode that payload with
27# NV_ENCODE_XDR so the resulting stream can be portably consumed across
28# endianness. Encoding the payload with NV_ENCODE_NATIVE produces a stream
29# that is unreadable on a receiver of the opposite endianness.
30#
31# zstream dump prints a single "nvlist encoding = ..." line per DRR_BEGIN
32# record that carries an nvlist payload. The possible values are:
33#
34#     NV_ENCODE_XDR
35#     NV_ENCODE_NATIVE (big-endian)
36#     NV_ENCODE_NATIVE (little-endian)
37#
38# Every test in this suite generates a stream whose DRR_BEGIN record
39# carries an nvlist payload, so the pass criterion is:
40#
41#   - At least one NV_ENCODE_XDR line appears, AND
42#   - No NV_ENCODE_NATIVE line appears.
43#
44# Requiring at least one XDR line catches the case where zstream dump
45# itself fails before producing any encoding output. Asserting on dump
46# content rather than dump exit status means a partial dump can still
47# fail the test on an NV_ENCODE_NATIVE seen before the failure point.
48#
49function verify_xdr_nvlist_encoding
50{
51	typeset stream=$1
52	typeset out
53
54	[[ -f "$stream" ]] || \
55	    log_fail "verify_xdr_nvlist_encoding: stream not found: $stream"
56
57	out=$(zstream dump "$stream" 2>/dev/null)
58
59	if echo "$out" | grep -q 'NV_ENCODE_NATIVE'; then
60		log_fail "verify_xdr_nvlist_encoding: " \
61		    "NV_ENCODE_NATIVE found in $stream"
62	fi
63	if ! echo "$out" | grep -q 'NV_ENCODE_XDR'; then
64		log_fail "verify_xdr_nvlist_encoding: " \
65		    "no NV_ENCODE_XDR found in $stream"
66	fi
67}
68