8699 Want NIC transceiver visibility
[unleashed.git] / usr / src / test / util-tests / tests / libsff / libsff_strings.c
bloba9d5a2252f089c066d800a0a210fc4025bb7a498
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright (c) 2017, Joyent, Inc.
17 * Test our ability to parse SFF string values which are space encoded. As this
18 * is shared between the SFP and QSFP logic, we end up only testing the SFP
19 * based data.
22 #include <stdio.h>
23 #include <errno.h>
24 #include <strings.h>
25 #include <err.h>
26 #include <libsff.h>
29 * Pick up private sff header file with offsets from lib/libsff. Strings are
30 * described as having spaces at the end of them. We mostly want to make sure
31 * that if we have strings without spaces that we parse them sanely as well as
32 * test what happens with embedded spaces and NUL characters.
34 #include "sff.h"
36 typedef struct {
37 uint8_t lss_bytes[16];
38 const char *lss_parsed;
39 } lsfs_string_pair_t;
41 static const lsfs_string_pair_t lsfs_bad_vals[] = {
42 /* All NULs */
43 { { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
44 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' },
45 "" },
46 /* Embedded NULs */
47 { { 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
48 '\0', 'a', 'a', 'a', 'a', 'a', 'a', 'a' },
49 "" },
50 /* Non-ASCII */
51 { { 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
52 156, 'a', 'a', 'a', 'a', 'a', 'a', 'a' },
53 "" },
54 /* All padding */
55 { { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
56 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
57 "" }
59 #define NBAD (sizeof (lsfs_bad_vals) / sizeof (lsfs_string_pair_t))
61 static const lsfs_string_pair_t lsfs_good_vals[] = {
62 /* Basic Name */
63 { { 'f', 'i', 'n', 'g', 'o', 'l', 'f', 'i',
64 'n', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
65 "fingolfin" },
66 /* Non-padding Space */
67 { { 'G', 'l', 'o', 'b', 'e', 'x', ' ', 'C',
68 'o', 'r', 'p', ' ', ' ', ' ', ' ', ' ' },
69 "Globex Corp" },
70 /* 1-character name to catch off by one */
71 { { '~', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
72 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
73 "~" },
74 /* Use all characters */
75 { { '!', '!', '!', '!', '!', '!', '!', '!',
76 '!', '!', '!', '!', '!', '!', '!', '!' },
77 "!!!!!!!!!!!!!!!!" }
79 #define NGOOD (sizeof (lsfs_good_vals) / sizeof (lsfs_string_pair_t))
81 int
82 main(void)
84 int ret, i;
85 uint8_t buf[256];
86 nvlist_t *nvl;
87 char *val;
89 for (i = 0; i < NBAD; i++) {
90 bzero(buf, sizeof (buf));
91 bcopy(lsfs_bad_vals[i].lss_bytes, &buf[SFF_8472_VENDOR],
92 SFF_8472_VENDOR_LEN);
95 if ((ret = libsff_parse(buf, sizeof (buf), 0xa0, &nvl)) != 0) {
96 errx(1, "TEST FAILED: failed to parse SFP bad string "
97 "case %d: %s\n", i, strerror(ret));
100 if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_VENDOR,
101 &val)) != ENOENT) {
102 errx(1, "TEST FALIED: found unexpected return value "
103 "for %s: %d\n", LIBSFF_KEY_VENDOR, ret);
105 nvlist_free(nvl);
108 for (i = 0; i < NGOOD; i++) {
109 bzero(buf, sizeof (buf));
110 bcopy(lsfs_good_vals[i].lss_bytes, &buf[SFF_8472_VENDOR],
111 SFF_8472_VENDOR_LEN);
113 if ((ret = libsff_parse(buf, sizeof (buf), 0xa0, &nvl)) != 0) {
114 errx(1, "TEST FAILED: failed to parse SFP good string "
115 "case %d: %s\n", i, strerror(ret));
118 if ((ret = nvlist_lookup_string(nvl, LIBSFF_KEY_VENDOR,
119 &val)) != 0) {
120 errx(1, "TEST FALIED: failed to find expected key "
121 "%s: %d", LIBSFF_KEY_VENDOR, ret);
124 if (strcmp(val, lsfs_good_vals[i].lss_parsed) != 0) {
125 errx(1, "TEST FAILED: expected string %s, found %s\n",
126 lsfs_good_vals[i].lss_parsed, val);
129 nvlist_free(nvl);
132 return (0);