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