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) 2018, Joyent, Inc.
14  * Copyright 2021 Oxide Computer Company
15  */
16 
17 /*
18  * Test basic functionality of libjedec.
19  */
20 
21 #include <sys/types.h>
22 #include <sys/sysmacros.h>
23 #include <libjedec.h>
24 #include <stdio.h>
25 #include <strings.h>
26 
27 /*
28  * Table of various values and expected vendors.
29  */
30 typedef struct {
31 	uint_t		ljtt_cont;
32 	uint_t		ljtt_vendor;
33 	const char	*ljtt_exp;
34 } libjedec_test_t;
35 
36 static const libjedec_test_t libjedec_expects[] = {
37 	{ 0x00, 0x01, "AMD" },
38 	{ 0x00, 0x19, "Xicor" },
39 	{ 0x00, 0x89, "Intel" },
40 	{ 0x00, 0xFE, "Numonyx Corporation" },
41 	{ 0x01, 0x15, "Hughes Aircraft" },
42 	{ 0x01, 0xF2, "Yamaha Corporation" },
43 	{ 0x02, 0x9E, "Corsair" },
44 	{ 0x02, 0x3E, "West Bay Semiconductor" },
45 	{ 0x02, 0xF8, "Galaxy Power" },
46 	{ 0x03, 0x26, "BOPS" },
47 	{ 0x03, 0x6B, "NVIDIA" },
48 	{ 0x03, 0x7A, "Astec International" },
49 	{ 0x04, 0x07, "Dotcast" },
50 	{ 0x04, 0x40, "Bandspeed" },
51 	{ 0x04, 0x6D, "Supreme Top Technology Ltd." },
52 	{ 0x05, 0x2A, "Atrua Technologies, Inc." },
53 	{ 0x05, 0x52, "New Japan Radio Co. Ltd." },
54 	{ 0x05, 0xEF, "MetaRAM" },
55 	{ 0x06, 0x0B, "Netxen" },
56 	{ 0x06, 0xF2, "Muscle Power" },
57 	{ 0x07, 0x9E, "Teikon" },
58 	{ 0x07, 0xCE, "Mustang" },
59 	{ 0x08, 0x1F, "Shenzhen City Gcai Electronics" },
60 	{ 0x08, 0xF1, "Asgard" },
61 	{ 0x09, 0x13, "Raspberry Pi Trading Ltd." },
62 	{ 0x09, 0xFE, "ALLFLASH Technology Limited" },
63 	{ 0x0a, 0x2C, "Diamond" },
64 	{ 0x0a, 0x6B, "Acer" },
65 	{ 0x0b, 0xE6, "NUVIA Inc" },
66 	{ 0x0c, 0xC4, "uFound" },
67 	{ 0x0d, 0x8A, "Aerospace Science Memory Shenzhen" },
68 	/* Various Failure cases */
69 	{ 0x00, 0x05, NULL },
70 	{ 0x0d, 0xFE, NULL },
71 	{ 0x20, 0x01, NULL }
72 };
73 
74 int
75 main(void)
76 {
77 	uint_t i, errs = 0;
78 
79 	for (i = 0; i < ARRAY_SIZE(libjedec_expects); i++) {
80 		const char *out;
81 
82 		out = libjedec_vendor_string(libjedec_expects[i].ljtt_cont,
83 		    libjedec_expects[i].ljtt_vendor);
84 		if (out == NULL && libjedec_expects[i].ljtt_exp != NULL) {
85 			errs++;
86 			(void) fprintf(stderr, "test %u failed, expected %s, "
87 			    "but lookup failed\n", i,
88 			    libjedec_expects[i].ljtt_exp);
89 		} else if (out != NULL && libjedec_expects[i].ljtt_exp ==
90 		    NULL) {
91 			errs++;
92 			(void) fprintf(stderr, "test %u failed, expected "
93 			    "lookup failure, but it succeeded with %s\n", i,
94 			    out);
95 		} else if (strcmp(out, libjedec_expects[i].ljtt_exp) != 0) {
96 			errs++;
97 			(void) fprintf(stderr, "test %u failed, expected %s, "
98 			    "found %s\n", i, libjedec_expects[i].ljtt_exp,
99 			    out);
100 		}
101 	}
102 
103 	return (errs);
104 }
105