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 
33 #include <gtest/gtest.h>
34 
35 #include <list>
36 #include <map>
37 #include <string>
38 
39 #include <devdctl/guid.h>
40 #include <devdctl/event.h>
41 #include <devdctl/event_factory.h>
42 
43 using namespace DevdCtl;
44 using namespace std;
45 using namespace testing;
46 
47 #define REGISTRY_SIZE 2
48 
49 struct DevNameTestParams
50 {
51 	const char* evs;
52 	bool is_disk;
53 	const char* devname;
54 };
55 
56 class DevNameTest : public TestWithParam<DevNameTestParams>{
57 protected:
58 	virtual void SetUp()
59 	{
60 		m_factory = new EventFactory();
61 		m_factory->UpdateRegistry(s_registry, REGISTRY_SIZE);
62 	}
63 
64 	virtual void TearDown()
65 	{
66 		if (m_ev) delete m_ev;
67 		if (m_factory) delete m_factory;
68 	}
69 
70 	EventFactory *m_factory;
71 	Event *m_ev;
72 	static EventFactory::Record s_registry[REGISTRY_SIZE];
73 };
74 
75 DevdCtl::EventFactory::Record DevNameTest::s_registry[REGISTRY_SIZE] = {
76 	{ Event::NOTIFY, "DEVFS", &DevfsEvent::Builder },
77 	{ Event::NOTIFY, "GEOM", &GeomEvent::Builder }
78 };
79 
80 TEST_P(DevNameTest, TestDevname) {
81 	std::string devname;
82 	DevNameTestParams param = GetParam();
83 
84 	string evString(param.evs);
85 	m_ev = Event::CreateEvent(*m_factory, evString);
86 	m_ev->DevName(devname);
87 	EXPECT_STREQ(param.devname, devname.c_str());
88 }
89 
90 TEST_P(DevNameTest, TestIsDiskDev) {
91 	DevNameTestParams param = GetParam();
92 
93 	string evString(param.evs);
94 	m_ev = Event::CreateEvent(*m_factory, evString);
95 	EXPECT_EQ(param.is_disk, m_ev->IsDiskDev());
96 }
97 
98 /* TODO: clean this up using C++-11 uniform initializers */
99 INSTANTIATE_TEST_CASE_P(IsDiskDevTestInstantiation, DevNameTest, Values(
100 	(DevNameTestParams){
101 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6\n",
102 		.is_disk = true, .devname = "da6"},
103 	(DevNameTestParams){.is_disk = false, .devname = "cuau0",
104 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=cuau0\n"},
105 	(DevNameTestParams){.is_disk = true, .devname = "ada6",
106 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6\n"},
107 	(DevNameTestParams){.is_disk = true, .devname = "da6p1",
108 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6p1\n"},
109 	(DevNameTestParams){.is_disk = true, .devname = "ada6p1",
110 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6p1\n"},
111 	(DevNameTestParams){.is_disk = true, .devname = "da6s0p1",
112 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=da6s0p1\n"},
113 	(DevNameTestParams){.is_disk = true, .devname = "ada6s0p1",
114 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=ada6s0p1\n"},
115 	/*
116 	 * Test physical path nodes.  These are currently all set to false since
117 	 * physical path nodes are implemented with symlinks, and most CAM and
118 	 * ZFS operations can't use symlinked device nodes
119 	 */
120 	/* A SpectraBSD-style physical path node*/
121 	(DevNameTestParams){.is_disk = false, .devname = "enc@50030480019f53fd/elmtype@array_device/slot@18/da",
122 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@50030480019f53fd/elmtype@array_device/slot@18/da\n"},
123 	(DevNameTestParams){.is_disk = false, .devname = "enc@50030480019f53fd/elmtype@array_device/slot@18/pass",
124 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@50030480019f53fd/elmtype@array_device/slot@18/pass\n"},
125 	/* A FreeBSD-style physical path node */
126 	(DevNameTestParams){.is_disk = true, .devname = "enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/da6",
127 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/da6\n"},
128 	(DevNameTestParams){.is_disk = false, .devname = "enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/pass6",
129 		.evs = "!system=DEVFS subsystem=CDEV type=CREATE cdev=enc@n50030480019f53fd/type@0/slot@18/elmdesc@ArrayDevice18/pass6\n"},
130 
131 	/*
132 	 * Test some GEOM events
133 	 */
134 	(DevNameTestParams){.is_disk = true, .devname = "da5",
135 		.evs = "!system=GEOM subsystem=disk type=GEOM::physpath devname=da5\n"})
136 );
137