diff options
author | mikaelpeltier <mikaelpeltier@google.com> | 2014-05-23 09:51:09 +0200 |
---|---|---|
committer | mikaelpeltier <mikaelpeltier@google.com> | 2014-05-23 12:12:45 +0200 |
commit | ab1f6e865434219a2b93b8ed201235edbfa2088b (patch) | |
tree | 30ab0822f3bd9bcf4b0ad4a203753a5591883606 | |
parent | b25f2dfdd5f793a9c416f0e2e65c5a1a360d31f1 (diff) | |
download | toolchain_jack-ab1f6e865434219a2b93b8ed201235edbfa2088b.zip toolchain_jack-ab1f6e865434219a2b93b8ed201235edbfa2088b.tar.gz toolchain_jack-ab1f6e865434219a2b93b8ed201235edbfa2088b.tar.bz2 |
Add tests on file access errors
Change-Id: If1c0125732fafb11deaf4e43094a6f3d03f57e00
3 files changed, 237 insertions, 1 deletions
diff --git a/jack/tests/com/android/jack/errorhandling/ErrorHandlingAllTests.java b/jack/tests/com/android/jack/errorhandling/ErrorHandlingAllTests.java index 1b2558b..f5cc5b7 100644 --- a/jack/tests/com/android/jack/errorhandling/ErrorHandlingAllTests.java +++ b/jack/tests/com/android/jack/errorhandling/ErrorHandlingAllTests.java @@ -20,6 +20,6 @@ import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses(value = {JackFormatErrorTest.class}) +@SuiteClasses(value = {JackFormatErrorTest.class, FileAccessErrorTest.class}) public class ErrorHandlingAllTests { }
\ No newline at end of file diff --git a/jack/tests/com/android/jack/errorhandling/FileAccessErrorTest.java b/jack/tests/com/android/jack/errorhandling/FileAccessErrorTest.java new file mode 100644 index 0000000..12cdf70 --- /dev/null +++ b/jack/tests/com/android/jack/errorhandling/FileAccessErrorTest.java @@ -0,0 +1,215 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.errorhandling; + +import com.android.jack.JackIOException; +import com.android.jack.Main; +import com.android.jack.Options; +import com.android.jack.TestTools; +import com.android.jack.category.KnownBugs; +import com.android.jack.frontend.FrontendCompilationException; +import com.android.sched.util.config.PropertyIdException; + +import junit.framework.Assert; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * JUnit test checking Jack behavior on file access error. + */ +public class FileAccessErrorTest { + + @BeforeClass + public static void setUpClass() { + Main.class.getClassLoader().setDefaultAssertionStatus(true); + } + + /** + * Checks that compilation fails correctly when folder to generate jack files is not readable. + */ + @Test + public void testFileAccessError001() throws Exception { + TestingEnvironment ite = new TestingEnvironment(); + + ite.addFile(ite.getSourceFolder(), "jack.incremental", "A.java", + "package jack.incremental; \n"+ + "public class A {} \n"); + + Options options = new Options(); + List<String> ecjArgs = new ArrayList<String>(); + ecjArgs.add(ite.getTestingFolder().getAbsolutePath()); + options.setEcjArguments(ecjArgs); + options.setClasspath(TestTools.getDefaultBootclasspathString()); + File jackOutputFile = TestTools.createTempDir("ErrorHandlingTest_", "001"); + options.setJayceOutputDir(jackOutputFile); + if (!jackOutputFile.setReadable(false)) { + Assert.fail("Fails to change file permissions of " + jackOutputFile.getAbsolutePath()); + } + + try { + ite.compile(options); + Assert.fail(); + } catch (PropertyIdException e) { + // Failure is ok since jack output folder is not readable + } finally { + if (!jackOutputFile.setReadable(true)) { + Assert.fail("Fails to change file permissions of " + jackOutputFile.getAbsolutePath()); + } + } + } + + /** + * Checks that compilation fails correctly when folder containing jack files is not readable. + */ + @Test + @Category(KnownBugs.class) + public void testFileAccessError002() throws Exception { + TestingEnvironment ite = new TestingEnvironment(); + + ite.addFile(ite.getSourceFolder(), "jack.incremental", "A.java", + "package jack.incremental; \n"+ + "public class A {} \n"); + + Options options = new Options(); + List<String> ecjArgs = new ArrayList<String>(); + ecjArgs.add(ite.getTestingFolder().getAbsolutePath()); + options.setEcjArguments(ecjArgs); + options.setClasspath(TestTools.getDefaultBootclasspathString()); + File jackOutputFile = TestTools.createTempDir("ErrorHandlingTest_", "001"); + options.setJayceOutputDir(jackOutputFile); + + ite.compile(options); + + ite.deleteJavaFile(ite.getSourceFolder(), "jack.incremental", "A.java"); + ite.addFile(ite.getSourceFolder(), "jack.incremental", "B.java", + "package jack.incremental; \n"+ + "public class B extends A {} \n"); + + options = new Options(); + ecjArgs = new ArrayList<String>(); + ecjArgs.add(ite.getTestingFolder().getAbsolutePath()); + options.setEcjArguments(ecjArgs); + options.setClasspath(TestTools.getDefaultBootclasspathString()); + options.addJayceImport(jackOutputFile); + + // Modify read permission of folder containing jack files + if (!jackOutputFile.setReadable(false)) { + Assert.fail("Fails to change file permissions of " + jackOutputFile.getAbsolutePath()); + } + try { + ite.startErrRedirection(); + ite.compile(options); + Assert.fail(); + } finally { + Assert.assertEquals("", ite.endErrorRedirection()); + if (!jackOutputFile.setReadable(true)) { + Assert.fail("Fails to change file permissions of " + jackOutputFile.getAbsolutePath()); + } + } + } + + /** + * Checks that compilation fails correctly when source file is not readable. + */ + @Test + public void testFileAccessError003() throws Exception { + TestingEnvironment ite = new TestingEnvironment(); + + File a = ite.addFile(ite.getSourceFolder(), "jack.incremental", "A.java", + "package jack.incremental; \n"+ + "public class A {} \n"); + if (!a.setReadable(false)) { + Assert.fail("Fails to change file permissions of " + a.getAbsolutePath()); + } + + Options options = new Options(); + List<String> ecjArgs = new ArrayList<String>(); + ecjArgs.add(ite.getTestingFolder().getAbsolutePath()); + options.setEcjArguments(ecjArgs); + options.setClasspath(TestTools.getDefaultBootclasspathString()); + + try { + ite.compile(options); + Assert.fail(); + } catch (FrontendCompilationException e) { + // Failure is ok since source file is not readable + } finally { + if (!a.setReadable(true)) { + Assert.fail("Fails to change file permissions of " + a.getAbsolutePath()); + } + } + } + + /** + * Checks that compilation fails correctly when jack file is not readable. + */ + @Test + public void testFileAccessError004() throws Exception { + TestingEnvironment ite = new TestingEnvironment(); + + ite.addFile(ite.getSourceFolder(), "jack.incremental", "A.java", + "package jack.incremental; \n"+ + "public class A {} \n"); + + Options options = new Options(); + List<String> ecjArgs = new ArrayList<String>(); + ecjArgs.add(ite.getTestingFolder().getAbsolutePath()); + options.setEcjArguments(ecjArgs); + options.setClasspath(TestTools.getDefaultBootclasspathString()); + options.setJayceOutputDir(ite.getJackFolder()); + + ite.compile(options); + + ite.deleteJavaFile(ite.getSourceFolder(), "jack.incremental", "A.java"); + + ite.addFile(ite.getSourceFolder(),"jack.incremental", "B.java", + "package jack.incremental; \n"+ + "public class B extends A {} \n"); + + options = new Options(); + ecjArgs = new ArrayList<String>(); + ecjArgs.add(ite.getTestingFolder().getAbsolutePath()); + options.setEcjArguments(ecjArgs); + options.setClasspath( + TestTools.getDefaultBootclasspathString() + File.pathSeparator + ite.getJackFolder()); + + try { + for (File jackFile : ite.getJackFiles(ite.getJackFolder())) { + if (!jackFile.setReadable(false)) { + Assert.fail("Fails to change file permissions of " + jackFile.getAbsolutePath()); + } + } + ite.startErrRedirection(); + ite.compile(options); + } catch (JackIOException e) { + // Failure is ok since jack file is not readable + } finally { + Assert.assertEquals("", ite.endErrorRedirection()); + for (File jackFile : ite.getJackFiles(ite.getJackFolder())) { + if (!jackFile.setReadable(true)) { + Assert.fail("Fails to change file permissions of " + jackFile.getAbsolutePath()); + } + } + } + } +} diff --git a/jack/tests/com/android/jack/errorhandling/TestingEnvironment.java b/jack/tests/com/android/jack/errorhandling/TestingEnvironment.java index ddaf96b..25464d2 100644 --- a/jack/tests/com/android/jack/errorhandling/TestingEnvironment.java +++ b/jack/tests/com/android/jack/errorhandling/TestingEnvironment.java @@ -23,6 +23,7 @@ import com.android.jack.NothingToDoException; import com.android.jack.Options; import com.android.jack.TestTools; import com.android.jack.backend.jayce.ImportConflictException; +import com.android.jack.backend.jayce.JayceFileImporter; import com.android.jack.ir.ast.JPackageLookupException; import com.android.jack.ir.ast.JTypeLookupException; import com.android.jack.util.NamingTools; @@ -33,6 +34,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; @@ -142,4 +145,22 @@ public class TestingEnvironment { errRedirectStream = new PrintStream(baos); System.setErr(errRedirectStream); } + + @Nonnull + public List<File> getJackFiles(@Nonnull File folder) { + assert folder.isDirectory(); + List<File> jackFiles = new ArrayList<File>(); + fillJackFiles(folder, jackFiles); + return jackFiles; + } + + private void fillJackFiles(@Nonnull File file, @Nonnull List<File> jackFiles) { + if (file.isDirectory()) { + for (File subFile : file.listFiles()) { + fillJackFiles(subFile, jackFiles); + } + } else if (file.getName().endsWith(JayceFileImporter.JAYCE_FILE_EXTENSION)) { + jackFiles.add(file); + } + } } |