readname-tests

This commit is contained in:
Bjorn Andersson 2006-08-24 22:04:27 +00:00
parent 966f095d1e
commit 72f8326077
4 changed files with 84 additions and 23 deletions

3
dns.c
View file

@ -46,7 +46,6 @@
static int host2dns(const char *, char *, int);
static int dns_write(int, int, char *, int, char);
static void dns_query(int, int, char *, int);
static int dns_parse_reply(char *, int, char *, int);
struct sockaddr_in peer;
char topdomain[256];
@ -284,7 +283,7 @@ dns_read(int fd, char *buf, int buflen)
return dns_parse_reply(buf, buflen, packet, r);
}
static int
int
dns_parse_reply(char *outbuf, int buflen, char *packet, int packetlen)
{
int rv;

2
dns.h
View file

@ -37,5 +37,7 @@ int dnsd_hasack();
void dnsd_forceack(int);
void dnsd_queuepacket(const char *, const int);
int dns_parse_reply(char *, int, char *, int);
#endif /* _DNS_H_ */

22
read.c
View file

@ -16,35 +16,37 @@
#include <string.h>
int
readname(char *packet, char **src, char *dst, size_t length)
static int
readname_loop(char *packet, char **src, char *dst, size_t length, size_t loop)
{
char *dummy;
int len;
char *p;
char c;
if (loop <= 0)
return 0;
len = 0;
p = *src;
while(*p && len < length) {
while(*p && len < length - 2) {
c = *p++;
len++;
/* is this a compressed label? */
if((c & 0xc0) == 0xc0) {
dummy = packet + (((p[-1] & 0x3f) << 8) | p[0]);
readname(packet, &dummy, dst, length - len);
len += readname_loop(packet, &dummy, dst, length - len, loop - 1);
break;
}
while(c && len < length) {
while(c && len < length - 2) {
*dst++ = *p++;
len++;
c--;
}
if (*p != 0)
if (*p != 0 && len < length - 2)
*dst++ = '.';
else
*dst++ = '\0';
@ -54,6 +56,12 @@ readname(char *packet, char **src, char *dst, size_t length)
return strlen(dst);
}
int
readname(char *packet, char **src, char *dst, size_t length)
{
return readname_loop(packet, src, dst, length, 10);
}
int
readshort(char *packet, char **src, short *dst)
{

80
test.c
View file

@ -14,36 +14,31 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <arpa/nameser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "structs.h"
#include "dns.h"
#include "read.h"
int
main()
static void
test_readputshort()
{
short tshort;
short temps;
short putted;
short *s;
long tint;
long tempi;
long putint;
long *l;
int i;
short temps;
char buf[4];
short *s;
char* p;
int i;
printf("** iodine test suite\n");
printf(" * Testing read/putshort... ");
fflush(stdout);
@ -69,6 +64,18 @@ main()
}
printf("OK\n");
}
static void
test_readputlong()
{
char buf[4];
long putint;
long tempi;
long tint;
long *l;
char* p;
int i;
printf(" * Testing read/putlong... ");
fflush(stdout);
@ -95,7 +102,52 @@ main()
}
printf("OK\n");
}
static void
test_readname()
{
char emptyloop[] = {
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 };
char infloop[] = {
'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 'A', 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 };
char buf[1024];
char *data;
int rv;
printf(" * Testing readname... ");
fflush(stdout);
bzero(buf, sizeof(buf));
data = emptyloop + sizeof(HEADER);
buf[1023] = 'A';
rv = readname(emptyloop, &data, buf, 1023);
assert(buf[1023] == 'A');
bzero(buf, sizeof(buf));
data = infloop + sizeof(HEADER);
buf[4] = '\a';
rv = readname(infloop, &data, buf, 4);
printf("%s\n", buf);
assert(buf[4] == '\a');
printf("OK\n");
}
int
main()
{
printf("** iodine test suite\n");
test_readputshort();
test_readputlong();
test_readname();
printf("** All went well :)\n");
return 0;