diff options
Diffstat (limited to 'cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json')
8 files changed, 625 insertions, 0 deletions
diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.general.d b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.general.d new file mode 100644 index 000000000000..4600811d95b9 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.general.d @@ -0,0 +1,179 @@ +/* + * This file and its contents are supplied under the terms of the + * Common Development and Distribution License ("CDDL"), version 1.0. + * You may only use this file in accordance with the terms of version + * 1.0 of the CDDL. + * + * A full copy of the text of the CDDL should have accompanied this + * source. A copy of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + */ + +/* + * Copyright 2012, Joyent, Inc. All rights reserved. + */ + +/* + * General functional tests of JSON parser for json(). + */ + +#pragma D option quiet +#pragma D option strsize=1k + +#define TST(name) \ + printf("\ntst |%s|\n", name) +#define IN2(vala, valb) \ + in = strjoin(vala, valb); \ + printf("in |%s|\n", in) +#define IN(val) \ + in = val; \ + printf("in |%s|\n", in) +#define SEL(ss) \ + out = json(in, ss); \ + printf("sel |%s|\nout |%s|\n", ss, \ + out != NULL ? out : "<NULL>") + +BEGIN +{ + TST("empty array"); + IN("[]"); + SEL("0"); + + TST("one-element array: integer"); + IN("[1]"); + SEL("0"); + SEL("1"); + SEL("100"); + SEL("-1"); + + TST("one-element array: hex integer (not in spec, not supported)"); + IN("[0x1000]"); + SEL("0"); + + TST("one-element array: float"); + IN("[1.5001]"); + SEL("0"); + + TST("one-element array: float + exponent"); + IN("[16.3e10]"); + SEL("0"); + + TST("one-element array: integer + whitespace"); + IN("[ \t 5\t]"); + SEL("0"); + + TST("one-element array: integer + exponent + whitespace"); + IN("[ \t \t 16E10 \t ]"); + SEL("0"); + + TST("one-element array: string"); + IN("[\"alpha\"]"); + SEL("0"); + + TST("alternative first-element indexing"); + IN("[1,5,10,15,20]"); + SEL("[0]"); + SEL("[3]"); + SEL("[4]"); + SEL("[5]"); + + TST("one-element array: object"); + IN("[ { \"first\": true, \"second\": false }]"); + SEL("0.first"); + SEL("0.second"); + SEL("0.third"); + + TST("many-element array: integers"); + IN("[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]"); + SEL("10"); /* F(10) = 55 */ + SEL("14"); /* F(14) = 377 */ + SEL("19"); + + TST("many-element array: multiple types"); + IN2("[\"string\",32,true,{\"a\":9,\"b\":false},100.3e10,false,200.5,", + "{\"key\":\"val\"},null]"); + SEL("0"); + SEL("0.notobject"); + SEL("1"); + SEL("2"); + SEL("3"); + SEL("3.a"); + SEL("3.b"); + SEL("3.c"); + SEL("4"); + SEL("5"); + SEL("6"); + SEL("7"); + SEL("7.key"); + SEL("7.key.notobject"); + SEL("7.nonexist"); + SEL("8"); + SEL("9"); + + TST("many-element array: multiple types + whitespace"); + IN2("\n[\t\"string\" ,\t32 , true\t,\t {\"a\": 9,\t\"b\": false},\t\t", + "100.3e10, false, 200.5,{\"key\" \t:\n \"val\"},\t\t null ]\t\t"); + SEL("0"); + SEL("0.notobject"); + SEL("1"); + SEL("2"); + SEL("3"); + SEL("3.a"); + SEL("3.b"); + SEL("3.c"); + SEL("4"); + SEL("5"); + SEL("6"); + SEL("7"); + SEL("7.key"); + SEL("7.key.notobject"); + SEL("7.nonexist"); + SEL("8"); + SEL("9"); + + TST("two-element array: various string escape codes"); + IN2("[\"abcd \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u0000 \\uf00F \", ", + "\"final\"]"); + SEL("0"); + SEL("1"); + + TST("three-element array: broken escape code"); + IN("[\"fine here\", \"dodgey \\u00AZ\", \"wont get here\"]"); + SEL("0"); + SEL("1"); + SEL("2"); + + TST("nested objects"); + IN2("{ \"top\": { \"mid\" : { \"legs\": \"feet\" }, \"number\": 9, ", + "\"array\":[0,1,{\"a\":true,\"bb\":[1,2,false,{\"x\":\"yz\"}]}]}}"); + SEL("top"); + SEL("fargo"); + SEL("top.mid"); + SEL("top.centre"); + SEL("top.mid.legs"); + SEL("top.mid.number"); + SEL("top.mid.array"); + SEL("top.number"); + SEL("top.array"); + SEL("top.array[0]"); + SEL("top.array[1]"); + SEL("top.array[2]"); + SEL("top.array[2].a"); + SEL("top.array[2].b"); + SEL("top.array[2].bb"); + SEL("top.array[2].bb[0]"); + SEL("top.array[2].bb[1]"); + SEL("top.array[2].bb[2]"); + SEL("top.array[2].bb[3]"); + SEL("top.array[2].bb[3].x"); + SEL("top.array[2].bb[3].x.nofurther"); + SEL("top.array[2].bb[4]"); + SEL("top.array[3]"); + + exit(0); +} + +ERROR +{ + exit(1); +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.general.d.out b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.general.d.out new file mode 100644 index 000000000000..a857ab91d831 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.general.d.out @@ -0,0 +1,218 @@ + +tst |empty array| +in |[]| +sel |0| +out |<NULL>| + +tst |one-element array: integer| +in |[1]| +sel |0| +out |1| +sel |1| +out |<NULL>| +sel |100| +out |<NULL>| +sel |-1| +out |<NULL>| + +tst |one-element array: hex integer (not in spec, not supported)| +in |[0x1000]| +sel |0| +out |<NULL>| + +tst |one-element array: float| +in |[1.5001]| +sel |0| +out |1.5001| + +tst |one-element array: float + exponent| +in |[16.3e10]| +sel |0| +out |16.3e10| + +tst |one-element array: integer + whitespace| +in |[ 5 ]| +sel |0| +out |5| + +tst |one-element array: integer + exponent + whitespace| +in |[ 16E10 ]| +sel |0| +out |16E10| + +tst |one-element array: string| +in |["alpha"]| +sel |0| +out |alpha| + +tst |alternative first-element indexing| +in |[1,5,10,15,20]| +sel |[0]| +out |1| +sel |[3]| +out |15| +sel |[4]| +out |20| +sel |[5]| +out |<NULL>| + +tst |one-element array: object| +in |[ { "first": true, "second": false }]| +sel |0.first| +out |true| +sel |0.second| +out |false| +sel |0.third| +out |<NULL>| + +tst |many-element array: integers| +in |[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]| +sel |10| +out |55| +sel |14| +out |377| +sel |19| +out |<NULL>| + +tst |many-element array: multiple types| +in |["string",32,true,{"a":9,"b":false},100.3e10,false,200.5,{"key":"val"},null]| +sel |0| +out |string| +sel |0.notobject| +out |<NULL>| +sel |1| +out |32| +sel |2| +out |true| +sel |3| +out |{"a":9,"b":false}| +sel |3.a| +out |9| +sel |3.b| +out |false| +sel |3.c| +out |<NULL>| +sel |4| +out |100.3e10| +sel |5| +out |false| +sel |6| +out |200.5| +sel |7| +out |{"key":"val"}| +sel |7.key| +out |val| +sel |7.key.notobject| +out |<NULL>| +sel |7.nonexist| +out |<NULL>| +sel |8| +out |null| +sel |9| +out |<NULL>| + +tst |many-element array: multiple types + whitespace| +in | +[ "string" , 32 , true , {"a": 9, "b": false}, 100.3e10, false, 200.5,{"key" : + "val"}, null ] | +sel |0| +out |string| +sel |0.notobject| +out |<NULL>| +sel |1| +out |32| +sel |2| +out |true| +sel |3| +out |{"a": 9, "b": false}| +sel |3.a| +out |9| +sel |3.b| +out |false| +sel |3.c| +out |<NULL>| +sel |4| +out |100.3e10| +sel |5| +out |false| +sel |6| +out |200.5| +sel |7| +out |{"key" : + "val"}| +sel |7.key| +out |val| +sel |7.key.notobject| +out |<NULL>| +sel |7.nonexist| +out |<NULL>| +sel |8| +out |null| +sel |9| +out |<NULL>| + +tst |two-element array: various string escape codes| +in |["abcd \" \\ \/ \b \f \n \r \t \u0000 \uf00F ", "final"]| +sel |0| +out |abcd \" \\ \/ \b \f \n \r \t \u0000 \uf00F | +sel |1| +out |final| + +tst |three-element array: broken escape code| +in |["fine here", "dodgey \u00AZ", "wont get here"]| +sel |0| +out |fine here| +sel |1| +out |<NULL>| +sel |2| +out |<NULL>| + +tst |nested objects| +in |{ "top": { "mid" : { "legs": "feet" }, "number": 9, "array":[0,1,{"a":true,"bb":[1,2,false,{"x":"yz"}]}]}}| +sel |top| +out |{ "mid" : { "legs": "feet" }, "number": 9, "array":[0,1,{"a":true,"bb":[1,2,false,{"x":"yz"}]}]}| +sel |fargo| +out |<NULL>| +sel |top.mid| +out |{ "legs": "feet" }| +sel |top.centre| +out |<NULL>| +sel |top.mid.legs| +out |feet| +sel |top.mid.number| +out |<NULL>| +sel |top.mid.array| +out |<NULL>| +sel |top.number| +out |9| +sel |top.array| +out |[0,1,{"a":true,"bb":[1,2,false,{"x":"yz"}]}]| +sel |top.array[0]| +out |0| +sel |top.array[1]| +out |1| +sel |top.array[2]| +out |{"a":true,"bb":[1,2,false,{"x":"yz"}]}| +sel |top.array[2].a| +out |true| +sel |top.array[2].b| +out |<NULL>| +sel |top.array[2].bb| +out |[1,2,false,{"x":"yz"}]| +sel |top.array[2].bb[0]| +out |1| +sel |top.array[2].bb[1]| +out |2| +sel |top.array[2].bb[2]| +out |false| +sel |top.array[2].bb[3]| +out |{"x":"yz"}| +sel |top.array[2].bb[3].x| +out |yz| +sel |top.array[2].bb[3].x.nofurther| +out |<NULL>| +sel |top.array[2].bb[4]| +out |<NULL>| +sel |top.array[3]| +out |<NULL>| + diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.strsize.d b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.strsize.d new file mode 100644 index 000000000000..6aa50b9ad02b --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.strsize.d @@ -0,0 +1,51 @@ +/* + * This file and its contents are supplied under the terms of the + * Common Development and Distribution License ("CDDL"), version 1.0. + * You may only use this file in accordance with the terms of version + * 1.0 of the CDDL. + * + * A full copy of the text of the CDDL should have accompanied this + * source. A copy of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + */ + +/* + * Copyright 2012, Joyent, Inc. All rights reserved. + */ + +/* + * ASSERTION: + * json() run time must be bounded above by strsize. This test makes strsize + * small and deliberately overflows it to prove we bail and return NULL in + * the event that we run off the end of the string. + * + */ + +#pragma D option quiet +#pragma D option strsize=18 + +BEGIN +{ + in = "{\"a\": 1024}"; /* length == 19 */ + out = json(in, "a"); + printf("|%s|\n%s\n\n", in, out != NULL ? out : "<NULL>"); + + in = "{\"a\": 1024}"; /* length == 11 */ + out = json(in, "a"); + printf("|%s|\n%s\n\n", in, out != NULL ? out : "<NULL>"); + + in = "{\"a\":false,\"b\":true}"; /* length == 20 */ + out = json(in, "b"); + printf("|%s|\n%s\n\n", in, out != NULL ? out : "<NULL>"); + + in = "{\"a\":false,\"b\":20}"; /* length == 18 */ + out = json(in, "b"); + printf("|%s|\n%s\n\n", in, out != NULL ? out : "<NULL>"); + + exit(0); +} + +ERROR +{ + exit(1); +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.strsize.d.out b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.strsize.d.out new file mode 100644 index 000000000000..7f1d79b6fedb --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.strsize.d.out @@ -0,0 +1,13 @@ +|{"a": 1024| +<NULL> + +|{"a": 1024}| +1024 + +|{"a":false,"b":tru| +<NULL> + +|{"a":false,"b":20}| +20 + + diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.c b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.c new file mode 100644 index 000000000000..307106d903b5 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.c @@ -0,0 +1,61 @@ +/* + * This file and its contents are supplied under the terms of the + * Common Development and Distribution License ("CDDL"), version 1.0. + * You may only use this file in accordance with the terms of version + * 1.0 of the CDDL. + * + * A full copy of the text of the CDDL should have accompanied this + * source. A copy of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + */ + +/* + * Copyright 2012 (c), Joyent, Inc. All rights reserved. + */ + +#include <sys/sdt.h> +#include "usdt.h" + +#define FMT "{" \ + " \"sizes\": [ \"first\", 2, %f ]," \ + " \"index\": %d," \ + " \"facts\": {" \ + " \"odd\": \"%s\"," \ + " \"even\": \"%s\"" \ + " }," \ + " \"action\": \"%s\"" \ + "}\n" + +int +waiting(volatile int *a) +{ + return (*a); +} + +int +main(int argc, char **argv) +{ + volatile int a = 0; + int idx; + double size = 250.5; + + while (waiting(&a) == 0) + continue; + + for (idx = 0; idx < 10; idx++) { + char *odd, *even, *json, *action; + + size *= 1.78; + odd = idx % 2 == 1 ? "true" : "false"; + even = idx % 2 == 0 ? "true" : "false"; + action = idx == 7 ? "ignore" : "print"; + + asprintf(&json, FMT, size, idx, odd, even, action); + BUNYAN_FAKE_LOG_DEBUG(json); + free(json); + } + + BUNYAN_FAKE_LOG_DEBUG("{\"finished\": true}"); + + return (0); +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.d b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.d new file mode 100644 index 000000000000..f0fbdd5cabab --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.d @@ -0,0 +1,65 @@ +/* + * This file and its contents are supplied under the terms of the + * Common Development and Distribution License ("CDDL"), version 1.0. + * You may only use this file in accordance with the terms of version + * 1.0 of the CDDL. + * + * A full copy of the text of the CDDL should have accompanied this + * source. A copy of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + */ + +/* + * Copyright (c) 2012, Joyent, Inc. All rights reserved. + */ + +#pragma D option strsize=4k +#pragma D option quiet +#pragma D option destructive + +/* + * This test reads a JSON string from a USDT probe, roughly simulating the + * primary motivating use case for the json() subroutine: filtering + * JSON-formatted log messages from a logging subsystem like node-bunyan. + */ + +pid$1:a.out:waiting:entry +{ + this->value = (int *)alloca(sizeof (int)); + *this->value = 1; + copyout(this->value, arg0, sizeof (int)); +} + +bunyan*$1:::log-* +{ + this->j = copyinstr(arg0); +} + +bunyan*$1:::log-* +/json(this->j, "finished") == NULL && json(this->j, "action") != "ignore"/ +{ + this->index = strtoll(json(this->j, "index")); + this->size = json(this->j, "sizes[2]"); + this->odd = json(this->j, "facts.odd"); + this->even = json(this->j, "facts.even"); + printf("[%d] sz %s odd %s even %s\n", this->index, this->size, + this->odd, this->even); +} + +bunyan*$1:::log-* +/json(this->j, "finished") != NULL/ +{ + printf("FINISHED!\n"); + exit(0); +} + +tick-10s +{ + printf("ERROR: Timed out before finish message!\n"); + exit(1); +} + +ERROR +{ + exit(1); +} diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.d.out b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.d.out new file mode 100644 index 000000000000..c7f58bb535dd --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/tst.usdt.d.out @@ -0,0 +1,11 @@ +[0] sz 445.890000 odd false even true +[1] sz 793.684200 odd true even false +[2] sz 1412.757876 odd false even true +[3] sz 2514.709019 odd true even false +[4] sz 4476.182054 odd false even true +[5] sz 7967.604057 odd true even false +[6] sz 14182.335221 odd false even true +[8] sz 44935.310914 odd false even true +[9] sz 79984.853427 odd true even false +FINISHED! + diff --git a/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/usdt.d b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/usdt.d new file mode 100644 index 000000000000..1a4fc87f60f4 --- /dev/null +++ b/cddl/contrib/opensolaris/cmd/dtrace/test/tst/common/json/usdt.d @@ -0,0 +1,27 @@ +/* + * This file and its contents are supplied under the terms of the + * Common Development and Distribution License ("CDDL"), version 1.0. + * You may only use this file in accordance with the terms of version + * 1.0 of the CDDL. + * + * A full copy of the text of the CDDL should have accompanied this + * source. A copy of the CDDL is also available via the Internet at + * http://www.illumos.org/license/CDDL. + */ + +/* + * Copyright 2012, Joyent, Inc. All rights reserved. + */ + +/* + * Sets up a fake node-bunyan-like USDT provider for use from C. + */ + +provider bunyan_fake { + probe log__trace(char *msg); + probe log__debug(char *msg); + probe log__info(char *msg); + probe log__warn(char *msg); + probe log__error(char *msg); + probe log__fatal(char *msg); +}; |