xref: /illumos-gate/usr/src/test/util-tests/tests/libsff/libsff_strings.c (revision 30165b7f6753bc3d48c52319bed7ec7b3ea36b3c)
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.
6  *
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.
10  */
11 
12 /*
13  * Copyright (c) 2017, Joyent, Inc.
14  */
15 
16 /*
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.
20  */
21 
22 #include <stdio.h>
23 #include <errno.h>
24 #include <strings.h>
25 #include <err.h>
26 #include <libsff.h>
27 
28 /*
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.
33  */
34 #include "sff.h"
35 
36 typedef struct {
37 	uint8_t	lss_bytes[16];
38 	const char *lss_parsed;
39 } lsfs_string_pair_t;
40 
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 	    "" }
58 };
59 #define	NBAD	(sizeof (lsfs_bad_vals) / sizeof (lsfs_string_pair_t))
60 
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 	    "!!!!!!!!!!!!!!!!" }
78 };
79 #define	NGOOD	(sizeof (lsfs_good_vals) / sizeof (lsfs_string_pair_t))
80 
81 int
82 main(void)
83 {
84 	int ret, i;
85 	uint8_t buf[256];
86 	nvlist_t *nvl;
87 	char *val;
88 
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);
93 
94 
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));
98 		}
99 
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);
104 		}
105 		nvlist_free(nvl);
106 	}
107 
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);
112 
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));
116 		}
117 
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);
122 		}
123 
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);
127 		}
128 
129 		nvlist_free(nvl);
130 	}
131 
132 	return (0);
133 }
134