File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\jenkins\security\RekeySecretAdminMonitorTest.java.html

1 package jenkins.security;
2
3 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
4 import com.gargoylesoftware.htmlunit.html.HtmlButton;
5 import com.gargoylesoftware.htmlunit.html.HtmlForm;
6 import com.gargoylesoftware.htmlunit.html.HtmlPage;
7 import com.trilead.ssh2.crypto.Base64;
8 import hudson.FilePath;
9 import hudson.Util;
10 import hudson.util.Secret;
11 import hudson.util.SecretHelper;
12 import org.apache.commons.io.FileUtils;
13 import org.jvnet.hudson.test.HudsonTestCase;
14 import org.jvnet.hudson.test.recipes.Recipe.Runner;
15 import org.xml.sax.SAXException;
16
17 import javax.crypto.Cipher;
18 import javax.inject.Inject;
19 import java.io.File;
20 import java.io.IOException;
21 import java.lang.annotation.Annotation;
22
23 /**
24 * @author Kohsuke Kawaguchi
25 */
26 public class RekeySecretAdminMonitorTest extends HudsonTestCase {
27 @Inject
28 RekeySecretAdminMonitor monitor;
29
30 @Override
31 protected void setUp() throws Exception {
32 SecretHelper.set(TEST_KEY);
33 super.setUp();
34 monitor.setNeeded();
35 }
36
37 @Override
38 protected void tearDown() throws Exception {
39 SecretHelper.set(null);
40 super.tearDown();
41 }
42
43 @Override
44 protected void recipe() throws Exception {
45 super.recipe();
46 recipes.add(new Runner() {
47 @Override
48 public void setup(HudsonTestCase testCase, Annotation recipe) throws Exception {
49 }
50
51 @Override
52 public void decorateHome(HudsonTestCase testCase, File home) throws Exception {
53 if (getName().endsWith("testScanOnBoot")) {
54 // schedule a scan on boot
55 File f = new File(home, RekeySecretAdminMonitor.class.getName() + "/scanOnBoot");
56 f.getParentFile().mkdirs();
57 new FilePath(f).touch(0);
58
59 // and stage some data
60 putSomeOldData(home);
61 }
62 }
63
64 @Override
65 public void tearDown(HudsonTestCase testCase, Annotation recipe) throws Exception {
66 }
67 });
68 }
69
70
71 private void putSomeOldData(File dir) throws Exception {
72 File xml = new File(dir, "foo.xml");
73 FileUtils.writeStringToFile(xml,"<foo>" + encryptOld(TEST_KEY) + "</foo>");
74 }
75
76 private void verifyRewrite(File dir) throws Exception {
77 File xml = new File(dir, "foo.xml");
78 assertEquals("<foo>" + encryptNew(TEST_KEY) + "</foo>".trim(),
79 FileUtils.readFileToString(xml).trim());
80 }
81
82 public void testBasicWorkflow() throws Exception {
83 putSomeOldData(jenkins.getRootDir());
84
85 WebClient wc = createWebClient();
86
87 // one should see the warning. try scheduling it
88 assertTrue(!monitor.isScanOnBoot());
89 HtmlForm form = getRekeyForm(wc);
90 submit(form, "schedule");
91 assertTrue(monitor.isScanOnBoot());
92 form = getRekeyForm(wc);
93 assertTrue(getButton(form, 1).isDisabled());
94
95 // run it now
96 assertTrue(!monitor.getLogFile().exists());
97 submit(form, "background");
98 assertTrue(monitor.getLogFile().exists());
99
100 // should be no warning/error now
101 HtmlPage manage = wc.goTo("/manage");
102 assertEquals(0,manage.selectNodes("//*[class='error']").size());
103 assertEquals(0,manage.selectNodes("//*[class='warning']").size());
104
105 // and the data should be rewritten
106 verifyRewrite(jenkins.getRootDir());
107 assertTrue(monitor.isDone());
108
109 // dismiss and the message will be gone
110 assertTrue(monitor.isEnabled());
111 form = getRekeyForm(wc);
112 submit(form, "dismiss");
113 assertFalse(monitor.isEnabled());
114 try {
115 getRekeyForm(wc);
116 fail();
117 } catch (ElementNotFoundException e) {
118 // expected
119 }
120 }
121
122 private HtmlForm getRekeyForm(WebClient wc) throws IOException, SAXException {
123 return wc.goTo("/manage").getFormByName("rekey");
124 }
125
126 private HtmlButton getButton(HtmlForm form, int index) {
127 return form.<HtmlButton>getHtmlElementsByTagName("button").get(index);
128 }
129
130 public void testScanOnBoot() throws Exception {
131 WebClient wc = createWebClient();
132
133 // scan on boot should have run the scan
134 assertTrue(monitor.getLogFile().exists());
135 assertTrue("scan on boot should have turned this off",!monitor.isScanOnBoot());
136
137 // and data should be migrated
138 verifyRewrite(jenkins.getRootDir());
139
140 // should be no warning/error now
141 HtmlPage manage = wc.goTo("/manage");
142 assertEquals(0,manage.selectNodes("//*[class='error']").size());
143 assertEquals(0,manage.selectNodes("//*[class='warning']").size());
144 }
145
146 private String encryptOld(String str) throws Exception {
147 Cipher cipher = Secret.getCipher("AES");
148 cipher.init(Cipher.ENCRYPT_MODE, Util.toAes128Key(TEST_KEY));
149 return new String(Base64.encode(cipher.doFinal((str + "::::MAGIC::::").getBytes("UTF-8"))));
150 }
151
152 private String encryptNew(String str) {
153 return Secret.fromString(str).getEncryptedValue();
154 }
155
156 private static final String TEST_KEY = "superDuperSecretWasNotSoSecretAfterAll";
157 }
158