From e2b576c4b9969eabcab08a7f5700c1957775e83e Mon Sep 17 00:00:00 2001 From: Martin George Date: Tue, 1 Dec 2026 15:14:35 +0530 Subject: [PATCH 1/2] tests: add netapp device-info mock tests The netapp smdevices and ontapdevices commands had no test that decoded real device data, so the E-Series label parsing and the ONTAP nsinfo log parsing were never exercised. Add a mock test that feeds vendor-specific Identify data and a C2 nsinfo log for an explicitly selected namespace and checks the decoded output: the E-Series array and volume labels, access state and volume ID, and the ONTAP vserver, subsystem, namespace path and UUID, in normal, column and JSON form. Cover the nsinfo paths with and without the namespace path TLV, a non-nsinfo log, a missing vserver/volume/namespace TLV, a missing subsystem name, a truncated log, and the Identify/Get Log Page failures of the per-device info gathering of both commands. This uses the standard /dev/nvme0n1 the mock maps, so nvme-fuzz seeds can record its valid device data as seeds of the device fuzz target. Link: https://github.com/linux-nvme/nvme-cli/issues/4315 Signed-off-by: Martin George --- tests/cli/netapp/meson.build | 8 + .../netapp/netapp_device_info_mock_test.py | 368 ++++++++++++++++++ 2 files changed, 376 insertions(+) create mode 100644 tests/cli/netapp/netapp_device_info_mock_test.py diff --git a/tests/cli/netapp/meson.build b/tests/cli/netapp/meson.build index 0e37b2e5b4..0352089f47 100644 --- a/tests/cli/netapp/meson.build +++ b/tests/cli/netapp/meson.build @@ -8,4 +8,12 @@ if have_mock_nvme env: cli_test_env, depends: nvme_runtime + [mock_nvme_lib], suite: 'mock') + + test('netapp - device-info', + python3, + args: [files('netapp_device_info_mock_test.py'), nvme_exe, + mock_nvme_lib], + env: cli_test_env, + depends: nvme_runtime + [mock_nvme_lib], + suite: 'mock') endif diff --git a/tests/cli/netapp/netapp_device_info_mock_test.py b/tests/cli/netapp/netapp_device_info_mock_test.py new file mode 100644 index 0000000000..043f6a26b2 --- /dev/null +++ b/tests/cli/netapp/netapp_device_info_mock_test.py @@ -0,0 +1,368 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +"""Tests that the NetApp commands decode device data correctly. + +These exercise the smdevices/ontapdevices printers end-to-end with +realistic vendor-specific Identify data and a proper ONTAP C2 nsinfo +log, so the label and TLV parsing is covered, not just reached. + +Usage: python3 netapp_device_info_mock_test.py +""" + +import errno +import json +import struct +import uuid + +from tests.cli.nvme_mock import (CNS_CTRL, CNS_NS, CNS_NS_DESC_LIST, + HAVE_JSON, ID_CTRL_SIZE, NVME_IOCTL_ID, + SC_INTERNAL, MockTestCase, Reply, id_ctrl, + id_ns, main) + + +_DEVICE = '/dev/nvme0n1' +_NSID = 7 +_ONTAP_LOG_ID = 0xc2 +_ONTAP_C2_LOG_SIZE = 4096 + +# Offsets of the vendor-specific areas the NetApp plugin reads. +_CTRL_VS = 3072 # nvme_id_ctrl.vs +_NS_VS = 384 # nvme_id_ns.vs +_NGUID_OFF = 104 # nvme_id_ns.nguid +_SUBNQN_OFFSET = 768 +_SUBNQN_SIZE = 256 + +# The nsinfo log layout the plugin parses. +_LSP_OFFSET = 16 # low nibble holds the log specific field +_LSP_NSINFO = 0x1 +_TLV_START = 32 +_TLV_VSERVER = 0x11 +_TLV_VOLUME = 0x12 +_TLV_NS_NAME = 0x13 +_TLV_NS_PATH = 0x14 + +_UUID = bytes(range(16)) +_UUID_STR = str(uuid.UUID(bytes=_UUID)) # 00010203-0405-...-0e0f +_NGUID = bytes(range(0x10, 0x20)) +_NGUID_STR = _NGUID.hex() # lower case, no separators + + +def _ucs2(text): + """The plugin squashes UCS-2 labels to ASCII by taking every second + byte (src[i * 2 + 1]), so encode the ASCII into those odd bytes.""" + out = bytearray() + for byte in text.encode('ascii'): + out += bytes((0, byte)) + return bytes(out) + + +def sm_controller(array_name, primary): + """An E-Series Identify Controller: the array label lives in vs[20], + and bit 0 of vs[0] is the single-LUN-target-access (A vs B) flag.""" + data = id_ctrl(mn='NetApp E-Series', fr='TESTFW') + data[_CTRL_VS] = 0x1 if primary else 0x0 + label = _ucs2(array_name) + data[_CTRL_VS + 20:_CTRL_VS + 20 + len(label)] = label + return data + + +def sm_namespace(volume_name): + """An Identify Namespace with the volume label in vs and an NGUID.""" + data = id_ns(lbaf=[(0, 12, 0)], nsze=100, nuse=25) + label = _ucs2(volume_name) + data[_NS_VS:_NS_VS + len(label)] = label + data[_NGUID_OFF:_NGUID_OFF + 16] = _NGUID + return data + + +def ontap_controller(subnqn=b'nqn.2014-08.org.nvmexpress:mock.netapp-subsystem'): + """An ONTAP Identify Controller with a subsystem NQN to parse.""" + data = id_ctrl(mn='NetApp ONTAP Controller', fr='TESTFW') + data[_SUBNQN_OFFSET:_SUBNQN_OFFSET + _SUBNQN_SIZE] = \ + subnqn.ljust(_SUBNQN_SIZE, b' ') + return data + + +def ns_desc_list(): + """An Identify Namespace descriptor list; the plugin copies the 16 + bytes right after the first descriptor header as the UUID.""" + buf = bytearray(ID_CTRL_SIZE) + buf[0] = 0x03 # NVME_NIDT_UUID + buf[1] = 16 # NIDL + buf[4:4 + 16] = _UUID + return buf + + +def _tlv(tlv_type, value): + """One nsinfo TLV: type, reserved, little-endian u16 length in units + of four bytes, then the value. The value is NUL padded to a multiple + of four; a NUL also terminates the decoded label.""" + data = bytearray(value.encode('ascii')) + data += b'\x00' * (-len(data) % 4) + return bytes((tlv_type, 0)) + struct.pack(' Date: Tue, 1 Dec 2026 15:26:15 +0530 Subject: [PATCH 2/2] tests: fuzz the netapp device-data parsers The netapp smdevices and ontapdevices commands decode vendor-specific data the device returns: the E-Series Identify labels, and the ONTAP C2 nsinfo log, whose length-delimited TLVs are parsed with offsets and lengths taken from the log itself. Nothing fuzzed that. Add both to the device fuzz target. They enumerate /dev themselves, but given the namespace device the target passes they query that one device instead, whose answers are the fuzz input, so the parsers run on it. Make the stat() of that device succeed (MOCK_NVME_STAT_PATH), which the commands do before opening it. The verbose and JSON printers are reached through the variant byte the target already applies; a column option set reaches the remaining column printers. nvme-fuzz seeds records the netapp mock tests as the seed corpus. Link: https://github.com/linux-nvme/nvme-cli/issues/4315 Signed-off-by: Martin George --- tests/fuzz/fuzz-device.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/fuzz/fuzz-device.c b/tests/fuzz/fuzz-device.c index e8b471157b..ff7b3fed43 100644 --- a/tests/fuzz/fuzz-device.c +++ b/tests/fuzz/fuzz-device.c @@ -452,6 +452,17 @@ static const struct { { { "ocp", "telemetry-string-log", "--print-only" }, .opts = { {}, { "--no-uuid" } } }, { { "feat", "voltage-meas" }, "--sel" }, + /* + * The netapp commands take a namespace device and decode the + * vendor data it returns: the E-Series labels, or the ONTAP nsinfo + * log with its length-delimited TLVs. They enumerate /dev without a + * device, but with the one the target passes they skip that and + * read the mock, see LLVMFuzzerInitialize(). + */ + { { "netapp", "smdevices" }, + .opts = { {}, { "--output-format=column" } } }, + { { "netapp", "ontapdevices" }, + .opts = { {}, { "--output-format=column" } } }, }; #define NR_COMMANDS ARRAY_SIZE(commands) @@ -809,6 +820,13 @@ int LLVMFuzzerInitialize(int *argc, char ***argv) check_commands(); pci_setup(); fw_setup(); + /* + * Commands which take a device argument stat() it before opening + * it, e.g. nvme netapp smdevices /dev/nvme0n1. Make that stat + * succeed for the namespace device the inputs pass, so they reach + * what reads the device; open() already maps it to the mock. + */ + setenv("MOCK_NVME_STAT_PATH", "/dev/nvme0n1", 1); return 0; }