summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/transform-streams/lipfuzz.any.js
blob: e334705db44354cbb79c130f25cbbd65d58633ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// META: global=window,worker,shadowrealm
'use strict';

class LipFuzzTransformer {
  constructor(substitutions) {
    this.substitutions = substitutions;
    this.partialChunk = '';
    this.lastIndex = undefined;
  }

  transform(chunk, controller) {
    chunk = this.partialChunk + chunk;
    this.partialChunk = '';
    // lastIndex is the index of the first character after the last substitution.
    this.lastIndex = 0;
    chunk = chunk.replace(/\{\{([a-zA-Z0-9_-]+)\}\}/g, this.replaceTag.bind(this));
    // Regular expression for an incomplete template at the end of a string.
    const partialAtEndRegexp = /\{(\{([a-zA-Z0-9_-]+(\})?)?)?$/g;
    // Avoid looking at any characters that have already been substituted.
    partialAtEndRegexp.lastIndex = this.lastIndex;
    this.lastIndex = undefined;
    const match = partialAtEndRegexp.exec(chunk);
    if (match) {
      this.partialChunk = chunk.substring(match.index);
      chunk = chunk.substring(0, match.index);
    }
    controller.enqueue(chunk);
  }

  flush(controller) {
    if (this.partialChunk.length > 0) {
      controller.enqueue(this.partialChunk);
    }
  }

  replaceTag(match, p1, offset) {
    let replacement = this.substitutions[p1];
    if (replacement === undefined) {
      replacement = '';
    }
    this.lastIndex = offset + replacement.length;
    return replacement;
  }
}

const substitutions = {
  in1: 'out1',
  in2: 'out2',
  quine: '{{quine}}',
  bogusPartial: '{{incompleteResult}'
};

const cases = [
  {
    input: [''],
    output: ['']
  },
  {
    input: [],
    output: []
  },
  {
    input: ['{{in1}}'],
    output: ['out1']
  },
  {
    input: ['z{{in1}}'],
    output: ['zout1']
  },
  {
    input: ['{{in1}}q'],
    output: ['out1q']
  },
  {
    input: ['{{in1}}{{in1}'],
    output: ['out1', '{{in1}']
  },
  {
    input: ['{{in1}}{{in1}', '}'],
    output: ['out1', 'out1']
  },
  {
    input: ['{{in1', '}}'],
    output: ['', 'out1']
  },
  {
    input: ['{{', 'in1}}'],
    output: ['', 'out1']
  },
  {
    input: ['{', '{in1}}'],
    output: ['', 'out1']
  },
  {
    input: ['{{', 'in1}'],
    output: ['', '', '{{in1}']
  },
  {
    input: ['{'],
    output: ['', '{']
  },
  {
    input: ['{', ''],
    output: ['', '', '{']
  },
  {
    input: ['{', '{', 'i', 'n', '1', '}', '}'],
    output: ['', '', '', '', '', '', 'out1']
  },
  {
    input: ['{{in1}}{{in2}}{{in1}}'],
    output: ['out1out2out1']
  },
  {
    input: ['{{wrong}}'],
    output: ['']
  },
  {
    input: ['{{wron', 'g}}'],
    output: ['', '']
  },
  {
    input: ['{{quine}}'],
    output: ['{{quine}}']
  },
  {
    input: ['{{bogusPartial}}'],
    output: ['{{incompleteResult}']
  },
  {
    input: ['{{bogusPartial}}}'],
    output: ['{{incompleteResult}}']
  }
];

for (const testCase of cases) {
  const inputChunks = testCase.input;
  const outputChunks = testCase.output;
  promise_test(() => {
    const lft = new TransformStream(new LipFuzzTransformer(substitutions));
    const writer = lft.writable.getWriter();
    const promises = [];
    for (const inputChunk of inputChunks) {
      promises.push(writer.write(inputChunk));
    }
    promises.push(writer.close());
    const reader = lft.readable.getReader();
    let readerChain = Promise.resolve();
    for (const outputChunk of outputChunks) {
      readerChain = readerChain.then(() => {
        return reader.read().then(({ value, done }) => {
          assert_false(done, `done should be false when reading ${outputChunk}`);
          assert_equals(value, outputChunk, `value should match outputChunk`);
        });
      });
    }
    readerChain = readerChain.then(() => {
      return reader.read().then(({ done }) => assert_true(done, `done should be true`));
    });
    promises.push(readerChain);
    return Promise.all(promises);
  }, `testing "${inputChunks}" (length ${inputChunks.length})`);
}