From 429cbc660ae14d4998715803b44c71abf0e4a339 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Thu, 23 Dec 2021 03:09:07 +0100 Subject: Add support for JSON format --- test/Makefile.am | 15 ++++- test/amp.test | 8 ++- test/data/data.bplist | Bin 0 -> 3718 bytes test/data/j1.plist | 1 + test/invalid_tag.test | 8 ++- test/json-invalid-types.test | 36 ++++++++++++ test/json1.test | 19 +++++++ test/json2.test | 22 ++++++++ test/malformed_dict.test | 8 ++- test/plist_cmp.c | 4 ++ test/plist_jtest.c | 131 +++++++++++++++++++++++++++++++++++++++++++ test/recursion.test | 8 ++- 12 files changed, 247 insertions(+), 13 deletions(-) create mode 100644 test/data/data.bplist create mode 100644 test/data/j1.plist create mode 100755 test/json-invalid-types.test create mode 100755 test/json1.test create mode 100755 test/json2.test create mode 100644 test/plist_jtest.c (limited to 'test') diff --git a/test/Makefile.am b/test/Makefile.am index 3fca55f..b70a85d 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -8,7 +8,8 @@ AM_LDFLAGS = noinst_PROGRAMS = \ plist_cmp \ plist_test \ - plist_btest + plist_btest \ + plist_jtest plist_cmp_SOURCES = plist_cmp.c plist_cmp_LDADD = \ @@ -21,6 +22,9 @@ plist_test_LDADD = $(top_builddir)/src/libplist-2.0.la plist_btest_SOURCES = plist_btest.c plist_btest_LDADD = $(top_builddir)/src/libplist-2.0.la +plist_jtest_SOURCES = plist_jtest.c +plist_jtest_LDADD = $(top_builddir)/src/libplist-2.0.la + TESTS = \ empty.test \ small.test \ @@ -45,7 +49,10 @@ TESTS = \ offsetsize.test \ refsize.test \ malformed_dict.test \ - uid.test + uid.test \ + json1.test \ + json2.test \ + json-invalid-types.test EXTRA_DIST = \ $(TESTS) \ @@ -89,7 +96,9 @@ EXTRA_DIST = \ data/signedunsigned.plist \ data/unsigned.bplist \ data/unsigned.plist \ - data/uid.bplist + data/uid.bplist \ + data/data.bplist \ + data/j1.plist TESTS_ENVIRONMENT = \ top_srcdir=$(top_srcdir) \ diff --git a/test/amp.test b/test/amp.test index 0815391..76b32ff 100755 --- a/test/amp.test +++ b/test/amp.test @@ -1,7 +1,5 @@ ## -*- sh -*- -set -e - DATASRC=$top_srcdir/test/data TESTFILE=amp.plist DATAIN0=$DATASRC/$TESTFILE @@ -9,6 +7,10 @@ DATAOUT0=$top_builddir/test/data/$TESTFILE.out rm -rf $DATAOUT0 $top_builddir/tools/plistutil -i $DATAIN0 -o $DATAOUT0 -if test -f $DATAOUT0; then + +# test succeeds if plistutil fails +if [ $? -eq 0 ]; then exit 1 +else + exit 0 fi diff --git a/test/data/data.bplist b/test/data/data.bplist new file mode 100644 index 0000000..955993f Binary files /dev/null and b/test/data/data.bplist differ diff --git a/test/data/j1.plist b/test/data/j1.plist new file mode 100644 index 0000000..2ae9acb --- /dev/null +++ b/test/data/j1.plist @@ -0,0 +1 @@ +{"test":[1,1],"foo":[[1],[1],[1],[1],[[1],[1],[1],[1],[[1],[1],[1],[1]]]],"more":{"a":"yo","b":[{"c":0.25},{"a":"yo","b":[{"c":0.25},{"a":"yo","b":[{"c":0.25}]}]}]}} \ No newline at end of file diff --git a/test/invalid_tag.test b/test/invalid_tag.test index 079bcdd..2c42a53 100755 --- a/test/invalid_tag.test +++ b/test/invalid_tag.test @@ -1,7 +1,5 @@ ## -*- sh -*- -set -e - DATASRC=$top_srcdir/test/data TESTFILE=invalid_tag.plist DATAIN0=$DATASRC/$TESTFILE @@ -9,6 +7,10 @@ DATAOUT0=$top_builddir/test/data/$TESTFILE.out rm -rf $DATAOUT0 $top_builddir/tools/plistutil -i $DATAIN0 -o $DATAOUT0 -if test -f $DATAOUT0; then + +# test succeeds if plistutil fails +if [ $? -eq 0 ]; then exit 1 +else + exit 0 fi diff --git a/test/json-invalid-types.test b/test/json-invalid-types.test new file mode 100755 index 0000000..0397c05 --- /dev/null +++ b/test/json-invalid-types.test @@ -0,0 +1,36 @@ +## -*- sh -*- + +DATASRC=$top_srcdir/test/data +DATAOUT=$top_builddir/test/data +TESTFILE0=data.bplist +TESTFILE1=7.plist +TESTFILE2=uid.bplist + +if ! test -d "$DATAOUT"; then + mkdir -p $DATAOUT +fi + +export PLIST_JSON_DEBUG=1 + +echo "Converting (failure expected)" +STDERR=`$top_builddir/tools/plistutil -f json -i $DATASRC/$TESTFILE0 -o /dev/null 2>&1` +echo "$STDERR" +if ! echo "$STDERR" |grep "PLIST_DATA type is not valid for JSON format"; then + exit 1 +fi + +echo "Converting (failure expected)" +STDERR=`$top_builddir/tools/plistutil -f json -i $DATASRC/$TESTFILE1 -o /dev/null 2>&1` +echo "$STDERR" +if ! echo "$STDERR" |grep "PLIST_DATE type is not valid for JSON format"; then + exit 2 +fi + +echo "Converting (failure expected)" +STDERR=`$top_builddir/tools/plistutil -f json -i $DATASRC/$TESTFILE2 -o /dev/null 2>&1` +echo "$STDERR" +if ! echo "$STDERR" |grep "PLIST_UID type is not valid for JSON format"; then + exit 3 +fi + +exit 0 diff --git a/test/json1.test b/test/json1.test new file mode 100755 index 0000000..dba4912 --- /dev/null +++ b/test/json1.test @@ -0,0 +1,19 @@ +## -*- sh -*- + +set -e + +DATASRC=$top_srcdir/test/data +DATAOUT=$top_builddir/test/data +TESTFILE=j1.plist + +if ! test -d "$DATAOUT"; then + mkdir -p $DATAOUT +fi + +export PLIST_JSON_DEBUG=1 + +echo "Converting" +$top_builddir/test/plist_jtest $DATASRC/$TESTFILE $DATAOUT/$TESTFILE.out + +echo "Comparing" +$top_builddir/test/plist_cmp $DATASRC/$TESTFILE $DATAOUT/$TESTFILE.out diff --git a/test/json2.test b/test/json2.test new file mode 100755 index 0000000..06a7007 --- /dev/null +++ b/test/json2.test @@ -0,0 +1,22 @@ +## -*- sh -*- + +set -e + +DATASRC=$top_srcdir/test/data +DATAOUT=$top_builddir/test/data +TESTFILE=entities.plist + +if ! test -d "$DATAOUT"; then + mkdir -p $DATAOUT +fi + +export PLIST_JSON_DEBUG=1 + +echo "Converting input file to JSON" +$top_builddir/tools/plistutil -f json -i $DATASRC/$TESTFILE -o $DATASRC/$TESTFILE.json + +echo "Converting to binary and back to JSON" +$top_builddir/test/plist_jtest $DATASRC/$TESTFILE.json $DATAOUT/$TESTFILE.json.out + +echo "Comparing" +$top_builddir/test/plist_cmp $DATASRC/$TESTFILE $DATAOUT/$TESTFILE.json.out diff --git a/test/malformed_dict.test b/test/malformed_dict.test index f45ae7e..cbad2bd 100755 --- a/test/malformed_dict.test +++ b/test/malformed_dict.test @@ -1,7 +1,5 @@ ## -*- sh -*- -set -e - DATASRC=$top_srcdir/test/data TESTFILE=malformed_dict.bplist DATAIN0=$DATASRC/$TESTFILE @@ -9,3 +7,9 @@ DATAOUT0=$top_builddir/test/data/$TESTFILE.out $top_builddir/tools/plistutil -i $DATAIN0 -o $DATAOUT0 +# test succeeds if plistutil fails +if [ $? -eq 0 ]; then + exit 1 +else + exit 0 +fi diff --git a/test/plist_cmp.c b/test/plist_cmp.c index c854446..85b92ee 100644 --- a/test/plist_cmp.c +++ b/test/plist_cmp.c @@ -126,11 +126,15 @@ int main(int argc, char *argv[]) if (memcmp(plist_1, "bplist00", 8) == 0) plist_from_bin(plist_1, size_in1, &root_node1); + else if (plist_1[0] == '[' || plist_1[0] == '{') + plist_from_json(plist_1, size_in1, &root_node1); else plist_from_xml(plist_1, size_in1, &root_node1); if (memcmp(plist_2, "bplist00", 8) == 0) plist_from_bin(plist_2, size_in2, &root_node2); + else if (plist_2[0] == '[' || plist_2[0] == '{') + plist_from_json(plist_2, size_in2, &root_node2); else plist_from_xml(plist_2, size_in2, &root_node2); diff --git a/test/plist_jtest.c b/test/plist_jtest.c new file mode 100644 index 0000000..130e3c7 --- /dev/null +++ b/test/plist_jtest.c @@ -0,0 +1,131 @@ +/* + * backup_test.c + * source libplist regression test + * + * Copyright (c) 2009 Jonathan Beck All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#include "plist/plist.h" + +#include +#include +#include +#include + +#ifdef _MSC_VER +#pragma warning(disable:4996) +#endif + + +int main(int argc, char *argv[]) +{ + FILE *iplist = NULL; + plist_t root_node1 = NULL; + plist_t root_node2 = NULL; + char *plist_json = NULL; + char *plist_json2 = NULL; + char *plist_bin = NULL; + int size_in = 0; + uint32_t size_out = 0; + uint32_t size_out2 = 0; + char *file_in = NULL; + char *file_out = NULL; + struct stat *filestats = (struct stat *) malloc(sizeof(struct stat)); + if (argc != 3) + { + printf("Wrong input\n"); + return 1; + } + + file_in = argv[1]; + file_out = argv[2]; + //read input file + iplist = fopen(file_in, "rb"); + + if (!iplist) + { + printf("File does not exists\n"); + return 2; + } + printf("File %s is open\n", file_in); + stat(file_in, filestats); + size_in = filestats->st_size; + plist_json = (char *) malloc(sizeof(char) * (size_in + 1)); + fread(plist_json, sizeof(char), size_in, iplist); + fclose(iplist); + plist_json[size_in] = 0; + + //convert one format to another + plist_from_json(plist_json, size_in, &root_node1); + if (!root_node1) + { + printf("PList JSON parsing failed\n"); + return 3; + } + + printf("PList JSON parsing succeeded\n"); + plist_to_bin(root_node1, &plist_bin, &size_out); + if (!plist_bin) + { + printf("PList BIN writing failed\n"); + return 4; + } + + printf("PList BIN writing succeeded\n"); + plist_from_bin(plist_bin, size_out, &root_node2); + if (!root_node2) + { + printf("PList BIN parsing failed\n"); + return 5; + } + + printf("PList BIN parsing succeeded\n"); + plist_to_json(root_node2, &plist_json2, &size_out2, 0); + if (!plist_json2) + { + printf("PList JSON writing failed\n"); + return 8; + } + + printf("PList JSON writing succeeded\n"); + if (plist_json2) + { + FILE *oplist = NULL; + oplist = fopen(file_out, "wb"); + fwrite(plist_json2, size_out2, sizeof(char), oplist); + fclose(oplist); + } + + plist_free(root_node1); + plist_free(root_node2); + free(plist_bin); + free(plist_json); + free(plist_json2); + free(filestats); + + if ((uint32_t)size_in != size_out2) + { + printf("Size of input and output is different\n"); + printf("Input size : %i\n", size_in); + printf("Output size : %i\n", size_out2); + } + + //success + return 0; +} + diff --git a/test/recursion.test b/test/recursion.test index 0120a12..9946d81 100755 --- a/test/recursion.test +++ b/test/recursion.test @@ -1,7 +1,5 @@ ## -*- sh -*- -set -e - DATASRC=$top_srcdir/test/data TESTFILE=recursion.bplist DATAIN0=$DATASRC/$TESTFILE @@ -9,3 +7,9 @@ DATAOUT0=$top_builddir/test/data/$TESTFILE.out $top_builddir/tools/plistutil -i $DATAIN0 -o $DATAOUT0 +# test succeeds if plistutil fails +if [ $? -eq 0 ]; then + exit 1 +else + exit 0 +fi -- cgit v1.1-32-gdbae