OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library TypedArraysRangeCheckTest; | |
6 import 'package:unittest/unittest.dart'; | |
7 import 'package:unittest/html_config.dart'; | |
8 import 'dart:html'; | 5 import 'dart:html'; |
9 import 'dart:typed_data'; | 6 import 'dart:typed_data'; |
10 | 7 |
| 8 import 'package:expect/minitest.dart'; |
| 9 |
11 const N = 1024; | 10 const N = 1024; |
12 | 11 |
13 class _TestList { | 12 class _TestList { |
14 _TestList(int n); | 13 _TestList(int n); |
15 | 14 |
16 operator[](int i) => i; | 15 operator[](int i) => i; |
17 operator[]=(int i, v) {} | 16 operator[]=(int i, v) {} |
18 } | 17 } |
19 | 18 |
20 _obfuscatedList() { | 19 _obfuscatedList() { |
21 var a = new Uint8List(N); | 20 var a = new Uint8List(N); |
22 var b = new _TestList(N); | 21 var b = new _TestList(N); |
23 var k = 0; | 22 var k = 0; |
24 for (int i = 0; i < 10; ++i) { | 23 for (int i = 0; i < 10; ++i) { |
25 k += i; | 24 k += i; |
26 } | 25 } |
27 return (k == 45) ? a : b; | 26 return (k == 45) ? a : b; |
28 } | 27 } |
29 | 28 |
30 main() { | 29 main() { |
31 useHtmlConfiguration(); | |
32 | |
33 // Only perform tests if ArrayBuffer is supported. | 30 // Only perform tests if ArrayBuffer is supported. |
34 if (!Platform.supportsTypedData) { | 31 if (!Platform.supportsTypedData) { |
35 return; | 32 return; |
36 } | 33 } |
37 | 34 |
38 test('outOfRangeAccess_dynamic', () { | 35 test('outOfRangeAccess', () { |
39 var a = _obfuscatedList(); | 36 var a = _obfuscatedList(); |
40 | 37 |
41 expect(() => a[a.length], throws); | 38 expect(() => a[a.length], throws); |
42 expect(() => a[a.length + 1], throws); | 39 expect(() => a[a.length + 1], throws); |
43 expect(() => a[a.length + N], throws); | 40 expect(() => a[a.length + N], throws); |
44 | 41 |
45 expect(() => a[-1], throws); | 42 expect(() => a[-1], throws); |
46 expect(() => a[1.5], throws); | 43 expect(() => a[1.5], throws); |
47 expect(() => a['length'], throws); | 44 expect(() => a['length'], throws); |
48 | 45 |
49 expect(() => a[a.length] = 0xdeadbeef, throws); | 46 expect(() => a[a.length] = 0xdeadbeef, throws); |
50 expect(() => a[a.length + 1] = 0xdeadbeef, throws); | 47 expect(() => a[a.length + 1] = 0xdeadbeef, throws); |
51 expect(() => a[a.length + N] = 0xdeadbeef, throws); | 48 expect(() => a[a.length + N] = 0xdeadbeef, throws); |
52 | 49 |
53 expect(() => a[-1] = 0xdeadbeef, throws); | 50 expect(() => a[-1] = 0xdeadbeef, throws); |
54 expect(() => a[1.5] = 0xdeadbeef, throws); | 51 expect(() => a[1.5] = 0xdeadbeef, throws); |
55 expect(() => a['length'] = 1, throws); | 52 expect(() => a['length'] = 1, throws); |
56 }); | 53 }); |
57 | |
58 test('outOfRange_typed', () { | |
59 Uint8List a = new Uint8List(N); | |
60 | |
61 expect(() => a[a.length], throws); | |
62 expect(() => a[a.length + 1], throws); | |
63 expect(() => a[a.length + N], throws); | |
64 | |
65 expect(() => a[-1], throws); | |
66 expect(() => a[1.5], throws); | |
67 expect(() => a['length'], throws); | |
68 | |
69 expect(() => a[a.length] = 0xdeadbeef, throws); | |
70 expect(() => a[a.length + 1] = 0xdeadbeef, throws); | |
71 expect(() => a[a.length + N] = 0xdeadbeef, throws); | |
72 | |
73 expect(() => a[-1] = 0xdeadbeef, throws); | |
74 expect(() => a[1.5] = 0xdeadbeef, throws); | |
75 expect(() => a['length'] = 1, throws); | |
76 }); | |
77 } | 54 } |
OLD | NEW |