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