1 /*******************************************************************************
2  *  Copyright (c) 2015, 2017 SAP SE and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *      SAP SE - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.equinox.p2.tests.touchpoint.natives;
15 
16 import static org.eclipse.equinox.p2.tests.AbstractProvisioningTest.assertOK;
17 import static org.hamcrest.CoreMatchers.equalTo;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assume.assumeThat;
20 
21 import java.io.File;
22 import java.util.*;
23 import java.util.prefs.Preferences;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.equinox.internal.p2.touchpoint.natives.NativeTouchpoint;
26 import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.ActionConstants;
27 import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.CheckAndPromptNativePackageWindowsRegistry;
28 import org.eclipse.equinox.p2.engine.IProfile;
29 import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
30 import org.eclipse.equinox.p2.tests.AbstractProvisioningTest.ProvisioningTestRuleAdapter;
31 import org.junit.*;
32 
33 public class CheckAndPromptNativePackageWindowsRegistryTest {
34 
35 	@BeforeClass
classSetUp()36 	public static void classSetUp() throws Exception {
37 		assumeThat("Windows only: uses Windows registry", Platform.getOS(), equalTo(Platform.OS_WIN32));
38 	}
39 
40 	@Rule
41 	public final ProvisioningTestRuleAdapter testHelper = new ProvisioningTestRuleAdapter();
42 
43 	private Preferences prefsNode;
44 
45 	@Before
setUp()46 	public void setUp() throws Exception {
47 		prefsNode = Preferences.userNodeForPackage(getClass());
48 		prefsNode.clear();
49 		prefsNode.flush();
50 	}
51 
52 	@Test
execute_StringAttribute()53 	public void execute_StringAttribute() throws Exception {
54 		String attributeName = "attribute";
55 		String attributeValue = "value";
56 		prefsNode.put(attributeName, attributeValue);
57 		prefsNode.flush();
58 
59 		Map<String, Object> parameters = new HashMap<>();
60 		NativeTouchpoint touchpoint = createTouchpoint(parameters);
61 		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
62 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
63 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
64 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
65 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME, attributeName);
66 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE, attributeValue);
67 		parameters = Collections.unmodifiableMap(parameters);
68 
69 		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
70 		action.setTouchpoint(touchpoint);
71 
72 		assertOK(action.execute(parameters));
73 		assertEquals(Collections.emptyList(), touchpoint.getPackagesToInstall());
74 	}
75 
76 	@Test
execute_StringAttribute_DifferentValues()77 	public void execute_StringAttribute_DifferentValues() throws Exception {
78 		String attributeName = "attribute";
79 		String attributeValue = "value";
80 		prefsNode.put(attributeName, attributeValue);
81 		prefsNode.flush();
82 
83 		Map<String, Object> parameters = new HashMap<>();
84 		NativeTouchpoint touchpoint = createTouchpoint(parameters);
85 		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
86 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
87 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
88 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
89 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME, attributeName);
90 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE, attributeValue + "_DIFF");
91 		parameters = Collections.unmodifiableMap(parameters);
92 
93 		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
94 		action.setTouchpoint(touchpoint);
95 
96 		assertOK(action.execute(parameters));
97 		assertEquals(touchpoint.getPackagesToInstall().toString(), 1, touchpoint.getPackagesToInstall().size());
98 	}
99 
100 	@Test
execute_IntAttribute()101 	public void execute_IntAttribute() throws Exception {
102 		String attributeName = "attribute";
103 		int attributeValue = 1;
104 		prefsNode.putInt(attributeName, attributeValue);
105 		prefsNode.flush();
106 
107 		Map<String, Object> parameters = new HashMap<>();
108 		NativeTouchpoint touchpoint = createTouchpoint(parameters);
109 		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
110 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
111 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
112 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
113 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME, attributeName);
114 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE, String.valueOf(attributeValue));
115 		parameters = Collections.unmodifiableMap(parameters);
116 
117 		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
118 		action.setTouchpoint(touchpoint);
119 
120 		assertOK(action.execute(parameters));
121 		assertEquals(Collections.emptyList(), touchpoint.getPackagesToInstall());
122 	}
123 
124 	@Test
execute_IntAttribute_DifferentLiteralValue()125 	public void execute_IntAttribute_DifferentLiteralValue() throws Exception {
126 		String attributeName = "attribute";
127 		int attributeValue = 1;
128 		prefsNode.putInt(attributeName, attributeValue);
129 		prefsNode.flush();
130 
131 		Map<String, Object> parameters = new HashMap<>();
132 		NativeTouchpoint touchpoint = createTouchpoint(parameters);
133 		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
134 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
135 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
136 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
137 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_NAME, attributeName);
138 		// we simulate a different literal value
139 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_ATTRIBUTE_VALUE, "0" + String.valueOf(attributeValue));
140 		parameters = Collections.unmodifiableMap(parameters);
141 
142 		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
143 		action.setTouchpoint(touchpoint);
144 
145 		assertOK(action.execute(parameters));
146 		assertEquals(Collections.emptyList(), touchpoint.getPackagesToInstall());
147 	}
148 
149 	@Test
execute_KeyExistence()150 	public void execute_KeyExistence() throws Exception {
151 		Map<String, Object> parameters = new HashMap<>();
152 		NativeTouchpoint touchpoint = createTouchpoint(parameters);
153 		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
154 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
155 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
156 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode));
157 		parameters = Collections.unmodifiableMap(parameters);
158 
159 		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
160 		action.setTouchpoint(touchpoint);
161 
162 		assertOK(action.execute(parameters));
163 		assertEquals(Collections.emptyList(), touchpoint.getPackagesToInstall());
164 	}
165 
166 	@Test
execute_KeyExistence_DifferentKeys()167 	public void execute_KeyExistence_DifferentKeys() throws Exception {
168 		Map<String, Object> parameters = new HashMap<>();
169 		NativeTouchpoint touchpoint = createTouchpoint(parameters);
170 		parameters.put(ActionConstants.PARM_LINUX_DISTRO, CheckAndPromptNativePackageWindowsRegistry.WINDOWS_DISTRO);
171 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_NAME, "windows package");
172 		parameters.put(ActionConstants.PARM_LINUX_PACKAGE_VERSION, "1.0");
173 		parameters.put(ActionConstants.PARM_WINDOWS_REGISTRY_KEY, registryPath(prefsNode) + "\\node");
174 		parameters = Collections.unmodifiableMap(parameters);
175 
176 		ProvisioningAction action = new CheckAndPromptNativePackageWindowsRegistry();
177 		action.setTouchpoint(touchpoint);
178 
179 		assertOK(action.execute(parameters));
180 		assertEquals(touchpoint.getPackagesToInstall().toString(), 1, touchpoint.getPackagesToInstall().size());
181 	}
182 
createTouchpoint(Map<String, Object> parameters)183 	private NativeTouchpoint createTouchpoint(Map<String, Object> parameters) {
184 		Map<String, String> profileProperties = new HashMap<>();
185 		File installFolder = testHelper.getTempFolder();
186 		profileProperties.put(IProfile.PROP_INSTALL_FOLDER, installFolder.toString());
187 		IProfile profile = testHelper.createProfile("test", profileProperties);
188 
189 		parameters.put(ActionConstants.PARM_PROFILE, profile);
190 		NativeTouchpoint touchpoint = new NativeTouchpoint();
191 		touchpoint.initializePhase(null, profile, "test", parameters);
192 		return touchpoint;
193 	}
194 
registryPath(Preferences prefs)195 	private static String registryPath(Preferences prefs) {
196 		String path = prefs.absolutePath().replace('/', '\\');
197 		String root = prefs.isUserNode() ? "HKCU" : "HKLM";
198 		return root + "\\Software\\JavaSoft\\Prefs" + path;
199 	}
200 
201 }