blob: 630a39e9660d84651a62e56f44356e76feeab8bf [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
33function arg_error(str) {
34 try {
35 eval(str);
36 } catch (e) {
37 assertTrue(/rgument/.test(e), str);
38 }
39}
40
41
42function 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
53if (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.mkdirp("hest/fisk/../fisk/ged");
105 os.system("ls", ["hest/fisk/ged"]);
106
107 os.setenv("FOO", "bar");
108 var environment = os.system("printenv");
109 assertTrue(/FOO=bar/.test(environment));
110
111 // Check we time out.
112 var have_sleep = true;
113 var have_echo = true;
114 try {
115 os.system("ls", ["/bin/sleep"]);
116 } catch (e) {
117 have_sleep = false;
118 }
119 try {
120 os.system("ls", ["/bin/echo"]);
121 } catch (e) {
122 have_echo = false;
123 }
124 if (have_sleep) {
125 assertThrows("os.system('sleep', ['2000'], 200);", "sleep 1");
126
127 // Check we time out with total time.
128 assertThrows("os.system('sleep', ['2000'], -1, 200);", "sleep 2");
129
130 // Check that -1 means no timeout.
131 os.system('sleep', ['1'], -1, -1);
132
133 }
134
135 // Check that we don't fill up the process table with zombies.
136 // Disabled because it's too slow.
137 if (have_echo) {
138 //for (var i = 0; i < 65536; i++) {
139 assertEquals("baz\n", os.system("echo", ["baz"]));
140 //}
141 }
142
143 os.chdir("..");
144 os.system("rm", ["-r", "d8-os-test-directory"]);
145
146 // Too few args.
147 arg_error("os.umask();");
148 arg_error("os.system();");
149 arg_error("os.mkdirp();");
150 arg_error("os.chdir();");
151 arg_error("os.setenv();");
152 arg_error("os.rmdir();");
153
154 // Too many args.
155 arg_error("os.setenv('FOO=bar');");
156 arg_error("os.umask(0, 0);");
157 arg_error("os.system('ls', [], -1, -1, -1);");
158 arg_error("os.mkdirp('foo', 0, 0)");
159 arg_error("os.chdir('foo', 'bar')");
160 arg_error("os.rmdir('foo', 'bar');");
161
162 // Wrong kind of args.
163 arg_error("os.umask([]);");
164 arg_error("os.system('ls', 'foo');");
165 arg_error("os.system('ls', 123);");
166 arg_error("os.system('ls', [], 'foo');");
167 arg_error("os.system('ls', [], -1, 'foo');");
168 arg_error("os.mkdirp('foo', 'bar');");
169
170 // Test broken toString().
171 str_error("os.system(e);");
172 str_error("os.system('ls', [e]);");
173 str_error("os.system('ls', ['.', e]);");
174 str_error("os.system('ls', [e, '.']);");
175 str_error("os.mkdirp(e);");
176 str_error("os.setenv(e, 'goo');");
177 str_error("os.setenv('goo', e);");
178 str_error("os.chdir(e);");
179 str_error("os.rmdir(e);");
180}