Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // Test the OS module of d8. This test only makes sense with d8. It |
| 29 | // only does non-trivial work on Unix since os.system() is not currently |
| 30 | // implemented on Windows, and even if it were then many of the things |
| 31 | // we are calling would not be available. |
| 32 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 33 | var TEST_DIR = "/tmp/d8-os-test-directory-" + ((Math.random() * (1<<30)) | 0); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 34 | |
| 35 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | function arg_error(str) { |
| 37 | try { |
| 38 | eval(str); |
| 39 | } catch (e) { |
| 40 | assertTrue(/rgument/.test(e), str); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | function str_error(str) { |
| 46 | var e = new Object(); |
| 47 | e.toString = function() { throw new Error("foo bar"); } |
| 48 | try { |
| 49 | eval(str); |
| 50 | } catch (exception) { |
| 51 | assertTrue(/tring conversion/.test(exception), str); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | if (this.os && os.system) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 57 | // Ensure that we have a valid working directory. |
| 58 | os.chdir("/tmp"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | try { |
| 60 | // Delete the dir if it is lying around from last time. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 61 | os.system("ls", [TEST_DIR]); |
| 62 | os.system("rm", ["-r", TEST_DIR]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | } catch (e) { |
| 64 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 65 | os.mkdirp(TEST_DIR); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | try { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 67 | // Check the chdir worked. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 68 | os.system('ls', [TEST_DIR]); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 69 | // Simple create dir. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 70 | os.mkdirp(TEST_DIR + "/dir"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 71 | // Create dir in dir. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 72 | os.mkdirp(TEST_DIR + "/dir/foo"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 73 | // Check that they are there. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 74 | os.system('ls', [TEST_DIR + '/dir/foo']); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 75 | // Check that we can detect when something is not there. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | assertThrows("os.system('ls', [TEST_DIR + '/dir/bar']);"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 77 | // Check that mkdirp makes intermediate directories. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 78 | os.mkdirp(TEST_DIR + "/dir2/foo"); |
| 79 | os.system("ls", [TEST_DIR + "/dir2/foo"]); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 80 | // Check that mkdirp doesn't mind if the dir is already there. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 81 | os.mkdirp(TEST_DIR + "/dir2/foo"); |
| 82 | os.mkdirp(TEST_DIR + "/dir2/foo/"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 83 | // Check that mkdirp can cope with trailing / |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 84 | os.mkdirp(TEST_DIR + "/dir3/"); |
| 85 | os.system("ls", [TEST_DIR + "/dir3"]); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 86 | // Check that we get an error if the name is taken by a file. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 87 | os.system("sh", ["-c", "echo foo > " + TEST_DIR + "/file1"]); |
| 88 | os.system("ls", [TEST_DIR + "/file1"]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | assertThrows("os.mkdirp(TEST_DIR + '/file1');"); |
| 90 | assertThrows("os.mkdirp(TEST_DIR + '/file1/foo');"); |
| 91 | assertThrows("os.mkdirp(TEST_DIR + '/file1/');"); |
| 92 | assertThrows("os.mkdirp(TEST_DIR + '/file1/foo/');"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 93 | // Create a dir we cannot read. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 94 | os.mkdirp(TEST_DIR + "/dir4", 0); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 95 | // This test fails if you are root since root can read any dir. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 96 | assertThrows("os.chdir(TEST_DIR + '/dir4');"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 97 | os.rmdir(TEST_DIR + "/dir4"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 98 | assertThrows("os.chdir(TEST_DIR + '/dir4');"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 99 | |
| 100 | // Set umask. This changes the umask for the whole process and is |
| 101 | // the reason why the test cannot be run multi-threaded. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 102 | var old_umask = os.umask(0777); |
| 103 | // Create a dir we cannot read. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 104 | os.mkdirp(TEST_DIR + "/dir5"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 105 | // This test fails if you are root since root can read any dir. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 106 | assertThrows("os.chdir(TEST_DIR + '/dir5');"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 107 | os.rmdir(TEST_DIR + "/dir5"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 108 | assertThrows("os.chdir(TEST_DIR + '/dir5');"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 109 | os.umask(old_umask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 111 | os.mkdirp(TEST_DIR + "/hest/fisk/../fisk/ged"); |
| 112 | os.system("ls", [TEST_DIR + "/hest/fisk/ged"]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 114 | os.setenv("FOO", "bar"); |
| 115 | var environment = os.system("printenv"); |
| 116 | assertTrue(/FOO=bar/.test(environment)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 118 | // Check we time out. |
| 119 | var have_sleep = true; |
| 120 | var have_echo = true; |
| 121 | try { |
| 122 | os.system("ls", ["/bin/sleep"]); |
| 123 | } catch (e) { |
| 124 | have_sleep = false; |
| 125 | } |
| 126 | try { |
| 127 | os.system("ls", ["/bin/echo"]); |
| 128 | } catch (e) { |
| 129 | have_echo = false; |
| 130 | } |
| 131 | if (have_sleep) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 132 | assertThrows("os.system('sleep', ['2000'], 20);"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 134 | // Check we time out with total time. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | assertThrows("os.system('sleep', ['2000'], -1, 20);"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 136 | |
| 137 | // Check that -1 means no timeout. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | os.system('sleep', ['1'], -1, -1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 139 | |
| 140 | } |
| 141 | |
| 142 | // Check that we don't fill up the process table with zombies. |
| 143 | // Disabled because it's too slow. |
| 144 | if (have_echo) { |
| 145 | //for (var i = 0; i < 65536; i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | assertEquals("baz\n", os.system("echo", ["baz"])); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 147 | //} |
| 148 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 149 | |
| 150 | // Too few args. |
| 151 | arg_error("os.umask();"); |
| 152 | arg_error("os.system();"); |
| 153 | arg_error("os.mkdirp();"); |
| 154 | arg_error("os.chdir();"); |
| 155 | arg_error("os.setenv();"); |
| 156 | arg_error("os.rmdir();"); |
| 157 | |
| 158 | // Too many args. |
| 159 | arg_error("os.setenv('FOO=bar');"); |
| 160 | arg_error("os.umask(0, 0);"); |
| 161 | arg_error("os.system('ls', [], -1, -1, -1);"); |
| 162 | arg_error("os.mkdirp('foo', 0, 0)"); |
| 163 | arg_error("os.chdir('foo', 'bar')"); |
| 164 | arg_error("os.rmdir('foo', 'bar');"); |
| 165 | |
| 166 | // Wrong kind of args. |
| 167 | arg_error("os.umask([]);"); |
| 168 | arg_error("os.system('ls', 'foo');"); |
| 169 | arg_error("os.system('ls', 123);"); |
| 170 | arg_error("os.system('ls', [], 'foo');"); |
| 171 | arg_error("os.system('ls', [], -1, 'foo');"); |
| 172 | arg_error("os.mkdirp('foo', 'bar');"); |
| 173 | |
| 174 | // Test broken toString(). |
| 175 | str_error("os.system(e);"); |
| 176 | str_error("os.system('ls', [e]);"); |
| 177 | str_error("os.system('ls', ['.', e]);"); |
| 178 | str_error("os.system('ls', [e, '.']);"); |
| 179 | str_error("os.mkdirp(e);"); |
| 180 | str_error("os.setenv(e, 'goo');"); |
| 181 | str_error("os.setenv('goo', e);"); |
| 182 | str_error("os.chdir(e);"); |
| 183 | str_error("os.rmdir(e);"); |
| 184 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 185 | } finally { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 186 | os.system("rm", ["-r", TEST_DIR]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | } |