|
Packit Service |
def718 |
#include "strerror_override.h"
|
|
Packit Service |
def718 |
#include "strerror_override_private.h"
|
|
Packit Service |
def718 |
#include <stdio.h>
|
|
Packit Service |
def718 |
#include <stdlib.h>
|
|
Packit Service |
def718 |
#include <stddef.h>
|
|
Packit Service |
def718 |
#include <string.h>
|
|
Packit Service |
def718 |
#include <fcntl.h>
|
|
Packit Service |
def718 |
#include <unistd.h>
|
|
Packit Service |
def718 |
#include <sys/types.h>
|
|
Packit Service |
def718 |
#include <sys/stat.h>
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#include "json.h"
|
|
Packit Service |
def718 |
#include "json_util.h"
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void test_read_valid_with_fd(const char *testdir);
|
|
Packit Service |
def718 |
static void test_read_nonexistant();
|
|
Packit Service |
def718 |
static void test_read_closed(void);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void test_write_to_file();
|
|
Packit Service |
def718 |
static void stat_and_cat(const char *file);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void test_write_to_file()
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
json_object *jso;
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
jso = json_tokener_parse("{"
|
|
Packit Service |
def718 |
"\"foo\":1234,"
|
|
Packit Service |
def718 |
"\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
|
|
Packit Service |
def718 |
"\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
|
|
Packit Service |
def718 |
"\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
|
|
Packit Service |
def718 |
"\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
|
|
Packit Service |
def718 |
"\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
|
|
Packit Service |
def718 |
"\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
|
|
Packit Service |
def718 |
"\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
|
|
Packit Service |
def718 |
"\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
|
|
Packit Service |
def718 |
"\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
|
|
Packit Service |
def718 |
"}");
|
|
Packit Service |
def718 |
const char *outfile = "json.out";
|
|
Packit Service |
def718 |
int rv = json_object_to_file(outfile, jso);
|
|
Packit Service |
def718 |
printf("%s: json_object_to_file(%s, jso)=%d\n",
|
|
Packit Service |
def718 |
(rv == 0) ? "OK" : "FAIL", outfile, rv);
|
|
Packit Service |
def718 |
if (rv == 0)
|
|
Packit Service |
def718 |
stat_and_cat(outfile);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
putchar('\n');
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
const char *outfile2 = "json2.out";
|
|
Packit Service |
def718 |
rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY);
|
|
Packit Service |
def718 |
printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
|
|
Packit Service |
def718 |
(rv == 0) ? "OK" : "FAIL", outfile2, rv);
|
|
Packit Service |
def718 |
if (rv == 0)
|
|
Packit Service |
def718 |
stat_and_cat(outfile2);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
const char *outfile3 = "json3.out";
|
|
Packit Service |
def718 |
int d = open(outfile3, O_WRONLY|O_CREAT, 0600);
|
|
Packit Service |
def718 |
if (d < 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("FAIL: unable to open %s %s\n", outfile3, strerror(errno));
|
|
Packit Service |
def718 |
return;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PRETTY);
|
|
Packit Service |
def718 |
printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
|
|
Packit Service |
def718 |
(rv == 0) ? "OK" : "FAIL", outfile3, rv);
|
|
Packit Service |
def718 |
// Write the same object twice
|
|
Packit Service |
def718 |
rv = json_object_to_fd(d, jso, JSON_C_TO_STRING_PLAIN);
|
|
Packit Service |
def718 |
printf("%s: json_object_to_fd(%s, jso, JSON_C_TO_STRING_PLAIN)=%d\n",
|
|
Packit Service |
def718 |
(rv == 0) ? "OK" : "FAIL", outfile3, rv);
|
|
Packit Service |
def718 |
close(d);
|
|
Packit Service |
def718 |
if (rv == 0)
|
|
Packit Service |
def718 |
stat_and_cat(outfile3);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object_put(jso);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void stat_and_cat(const char *file)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
struct stat sb;
|
|
Packit Service |
def718 |
int d = open(file, O_RDONLY, 0600);
|
|
Packit Service |
def718 |
if (d < 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("FAIL: unable to open %s: %s\n",
|
|
Packit Service |
def718 |
file, strerror(errno));
|
|
Packit Service |
def718 |
return;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
if (fstat(d, &sb) < 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("FAIL: unable to stat %s: %s\n",
|
|
Packit Service |
def718 |
file, strerror(errno));
|
|
Packit Service |
def718 |
close(d);
|
|
Packit Service |
def718 |
return;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
char *buf = malloc(sb.st_size + 1);
|
|
Packit Service |
def718 |
if(!buf)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("FAIL: unable to allocate memory\n");
|
|
Packit Service |
def718 |
close(d);
|
|
Packit Service |
def718 |
return;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
if (read(d, buf, sb.st_size) < sb.st_size)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("FAIL: unable to read all of %s: %s\n",
|
|
Packit Service |
def718 |
file, strerror(errno));
|
|
Packit Service |
def718 |
free(buf);
|
|
Packit Service |
def718 |
close(d);
|
|
Packit Service |
def718 |
return;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
buf[sb.st_size] = '\0';
|
|
Packit Service |
def718 |
printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
|
|
Packit Service |
def718 |
free(buf);
|
|
Packit Service |
def718 |
close(d);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int main(int argc, char **argv)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
// json_object_to_file(file, obj);
|
|
Packit Service |
def718 |
// json_object_to_file_ext(file, obj, flags);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
_json_c_strerror_enable = 1;
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
const char *testdir;
|
|
Packit Service |
def718 |
if (argc < 2)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
fprintf(stderr,
|
|
Packit Service |
def718 |
"Usage: %s <testdir>\n"
|
|
Packit Service |
def718 |
" <testdir> is the location of input files\n",
|
|
Packit Service |
def718 |
argv[0]);
|
|
Packit Service |
def718 |
return EXIT_FAILURE;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
testdir = argv[1];
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
test_read_valid_with_fd(testdir);
|
|
Packit Service |
def718 |
test_read_nonexistant();
|
|
Packit Service |
def718 |
test_read_closed();
|
|
Packit Service |
def718 |
test_write_to_file();
|
|
Packit Service |
def718 |
return EXIT_SUCCESS;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void test_read_valid_with_fd(const char *testdir)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
const char *filename = "./valid.json";
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int d = open(filename, O_RDONLY, 0);
|
|
Packit Service |
def718 |
if (d < 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
fprintf(stderr,
|
|
Packit Service |
def718 |
"FAIL: unable to open %s: %s\n",
|
|
Packit Service |
def718 |
filename, strerror(errno));
|
|
Packit Service |
def718 |
exit(EXIT_FAILURE);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
json_object *jso = json_object_from_fd(d);
|
|
Packit Service |
def718 |
if (jso != NULL)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("OK: json_object_from_fd(%s)=%s\n",
|
|
Packit Service |
def718 |
filename, json_object_to_json_string(jso));
|
|
Packit Service |
def718 |
json_object_put(jso);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
else
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
fprintf(stderr,
|
|
Packit Service |
def718 |
"FAIL: unable to parse contents of %s: %s\n",
|
|
Packit Service |
def718 |
filename, json_util_get_last_err());
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
close(d);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void test_read_nonexistant()
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
const char *filename = "./not_present.json";
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object *jso = json_object_from_file(filename);
|
|
Packit Service |
def718 |
if (jso != NULL)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n",
|
|
Packit Service |
def718 |
filename, (void *)jso);
|
|
Packit Service |
def718 |
json_object_put(jso);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
else
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n",
|
|
Packit Service |
def718 |
filename, json_util_get_last_err());
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static void test_read_closed()
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
// Test reading from a closed fd
|
|
Packit Service |
def718 |
int d = open("/dev/null", O_RDONLY, 0);
|
|
Packit Service |
def718 |
if(d < 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
puts("FAIL: unable to open");
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
// Copy over to a fixed fd number so test output is consistent.
|
|
Packit Service |
def718 |
int fixed_d = 10;
|
|
Packit Service |
def718 |
if (dup2(d, fixed_d) < 0)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("FAIL: unable to dup to fd %d", fixed_d);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
close(d);
|
|
Packit Service |
def718 |
close(fixed_d);
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
json_object *jso = json_object_from_fd(fixed_d);
|
|
Packit Service |
def718 |
if (jso != NULL)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
printf("FAIL: read from closed fd returning non-NULL: %p\n",
|
|
Packit Service |
def718 |
(void *)jso);
|
|
Packit Service |
def718 |
fflush(stdout);
|
|
Packit Service |
def718 |
printf(" jso=%s\n", json_object_to_json_string(jso));
|
|
Packit Service |
def718 |
json_object_put(jso);
|
|
Packit Service |
def718 |
return;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
printf("OK: json_object_from_fd(closed_fd), "
|
|
Packit Service |
def718 |
"expecting NULL, EBADF, got:NULL, %s\n",
|
|
Packit Service |
def718 |
json_util_get_last_err());
|
|
Packit Service |
def718 |
}
|