xref: /freebsd/lib/libdevdctl/tests/libdevdctl_unittest.cc (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1*7a0c41d5SAlan Somers /*-
2*7a0c41d5SAlan Somers  * Copyright (c) 2016 Spectra Logic Corporation
3*7a0c41d5SAlan Somers  * All rights reserved.
4*7a0c41d5SAlan Somers  *
5*7a0c41d5SAlan Somers  * Redistribution and use in source and binary forms, with or without
6*7a0c41d5SAlan Somers  * modification, are permitted provided that the following conditions
7*7a0c41d5SAlan Somers  * are met:
8*7a0c41d5SAlan Somers  * 1. Redistributions of source code must retain the above copyright
9*7a0c41d5SAlan Somers  *    notice, this list of conditions, and the following disclaimer,
10*7a0c41d5SAlan Somers  *    without modification.
11*7a0c41d5SAlan Somers  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12*7a0c41d5SAlan Somers  *    substantially similar to the "NO WARRANTY" disclaimer below
13*7a0c41d5SAlan Somers  *    ("Disclaimer") and any redistribution must be conditioned upon
14*7a0c41d5SAlan Somers  *    including a substantially similar Disclaimer requirement for further
15*7a0c41d5SAlan Somers  *    binary redistribution.
16*7a0c41d5SAlan Somers  *
17*7a0c41d5SAlan Somers  * NO WARRANTY
18*7a0c41d5SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*7a0c41d5SAlan Somers  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*7a0c41d5SAlan Somers  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21*7a0c41d5SAlan Somers  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*7a0c41d5SAlan Somers  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*7a0c41d5SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*7a0c41d5SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*7a0c41d5SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26*7a0c41d5SAlan Somers  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27*7a0c41d5SAlan Somers  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*7a0c41d5SAlan Somers  * POSSIBILITY OF SUCH DAMAGES.
29*7a0c41d5SAlan Somers  *
30*7a0c41d5SAlan Somers  * Authors: Alan Somers         (Spectra Logic Corporation)
31*7a0c41d5SAlan Somers  */
32*7a0c41d5SAlan Somers 
33*7a0c41d5SAlan Somers #include <gtest/gtest.h>
34*7a0c41d5SAlan Somers 
35*7a0c41d5SAlan Somers #include <list>
36*7a0c41d5SAlan Somers #include <map>
37*7a0c41d5SAlan Somers #include <string>
38*7a0c41d5SAlan Somers 
39*7a0c41d5SAlan Somers #include <devdctl/guid.h>
40*7a0c41d5SAlan Somers #include <devdctl/event.h>
41*7a0c41d5SAlan Somers #include <devdctl/event_factory.h>
42*7a0c41d5SAlan Somers 
43*7a0c41d5SAlan Somers using namespace DevdCtl;
44*7a0c41d5SAlan Somers using namespace std;
45*7a0c41d5SAlan Somers using namespace testing;
46*7a0c41d5SAlan Somers 
47*7a0c41d5SAlan Somers #define REGISTRY_SIZE 2
48*7a0c41d5SAlan Somers 
49*7a0c41d5SAlan Somers struct DevNameTestParams
50*7a0c41d5SAlan Somers {
51*7a0c41d5SAlan Somers 	const char* evs;
52*7a0c41d5SAlan Somers 	bool is_disk;
53*7a0c41d5SAlan Somers 	const char* devname;
54*7a0c41d5SAlan Somers };
55*7a0c41d5SAlan Somers 
56*7a0c41d5SAlan Somers class DevNameTest : public TestWithParam<DevNameTestParams>{
57*7a0c41d5SAlan Somers protected:
SetUp()58*7a0c41d5SAlan Somers 	virtual void SetUp()
59*7a0c41d5SAlan Somers 	{
60*7a0c41d5SAlan Somers 		m_factory = new EventFactory();
61*7a0c41d5SAlan Somers 		m_factory->UpdateRegistry(s_registry, REGISTRY_SIZE);
62*7a0c41d5SAlan Somers 	}
63*7a0c41d5SAlan Somers 
TearDown()64*7a0c41d5SAlan Somers 	virtual void TearDown()
65*7a0c41d5SAlan Somers 	{
66*7a0c41d5SAlan Somers 		if (m_ev) delete m_ev;
67*7a0c41d5SAlan Somers 		if (m_factory) delete m_factory;
68*7a0c41d5SAlan Somers 	}
69*7a0c41d5SAlan Somers 
70*7a0c41d5SAlan Somers 	EventFactory *m_factory;
71*7a0c41d5SAlan Somers 	Event *m_ev;
72*7a0c41d5SAlan Somers 	static EventFactory::Record s_registry[REGISTRY_SIZE];
73*7a0c41d5SAlan Somers };
74*7a0c41d5SAlan Somers 
75*7a0c41d5SAlan Somers DevdCtl::EventFactory::Record DevNameTest::s_registry[REGISTRY_SIZE] = {
76*7a0c41d5SAlan Somers 	{ Event::NOTIFY, "DEVFS", &DevfsEvent::Builder },
77*7a0c41d5SAlan Somers 	{ Event::NOTIFY, "GEOM", &GeomEvent::Builder }
78*7a0c41d5SAlan Somers };
79*7a0c41d5SAlan Somers 
TEST_P(DevNameTest,TestDevname)80*7a0c41d5SAlan Somers TEST_P(DevNameTest, TestDevname) {
81*7a0c41d5SAlan Somers 	std::string devname;
82*7a0c41d5SAlan Somers 	DevNameTestParams param = GetParam();
83*7a0c41d5SAlan Somers 
84*7a0c41d5SAlan Somers 	string evString(param.evs);
85*7a0c41d5SAlan Somers 	m_ev = Event::CreateEvent(*m_factory, evString);
86*7a0c41d5SAlan Somers 	m_ev->DevName(devname);
87*7a0c41d5SAlan Somers 	EXPECT_STREQ(param.devname, devname.c_str());
88*7a0c41d5SAlan Somers }
89*7a0c41d5SAlan Somers 
TEST_P(DevNameTest,TestIsDiskDev)90*7a0c41d5SAlan Somers TEST_P(DevNameTest, TestIsDiskDev) {
91*7a0c41d5SAlan Somers 	DevNameTestParams param = GetParam();
92*7a0c41d5SAlan Somers 
93*7a0c41d5SAlan Somers 	string evString(param.evs);
94*7a0c41d5SAlan Somers 	m_ev = Event::CreateEvent(*m_factory, evString);
95*7a0c41d5SAlan Somers 	EXPECT_EQ(param.is_disk, m_ev->IsDiskDev());
96*7a0c41d5SAlan Somers }
97*7a0c41d5SAlan Somers 
98*7a0c41d5SAlan Somers /* TODO: clean this up using C++-11 uniform initializers */
99*7a0c41d5SAlan Somers INSTANTIATE_TEST_CASE_P(IsDiskDevTestInstantiation, DevNameTest, Values(
100*7a0c41d5SAlan Somers 	(DevNameTestParams){
101*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6\n",
102*7a0c41d5SAlan Somers 		.is_disk = true, .devname = "da6"},
103*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = false, .devname = "cuau0",
104*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=cuau0\n"},
105*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = true, .devname = "ada6",
106*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6\n"},
107*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = true, .devname = "da6p1",
108*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6p1\n"},
109*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = true, .devname = "ada6p1",
110*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6p1\n"},
111*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = true, .devname = "da6s0p1",
112*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6s0p1\n"},
113*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = true, .devname = "ada6s0p1",
114*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6s0p1\n"},
115*7a0c41d5SAlan Somers 	/*
116*7a0c41d5SAlan Somers 	 * Test physical path nodes.  These are currently all set to false since
117*7a0c41d5SAlan Somers 	 * physical path nodes are implemented with symlinks, and most CAM and
118*7a0c41d5SAlan Somers 	 * ZFS operations can't use symlinked device nodes
119*7a0c41d5SAlan Somers 	 */
120*7a0c41d5SAlan Somers 	/* A SpectraBSD-style physical path node*/
121*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = false, .devname = "enc@50030480019f53fd/elmtype@array_device/slot@18/da",
122*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@50030480019f53fd/elmtype@array_device/slot@18/da\n"},
123*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = false, .devname = "enc@50030480019f53fd/elmtype@array_device/slot@18/pass",
124*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@50030480019f53fd/elmtype@array_device/slot@18/pass\n"},
125*7a0c41d5SAlan Somers 	/* A FreeBSD-style physical path node */
126*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = true, .devname = "enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/da6",
127*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/da6\n"},
128*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = false, .devname = "enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/pass6",
129*7a0c41d5SAlan Somers 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/pass6\n"},
130*7a0c41d5SAlan Somers 
131*7a0c41d5SAlan Somers 	/*
132*7a0c41d5SAlan Somers 	 * Test some GEOM events
133*7a0c41d5SAlan Somers 	 */
134*7a0c41d5SAlan Somers 	(DevNameTestParams){.is_disk = true, .devname = "da5",
135*7a0c41d5SAlan Somers 		.evs = "!system=GEOM subsystem=disk type=GEOM::physpath devname=da5\n"})
136*7a0c41d5SAlan Somers );
137