OLD | NEW |
(Empty) | |
| 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 |
| 33 function arg_error(str) { |
| 34 try { |
| 35 eval(str); |
| 36 } catch (e) { |
| 37 assertTrue(/rgument/.test(e), str); |
| 38 } |
| 39 } |
| 40 |
| 41 |
| 42 function str_error(str) { |
| 43 var e = new Object(); |
| 44 e.toString = function() { throw new Error("foo bar"); } |
| 45 try { |
| 46 eval(str); |
| 47 } catch (exception) { |
| 48 assertTrue(/tring conversion/.test(exception), str); |
| 49 } |
| 50 } |
| 51 |
| 52 |
| 53 if (this.os && os.system) { |
| 54 try { |
| 55 // Delete the dir if it is lying around from last time. |
| 56 os.system("ls", ["d8-os-test-directory"]); |
| 57 os.system("rm", ["-r", "d8-os-test-directory"]); |
| 58 } catch (e) { |
| 59 } |
| 60 os.mkdirp("d8-os-test-directory"); |
| 61 os.chdir("d8-os-test-directory"); |
| 62 // Check the chdir worked. |
| 63 os.system('ls', ['../d8-os-test-directory']); |
| 64 // Simple create dir. |
| 65 os.mkdirp("dir"); |
| 66 // Create dir in dir. |
| 67 os.mkdirp("dir/foo"); |
| 68 // Check that they are there. |
| 69 os.system('ls', ['dir/foo']); |
| 70 // Check that we can detect when something is not there. |
| 71 assertThrows("os.system('ls', ['dir/bar']);", "dir not there"); |
| 72 // Check that mkdirp makes intermediate directories. |
| 73 os.mkdirp("dir2/foo"); |
| 74 os.system("ls", ["dir2/foo"]); |
| 75 // Check that mkdirp doesn't mind if the dir is already there. |
| 76 os.mkdirp("dir2/foo"); |
| 77 os.mkdirp("dir2/foo/"); |
| 78 // Check that mkdirp can cope with trailing / |
| 79 os.mkdirp("dir3/"); |
| 80 os.system("ls", ["dir3"]); |
| 81 // Check that we get an error if the name is taken by a file. |
| 82 os.system("sh", ["-c", "echo foo > file1"]); |
| 83 os.system("ls", ["file1"]); |
| 84 assertThrows("os.mkdirp('file1');", "mkdir over file1"); |
| 85 assertThrows("os.mkdirp('file1/foo');", "mkdir over file2"); |
| 86 assertThrows("os.mkdirp('file1/');", "mkdir over file3"); |
| 87 assertThrows("os.mkdirp('file1/foo/');", "mkdir over file4"); |
| 88 // Create a dir we cannot read. |
| 89 os.mkdirp("dir4", 0); |
| 90 // This test fails if you are root since root can read any dir. |
| 91 assertThrows("os.chdir('dir4');", "chdir dir4 I"); |
| 92 os.rmdir("dir4"); |
| 93 assertThrows("os.chdir('dir4');", "chdir dir4 II"); |
| 94 // Set umask. |
| 95 var old_umask = os.umask(0777); |
| 96 // Create a dir we cannot read. |
| 97 os.mkdirp("dir5"); |
| 98 // This test fails if you are root since root can read any dir. |
| 99 assertThrows("os.chdir('dir5');", "cd dir5 I"); |
| 100 os.rmdir("dir5"); |
| 101 assertThrows("os.chdir('dir5');", "chdir dir5 II"); |
| 102 os.umask(old_umask); |
| 103 |
| 104 os.setenv("FOO", "bar"); |
| 105 var environment = os.system("printenv"); |
| 106 assertTrue(/FOO=bar/.test(environment)); |
| 107 |
| 108 // Check we time out. |
| 109 var have_sleep = true; |
| 110 var have_echo = true; |
| 111 try { |
| 112 os.system("ls", ["/bin/sleep"]); |
| 113 } catch (e) { |
| 114 have_sleep = false; |
| 115 } |
| 116 try { |
| 117 os.system("ls", ["/bin/echo"]); |
| 118 } catch (e) { |
| 119 have_echo = false; |
| 120 } |
| 121 if (have_sleep) { |
| 122 assertThrows("os.system('sleep', ['2000'], 200);", "sleep 1"); |
| 123 |
| 124 // Check we time out with total time. |
| 125 assertThrows("os.system('sleep', ['2000'], -1, 200);", "sleep 2"); |
| 126 |
| 127 // Check that -1 means no timeout. |
| 128 os.system('sleep', ['1'], -1, -1); |
| 129 |
| 130 } |
| 131 |
| 132 // Check that we don't fill up the process table with zombies. |
| 133 // Disabled because it's too slow. |
| 134 if (have_echo) { |
| 135 //for (var i = 0; i < 65536; i++) { |
| 136 assertEquals("baz\n", os.system("echo", ["baz"])); |
| 137 //} |
| 138 } |
| 139 |
| 140 os.chdir(".."); |
| 141 os.system("rm", ["-r", "d8-os-test-directory"]); |
| 142 |
| 143 // Too few args. |
| 144 arg_error("os.umask();"); |
| 145 arg_error("os.system();"); |
| 146 arg_error("os.mkdirp();"); |
| 147 arg_error("os.chdir();"); |
| 148 arg_error("os.setenv();"); |
| 149 arg_error("os.rmdir();"); |
| 150 |
| 151 // Too many args. |
| 152 arg_error("os.setenv('FOO=bar');"); |
| 153 arg_error("os.umask(0, 0);"); |
| 154 arg_error("os.system('ls', [], -1, -1, -1);"); |
| 155 arg_error("os.mkdirp('foo', 0, 0)"); |
| 156 arg_error("os.chdir('foo', 'bar')"); |
| 157 arg_error("os.rmdir('foo', 'bar');"); |
| 158 |
| 159 // Wrong kind of args. |
| 160 arg_error("os.umask([]);"); |
| 161 arg_error("os.system('ls', 'foo');"); |
| 162 arg_error("os.system('ls', 123);"); |
| 163 arg_error("os.system('ls', [], 'foo');"); |
| 164 arg_error("os.system('ls', [], -1, 'foo');"); |
| 165 arg_error("os.mkdirp('foo', 'bar');"); |
| 166 |
| 167 // Test broken toString(). |
| 168 str_error("os.system(e);"); |
| 169 str_error("os.system('ls', [e]);"); |
| 170 str_error("os.system('ls', ['.', e]);"); |
| 171 str_error("os.system('ls', [e, '.']);"); |
| 172 str_error("os.mkdirp(e);"); |
| 173 str_error("os.setenv(e, 'goo');"); |
| 174 str_error("os.setenv('goo', e);"); |
| 175 str_error("os.chdir(e);"); |
| 176 str_error("os.rmdir(e);"); |
| 177 } |
OLD | NEW |