OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/policy/core/common/schema.h" | 5 #include "components/policy/core/common/schema.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 " \"properties\": {" | 265 " \"properties\": {" |
266 " \"sub\": {" | 266 " \"sub\": {" |
267 " \"type\": \"object\"," | 267 " \"type\": \"object\"," |
268 " \"properties\": {" | 268 " \"properties\": {" |
269 " \"subsub\": { \"type\": \"string\" }" | 269 " \"subsub\": { \"type\": \"string\" }" |
270 " }" | 270 " }" |
271 " }" | 271 " }" |
272 " }" | 272 " }" |
273 "}", &error); | 273 "}", &error); |
274 ASSERT_TRUE(schema.valid()) << error; | 274 ASSERT_TRUE(schema.valid()) << error; |
275 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 275 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
276 | 276 |
277 schema = schema.GetKnownProperty("sub"); | 277 schema = schema.GetKnownProperty("sub"); |
278 ASSERT_TRUE(schema.valid()); | 278 ASSERT_TRUE(schema.valid()); |
279 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 279 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
280 | 280 |
281 { | 281 { |
282 Schema::Iterator it = schema.GetPropertiesIterator(); | 282 Schema::Iterator it = schema.GetPropertiesIterator(); |
283 ASSERT_FALSE(it.IsAtEnd()); | 283 ASSERT_FALSE(it.IsAtEnd()); |
284 EXPECT_STREQ("subsub", it.key()); | 284 EXPECT_STREQ("subsub", it.key()); |
285 | 285 |
286 schema = it.schema(); | 286 schema = it.schema(); |
287 it.Advance(); | 287 it.Advance(); |
288 EXPECT_TRUE(it.IsAtEnd()); | 288 EXPECT_TRUE(it.IsAtEnd()); |
289 } | 289 } |
290 | 290 |
291 ASSERT_TRUE(schema.valid()); | 291 ASSERT_TRUE(schema.valid()); |
292 EXPECT_EQ(base::Value::TYPE_STRING, schema.type()); | 292 EXPECT_EQ(base::Value::Type::STRING, schema.type()); |
293 | 293 |
294 // This test shouldn't leak nor use invalid memory. | 294 // This test shouldn't leak nor use invalid memory. |
295 } | 295 } |
296 | 296 |
297 TEST(SchemaTest, ValidSchema) { | 297 TEST(SchemaTest, ValidSchema) { |
298 std::string error; | 298 std::string error; |
299 Schema schema = Schema::Parse(kTestSchema, &error); | 299 Schema schema = Schema::Parse(kTestSchema, &error); |
300 ASSERT_TRUE(schema.valid()) << error; | 300 ASSERT_TRUE(schema.valid()) << error; |
301 | 301 |
302 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 302 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
303 EXPECT_FALSE(schema.GetProperty("invalid").valid()); | 303 EXPECT_FALSE(schema.GetProperty("invalid").valid()); |
304 | 304 |
305 Schema sub = schema.GetProperty("Boolean"); | 305 Schema sub = schema.GetProperty("Boolean"); |
306 ASSERT_TRUE(sub.valid()); | 306 ASSERT_TRUE(sub.valid()); |
307 EXPECT_EQ(base::Value::TYPE_BOOLEAN, sub.type()); | 307 EXPECT_EQ(base::Value::Type::BOOLEAN, sub.type()); |
308 | 308 |
309 sub = schema.GetProperty("Integer"); | 309 sub = schema.GetProperty("Integer"); |
310 ASSERT_TRUE(sub.valid()); | 310 ASSERT_TRUE(sub.valid()); |
311 EXPECT_EQ(base::Value::TYPE_INTEGER, sub.type()); | 311 EXPECT_EQ(base::Value::Type::INTEGER, sub.type()); |
312 | 312 |
313 sub = schema.GetProperty("Null"); | 313 sub = schema.GetProperty("Null"); |
314 ASSERT_TRUE(sub.valid()); | 314 ASSERT_TRUE(sub.valid()); |
315 EXPECT_EQ(base::Value::TYPE_NULL, sub.type()); | 315 EXPECT_EQ(base::Value::Type::NONE, sub.type()); |
316 | 316 |
317 sub = schema.GetProperty("Number"); | 317 sub = schema.GetProperty("Number"); |
318 ASSERT_TRUE(sub.valid()); | 318 ASSERT_TRUE(sub.valid()); |
319 EXPECT_EQ(base::Value::TYPE_DOUBLE, sub.type()); | 319 EXPECT_EQ(base::Value::Type::DOUBLE, sub.type()); |
320 | 320 |
321 sub = schema.GetProperty("String"); | 321 sub = schema.GetProperty("String"); |
322 ASSERT_TRUE(sub.valid()); | 322 ASSERT_TRUE(sub.valid()); |
323 EXPECT_EQ(base::Value::TYPE_STRING, sub.type()); | 323 EXPECT_EQ(base::Value::Type::STRING, sub.type()); |
324 | 324 |
325 sub = schema.GetProperty("Array"); | 325 sub = schema.GetProperty("Array"); |
326 ASSERT_TRUE(sub.valid()); | 326 ASSERT_TRUE(sub.valid()); |
327 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); | 327 ASSERT_EQ(base::Value::Type::LIST, sub.type()); |
328 sub = sub.GetItems(); | 328 sub = sub.GetItems(); |
329 ASSERT_TRUE(sub.valid()); | 329 ASSERT_TRUE(sub.valid()); |
330 EXPECT_EQ(base::Value::TYPE_STRING, sub.type()); | 330 EXPECT_EQ(base::Value::Type::STRING, sub.type()); |
331 | 331 |
332 sub = schema.GetProperty("ArrayOfObjects"); | 332 sub = schema.GetProperty("ArrayOfObjects"); |
333 ASSERT_TRUE(sub.valid()); | 333 ASSERT_TRUE(sub.valid()); |
334 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); | 334 ASSERT_EQ(base::Value::Type::LIST, sub.type()); |
335 sub = sub.GetItems(); | 335 sub = sub.GetItems(); |
336 ASSERT_TRUE(sub.valid()); | 336 ASSERT_TRUE(sub.valid()); |
337 EXPECT_EQ(base::Value::TYPE_DICTIONARY, sub.type()); | 337 EXPECT_EQ(base::Value::Type::DICTIONARY, sub.type()); |
338 Schema subsub = sub.GetProperty("one"); | 338 Schema subsub = sub.GetProperty("one"); |
339 ASSERT_TRUE(subsub.valid()); | 339 ASSERT_TRUE(subsub.valid()); |
340 EXPECT_EQ(base::Value::TYPE_STRING, subsub.type()); | 340 EXPECT_EQ(base::Value::Type::STRING, subsub.type()); |
341 subsub = sub.GetProperty("two"); | 341 subsub = sub.GetProperty("two"); |
342 ASSERT_TRUE(subsub.valid()); | 342 ASSERT_TRUE(subsub.valid()); |
343 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub.type()); | 343 EXPECT_EQ(base::Value::Type::INTEGER, subsub.type()); |
344 subsub = sub.GetProperty("invalid"); | 344 subsub = sub.GetProperty("invalid"); |
345 EXPECT_FALSE(subsub.valid()); | 345 EXPECT_FALSE(subsub.valid()); |
346 | 346 |
347 sub = schema.GetProperty("ArrayOfArray"); | 347 sub = schema.GetProperty("ArrayOfArray"); |
348 ASSERT_TRUE(sub.valid()); | 348 ASSERT_TRUE(sub.valid()); |
349 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); | 349 ASSERT_EQ(base::Value::Type::LIST, sub.type()); |
350 sub = sub.GetItems(); | 350 sub = sub.GetItems(); |
351 ASSERT_TRUE(sub.valid()); | 351 ASSERT_TRUE(sub.valid()); |
352 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); | 352 ASSERT_EQ(base::Value::Type::LIST, sub.type()); |
353 sub = sub.GetItems(); | 353 sub = sub.GetItems(); |
354 ASSERT_TRUE(sub.valid()); | 354 ASSERT_TRUE(sub.valid()); |
355 EXPECT_EQ(base::Value::TYPE_STRING, sub.type()); | 355 EXPECT_EQ(base::Value::Type::STRING, sub.type()); |
356 | 356 |
357 sub = schema.GetProperty("Object"); | 357 sub = schema.GetProperty("Object"); |
358 ASSERT_TRUE(sub.valid()); | 358 ASSERT_TRUE(sub.valid()); |
359 ASSERT_EQ(base::Value::TYPE_DICTIONARY, sub.type()); | 359 ASSERT_EQ(base::Value::Type::DICTIONARY, sub.type()); |
360 subsub = sub.GetProperty("one"); | 360 subsub = sub.GetProperty("one"); |
361 ASSERT_TRUE(subsub.valid()); | 361 ASSERT_TRUE(subsub.valid()); |
362 EXPECT_EQ(base::Value::TYPE_BOOLEAN, subsub.type()); | 362 EXPECT_EQ(base::Value::Type::BOOLEAN, subsub.type()); |
363 subsub = sub.GetProperty("two"); | 363 subsub = sub.GetProperty("two"); |
364 ASSERT_TRUE(subsub.valid()); | 364 ASSERT_TRUE(subsub.valid()); |
365 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub.type()); | 365 EXPECT_EQ(base::Value::Type::INTEGER, subsub.type()); |
366 subsub = sub.GetProperty("undeclared"); | 366 subsub = sub.GetProperty("undeclared"); |
367 ASSERT_TRUE(subsub.valid()); | 367 ASSERT_TRUE(subsub.valid()); |
368 EXPECT_EQ(base::Value::TYPE_STRING, subsub.type()); | 368 EXPECT_EQ(base::Value::Type::STRING, subsub.type()); |
369 | 369 |
370 sub = schema.GetProperty("IntegerWithEnums"); | 370 sub = schema.GetProperty("IntegerWithEnums"); |
371 ASSERT_TRUE(sub.valid()); | 371 ASSERT_TRUE(sub.valid()); |
372 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); | 372 ASSERT_EQ(base::Value::Type::INTEGER, sub.type()); |
373 | 373 |
374 sub = schema.GetProperty("IntegerWithEnumsGaps"); | 374 sub = schema.GetProperty("IntegerWithEnumsGaps"); |
375 ASSERT_TRUE(sub.valid()); | 375 ASSERT_TRUE(sub.valid()); |
376 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); | 376 ASSERT_EQ(base::Value::Type::INTEGER, sub.type()); |
377 | 377 |
378 sub = schema.GetProperty("StringWithEnums"); | 378 sub = schema.GetProperty("StringWithEnums"); |
379 ASSERT_TRUE(sub.valid()); | 379 ASSERT_TRUE(sub.valid()); |
380 ASSERT_EQ(base::Value::TYPE_STRING, sub.type()); | 380 ASSERT_EQ(base::Value::Type::STRING, sub.type()); |
381 | 381 |
382 sub = schema.GetProperty("IntegerWithRange"); | 382 sub = schema.GetProperty("IntegerWithRange"); |
383 ASSERT_TRUE(sub.valid()); | 383 ASSERT_TRUE(sub.valid()); |
384 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); | 384 ASSERT_EQ(base::Value::Type::INTEGER, sub.type()); |
385 | 385 |
386 sub = schema.GetProperty("StringWithPattern"); | 386 sub = schema.GetProperty("StringWithPattern"); |
387 ASSERT_TRUE(sub.valid()); | 387 ASSERT_TRUE(sub.valid()); |
388 ASSERT_EQ(base::Value::TYPE_STRING, sub.type()); | 388 ASSERT_EQ(base::Value::Type::STRING, sub.type()); |
389 | 389 |
390 sub = schema.GetProperty("ObjectWithPatternProperties"); | 390 sub = schema.GetProperty("ObjectWithPatternProperties"); |
391 ASSERT_TRUE(sub.valid()); | 391 ASSERT_TRUE(sub.valid()); |
392 ASSERT_EQ(base::Value::TYPE_DICTIONARY, sub.type()); | 392 ASSERT_EQ(base::Value::Type::DICTIONARY, sub.type()); |
393 | 393 |
394 struct { | 394 struct { |
395 const char* expected_key; | 395 const char* expected_key; |
396 base::Value::Type expected_type; | 396 base::Value::Type expected_type; |
397 } kExpectedProperties[] = { | 397 } kExpectedProperties[] = { |
398 { "Array", base::Value::TYPE_LIST }, | 398 { "Array", base::Value::Type::LIST }, |
399 { "ArrayOfArray", base::Value::TYPE_LIST }, | 399 { "ArrayOfArray", base::Value::Type::LIST }, |
400 { "ArrayOfObjectOfArray", base::Value::TYPE_LIST }, | 400 { "ArrayOfObjectOfArray", base::Value::Type::LIST }, |
401 { "ArrayOfObjects", base::Value::TYPE_LIST }, | 401 { "ArrayOfObjects", base::Value::Type::LIST }, |
402 { "Boolean", base::Value::TYPE_BOOLEAN }, | 402 { "Boolean", base::Value::Type::BOOLEAN }, |
403 { "Integer", base::Value::TYPE_INTEGER }, | 403 { "Integer", base::Value::Type::INTEGER }, |
404 { "IntegerWithEnums", base::Value::TYPE_INTEGER }, | 404 { "IntegerWithEnums", base::Value::Type::INTEGER }, |
405 { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER }, | 405 { "IntegerWithEnumsGaps", base::Value::Type::INTEGER }, |
406 { "IntegerWithRange", base::Value::TYPE_INTEGER }, | 406 { "IntegerWithRange", base::Value::Type::INTEGER }, |
407 { "Null", base::Value::TYPE_NULL }, | 407 { "Null", base::Value::Type::NONE }, |
408 { "Number", base::Value::TYPE_DOUBLE }, | 408 { "Number", base::Value::Type::DOUBLE }, |
409 { "Object", base::Value::TYPE_DICTIONARY }, | 409 { "Object", base::Value::Type::DICTIONARY }, |
410 { "ObjectOfArray", base::Value::TYPE_DICTIONARY }, | 410 { "ObjectOfArray", base::Value::Type::DICTIONARY }, |
411 { "ObjectOfObject", base::Value::TYPE_DICTIONARY }, | 411 { "ObjectOfObject", base::Value::Type::DICTIONARY }, |
412 { "ObjectWithPatternProperties", base::Value::TYPE_DICTIONARY }, | 412 { "ObjectWithPatternProperties", base::Value::Type::DICTIONARY }, |
413 { "String", base::Value::TYPE_STRING }, | 413 { "String", base::Value::Type::STRING }, |
414 { "StringWithEnums", base::Value::TYPE_STRING }, | 414 { "StringWithEnums", base::Value::Type::STRING }, |
415 { "StringWithPattern", base::Value::TYPE_STRING }, | 415 { "StringWithPattern", base::Value::Type::STRING }, |
416 }; | 416 }; |
417 Schema::Iterator it = schema.GetPropertiesIterator(); | 417 Schema::Iterator it = schema.GetPropertiesIterator(); |
418 for (size_t i = 0; i < arraysize(kExpectedProperties); ++i) { | 418 for (size_t i = 0; i < arraysize(kExpectedProperties); ++i) { |
419 ASSERT_FALSE(it.IsAtEnd()); | 419 ASSERT_FALSE(it.IsAtEnd()); |
420 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); | 420 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); |
421 ASSERT_TRUE(it.schema().valid()); | 421 ASSERT_TRUE(it.schema().valid()); |
422 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); | 422 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); |
423 it.Advance(); | 423 it.Advance(); |
424 } | 424 } |
425 EXPECT_TRUE(it.IsAtEnd()); | 425 EXPECT_TRUE(it.IsAtEnd()); |
426 } | 426 } |
427 | 427 |
428 TEST(SchemaTest, Lookups) { | 428 TEST(SchemaTest, Lookups) { |
429 std::string error; | 429 std::string error; |
430 | 430 |
431 Schema schema = Schema::Parse("{ \"type\": \"object\" }", &error); | 431 Schema schema = Schema::Parse("{ \"type\": \"object\" }", &error); |
432 ASSERT_TRUE(schema.valid()) << error; | 432 ASSERT_TRUE(schema.valid()) << error; |
433 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 433 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
434 | 434 |
435 // This empty schema should never find named properties. | 435 // This empty schema should never find named properties. |
436 EXPECT_FALSE(schema.GetKnownProperty("").valid()); | 436 EXPECT_FALSE(schema.GetKnownProperty("").valid()); |
437 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); | 437 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); |
438 EXPECT_TRUE(schema.GetPropertiesIterator().IsAtEnd()); | 438 EXPECT_TRUE(schema.GetPropertiesIterator().IsAtEnd()); |
439 | 439 |
440 schema = Schema::Parse( | 440 schema = Schema::Parse( |
441 "{" | 441 "{" |
442 " \"type\": \"object\"," | 442 " \"type\": \"object\"," |
443 " \"properties\": {" | 443 " \"properties\": {" |
444 " \"Boolean\": { \"type\": \"boolean\" }" | 444 " \"Boolean\": { \"type\": \"boolean\" }" |
445 " }" | 445 " }" |
446 "}", &error); | 446 "}", &error); |
447 ASSERT_TRUE(schema.valid()) << error; | 447 ASSERT_TRUE(schema.valid()) << error; |
448 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 448 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
449 | 449 |
450 EXPECT_FALSE(schema.GetKnownProperty("").valid()); | 450 EXPECT_FALSE(schema.GetKnownProperty("").valid()); |
451 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); | 451 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); |
452 EXPECT_TRUE(schema.GetKnownProperty("Boolean").valid()); | 452 EXPECT_TRUE(schema.GetKnownProperty("Boolean").valid()); |
453 | 453 |
454 schema = Schema::Parse( | 454 schema = Schema::Parse( |
455 "{" | 455 "{" |
456 " \"type\": \"object\"," | 456 " \"type\": \"object\"," |
457 " \"properties\": {" | 457 " \"properties\": {" |
458 " \"bb\" : { \"type\": \"null\" }," | 458 " \"bb\" : { \"type\": \"null\" }," |
459 " \"aa\" : { \"type\": \"boolean\" }," | 459 " \"aa\" : { \"type\": \"boolean\" }," |
460 " \"abab\" : { \"type\": \"string\" }," | 460 " \"abab\" : { \"type\": \"string\" }," |
461 " \"ab\" : { \"type\": \"number\" }," | 461 " \"ab\" : { \"type\": \"number\" }," |
462 " \"aba\" : { \"type\": \"integer\" }" | 462 " \"aba\" : { \"type\": \"integer\" }" |
463 " }" | 463 " }" |
464 "}", &error); | 464 "}", &error); |
465 ASSERT_TRUE(schema.valid()) << error; | 465 ASSERT_TRUE(schema.valid()) << error; |
466 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 466 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
467 | 467 |
468 EXPECT_FALSE(schema.GetKnownProperty("").valid()); | 468 EXPECT_FALSE(schema.GetKnownProperty("").valid()); |
469 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); | 469 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); |
470 | 470 |
471 struct { | 471 struct { |
472 const char* expected_key; | 472 const char* expected_key; |
473 base::Value::Type expected_type; | 473 base::Value::Type expected_type; |
474 } kExpectedKeys[] = { | 474 } kExpectedKeys[] = { |
475 { "aa", base::Value::TYPE_BOOLEAN }, | 475 { "aa", base::Value::Type::BOOLEAN }, |
476 { "ab", base::Value::TYPE_DOUBLE }, | 476 { "ab", base::Value::Type::DOUBLE }, |
477 { "aba", base::Value::TYPE_INTEGER }, | 477 { "aba", base::Value::Type::INTEGER }, |
478 { "abab", base::Value::TYPE_STRING }, | 478 { "abab", base::Value::Type::STRING }, |
479 { "bb", base::Value::TYPE_NULL }, | 479 { "bb", base::Value::Type::NONE }, |
480 }; | 480 }; |
481 for (size_t i = 0; i < arraysize(kExpectedKeys); ++i) { | 481 for (size_t i = 0; i < arraysize(kExpectedKeys); ++i) { |
482 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key); | 482 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key); |
483 ASSERT_TRUE(sub.valid()); | 483 ASSERT_TRUE(sub.valid()); |
484 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type()); | 484 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type()); |
485 } | 485 } |
486 } | 486 } |
487 | 487 |
488 TEST(SchemaTest, Wrap) { | 488 TEST(SchemaTest, Wrap) { |
489 const internal::SchemaNode kSchemas[] = { | 489 const internal::SchemaNode kSchemas[] = { |
490 { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node | 490 { base::Value::Type::DICTIONARY, 0 }, // 0: root node |
491 { base::Value::TYPE_BOOLEAN, -1 }, // 1 | 491 { base::Value::Type::BOOLEAN, -1 }, // 1 |
492 { base::Value::TYPE_INTEGER, -1 }, // 2 | 492 { base::Value::Type::INTEGER, -1 }, // 2 |
493 { base::Value::TYPE_DOUBLE, -1 }, // 3 | 493 { base::Value::Type::DOUBLE, -1 }, // 3 |
494 { base::Value::TYPE_STRING, -1 }, // 4 | 494 { base::Value::Type::STRING, -1 }, // 4 |
495 { base::Value::TYPE_LIST, 4 }, // 5: list of strings. | 495 { base::Value::Type::LIST, 4 }, // 5: list of strings. |
496 { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings. | 496 { base::Value::Type::LIST, 5 }, // 6: list of lists of strings. |
497 { base::Value::TYPE_INTEGER, 0 }, // 7: integer enumerations. | 497 { base::Value::Type::INTEGER, 0 }, // 7: integer enumerations. |
498 { base::Value::TYPE_INTEGER, 1 }, // 8: ranged integers. | 498 { base::Value::Type::INTEGER, 1 }, // 8: ranged integers. |
499 { base::Value::TYPE_STRING, 2 }, // 9: string enumerations. | 499 { base::Value::Type::STRING, 2 }, // 9: string enumerations. |
500 { base::Value::TYPE_STRING, 3 }, // 10: string with pattern. | 500 { base::Value::Type::STRING, 3 }, // 10: string with pattern. |
501 }; | 501 }; |
502 | 502 |
503 const internal::PropertyNode kPropertyNodes[] = { | 503 const internal::PropertyNode kPropertyNodes[] = { |
504 { "Boolean", 1 }, // 0 | 504 { "Boolean", 1 }, // 0 |
505 { "Integer", 2 }, // 1 | 505 { "Integer", 2 }, // 1 |
506 { "Number", 3 }, // 2 | 506 { "Number", 3 }, // 2 |
507 { "String", 4 }, // 3 | 507 { "String", 4 }, // 3 |
508 { "List", 5 }, // 4 | 508 { "List", 5 }, // 4 |
509 { "IntEnum", 7 }, // 5 | 509 { "IntEnum", 7 }, // 5 |
510 { "RangedInt", 8 }, // 6 | 510 { "RangedInt", 8 }, // 6 |
(...skipping 28 matching lines...) Expand all Loading... |
539 kSchemas, | 539 kSchemas, |
540 kPropertyNodes, | 540 kPropertyNodes, |
541 kProperties, | 541 kProperties, |
542 kRestriction, | 542 kRestriction, |
543 kIntEnums, | 543 kIntEnums, |
544 kStringEnums, | 544 kStringEnums, |
545 }; | 545 }; |
546 | 546 |
547 Schema schema = Schema::Wrap(&kData); | 547 Schema schema = Schema::Wrap(&kData); |
548 ASSERT_TRUE(schema.valid()); | 548 ASSERT_TRUE(schema.valid()); |
549 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 549 EXPECT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
550 | 550 |
551 struct { | 551 struct { |
552 const char* key; | 552 const char* key; |
553 base::Value::Type type; | 553 base::Value::Type type; |
554 } kExpectedProperties[] = { | 554 } kExpectedProperties[] = { |
555 { "Boolean", base::Value::TYPE_BOOLEAN }, | 555 { "Boolean", base::Value::Type::BOOLEAN }, |
556 { "Integer", base::Value::TYPE_INTEGER }, | 556 { "Integer", base::Value::Type::INTEGER }, |
557 { "Number", base::Value::TYPE_DOUBLE }, | 557 { "Number", base::Value::Type::DOUBLE }, |
558 { "String", base::Value::TYPE_STRING }, | 558 { "String", base::Value::Type::STRING }, |
559 { "List", base::Value::TYPE_LIST }, | 559 { "List", base::Value::Type::LIST }, |
560 { "IntEnum", base::Value::TYPE_INTEGER }, | 560 { "IntEnum", base::Value::Type::INTEGER }, |
561 { "RangedInt", base::Value::TYPE_INTEGER }, | 561 { "RangedInt", base::Value::Type::INTEGER }, |
562 { "StrEnum", base::Value::TYPE_STRING }, | 562 { "StrEnum", base::Value::Type::STRING }, |
563 { "StrPat", base::Value::TYPE_STRING }, | 563 { "StrPat", base::Value::Type::STRING }, |
564 }; | 564 }; |
565 | 565 |
566 Schema::Iterator it = schema.GetPropertiesIterator(); | 566 Schema::Iterator it = schema.GetPropertiesIterator(); |
567 for (size_t i = 0; i < arraysize(kExpectedProperties); ++i) { | 567 for (size_t i = 0; i < arraysize(kExpectedProperties); ++i) { |
568 ASSERT_FALSE(it.IsAtEnd()); | 568 ASSERT_FALSE(it.IsAtEnd()); |
569 EXPECT_STREQ(kExpectedProperties[i].key, it.key()); | 569 EXPECT_STREQ(kExpectedProperties[i].key, it.key()); |
570 Schema sub = it.schema(); | 570 Schema sub = it.schema(); |
571 ASSERT_TRUE(sub.valid()); | 571 ASSERT_TRUE(sub.valid()); |
572 EXPECT_EQ(kExpectedProperties[i].type, sub.type()); | 572 EXPECT_EQ(kExpectedProperties[i].type, sub.type()); |
573 | 573 |
574 if (sub.type() == base::Value::TYPE_LIST) { | 574 if (sub.type() == base::Value::Type::LIST) { |
575 Schema items = sub.GetItems(); | 575 Schema items = sub.GetItems(); |
576 ASSERT_TRUE(items.valid()); | 576 ASSERT_TRUE(items.valid()); |
577 EXPECT_EQ(base::Value::TYPE_STRING, items.type()); | 577 EXPECT_EQ(base::Value::Type::STRING, items.type()); |
578 } | 578 } |
579 | 579 |
580 it.Advance(); | 580 it.Advance(); |
581 } | 581 } |
582 EXPECT_TRUE(it.IsAtEnd()); | 582 EXPECT_TRUE(it.IsAtEnd()); |
583 | 583 |
584 Schema sub = schema.GetAdditionalProperties(); | 584 Schema sub = schema.GetAdditionalProperties(); |
585 ASSERT_TRUE(sub.valid()); | 585 ASSERT_TRUE(sub.valid()); |
586 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); | 586 ASSERT_EQ(base::Value::Type::LIST, sub.type()); |
587 Schema subsub = sub.GetItems(); | 587 Schema subsub = sub.GetItems(); |
588 ASSERT_TRUE(subsub.valid()); | 588 ASSERT_TRUE(subsub.valid()); |
589 ASSERT_EQ(base::Value::TYPE_LIST, subsub.type()); | 589 ASSERT_EQ(base::Value::Type::LIST, subsub.type()); |
590 Schema subsubsub = subsub.GetItems(); | 590 Schema subsubsub = subsub.GetItems(); |
591 ASSERT_TRUE(subsubsub.valid()); | 591 ASSERT_TRUE(subsubsub.valid()); |
592 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); | 592 ASSERT_EQ(base::Value::Type::STRING, subsubsub.type()); |
593 | 593 |
594 SchemaList schema_list = schema.GetPatternProperties("barr"); | 594 SchemaList schema_list = schema.GetPatternProperties("barr"); |
595 ASSERT_EQ(1u, schema_list.size()); | 595 ASSERT_EQ(1u, schema_list.size()); |
596 sub = schema_list[0]; | 596 sub = schema_list[0]; |
597 ASSERT_TRUE(sub.valid()); | 597 ASSERT_TRUE(sub.valid()); |
598 EXPECT_EQ(base::Value::TYPE_STRING, sub.type()); | 598 EXPECT_EQ(base::Value::Type::STRING, sub.type()); |
599 | 599 |
600 EXPECT_TRUE(schema.GetPatternProperties("ba").empty()); | 600 EXPECT_TRUE(schema.GetPatternProperties("ba").empty()); |
601 EXPECT_TRUE(schema.GetPatternProperties("bar+$").empty()); | 601 EXPECT_TRUE(schema.GetPatternProperties("bar+$").empty()); |
602 } | 602 } |
603 | 603 |
604 TEST(SchemaTest, Validate) { | 604 TEST(SchemaTest, Validate) { |
605 std::string error; | 605 std::string error; |
606 Schema schema = Schema::Parse(kTestSchema, &error); | 606 Schema schema = Schema::Parse(kTestSchema, &error); |
607 ASSERT_TRUE(schema.valid()) << error; | 607 ASSERT_TRUE(schema.valid()) << error; |
608 | 608 |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
994 " \"properties\": {" | 994 " \"properties\": {" |
995 " \"name\": { \"type\": \"string\" }," | 995 " \"name\": { \"type\": \"string\" }," |
996 " \"url\": { \"type\": \"string\" }," | 996 " \"url\": { \"type\": \"string\" }," |
997 " \"children\": { \"$ref\": \"ListOfBookmarks\" }" | 997 " \"children\": { \"$ref\": \"ListOfBookmarks\" }" |
998 " }" | 998 " }" |
999 " }" | 999 " }" |
1000 " }" | 1000 " }" |
1001 " }" | 1001 " }" |
1002 "}", &error); | 1002 "}", &error); |
1003 ASSERT_TRUE(schema.valid()) << error; | 1003 ASSERT_TRUE(schema.valid()) << error; |
1004 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 1004 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
1005 | 1005 |
1006 Schema parent = schema.GetKnownProperty("bookmarks"); | 1006 Schema parent = schema.GetKnownProperty("bookmarks"); |
1007 ASSERT_TRUE(parent.valid()); | 1007 ASSERT_TRUE(parent.valid()); |
1008 ASSERT_EQ(base::Value::TYPE_LIST, parent.type()); | 1008 ASSERT_EQ(base::Value::Type::LIST, parent.type()); |
1009 | 1009 |
1010 // Check the recursive type a number of times. | 1010 // Check the recursive type a number of times. |
1011 for (int i = 0; i < 10; ++i) { | 1011 for (int i = 0; i < 10; ++i) { |
1012 Schema items = parent.GetItems(); | 1012 Schema items = parent.GetItems(); |
1013 ASSERT_TRUE(items.valid()); | 1013 ASSERT_TRUE(items.valid()); |
1014 ASSERT_EQ(base::Value::TYPE_DICTIONARY, items.type()); | 1014 ASSERT_EQ(base::Value::Type::DICTIONARY, items.type()); |
1015 | 1015 |
1016 Schema prop = items.GetKnownProperty("name"); | 1016 Schema prop = items.GetKnownProperty("name"); |
1017 ASSERT_TRUE(prop.valid()); | 1017 ASSERT_TRUE(prop.valid()); |
1018 ASSERT_EQ(base::Value::TYPE_STRING, prop.type()); | 1018 ASSERT_EQ(base::Value::Type::STRING, prop.type()); |
1019 | 1019 |
1020 prop = items.GetKnownProperty("url"); | 1020 prop = items.GetKnownProperty("url"); |
1021 ASSERT_TRUE(prop.valid()); | 1021 ASSERT_TRUE(prop.valid()); |
1022 ASSERT_EQ(base::Value::TYPE_STRING, prop.type()); | 1022 ASSERT_EQ(base::Value::Type::STRING, prop.type()); |
1023 | 1023 |
1024 prop = items.GetKnownProperty("children"); | 1024 prop = items.GetKnownProperty("children"); |
1025 ASSERT_TRUE(prop.valid()); | 1025 ASSERT_TRUE(prop.valid()); |
1026 ASSERT_EQ(base::Value::TYPE_LIST, prop.type()); | 1026 ASSERT_EQ(base::Value::Type::LIST, prop.type()); |
1027 | 1027 |
1028 parent = prop; | 1028 parent = prop; |
1029 } | 1029 } |
1030 } | 1030 } |
1031 | 1031 |
1032 TEST(SchemaTest, UnorderedReferences) { | 1032 TEST(SchemaTest, UnorderedReferences) { |
1033 // Verifies that references and IDs can come in any order. | 1033 // Verifies that references and IDs can come in any order. |
1034 std::string error; | 1034 std::string error; |
1035 Schema schema = Schema::Parse( | 1035 Schema schema = Schema::Parse( |
1036 "{" | 1036 "{" |
1037 " \"type\": \"object\"," | 1037 " \"type\": \"object\"," |
1038 " \"properties\": {" | 1038 " \"properties\": {" |
1039 " \"a\": { \"$ref\": \"shared\" }," | 1039 " \"a\": { \"$ref\": \"shared\" }," |
1040 " \"b\": { \"$ref\": \"shared\" }," | 1040 " \"b\": { \"$ref\": \"shared\" }," |
1041 " \"c\": { \"$ref\": \"shared\" }," | 1041 " \"c\": { \"$ref\": \"shared\" }," |
1042 " \"d\": { \"$ref\": \"shared\" }," | 1042 " \"d\": { \"$ref\": \"shared\" }," |
1043 " \"e\": {" | 1043 " \"e\": {" |
1044 " \"type\": \"boolean\"," | 1044 " \"type\": \"boolean\"," |
1045 " \"id\": \"shared\"" | 1045 " \"id\": \"shared\"" |
1046 " }," | 1046 " }," |
1047 " \"f\": { \"$ref\": \"shared\" }," | 1047 " \"f\": { \"$ref\": \"shared\" }," |
1048 " \"g\": { \"$ref\": \"shared\" }," | 1048 " \"g\": { \"$ref\": \"shared\" }," |
1049 " \"h\": { \"$ref\": \"shared\" }," | 1049 " \"h\": { \"$ref\": \"shared\" }," |
1050 " \"i\": { \"$ref\": \"shared\" }" | 1050 " \"i\": { \"$ref\": \"shared\" }" |
1051 " }" | 1051 " }" |
1052 "}", &error); | 1052 "}", &error); |
1053 ASSERT_TRUE(schema.valid()) << error; | 1053 ASSERT_TRUE(schema.valid()) << error; |
1054 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 1054 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
1055 | 1055 |
1056 for (char c = 'a'; c <= 'i'; ++c) { | 1056 for (char c = 'a'; c <= 'i'; ++c) { |
1057 Schema sub = schema.GetKnownProperty(std::string(1, c)); | 1057 Schema sub = schema.GetKnownProperty(std::string(1, c)); |
1058 ASSERT_TRUE(sub.valid()) << c; | 1058 ASSERT_TRUE(sub.valid()) << c; |
1059 ASSERT_EQ(base::Value::TYPE_BOOLEAN, sub.type()) << c; | 1059 ASSERT_EQ(base::Value::Type::BOOLEAN, sub.type()) << c; |
1060 } | 1060 } |
1061 } | 1061 } |
1062 | 1062 |
1063 TEST(SchemaTest, AdditionalPropertiesReference) { | 1063 TEST(SchemaTest, AdditionalPropertiesReference) { |
1064 // Verifies that "additionalProperties" can be a reference. | 1064 // Verifies that "additionalProperties" can be a reference. |
1065 std::string error; | 1065 std::string error; |
1066 Schema schema = Schema::Parse( | 1066 Schema schema = Schema::Parse( |
1067 "{" | 1067 "{" |
1068 " \"type\": \"object\"," | 1068 " \"type\": \"object\"," |
1069 " \"properties\": {" | 1069 " \"properties\": {" |
1070 " \"policy\": {" | 1070 " \"policy\": {" |
1071 " \"type\": \"object\"," | 1071 " \"type\": \"object\"," |
1072 " \"properties\": {" | 1072 " \"properties\": {" |
1073 " \"foo\": {" | 1073 " \"foo\": {" |
1074 " \"type\": \"boolean\"," | 1074 " \"type\": \"boolean\"," |
1075 " \"id\": \"FooId\"" | 1075 " \"id\": \"FooId\"" |
1076 " }" | 1076 " }" |
1077 " }," | 1077 " }," |
1078 " \"additionalProperties\": { \"$ref\": \"FooId\" }" | 1078 " \"additionalProperties\": { \"$ref\": \"FooId\" }" |
1079 " }" | 1079 " }" |
1080 " }" | 1080 " }" |
1081 "}", &error); | 1081 "}", &error); |
1082 ASSERT_TRUE(schema.valid()) << error; | 1082 ASSERT_TRUE(schema.valid()) << error; |
1083 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 1083 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
1084 | 1084 |
1085 Schema policy = schema.GetKnownProperty("policy"); | 1085 Schema policy = schema.GetKnownProperty("policy"); |
1086 ASSERT_TRUE(policy.valid()); | 1086 ASSERT_TRUE(policy.valid()); |
1087 ASSERT_EQ(base::Value::TYPE_DICTIONARY, policy.type()); | 1087 ASSERT_EQ(base::Value::Type::DICTIONARY, policy.type()); |
1088 | 1088 |
1089 Schema foo = policy.GetKnownProperty("foo"); | 1089 Schema foo = policy.GetKnownProperty("foo"); |
1090 ASSERT_TRUE(foo.valid()); | 1090 ASSERT_TRUE(foo.valid()); |
1091 EXPECT_EQ(base::Value::TYPE_BOOLEAN, foo.type()); | 1091 EXPECT_EQ(base::Value::Type::BOOLEAN, foo.type()); |
1092 | 1092 |
1093 Schema additional = policy.GetAdditionalProperties(); | 1093 Schema additional = policy.GetAdditionalProperties(); |
1094 ASSERT_TRUE(additional.valid()); | 1094 ASSERT_TRUE(additional.valid()); |
1095 EXPECT_EQ(base::Value::TYPE_BOOLEAN, additional.type()); | 1095 EXPECT_EQ(base::Value::Type::BOOLEAN, additional.type()); |
1096 | 1096 |
1097 Schema x = policy.GetProperty("x"); | 1097 Schema x = policy.GetProperty("x"); |
1098 ASSERT_TRUE(x.valid()); | 1098 ASSERT_TRUE(x.valid()); |
1099 EXPECT_EQ(base::Value::TYPE_BOOLEAN, x.type()); | 1099 EXPECT_EQ(base::Value::Type::BOOLEAN, x.type()); |
1100 } | 1100 } |
1101 | 1101 |
1102 TEST(SchemaTest, ItemsReference) { | 1102 TEST(SchemaTest, ItemsReference) { |
1103 // Verifies that "items" can be a reference. | 1103 // Verifies that "items" can be a reference. |
1104 std::string error; | 1104 std::string error; |
1105 Schema schema = Schema::Parse( | 1105 Schema schema = Schema::Parse( |
1106 "{" | 1106 "{" |
1107 " \"type\": \"object\"," | 1107 " \"type\": \"object\"," |
1108 " \"properties\": {" | 1108 " \"properties\": {" |
1109 " \"foo\": {" | 1109 " \"foo\": {" |
1110 " \"type\": \"boolean\"," | 1110 " \"type\": \"boolean\"," |
1111 " \"id\": \"FooId\"" | 1111 " \"id\": \"FooId\"" |
1112 " }," | 1112 " }," |
1113 " \"list\": {" | 1113 " \"list\": {" |
1114 " \"type\": \"array\"," | 1114 " \"type\": \"array\"," |
1115 " \"items\": { \"$ref\": \"FooId\" }" | 1115 " \"items\": { \"$ref\": \"FooId\" }" |
1116 " }" | 1116 " }" |
1117 " }" | 1117 " }" |
1118 "}", &error); | 1118 "}", &error); |
1119 ASSERT_TRUE(schema.valid()) << error; | 1119 ASSERT_TRUE(schema.valid()) << error; |
1120 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 1120 ASSERT_EQ(base::Value::Type::DICTIONARY, schema.type()); |
1121 | 1121 |
1122 Schema foo = schema.GetKnownProperty("foo"); | 1122 Schema foo = schema.GetKnownProperty("foo"); |
1123 ASSERT_TRUE(foo.valid()); | 1123 ASSERT_TRUE(foo.valid()); |
1124 EXPECT_EQ(base::Value::TYPE_BOOLEAN, foo.type()); | 1124 EXPECT_EQ(base::Value::Type::BOOLEAN, foo.type()); |
1125 | 1125 |
1126 Schema list = schema.GetKnownProperty("list"); | 1126 Schema list = schema.GetKnownProperty("list"); |
1127 ASSERT_TRUE(list.valid()); | 1127 ASSERT_TRUE(list.valid()); |
1128 ASSERT_EQ(base::Value::TYPE_LIST, list.type()); | 1128 ASSERT_EQ(base::Value::Type::LIST, list.type()); |
1129 | 1129 |
1130 Schema items = list.GetItems(); | 1130 Schema items = list.GetItems(); |
1131 ASSERT_TRUE(items.valid()); | 1131 ASSERT_TRUE(items.valid()); |
1132 ASSERT_EQ(base::Value::TYPE_BOOLEAN, items.type()); | 1132 ASSERT_EQ(base::Value::Type::BOOLEAN, items.type()); |
1133 } | 1133 } |
1134 | 1134 |
1135 TEST(SchemaTest, EnumerationRestriction) { | 1135 TEST(SchemaTest, EnumerationRestriction) { |
1136 // Enum attribute is a list. | 1136 // Enum attribute is a list. |
1137 EXPECT_TRUE(ParseFails(SchemaObjectWrapper( | 1137 EXPECT_TRUE(ParseFails(SchemaObjectWrapper( |
1138 "{" | 1138 "{" |
1139 " \"type\": \"string\"," | 1139 " \"type\": \"string\"," |
1140 " \"enum\": 12" | 1140 " \"enum\": 12" |
1141 "}"))); | 1141 "}"))); |
1142 | 1142 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1177 | 1177 |
1178 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( | 1178 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( |
1179 "{" | 1179 "{" |
1180 " \"type\": \"integer\"," | 1180 " \"type\": \"integer\"," |
1181 " \"minimum\": 10," | 1181 " \"minimum\": 10," |
1182 " \"maximum\": 20" | 1182 " \"maximum\": 20" |
1183 "}"))); | 1183 "}"))); |
1184 } | 1184 } |
1185 | 1185 |
1186 } // namespace policy | 1186 } // namespace policy |
OLD | NEW |