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