OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import "package:http_server/http_server.dart"; | 8 import "package:http_server/http_server.dart"; |
9 import 'package:path/path.dart' as pathos; | 9 import 'package:path/path.dart' as pathos; |
10 import "package:unittest/unittest.dart"; | 10 import "package:unittest/unittest.dart"; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 }); | 92 }); |
93 | 93 |
94 group('serve-dir', () { | 94 group('serve-dir', () { |
95 group('top-level', () { | 95 group('top-level', () { |
96 testVirtualDir('simple', (dir) { | 96 testVirtualDir('simple', (dir) { |
97 var virDir = new VirtualDirectory(dir.path); | 97 var virDir = new VirtualDirectory(dir.path); |
98 virDir.allowDirectoryListing = true; | 98 virDir.allowDirectoryListing = true; |
99 | 99 |
100 return getAsString(virDir, '/') | 100 return getAsString(virDir, '/') |
101 .then((result) { | 101 .then((result) { |
102 expect(result, contains('Index of /')); | 102 expect(result, contains('Index of /')); |
103 }); | 103 }); |
104 }); | 104 }); |
105 | 105 |
106 testVirtualDir('files', (dir) { | 106 testVirtualDir('files', (dir) { |
107 var virDir = new VirtualDirectory(dir.path); | 107 var virDir = new VirtualDirectory(dir.path); |
108 for (int i = 0; i < 10; i++) { | 108 for (int i = 0; i < 10; i++) { |
109 new File('${dir.path}/$i').createSync(); | 109 new File('${dir.path}/$i').createSync(); |
110 } | 110 } |
111 virDir.allowDirectoryListing = true; | 111 virDir.allowDirectoryListing = true; |
112 | 112 |
113 return getAsString(virDir, '/') | 113 return getAsString(virDir, '/') |
114 .then((result) { | 114 .then((result) { |
115 expect(result, contains('Index of /')); | 115 expect(result, contains('Index of /')); |
116 }); | 116 }); |
117 }); | 117 }); |
118 | 118 |
119 testVirtualDir('dirs', (dir) { | 119 testVirtualDir('dirs', (dir) { |
120 var virDir = new VirtualDirectory(dir.path); | 120 var virDir = new VirtualDirectory(dir.path); |
121 for (int i = 0; i < 10; i++) { | 121 for (int i = 0; i < 10; i++) { |
122 new Directory('${dir.path}/$i').createSync(); | 122 new Directory('${dir.path}/$i').createSync(); |
123 } | 123 } |
124 virDir.allowDirectoryListing = true; | 124 virDir.allowDirectoryListing = true; |
125 | 125 |
126 return getAsString(virDir, '/') | 126 return getAsString(virDir, '/') |
127 .then((result) { | 127 .then((result) { |
128 expect(result, contains('Index of /')); | 128 expect(result, contains('Index of /')); |
129 }); | 129 }); |
130 }); | 130 }); |
131 | 131 |
| 132 testVirtualDir('encoded-dir', (dir) { |
| 133 var virDir = new VirtualDirectory(dir.path); |
| 134 new Directory('${dir.path}/alert(\'hacked!\');').createSync(); |
| 135 virDir.allowDirectoryListing = true; |
| 136 |
| 137 return getAsString(virDir, '/alert(\'hacked!\');') |
| 138 .then((result) { |
| 139 expect(result, contains('/alert('hacked!');/')); |
| 140 }); |
| 141 }); |
| 142 |
| 143 testVirtualDir('encoded-path', (dir) { |
| 144 var virDir = new VirtualDirectory(dir.path); |
| 145 new Directory('${dir.path}/javascript:alert(document);"').createSync(); |
| 146 virDir.allowDirectoryListing = true; |
| 147 |
| 148 return getAsString(virDir, '/') |
| 149 .then((result) { |
| 150 expect(result, contains('%2Fjavascript%3Aalert(document)%3B%22')); |
| 151 }); |
| 152 }); |
| 153 |
| 154 testVirtualDir('encoded-special', (dir) { |
| 155 var virDir = new VirtualDirectory(dir.path); |
| 156 new Directory('${dir.path}/<>&"').createSync(); |
| 157 virDir.allowDirectoryListing = true; |
| 158 |
| 159 return getAsString(virDir, '/') |
| 160 .then((result) { |
| 161 expect(result, contains('<>&"/')); |
| 162 expect(result, contains('href="%2F%3C%3E%26%22"')); |
| 163 }); |
| 164 }); |
| 165 |
132 if (!Platform.isWindows) { | 166 if (!Platform.isWindows) { |
133 testVirtualDir('recursive-link', (dir) { | 167 testVirtualDir('recursive-link', (dir) { |
134 var link = new Link('${dir.path}/recursive')..createSync('.'); | 168 var link = new Link('${dir.path}/recursive')..createSync('.'); |
135 var virDir = new VirtualDirectory(dir.path); | 169 var virDir = new VirtualDirectory(dir.path); |
136 virDir.allowDirectoryListing = true; | 170 virDir.allowDirectoryListing = true; |
137 | 171 |
138 return Future.wait([ | 172 return Future.wait([ |
139 getAsString(virDir, '/').then( | 173 getAsString(virDir, '/').then( |
140 (s) => s.contains('recursive/')), | 174 (s) => s.contains('recursive/')), |
141 getAsString(virDir, '/').then( | 175 getAsString(virDir, '/').then( |
142 (s) => !s.contains('../')), | 176 (s) => !s.contains('../')), |
143 getAsString(virDir, '/').then( | 177 getAsString(virDir, '/').then( |
144 (s) => s.contains('Index of /')), | 178 (s) => s.contains('Index of /')), |
145 getAsString(virDir, '/recursive').then( | 179 getAsString(virDir, '/recursive').then( |
146 (s) => s.contains('recursive/')), | 180 (s) => s.contains('recursive/')), |
147 getAsString(virDir, '/recursive').then( | 181 getAsString(virDir, '/recursive').then( |
148 (s) => s.contains('../')), | 182 (s) => s.contains('../')), |
149 getAsString(virDir, '/recursive').then( | 183 getAsString(virDir, '/recursive').then( |
150 (s) => s.contains('Index of /recursive'))]) | 184 (s) => s.contains('Index of /recursive'))]) |
151 .then((result) { | 185 .then((result) { |
152 expect(result, equals([true, true, true, true, true, true])); | 186 expect(result, equals([true, true, true, true, true, true])); |
153 }); | 187 }); |
154 }); | 188 }); |
155 } | 189 } |
156 }); | 190 }); |
157 | 191 |
158 group('custom', () { | 192 group('custom', () { |
159 testVirtualDir('simple', (dir) { | 193 testVirtualDir('simple', (dir) { |
160 var virDir = new VirtualDirectory(dir.path); | 194 var virDir = new VirtualDirectory(dir.path); |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 return virDir.serveFile(new File('${d.path}/file'), request); | 564 return virDir.serveFile(new File('${d.path}/file'), request); |
531 }; | 565 }; |
532 | 566 |
533 return getAsString(virDir, '/') | 567 return getAsString(virDir, '/') |
534 .then((result) { | 568 .then((result) { |
535 expect(result, 'file contents'); | 569 expect(result, 'file contents'); |
536 }); | 570 }); |
537 }); | 571 }); |
538 }); | 572 }); |
539 } | 573 } |
OLD | NEW |