File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\model\UpdateCenterTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import com.trilead.ssh2.crypto.Base64;
27 import hudson.util.FormValidation;
28 import hudson.util.FormValidation.Kind;
29 import hudson.util.TimeUnit2;
30 import junit.framework.TestCase;
31 import net.sf.json.JSONObject;
32 import org.apache.commons.io.IOUtils;
33
34 import java.io.ByteArrayInputStream;
35 import java.io.IOException;
36 import java.net.URL;
37 import java.security.cert.CertificateExpiredException;
38 import java.security.cert.CertificateFactory;
39 import java.security.cert.CertificateNotYetValidException;
40 import java.security.cert.X509Certificate;
41 import java.util.Date;
42
43 /**
44 * Quick test for {@link UpdateCenter}.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public class UpdateCenterTest extends TestCase {
49 public void testData() throws Exception {
50 // check if we have the internet connectivity. See HUDSON-2095
51 try {
52 new URL("http://updates.jenkins-ci.org/").openStream();
53 } catch (IOException e) {
54 System.out.println("Skipping this test. No internet connectivity");
55 return;
56 }
57
58 URL url = new URL("http://updates.jenkins-ci.org/update-center.json?version=build");
59 String jsonp = IOUtils.toString(url.openStream());
60 JSONObject json = JSONObject.fromObject(jsonp.substring(jsonp.indexOf('(') + 1, jsonp.lastIndexOf(')')));
61
62 UpdateSite us = new UpdateSite("default", url.toExternalForm());
63 UpdateSite.Data data = us.new Data(json);
64 assertTrue(data.core.url.startsWith("http://updates.jenkins-ci.org/"));
65 assertTrue(data.plugins.containsKey("rake"));
66 System.out.println(data.core.url);
67
68 // make sure the certificate is valid for a while more
69 CertificateFactory cf = CertificateFactory.getInstance("X509");
70 JSONObject signature = json.getJSONObject("signature");
71 for (Object cert : signature.getJSONArray("certificates")) {
72 X509Certificate c = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(Base64.decode(cert.toString().toCharArray())));
73 c.checkValidity(new Date(System.currentTimeMillis() + TimeUnit2.DAYS.toMillis(30)));
74 }
75 }
76 }
77