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