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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
// Copyright 2015 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview a fuzzing JSON generator.
*
* This class generates a random JSON-compatible array object under the
* following rules, (n) n being the relative weight of enum/discrete values
* of a stochastic variable:
* 1. Total number of elements for the generated JSON array: [1, 10)
* 2. Each element: with message (1), array (1)
* 3. Each message: number of fields: [0, 5); field type: with
* message (5), string (1), number (1), boolean (1), array (1), null (1)
* 4. Message may be nested, and will be terminated randomly with
* a max depth equal to 5
* 5. Each array: length [0, 5), and may be nested too
*/
goog.provide('goog.labs.testing.JsonFuzzing');
goog.require('goog.string');
goog.require('goog.testing.PseudoRandom');
/**
* The JSON fuzzing generator.
*
* @param {!goog.labs.testing.JsonFuzzing.Options=} opt_options Configuration
* for the fuzzing json generator.
* @param {number=} opt_seed The seed for the random generator.
* @constructor
* @struct
*/
goog.labs.testing.JsonFuzzing = function(opt_options, opt_seed) {
/**
* The config options.
* @private {!goog.labs.testing.JsonFuzzing.Options}
*/
this.options_ =
opt_options || {jsonSize: 10, numFields: 5, arraySize: 5, maxDepth: 5};
/**
* The random generator
* @private {!goog.testing.PseudoRandom}
*/
this.random_ = new goog.testing.PseudoRandom(opt_seed);
/**
* The depth limit, which defaults to 5.
* @private {number}
*/
this.maxDepth_ = this.options_.maxDepth;
};
/**
* Configuration spec.
*
* jsonSize: default to [1, 10) for the entire JSON object (array)
* numFields: default to [0, 5)
* arraySize: default to [0, 5) for the length of nested arrays
* maxDepth: default to 5
*
* @typedef {{
* jsonSize: number,
* numFields: number,
* arraySize: number,
* maxDepth: number
* }}
*/
goog.labs.testing.JsonFuzzing.Options;
/**
* Gets a fuzzily-generated JSON object (an array).
*
* TODO(user): whitespaces
*
* @return {!Array} A new JSON compliant array object.
*/
goog.labs.testing.JsonFuzzing.prototype.newArray = function() {
var result = [];
var depth = 0;
var maxSize = this.options_.jsonSize;
var size = this.nextInt(1, maxSize);
for (var i = 0; i < size; i++) {
result.push(this.nextElm_(depth));
}
return result;
};
/**
* Gets a new integer.
*
* @param {number} min Inclusive
* @param {number} max Exclusive
* @return {number} A random integer
*/
goog.labs.testing.JsonFuzzing.prototype.nextInt = function(min, max) {
var random = this.random_.random();
return Math.floor(random * (max - min)) + min;
};
/**
* Gets a new element type, randomly.
*
* @return {number} 0 for message and 1 for array.
* @private
*/
goog.labs.testing.JsonFuzzing.prototype.nextElmType_ = function() {
var random = this.random_.random();
if (random < 0.5) {
return 0;
} else {
return 1;
}
};
/**
* Enum type for the field type (of a message).
* @enum {number}
* @private
*/
goog.labs.testing.JsonFuzzing.FieldType_ = {
/**
* Message field.
*/
MESSAGE: 0,
/**
* Array field.
*/
ARRAY: 1,
/**
* String field.
*/
STRING: 2,
/**
* Numeric field.
*/
NUMBER: 3,
/**
* Boolean field.
*/
BOOLEAN: 4,
/**
* Null field.
*/
NULL: 5
};
/**
* Get a new field type, randomly.
*
* @return {!goog.labs.testing.JsonFuzzing.FieldType_} the field type.
* @private
*/
goog.labs.testing.JsonFuzzing.prototype.nextFieldType_ = function() {
var FieldType = goog.labs.testing.JsonFuzzing.FieldType_;
var random = this.random_.random();
if (random < 0.5) {
return FieldType.MESSAGE;
} else if (random < 0.6) {
return FieldType.ARRAY;
} else if (random < 0.7) {
return FieldType.STRING;
} else if (random < 0.8) {
return FieldType.NUMBER;
} else if (random < 0.9) {
return FieldType.BOOLEAN;
} else {
return FieldType.NULL;
}
};
/**
* Gets a new element.
*
* @param {number} depth The depth
* @return {!Object} a random element, msg or array
* @private
*/
goog.labs.testing.JsonFuzzing.prototype.nextElm_ = function(depth) {
switch (this.nextElmType_()) {
case 0:
return this.nextMessage_(depth);
case 1:
return this.nextArray_(depth);
default:
throw new Error('invalid elm type encounted.');
}
};
/**
* Gets a new message.
*
* @param {number} depth The depth
* @return {!Object} a random message.
* @private
*/
goog.labs.testing.JsonFuzzing.prototype.nextMessage_ = function(depth) {
if (depth > this.maxDepth_) {
return {};
}
var numFields = this.options_.numFields;
var random_num = this.nextInt(0, numFields);
var result = {};
// TODO(user): unicode and random keys
for (var i = 0; i < random_num; i++) {
switch (this.nextFieldType_()) {
case 0:
result['f' + i] = this.nextMessage_(depth++);
continue;
case 1:
result['f' + i] = this.nextArray_(depth++);
continue;
case 2:
result['f' + i] = goog.string.getRandomString();
continue;
case 3:
result['f' + i] = this.nextNumber_();
continue;
case 4:
result['f' + i] = this.nextBoolean_();
continue;
case 5:
result['f' + i] = null;
continue;
default:
throw new Error('invalid field type encounted.');
}
}
return result;
};
/**
* Gets a new array.
*
* @param {number} depth The depth
* @return {!Array} a random array.
* @private
*/
goog.labs.testing.JsonFuzzing.prototype.nextArray_ = function(depth) {
if (depth > this.maxDepth_) {
return [];
}
var size = this.options_.arraySize;
var random_size = this.nextInt(0, size);
var result = [];
// mixed content
for (var i = 0; i < random_size; i++) {
switch (this.nextFieldType_()) {
case 0:
result.push(this.nextMessage_(depth++));
continue;
case 1:
result.push(this.nextArray_(depth++));
continue;
case 2:
result.push(goog.string.getRandomString());
continue;
case 3:
result.push(this.nextNumber_());
continue;
case 4:
result.push(this.nextBoolean_());
continue;
case 5:
result.push(null);
continue;
default:
throw new Error('invalid field type encounted.');
}
}
return result;
};
/**
* Gets a new boolean.
*
* @return {boolean} a random boolean.
* @private
*/
goog.labs.testing.JsonFuzzing.prototype.nextBoolean_ = function() {
var random = this.random_.random();
return random < 0.5;
};
/**
* Gets a new number.
*
* @return {number} a random number..
* @private
*/
goog.labs.testing.JsonFuzzing.prototype.nextNumber_ = function() {
var result = this.random_.random();
var random = this.random_.random();
if (random < 0.5) {
result *= 1000;
}
random = this.random_.random();
if (random < 0.5) {
result = Math.floor(result);
}
random = this.random_.random();
if (random < 0.5) {
result *= -1;
}
// TODO(user); more random numbers
return result;
};
|