1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.jfr.event.security;
  25 
  26 import java.util.List;
  27 
  28 import jdk.jfr.AnnotationElement;
  29 import jdk.jfr.EventType;
  30 import jdk.jfr.FlightRecorder;
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.consumer.RecordedEvent;
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.jfr.Events;
  35 import jdk.test.lib.jfr.EventNames;
  36 import jdk.test.lib.security.TestCertificate;
  37 
  38 /*
  39  * @test
  40  * @bug 8148188
  41  * @summary Enhance the security libraries to record events of interest
  42  * @key jfr
  43  * @library /test/lib
  44  * @modules jdk.jfr/jdk.jfr.events
  45  * @run main/othervm jdk.jfr.event.security.TestX509ValidationEvent
  46  */
  47 public class TestX509ValidationEvent {
  48     public static void main(String[] args) throws Exception {
  49         try (Recording recording = new Recording()) {
  50             recording.enable(EventNames.X509Validation);
  51             recording.start();
  52             // intermeditate certificate test
  53             TestCertificate.generateChain(false);
  54             recording.stop();
  55             List<RecordedEvent> events = Events.fromRecording(recording);
  56             Asserts.assertEquals(events.size(), 3, "Incorrect number of events");
  57             assertEvent1(events);
  58         }
  59 
  60         try (Recording recording = new Recording()) {
  61             recording.enable(EventNames.X509Validation);
  62             recording.start();
  63             // self signed certificate test
  64             TestCertificate.generateChain(true);
  65             recording.stop();
  66             List<RecordedEvent> events = Events.fromRecording(recording);
  67             Asserts.assertEquals(events.size(), 2, "Incorrect number of events");
  68             assertEvent2(events);
  69         }
  70     }
  71 
  72     private static void assertEvent1(List<RecordedEvent> events) throws Exception {
  73         for (RecordedEvent e : events) {
  74             int pos = e.getInt("certificatePosition");
  75             switch (pos) {
  76                 case 1:
  77                     Events.assertField(e, "hashCode")
  78                             .equal(TestCertificate.ROOT_CA.hashCode);
  79                     break;
  80                 case 2:
  81                     Events.assertField(e, "hashCode")
  82                             .equal(TestCertificate.TWO.hashCode);
  83                     break;
  84                 case 3:
  85                     Events.assertField(e, "hashCode")
  86                             .equal(TestCertificate.ONE.hashCode);
  87                     break;
  88                 default:
  89                     System.out.println(events);
  90                     throw new Exception("Unexpected position:" + pos);
  91             }
  92         }
  93     }
  94 
  95     /*
  96      * Self signed certificate test
  97      */
  98     private static void assertEvent2(List<RecordedEvent> events) throws Exception {
  99         for (RecordedEvent e : events) {
 100             int pos = e.getInt("certificatePosition");
 101             switch (pos) {
 102                 case 1:
 103                 case 2:
 104                     Events.assertField(e, "hashCode")
 105                             .equal(TestCertificate.ROOT_CA.hashCode);
 106                     break;
 107                 default:
 108                     System.out.println(events);
 109                     throw new Exception("Unexpected position:" + pos);
 110             }
 111         }
 112     }
 113 }