1 /*
   2  * Copyright (c) 2015, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.api.tree;
  26 
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.nio.charset.Charset;
  30 import java.nio.charset.StandardCharsets;
  31 import java.nio.file.Files;
  32 import org.testng.Assert;
  33 import org.testng.annotations.AfterClass;
  34 import org.testng.annotations.BeforeClass;
  35 import org.testng.annotations.Test;
  36 
  37 /**
  38  * Test for nashorn Parser API (jdk.nashorn.api.tree.*)
  39  */
  40 public class ParseAPITest {
  41     
  42     private static final boolean VERBOSE   = Boolean.valueOf(System.getProperty("parserapitest.verbose"));
  43     private static final boolean TEST262   = Boolean.valueOf(System.getProperty("parserapitest.test262"));
  44 
  45     private static final String TEST_BASIC_DIR     = System.getProperty("test.basic.dir");
  46     private static final String TEST_MAPTESTS_DIR  = System.getProperty("test.maptests.dir");
  47     private static final String TEST_SANDBOX_DIR   = System.getProperty("test.sandbox.dir");
  48     private static final String TEST_TRUSTED_DIR   = System.getProperty("test.trusted.dir");
  49     private static final String TEST262_SUITE_DIR  = System.getProperty("test262.suite.dir");
  50 
  51     interface TestFilter {
  52         public boolean exclude(File file, String content);
  53     }
  54 
  55     private void log(String msg) {
  56         org.testng.Reporter.log(msg, true);
  57     }
  58 
  59     private static final String[] options = new String[] {
  60         "-scripting", "--const-as-var"
  61     };
  62     
  63     @Test
  64     public void parseAllTests() {
  65         if (TEST262) {
  66             parseTestSet(TEST262_SUITE_DIR, new TestFilter() {
  67                 @Override
  68                 public boolean exclude(final File file, final String content) {
  69                     return content.indexOf("@negative") != -1;
  70                 }
  71             });
  72         }
  73         parseTestSet(TEST_BASIC_DIR, new TestFilter() {
  74             @Override
  75             public boolean exclude(final File file, final String content) {
  76                 return file.getParentFile().getName().equals("es6");
  77             }
  78         });
  79         parseTestSet(TEST_MAPTESTS_DIR, null);
  80         parseTestSet(TEST_SANDBOX_DIR, null);
  81         parseTestSet(TEST_TRUSTED_DIR, null);
  82     }
  83 
  84     private void parseTestSet(final String testSet, final TestFilter filter) {
  85         passed  = 0;
  86         failed  = 0;
  87         skipped = 0;
  88 
  89         final File testSetDir = new File(testSet);
  90         if (! testSetDir.isDirectory()) {
  91             log("WARNING: " + testSetDir + " not found or not a directory");
  92             return;
  93         }
  94         log(testSetDir.getAbsolutePath());
  95         parseJSDirectory(testSetDir, filter);
  96 
  97         log(testSet + " parse API done!");
  98         log("parse API ok: " + passed);
  99         log("parse API failed: " + failed);
 100         log("parse API skipped: " + skipped);
 101         if (failed != 0) {
 102             Assert.fail(failed + " tests failed to parse in " + testSetDir.getAbsolutePath());
 103         }
 104     }
 105 
 106     // number of scripts that parsed fine
 107     private int passed;
 108     // number of scripts resulting in parse failure
 109     private int failed;
 110     // scripts that were skipped - all tests with @negative are
 111     // skipped for now.
 112     private int skipped;
 113 
 114     private void parseJSDirectory(final File dir, final TestFilter filter) {
 115         for (final File f : dir.listFiles()) {
 116             if (f.isDirectory()) {
 117                 parseJSDirectory(f, filter);
 118             } else if (f.getName().endsWith(".js")) {
 119                 parseJSFile(f, filter);
 120             }
 121         }
 122     }
 123 
 124     private void parseJSFile(final File file, final TestFilter filter) {
 125         if (VERBOSE) {
 126             log("Begin parsing " + file.getAbsolutePath());
 127         }
 128 
 129         try {
 130             final char[] buffer = readFully(file);
 131             final String content = new String(buffer);
 132             boolean excluded = false;
 133             if (filter != null) {
 134                 excluded = filter.exclude(file, content);
 135             }
 136 
 137             if (excluded) {
 138                 if (VERBOSE) {
 139                     log("Skipping " + file.getAbsolutePath());
 140                 }
 141                 skipped++;
 142                 return;
 143             }
 144             
 145             final Parser parser = Parser.create(options);
 146             final Tree tree = parser.parse(file.getAbsolutePath(), content, null);
 147             tree.accept(new SimpleTreeVisitorES5_1<Void, Void>(), null);
 148             passed++;
 149         } catch (final Throwable exp) {
 150             log("Parse API failed: " + file.getAbsolutePath() + " : " + exp);
 151             if (VERBOSE) {
 152                 exp.printStackTrace(System.out);
 153             }
 154             failed++;
 155         }
 156 
 157         if (VERBOSE) {
 158             log("Done parsing via parser API " + file.getAbsolutePath());
 159         }
 160     }
 161     
 162     private static char[] byteToCharArray(final byte[] bytes) {
 163         Charset cs = StandardCharsets.UTF_8;
 164         int start = 0;
 165         // BOM detection.
 166         if (bytes.length > 1 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) {
 167             start = 2;
 168             cs = StandardCharsets.UTF_16BE;
 169         } else if (bytes.length > 1 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) {
 170             start = 2;
 171             cs = StandardCharsets.UTF_16LE;
 172         } else if (bytes.length > 2 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
 173             start = 3;
 174             cs = StandardCharsets.UTF_8;
 175         } else if (bytes.length > 3 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE && bytes[2] == 0 && bytes[3] == 0) {
 176             start = 4;
 177             cs = Charset.forName("UTF-32LE");
 178         } else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF) {
 179             start = 4;
 180             cs = Charset.forName("UTF-32BE");
 181         }
 182 
 183         return new String(bytes, start, bytes.length - start, cs).toCharArray();
 184     }
 185     
 186     private static char[] readFully(final File file) throws IOException {
 187         final byte[] buf = Files.readAllBytes(file.toPath());
 188         return byteToCharArray(buf);
 189     }
 190 }