d8-os.js revision 589d6979ff2ef66fca2d8fa51404c369ca5e9250
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
33var TEST_DIR = "/tmp/d8-os-test-directory-" + ((Math.random() * (1<<30)) | 0);
34
35
36function arg_error(str) {
37  try {
38    eval(str);
39  } catch (e) {
40    assertTrue(/rgument/.test(e), str);
41  }
42}
43
44
45function 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
56if (this.os && os.system) {
57  try {
58    // Delete the dir if it is lying around from last time.
59    os.system("ls", [TEST_DIR]);
60    os.system("rm", ["-r", TEST_DIR]);
61  } catch (e) {
62  }
63  os.mkdirp(TEST_DIR);
64  os.chdir(TEST_DIR);
65  try {
66    // Check the chdir worked.
67    os.system('ls', [TEST_DIR]);
68    // Simple create dir.
69    os.mkdirp("dir");
70    // Create dir in dir.
71    os.mkdirp("dir/foo");
72    // Check that they are there.
73    os.system('ls', ['dir/foo']);
74    // Check that we can detect when something is not there.
75    assertThrows("os.system('ls', ['dir/bar']);", "dir not there");
76    // Check that mkdirp makes intermediate directories.
77    os.mkdirp("dir2/foo");
78    os.system("ls", ["dir2/foo"]);
79    // Check that mkdirp doesn't mind if the dir is already there.
80    os.mkdirp("dir2/foo");
81    os.mkdirp("dir2/foo/");
82    // Check that mkdirp can cope with trailing /
83    os.mkdirp("dir3/");
84    os.system("ls", ["dir3"]);
85    // Check that we get an error if the name is taken by a file.
86    os.system("sh", ["-c", "echo foo > file1"]);
87    os.system("ls", ["file1"]);
88    assertThrows("os.mkdirp('file1');", "mkdir over file1");
89    assertThrows("os.mkdirp('file1/foo');", "mkdir over file2");
90    assertThrows("os.mkdirp('file1/');", "mkdir over file3");
91    assertThrows("os.mkdirp('file1/foo/');", "mkdir over file4");
92    // Create a dir we cannot read.
93    os.mkdirp("dir4", 0);
94    // This test fails if you are root since root can read any dir.
95    assertThrows("os.chdir('dir4');", "chdir dir4 I");
96    os.rmdir("dir4");
97    assertThrows("os.chdir('dir4');", "chdir dir4 II");
98    // Set umask.
99    var old_umask = os.umask(0777);
100    // Create a dir we cannot read.
101    os.mkdirp("dir5");
102    // This test fails if you are root since root can read any dir.
103    assertThrows("os.chdir('dir5');", "cd dir5 I");
104    os.rmdir("dir5");
105    assertThrows("os.chdir('dir5');", "chdir dir5 II");
106    os.umask(old_umask);
107
108    os.mkdirp("hest/fisk/../fisk/ged");
109    os.system("ls", ["hest/fisk/ged"]);
110
111    os.setenv("FOO", "bar");
112    var environment = os.system("printenv");
113    assertTrue(/FOO=bar/.test(environment));
114
115    // Check we time out.
116    var have_sleep = true;
117    var have_echo = true;
118    try {
119      os.system("ls", ["/bin/sleep"]);
120    } catch (e) {
121      have_sleep = false;
122    }
123    try {
124      os.system("ls", ["/bin/echo"]);
125    } catch (e) {
126      have_echo = false;
127    }
128    if (have_sleep) {
129      assertThrows("os.system('sleep', ['2000'], 200);", "sleep 1");
130
131      // Check we time out with total time.
132      assertThrows("os.system('sleep', ['2000'], -1, 200);", "sleep 2");
133
134      // Check that -1 means no timeout.
135      os.system('sleep', ['1'], -1, -1);
136
137    }
138
139    // Check that we don't fill up the process table with zombies.
140    // Disabled because it's too slow.
141    if (have_echo) {
142      //for (var i = 0; i < 65536; i++) {
143      assertEquals("baz\n", os.system("echo", ["baz"]));
144      //}
145    }
146  } finally {
147    os.system("rm", ["-r", TEST_DIR]);
148  }
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}
185