|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, 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 3 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 3 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 | + * 3 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 | +package com.oracle.truffle.r.test.library.utils; |
| 24 | + |
| 25 | +import com.oracle.truffle.r.runtime.FastROptions; |
| 26 | +import com.oracle.truffle.r.runtime.REnvVars; |
| 27 | +import static org.junit.Assert.assertFalse; |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | + |
| 30 | +import com.oracle.truffle.r.runtime.RInternalError; |
| 31 | +import com.oracle.truffle.r.runtime.context.RContext; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import com.oracle.truffle.r.test.TestBase; |
| 35 | +import java.io.ByteArrayOutputStream; |
| 36 | +import java.io.IOException; |
| 37 | +import java.io.PrintStream; |
| 38 | +import java.nio.file.FileSystems; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.nio.file.attribute.PosixFilePermission; |
| 42 | +import java.util.Set; |
| 43 | + |
| 44 | +public class TestFastrErrorsLog extends TestBase { |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testThrowInternalError() throws Exception { |
| 48 | + boolean origValue = FastROptions.PrintErrorStacktracesToFile.getBooleanValue(); |
| 49 | + FastROptions.setValue(FastROptions.PrintErrorStacktracesToFile.name(), true); |
| 50 | + try { |
| 51 | + String fastrErrorsLog = "fastr_errors"; // Copy of RInternalError.FASTR_ERRORS_LOG |
| 52 | + int pid = RContext.getInitialPid(); |
| 53 | + String baseName = fastrErrorsLog + "_pid" + Integer.toString(pid) + ".log"; |
| 54 | + if (RContext.isEmbedded()) { |
| 55 | + String dir1 = System.getProperty("java.io.tmpdir"); |
| 56 | + Path path1 = FileSystems.getDefault().getPath(dir1, baseName); |
| 57 | + try { |
| 58 | + assertFalse(Files.exists(path1)); |
| 59 | + reportError(); |
| 60 | + assertTrue(Files.exists(path1)); |
| 61 | + } finally { |
| 62 | + try { |
| 63 | + Files.delete(path1); |
| 64 | + } catch (IOException ex) { |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + String dir1 = System.getProperty("user.dir"); |
| 69 | + Path path1 = FileSystems.getDefault().getPath(dir1, baseName); |
| 70 | + |
| 71 | + String dir2 = System.getProperty("user.home"); |
| 72 | + Path path2 = FileSystems.getDefault().getPath(dir2, baseName); |
| 73 | + |
| 74 | + String dir3 = System.getProperty("java.io.tmpdir"); |
| 75 | + Path path3 = FileSystems.getDefault().getPath(dir3, baseName); |
| 76 | + |
| 77 | + String dir4 = REnvVars.rHome(); |
| 78 | + Path path4 = FileSystems.getDefault().getPath(dir4, baseName); |
| 79 | + |
| 80 | + try { |
| 81 | + // Intentionally not testing that the particular log file is not present yet |
| 82 | + // to not depend on in which directory the test gets started. |
| 83 | + // assertFalse(Files.exists(path1)); |
| 84 | + reportError(); |
| 85 | + assertTrue(Files.exists(path1)); |
| 86 | + setReadonly(path1, true); |
| 87 | + |
| 88 | + reportError(); |
| 89 | + assertTrue(Files.exists(path2)); |
| 90 | + setReadonly(path2, true); |
| 91 | + |
| 92 | + reportError(); |
| 93 | + assertTrue(Files.exists(path3)); |
| 94 | + setReadonly(path3, true); |
| 95 | + |
| 96 | + reportError(); |
| 97 | + assertTrue(Files.exists(path4)); |
| 98 | + } finally { |
| 99 | + try { |
| 100 | + setReadonly(path1, false); |
| 101 | + Files.delete(path1); |
| 102 | + } catch (IOException ex) { |
| 103 | + } |
| 104 | + try { |
| 105 | + setReadonly(path2, false); |
| 106 | + Files.delete(path2); |
| 107 | + } catch (IOException ex) { |
| 108 | + } |
| 109 | + try { |
| 110 | + setReadonly(path3, false); |
| 111 | + Files.delete(path3); |
| 112 | + } catch (IOException ex) { |
| 113 | + } |
| 114 | + try { |
| 115 | + Files.delete(path4); |
| 116 | + } catch (IOException ex) { |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } finally { |
| 121 | + FastROptions.setValue(FastROptions.PrintErrorStacktracesToFile.name(), origValue); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static void setReadonly(Path path, boolean readonly) throws IOException { |
| 126 | + Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path); |
| 127 | + if (readonly) { |
| 128 | + perms.remove(PosixFilePermission.OWNER_WRITE); |
| 129 | + } else { |
| 130 | + perms.add(PosixFilePermission.OWNER_WRITE); |
| 131 | + } |
| 132 | + Files.setPosixFilePermissions(path, perms); |
| 133 | + } |
| 134 | + |
| 135 | + private static void reportError() { |
| 136 | + PrintStream temp = new PrintStream(new ByteArrayOutputStream()); |
| 137 | + PrintStream origErr = System.err; |
| 138 | + System.setErr(temp); |
| 139 | + RInternalError.reportError(new Throwable("Throwing expected error in test.")); |
| 140 | + System.setErr(origErr); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments