legacy/libisoburn/trunk/test/aaip_0_2_test.c

245 lines
5.9 KiB
C
Raw Normal View History

/*
Arbitrary Attribute Interchange Protocol , AAIP version 0.2
Demonstration program for encoding and decoding EA and ACL.
See http://libburnia-project.org/wiki/AAIP
test/aaip_0_2_test.c - Main program for test binary
2008-12-25 14:01:36 +00:00
Compile: cc -g -Wall -o test/aaip test/aaip_0_2.c test/aaip_0_2_test.c -lacl
Usage: ./aaip name value
Long parameters ./aaip -name"x100" -value"x100"
>>> ACLs
*/
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
2008-12-25 14:01:36 +00:00
#include <errno.h>
#include <sys/acl.h>
#include "aaip_0_2.h"
#define Aaip_test_name_sizE 1024
#define Aaip_test_value_sizE 1024
2008-12-25 14:01:36 +00:00
static int print_result(unsigned char *result, size_t result_len, int flag)
{
int i;
printf(
" - - - - - - 0 - - 1 - - 2 - - 3 - - 4 - - 5 - - 6 - - 7 - - 8 - - 9\n");
printf("\n");
printf("%4u : ", 0);
for(i= 0; i < result_len; i++) {
if(result[i] >= 32 && result[i] <= 126)
printf("'%c' ", result[i]);
else
printf("%3u ", (unsigned int) ((unsigned char *) result)[i]);
if((i % 10) == 9)
printf("\n%4u : ", (unsigned int) (i + 1));
}
printf("\n\n");
printf(
" - - - - - - 0 - - 1 - - 2 - - 3 - - 4 - - 5 - - 6 - - 7 - - 8 - - 9\n");
printf("\n");
return(1);
}
static int test_acl(char *path, int flag)
{
int ret;
acl_t acl= NULL;
char *acl_text= NULL;
unsigned char *result= NULL;
size_t result_len;
acl= acl_get_file(path, ACL_TYPE_ACCESS);
if(acl == NULL) {
fprintf(stderr, "acl_get_file failed: %d %s\n",
errno, errno != 0 ? strerror(errno) : "");
ret= 0; goto ex;
}
acl_text= acl_to_text(acl, NULL);
if(acl_text == NULL) {
fprintf(stderr, "acl_to_text failed: %d %s\n",
errno, errno != 0 ? strerror(errno) : "");
ret= 0; goto ex;
}
printf("--- ACL:\n%s--- end of ACL\n\n", acl_text);
ret= aaip_encode_acl(acl_text, &result_len, &result, 0);
if(ret <= 0) {
fprintf(stderr, "aaip_encode_acl(text) failed: ret= %d\n", ret);
ret= 0; goto ex;
}
print_result(result, result_len, 0);
free(result); result= NULL;
ret= aaip_encode_acl(acl_text, &result_len, &result, 2);
if(ret <= 0) {
fprintf(stderr, "aaip_encode_acl(num) failed: ret= %d\n", ret);
ret= 0; goto ex;
}
print_result(result, result_len, 0);
/* >>> */;
ret= 1;
ex:;
if(acl_text != NULL)
acl_free(acl_text);
if(acl != NULL)
acl_free(acl);
if(result != NULL)
free(result);
return(ret);
}
int main(int argc, char **argv)
{
int ret, l, mult= 0, k;
size_t result_len, i;
unsigned char *result= NULL;
char **names= NULL, **values= NULL, *cpt, **param;
size_t *value_lengths= NULL;
struct aaip_state *aaip;
size_t consumed= 0;
char name[Aaip_test_name_sizE + 1];
size_t name_fill;
char value[Aaip_test_value_sizE + 1];
size_t value_fill;
size_t todo;
int submit_data, is_done;
unsigned char *rpt;
unsigned int skipped, was_skipped= 0;
2008-12-25 14:01:36 +00:00
test_acl("/u/test/acl", 0);
if(argc < 3 || (argc % 2) == 0) {
fprintf(stderr, "usage: %s [-]name[xNNN] [-]value[xNNN] ...\n", argv[0]);
exit(1);
}
aaip= (struct aaip_state *) calloc(aaip_sizeof_aaip_state(), 1);
names= calloc(sizeof(char *), (argc - 1) / 2);
values= calloc(sizeof(char *), (argc - 1) / 2);
value_lengths= calloc(sizeof(size_t), (argc - 1) / 2);
for(i= 0; i < argc - 1; i++) {
if(i % 2)
param= values + i / 2;
else
param= names + i / 2;
(*param)= argv[i + 1];
if(argv[i + 1][0] == '-') {
cpt= strchr(argv[i + 1], 'x');
if(cpt != NULL) {
l= cpt - argv[i + 1] - 1;
if(l > 0)
sscanf(cpt + 1, "%d", &mult);
if(l > 0 && mult > 0) {
(*param)= calloc(1, l * mult + 1);
if((*param) != NULL) {
for(k= 0; k < mult; k++)
memcpy((*param) + k * l, argv[i + 1] + 1, l);
(*param)[mult * l]= 0;
} else
(*param)= argv[i + 1];
}
}
}
if(i % 2)
value_lengths[i / 2]= strlen(values[i / 2]);
}
ret= aaip_encode("AA", (unsigned int) ((argc - 1) / 2), names,
value_lengths, values,
&result_len, &result, 0);
if(ret <= 0) {
fprintf(stderr, "%s : aaip_encode failed with ret= %d\n", argv[0], ret);
exit(2);
}
2008-12-25 14:01:36 +00:00
print_result(result, result_len, 0);
aaip_init(aaip, "AA", 0);
rpt= result;
submit_data= 1;
is_done= 0;
while(!is_done) {
if(submit_data) {
todo= result_len - (rpt - result);
if(todo > 2048)
todo= 2048;
if(todo == 0) {
fprintf(stderr, "%s : Out of data while still prompted to submit\n",
argv[0]);
exit(5);
}
} else
todo= 0;
ret= aaip_decode_pair(aaip, rpt, todo, &consumed,
name, Aaip_test_name_sizE, &name_fill,
value, Aaip_test_value_sizE, &value_fill, 0);
skipped= aaip_get_pairs_skipped(aaip, 0);
if(skipped > was_skipped) {
printf("- skipped %d pair%s -\n\n", skipped - was_skipped,
skipped - was_skipped > 1 ? "s" : "");
was_skipped= skipped;
}
if(ret < 0) {
fprintf(stderr, "%s : aaip_decode_pair failed with ret= %d\n",
argv[0], ret);
exit(3);
}
rpt+= todo;
if(ret == 0) {
rpt-= todo;
submit_data= 0;
continue;
} else if(ret == 1) {
submit_data= 1;
continue;
} else if(ret == 2) {
submit_data= 1;
} else if(ret == 3) {
submit_data= 0;
} else if(ret == 4) {
is_done= 1;
} else if(ret == 5) {
is_done= 1;
break;
} else {
fprintf(stderr, "%s : Unknown return %d from aaip_decode_pair()\n",
argv[0], ret);
exit(4);
}
name[name_fill]= 0;
value[value_fill]= 0;
if((name_fill < 1000 && value_fill < 1000)) {
printf("name = '%s' (%lu)\n", name, (unsigned long) name_fill);
printf("value= '%s' (%lu)\n", value, (unsigned long) value_fill);
} else {
printf("name = (%lu)\n", (unsigned long) name_fill);
printf("value= (%lu)\n", (unsigned long) value_fill);
}
printf("\n");
}
exit(0);
}