1645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez// Copyright 2014 The Chromium Authors. All rights reserved.
2645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez// Use of this source code is governed by a BSD-style license that can be
3645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez// found in the LICENSE file.
4645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
5645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavezdefine([
6645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "console",
7645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "file",
8645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "gin/test/expect",
9645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom",
10645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/js/buffer",
11645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/js/codec",
12645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/js/connection",
13645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/js/connector",
14645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/js/core",
15645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/js/test/validation_test_input_parser",
16645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/js/router",
17645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    "mojo/public/js/validator",
18645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez], function(console,
19645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            file,
20645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            expect,
21645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            testInterface,
22645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            buffer,
23645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            codec,
24645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            connection,
25645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            connector,
26645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            core,
27645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            parser,
28645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            router,
29645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            validator) {
30645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
31645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  var noError = validator.validationError.NONE;
32645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
33645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function checkTestMessageParser() {
34645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function TestMessageParserFailure(message, input) {
35645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      this.message = message;
36645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      this.input = input;
37645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
38645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
39645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    TestMessageParserFailure.prototype.toString = function() {
40645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      return 'Error: ' + this.message + ' for "' + this.input + '"';
41645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
42645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
43645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function checkData(data, expectedData, input) {
44645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      if (data.byteLength != expectedData.byteLength) {
45645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        var s = "message length (" + data.byteLength + ") doesn't match " +
46645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            "expected length: " + expectedData.byteLength;
47645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        throw new TestMessageParserFailure(s, input);
48645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      }
49645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
50645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      for (var i = 0; i < data.byteLength; i++) {
51645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        if (data.getUint8(i) != expectedData.getUint8(i)) {
52645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          var s = 'message data mismatch at byte offset ' + i;
53645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          throw new TestMessageParserFailure(s, input);
54645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        }
55645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      }
56645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
57645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
58645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testFloatItems() {
59645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var input = '[f]+.3e9 [d]-10.03';
60645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var msg = parser.parseTestMessage(input);
61645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var expectedData = new buffer.Buffer(12);
62645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setFloat32(0, +.3e9);
63645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setFloat64(4, -10.03);
64645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      checkData(msg.buffer, expectedData, input);
65645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
66645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
67645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testUnsignedIntegerItems() {
68645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var input = '[u1]0x10// hello world !! \n\r  \t [u2]65535 \n' +
69645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          '[u4]65536 [u8]0xFFFFFFFFFFFFF 0 0Xff';
70645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var msg = parser.parseTestMessage(input);
71645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var expectedData = new buffer.Buffer(17);
72645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint8(0, 0x10);
73645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint16(1, 65535);
74645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint32(3, 65536);
75645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint64(7, 0xFFFFFFFFFFFFF);
76645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint8(15, 0);
77645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint8(16, 0xff);
78645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      checkData(msg.buffer, expectedData, input);
79645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
80645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
81645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testSignedIntegerItems() {
82645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var input = '[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40';
83645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var msg = parser.parseTestMessage(input);
84645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var expectedData = new buffer.Buffer(15);
85645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setInt64(0, -0x800);
86645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setInt8(8, -128);
87645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setInt16(9, 0);
88645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setInt32(11, -40);
89645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      checkData(msg.buffer, expectedData, input);
90645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
91645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
92645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testByteItems() {
93645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var input = '[b]00001011 [b]10000000  // hello world\n [b]00000000';
94645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var msg = parser.parseTestMessage(input);
95645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var expectedData = new buffer.Buffer(3);
96645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint8(0, 11);
97645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint8(1, 128);
98645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint8(2, 0);
99645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      checkData(msg.buffer, expectedData, input);
100645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
101645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
102645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testAnchors() {
103645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var input = '[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar';
104645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var msg = parser.parseTestMessage(input);
105645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var expectedData = new buffer.Buffer(14);
106645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint32(0, 14);
107645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint8(4, 0);
108645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint64(5, 9);
109645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint8(13, 0);
110645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      checkData(msg.buffer, expectedData, input);
111645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
112645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
113645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testHandles() {
114645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var input = '// This message has handles! \n[handles]50 [u8]2';
115645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var msg = parser.parseTestMessage(input);
116645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var expectedData = new buffer.Buffer(8);
117645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expectedData.setUint64(0, 2);
118645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
119645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      if (msg.handleCount != 50) {
120645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        var s = 'wrong handle count (' + msg.handleCount + ')';
121645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        throw new TestMessageParserFailure(s, input);
122645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      }
123645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      checkData(msg.buffer, expectedData, input);
124645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
125645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
126645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testEmptyInput() {
127645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var msg = parser.parseTestMessage('');
128645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      if (msg.buffer.byteLength != 0)
129645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        throw new TestMessageParserFailure('expected empty message', '');
130645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
131645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
132645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testBlankInput() {
133645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var input = '    \t  // hello world \n\r \t// the answer is 42   ';
134645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var msg = parser.parseTestMessage(input);
135645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      if (msg.buffer.byteLength != 0)
136645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        throw new TestMessageParserFailure('expected empty message', input);
137645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
138645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
139645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    function testInvalidInput() {
140645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      function parserShouldFail(input) {
141645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        try {
142645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          parser.parseTestMessage(input);
143645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        } catch (e) {
144645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          if (e instanceof parser.InputError)
145645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            return;
146645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          throw new TestMessageParserFailure(
147645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez            'unexpected exception ' + e.toString(), input);
148645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        }
149645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        throw new TestMessageParserFailure("didn't detect invalid input", file);
150645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      }
151645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
152645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      ['/ hello world',
153645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[u1]x',
154645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[u2]-1000',
155645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[u1]0x100',
156645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[s2]-0x8001',
157645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[b]1',
158645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[b]1111111k',
159645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[dist4]unmatched',
160645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[anchr]hello [dist8]hello',
161645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '[dist4]a [dist4]a [anchr]a',
162645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       // '[dist4]a [anchr]a [dist4]a [anchr]a',
163645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez       '0 [handles]50'
164645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      ].forEach(parserShouldFail);
165645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
166645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
167645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    try {
168645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testFloatItems();
169645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testUnsignedIntegerItems();
170645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testSignedIntegerItems();
171645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testByteItems();
172645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testInvalidInput();
173645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testEmptyInput();
174645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testBlankInput();
175645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testHandles();
176645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testAnchors();
177645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    } catch (e) {
178645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      return e.toString();
179645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
180645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    return null;
181645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
182645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
183645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function getMessageTestFiles(prefix) {
184645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var sourceRoot = file.getSourceRootDirectory();
185645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(sourceRoot).not.toBeNull();
186645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
187645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var testDir = sourceRoot +
188645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      "/mojo/public/interfaces/bindings/tests/data/validation/";
189645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var testFiles = file.getFilesInDirectory(testDir);
190645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(testFiles).not.toBeNull();
191645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(testFiles.length).toBeGreaterThan(0);
192645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
193645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    // The matching ".data" pathnames with the extension removed.
194645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    return testFiles.filter(function(s) {
195645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      return s.substr(-5) == ".data" && s.indexOf(prefix) == 0;
196645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }).map(function(s) {
197645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      return testDir + s.slice(0, -5);
198645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    });
199645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
200645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
201645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function readTestMessage(filename) {
202645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var contents = file.readFileToString(filename + ".data");
203645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(contents).not.toBeNull();
204645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    return parser.parseTestMessage(contents);
205645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
206645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
207645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function readTestExpected(filename) {
208645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var contents = file.readFileToString(filename + ".expected");
209645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(contents).not.toBeNull();
210645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    return contents.trim();
211645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
212645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
213645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function checkValidationResult(testFile, err) {
214645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var actualResult = (err === noError) ? "PASS" : err;
215645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var expectedResult = readTestExpected(testFile);
216645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    if (actualResult != expectedResult)
217645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      console.log("[Test message validation failed: " + testFile + " ]");
218645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(actualResult).toEqual(expectedResult);
219645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
220645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
221645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testMessageValidation(prefix, filters) {
222645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var testFiles = getMessageTestFiles(prefix);
223645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(testFiles.length).toBeGreaterThan(0);
224645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
225645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    for (var i = 0; i < testFiles.length; i++) {
226645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // TODO(hansmuller) Temporarily skipping array pointer overflow tests
227645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // because JS numbers are limited to 53 bits.
228645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // TODO(yzshen) Skipping struct versioning tests (tests with "mthd11"
229645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // in the name) because the feature is not supported in JS yet.
230645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // TODO(yzshen) Skipping enum validation tests (tests with "enum" in the
231645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // name) because the feature is not supported in JS yet. crbug.com/581390
232645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // TODO(rudominer): Temporarily skipping 'no-such-method',
233645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // 'invalid_request_flags', and 'invalid_response_flags' until additional
234645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // logic in *RequestValidator and *ResponseValidator is ported from
235645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      // cpp to js.
236645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      if (testFiles[i].indexOf("overflow") != -1 ||
237645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          testFiles[i].indexOf("mthd11") != -1 ||
238645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          testFiles[i].indexOf("enum") != -1 ||
239645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          testFiles[i].indexOf("no_such_method") != -1 ||
240645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          testFiles[i].indexOf("invalid_request_flags") != -1 ||
241645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          testFiles[i].indexOf("invalid_response_flags") != -1) {
242645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        console.log("[Skipping " + testFiles[i] + "]");
243645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        continue;
244645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      }
245645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
246645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var testMessage = readTestMessage(testFiles[i]);
247645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var handles = new Array(testMessage.handleCount);
248645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var message = new codec.Message(testMessage.buffer, handles);
249645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var messageValidator = new validator.Validator(message);
250645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
251645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var err = messageValidator.validateMessageHeader();
252645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      for (var j = 0; err === noError && j < filters.length; ++j)
253645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        err = filters[j](messageValidator);
254645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
255645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      checkValidationResult(testFiles[i], err);
256645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
257645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
258645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
259645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testConformanceMessageValidation() {
260645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testMessageValidation("conformance_", [
261645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testInterface.ConformanceTestInterface.validateRequest]);
262645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
263645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
264645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testBoundsCheckMessageValidation() {
265645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testMessageValidation("boundscheck_", [
266645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testInterface.BoundsCheckTestInterface.validateRequest]);
267645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
268645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
269645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testResponseConformanceMessageValidation() {
270645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testMessageValidation("resp_conformance_", [
271645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testInterface.ConformanceTestInterface.validateResponse]);
272645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
273645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
274645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testResponseBoundsCheckMessageValidation() {
275645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testMessageValidation("resp_boundscheck_", [
276645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testInterface.BoundsCheckTestInterface.validateResponse]);
277645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
278645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
279645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testIntegratedMessageValidation(testFilesPattern,
280645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez                                           localFactory,
281645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez                                           remoteFactory) {
282645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var testFiles = getMessageTestFiles(testFilesPattern);
283645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(testFiles.length).toBeGreaterThan(0);
284645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
285645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var testMessagePipe = core.createMessagePipe();
286645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(testMessagePipe.result).toBe(core.RESULT_OK);
287645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    var testConnection = new connection.TestConnection(
288645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testMessagePipe.handle1, localFactory, remoteFactory);
289645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
290645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    for (var i = 0; i < testFiles.length; i++) {
291645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var testMessage = readTestMessage(testFiles[i]);
292645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var handles = new Array(testMessage.handleCount);
293645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
294645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var writeMessageValue = core.writeMessage(
295645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          testMessagePipe.handle0,
296645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          new Uint8Array(testMessage.buffer.arrayBuffer),
297645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          new Array(testMessage.handleCount),
298645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez          core.WRITE_MESSAGE_FLAG_NONE);
299645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      expect(writeMessageValue).toBe(core.RESULT_OK);
300645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
301645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      var validationError = noError;
302645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testConnection.router_.validationErrorHandler = function(err) {
303645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        validationError = err;
304645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      }
305645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
306645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      testConnection.router_.connector_.waitForNextMessage();
307645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez      checkValidationResult(testFiles[i], validationError);
308645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    }
309645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
310645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testConnection.close();
311645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    expect(core.close(testMessagePipe.handle0)).toBe(core.RESULT_OK);
312645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
313645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
314645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testIntegratedMessageHeaderValidation() {
315645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testIntegratedMessageValidation(
316645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        "integration_msghdr",
317645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testInterface.IntegrationTestInterface.stubClass,
318645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        undefined);
319645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testIntegratedMessageValidation(
320645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        "integration_msghdr",
321645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        undefined,
322645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testInterface.IntegrationTestInterface.proxyClass);
323645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
324645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
325645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testIntegratedRequestMessageValidation() {
326645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testIntegratedMessageValidation(
327645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        "integration_intf_rqst",
328645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testInterface.IntegrationTestInterface.stubClass,
329645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        undefined);
330645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
331645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
332645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  function testIntegratedResponseMessageValidation() {
333645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez    testIntegratedMessageValidation(
334645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        "integration_intf_resp",
335645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        undefined,
336645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez        testInterface.IntegrationTestInterface.proxyClass);
337645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  }
338645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
339645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  expect(checkTestMessageParser()).toBeNull();
340645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  testConformanceMessageValidation();
341645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  testBoundsCheckMessageValidation();
342645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  testResponseConformanceMessageValidation();
343645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  testResponseBoundsCheckMessageValidation();
344645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  testIntegratedMessageHeaderValidation();
345645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  testIntegratedResponseMessageValidation();
346645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  testIntegratedRequestMessageValidation();
347645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez
348645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez  this.result = "PASS";
349645501c2ab19a559ce82a1d5a29ced159a4c30fbLuis Hector Chavez});
350