[209] | 1 | // Copyright Benoit Blanchon 2014-2016
|
---|
| 2 | // MIT License
|
---|
| 3 | //
|
---|
| 4 | // Arduino JSON library
|
---|
| 5 | // https://github.com/bblanchon/ArduinoJson
|
---|
| 6 | // If you like this project, please add a star!
|
---|
| 7 |
|
---|
| 8 | #include <gtest/gtest.h>
|
---|
| 9 | #include <ArduinoJson.h>
|
---|
| 10 |
|
---|
| 11 | class JsonParser_NestingLimit_Tests : public testing::Test {
|
---|
| 12 | protected:
|
---|
| 13 | void whenNestingLimitIs(uint8_t nestingLimit) {
|
---|
| 14 | _nestingLimit = nestingLimit;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | void parseArrayMustFail(const char *json) {
|
---|
| 18 | ASSERT_FALSE(tryParseArray(json));
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | void parseArrayMustSucceed(const char *json) {
|
---|
| 22 | ASSERT_TRUE(tryParseArray(json));
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | void parseObjectMustFail(const char *json) {
|
---|
| 26 | ASSERT_FALSE(tryParseObject(json));
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | void parseObjectMustSucceed(const char *json) {
|
---|
| 30 | ASSERT_TRUE(tryParseObject(json));
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | private:
|
---|
| 34 | bool tryParseArray(const char *json) {
|
---|
| 35 | DynamicJsonBuffer buffer;
|
---|
| 36 | char s[256];
|
---|
| 37 | strcpy(s, json);
|
---|
| 38 | return buffer.parseArray(s, _nestingLimit).success();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | bool tryParseObject(const char *json) {
|
---|
| 42 | DynamicJsonBuffer buffer;
|
---|
| 43 | char s[256];
|
---|
| 44 | strcpy(s, json);
|
---|
| 45 | return buffer.parseObject(s, _nestingLimit).success();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | uint8_t _nestingLimit;
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit0) {
|
---|
| 52 | whenNestingLimitIs(0);
|
---|
| 53 | parseArrayMustSucceed("[]");
|
---|
| 54 | parseArrayMustFail("[[]]");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit1) {
|
---|
| 58 | whenNestingLimitIs(1);
|
---|
| 59 | parseArrayMustSucceed("[[]]");
|
---|
| 60 | parseArrayMustFail("[[[]]]");
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit2) {
|
---|
| 64 | whenNestingLimitIs(2);
|
---|
| 65 | parseArrayMustSucceed("[[[]]]");
|
---|
| 66 | parseArrayMustFail("[[[[]]]]");
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit0) {
|
---|
| 70 | whenNestingLimitIs(0);
|
---|
| 71 | parseObjectMustSucceed("{}");
|
---|
| 72 | parseObjectMustFail("{\"key\":{}}");
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit1) {
|
---|
| 76 | whenNestingLimitIs(1);
|
---|
| 77 | parseObjectMustSucceed("{\"key\":{}}");
|
---|
| 78 | parseObjectMustFail("{\"key\":{\"key\":{}}}");
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit2) {
|
---|
| 82 | whenNestingLimitIs(2);
|
---|
| 83 | parseObjectMustSucceed("{\"key\":{\"key\":{}}}");
|
---|
| 84 | parseObjectMustFail("{\"key\":{\"key\":{\"key\":{}}}}");
|
---|
| 85 | }
|
---|