mfgtest.c 8.88 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <bbcard.h>
#define _OS_CONT_H_	/* kludge */
#include "bbclocal.h"

#define	BLK_SIZE	0x4000	/* flash block size 16KB */

#ifdef DEBUG
#define VERBOSE fprintf
#else 
#define VERBOSE(format, args...)
#endif

static void
cid2name(ContentId cid, char *str)
{
    sprintf(str, "%08x.app", cid);
}

int
storecont(BBCHandle h, const char *locname, ContentId cid)
{
	struct stat sb;
	u8 *buf;
	int fd, len, rv;

	VERBOSE(stderr, "storecontent %s id 0x%x\n", locname, cid);
	if ((fd = open(locname, O_RDONLY)) < 0) {
		perror(locname);
		exit(1);
	}
	if (fstat(fd, &sb) < 0) {
		perror(locname);
		exit(1);
	}
	len = sb.st_size;
	if ((buf = malloc(len)) == NULL) {
		perror("malloc");
		exit(1);
	}
	if ((rv = read(fd, buf, len)) < len) {
		fprintf(stderr, "read %s len %d returns %d\n", locname, len, rv);
		exit(1);
	}
	close(fd);

	/* have the file contents, now store it as a content object */
	rv = BBCStoreContent(h, cid, buf, len);
	if (rv != BBC_OK)
		VERBOSE(stderr, "storecontent %s id 0x%x len %d returns %d\n", locname, cid, len, rv);
	free(buf);
	VERBOSE(stderr, "storecontent %s id 0x%x len %d returns %d\n", locname, cid, len, rv);
	return rv;
}

bbc_hand *
convert_handle(BBCHandle h)
{
	return handles[h];
}

int
cmpfile(BBCHandle h, const char *locname, const char *bbname)
{
	bbc_hand *hp = convert_handle(h);
	struct stat sb;
	OSBbStatBuf bbsb;
	u8 *buf1, *buf2;
	int fd;
	int len1, len2;
	int rv;

	VERBOSE(stderr, "cmpfile %s %s\n", locname, bbname);
	if ((fd = open(locname, O_RDONLY)) < 0) {
		perror(locname);
		exit(1);
	}
	if (fstat(fd, &sb) < 0) {
		perror(locname);
		exit(1);
	}
	len1 = sb.st_size;
	if ((buf1 = malloc(len1)) == NULL) {
		perror("malloc");
		exit(1);
	}
	if ((rv = read(fd, buf1, len1)) < len1) {
		fprintf(stderr, "read %s len %d returns %d\n", locname, len1, rv);
		exit(1);
	}
	close(fd);
	if ((fd = __BBC_FStat(hp, bbname, &bbsb, NULL, 0)) < 0) {
		fprintf(stderr, "__BBC_Fstate %s returns %d\n", bbname, fd);
		exit(1);
	}
	len2 = bbsb.size;
	if ((buf2 = malloc(len2)) == NULL) {
		perror("malloc");
		exit(1);
	}
	if ((rv = __BBC_FRead(hp, fd, 0, buf2, len2)) < len2) {
		fprintf(stderr, "read %s len %d returns %d\n", bbname, len2, rv);
		exit(1);
	}
	if (len1 != len2) {
		fprintf(stderr, "file sizes are not equal %d != %d\n", len1, len2);
		rv = -1;
		goto err;
	}
	if (memcmp(buf1, buf2, len1)) {
		rv = -2;
		goto err;
	}
	rv = 0;
	goto out;
err:
out:
	free(buf1);
	free(buf2);
	return rv;
}

int
readfile(BBCHandle h, const char *bbname)
{
	bbc_hand *hp = convert_handle(h);
	OSBbStatBuf bbsb;
	u8 *buf;
	int fd;
	int len;
	int rv;

	VERBOSE(stderr, "readfile %s\n", bbname);
	if ((fd = __BBC_FStat(hp, bbname, &bbsb, NULL, 0)) < 0) {
		fprintf(stderr, "__BBC_Fstate %s returns %d\n", bbname, fd);
		exit(1);
	}
	len = bbsb.size;
	if ((buf = malloc(len)) == NULL) {
		perror("malloc");
		exit(1);
	}
	if ((rv = __BBC_FRead(hp, fd, 0, buf, len)) < len) {
		fprintf(stderr, "read %s len %d returns %d\n", bbname, len, rv);
		if (rv >= 0)
			/* short read, make up a number */
			rv = -42;
	}
	free(buf);
	return rv;
}

int
storecmpcontent(BBCHandle h, const char *locname, ContentId cid)
{
	char bbname[16];
	int rv;

	if ((rv = storecont(h, locname, cid)) < 0)
		return rv;
	cid2name(cid, bbname);
	rv = cmpfile(h, locname, bbname);
	VERBOSE(stderr, "store and compare %s, cid 0x%x returns %d\n", locname, cid, rv);

	return rv;
}

char pname[256];
char *myname;
char *dname = "/dev/sg0";

int maxcid;

static void
do_fill_card(BBCHandle h)
{
	int rv;
	int cid;
	int errs = 0;
	char fname[32];
	char bbname[32];
	int len, rlen;
	int cardsize, freeblks, reservedblks, badblks;

	/*
	 * start with a random length file full of zeros
	 */
	len = rand() % 40;
	if (len == 0) len++;
	rlen = len * BLK_SIZE;
	sprintf(fname, "zerofile.%d", len);
	cid = 500;
	if ((rv = storecont(h, fname, cid)) < 0) {
		fprintf(stderr, "%s: storecont %s fails: %d\n", pname, fname, rv);
		exit(1);
	}

	for (cid = 0; ; cid++) {
		rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks);
		VERBOSE(stderr, "%s: BBCStats returns %d, cardsize %d, freeblks %d\n", pname, rv, cardsize, freeblks);
		if (rv < 0) return;
		if (freeblks < 32) {
		        VERBOSE(stderr, "%s: card is full ... check files\n", pname);
			break;
		}
		sprintf(fname, "testfile.%d", cid);
		if ((rv = storecont(h, fname, cid)) < 0) {
			/* BADHANDLE can be returned on out-of-space because of bug in library */
			if (rv == BBC_NOSPACE || rv == BBC_BADHANDLE)
				break;
			fprintf(stderr, "%s: FAIL: storecont %s fails: %d\n", pname, fname, rv);
			exit(1);
		}
	}
	maxcid = cid;

	/*
	 * Now delete all the ones with odd CIDs to create holes
	 */
	VERBOSE(stderr, "%s: delete half the content files\n", pname);
	for (cid = 1; cid < maxcid; cid += 2) {
		if ((rv = BBCRemoveContent(h, cid)) < 0) {
			fprintf(stderr, "%s: remove content id %x fails: %d\n", pname, cid, rv);
			exit(1);
		}
	}

	VERBOSE(stderr, "%s: check remaining content files\n", pname);
	for (cid = 0; cid < maxcid; cid += 2) {
		sprintf(fname, "testfile.%d", cid);
		sprintf(bbname, "%08x.app", cid);
		rv = cmpfile(h, fname, bbname);
		if (rv != 0) {
			errs++;
			printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
		}
	}
	if (errs > 0) {
		fprintf(stderr, "%s: FAIL: total %d miscompares, exiting\n", pname, errs);
		exit(1);
	}
	return;
}

static void
do_check_files(BBCHandle h)
{
	int cid;
	int rv;
	char fname[32];
	char bbname[32];
	int errs = 0;

	printf("%s: check all content files\n", pname);
	for (cid = 0; cid < maxcid; cid += 2) {
		sprintf(fname, "testfile.%d", cid);
		sprintf(bbname, "%08x.app", cid);
		rv = cmpfile(h, fname, bbname);
		if (rv != 0) {
			errs++;
			printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
		}
	}
	if (errs > 0) {
		printf("%s: total %d miscompares, exiting\n", pname, errs);
		exit(1);
	}
}

static int
write_sksa(BBCHandle h, const char *fname)
{
	unsigned char *buf;
	struct stat sb;
	int len;
	int fd;
	int rv1, rv2;

	if ((fd = open(fname, O_RDONLY)) < 0) {
		printf("%s: open %s fails (errno %d)\n", pname, fname, errno);
		return fd;
	}

	if (fstat(fd, &sb) < 0) {
		printf("%s: stat %s fails (errno %d)\n", pname, fname, errno);
		return -1;
	}

	len = sb.st_size;
	if ((buf = malloc(len)) == NULL) {
		perror("malloc");
		exit(1);
	}

	if ((rv1 = read(fd, buf, len)) < len) {
		printf("%s: read %s fails (ret %d)\n", pname, fname, rv1);
		rv1 = -1;
		goto out;
	}

	rv1 = BBCVerifySKSA(h, buf, len);
	VERBOSE(stderr, "%s: BBCVerifySKSA %s returns %d\n", pname, fname, rv1);
	if (rv1 != 0 && rv1 != len) {
		fprintf(stderr, "%s: FAIL: BBCVerifySKSA %s returns %d\n", pname, fname, rv1);
		exit(1);
	}
	rv2 = BBCVerifySKSA(h, buf, len);
	VERBOSE(stderr, "%s: BBCVerifySKSA-2 %s returns %d\n", pname, fname, rv2);
	if (rv2 != 0) {
		fprintf(stderr, "%s: FAIL: BBCVerifySKSA-2 %s return not zero\n", pname, fname);
		exit(1);
	}

out:
	close(fd);
	free(buf);
	return rv1;
}

static void
do_sksa_test(BBCHandle h)
{
	int rv;
	int blks;
	char fname[32];
	int len;

	/*
	 * Write biggest file that fits first
	 */
	for (blks = 62; ; blks--) {
		sprintf(fname, "sksafile.%d", blks);
		if ((rv = write_sksa(h, fname)) >= 0)
			break;
	}

	for (blks = 1; blks < 62 ; blks++) {
		len = blks * BLK_SIZE;
		sprintf(fname, "sksafile.%d", blks);
		if ((rv = write_sksa(h, fname)) != len) {
			printf("%s: write_sksa file %s FAILS, len %d ret %d\n", pname, fname, len, rv);
		}
	}
}

#define	LOOP_MAX 16

int
main(int argc, char **argv)
{
	BBCHandle h;
	int loops = 0;
	int cardsize, freeblks, reserved, bad;
	int rv;

	myname = argv[0];
	if (argc > 1)
		dname = argv[argc - 1];
	setbuf(stdout, NULL);
	setbuf(stderr, NULL);

	while (++loops < LOOP_MAX) {
		sprintf(pname, "%s(%d)", myname, loops);
		printf("%s: start test loop #%d\n", pname, loops);
		h = BBCInit(dname, BBC_SYNC);
		VERBOSE(stderr, "%s: BBCInit(%s) returns %d\n", pname, dname, h);
		if (h < 0) {
			fprintf(stderr, "%s: test fails, check card reader %s\n", pname, dname);
			exit(1);
		}
		if (!BBCCardPresent(h))  {
			fprintf(stderr, "%s: test fails, no card found in card reader %s\n", pname, dname);
			exit(1);
		}
		if ((rv = BBCStats(h, &cardsize, &reserved, &freeblks, &bad)) < 0 && rv != BBC_NOFORMAT) {
			fprintf(stderr, "%s: test fails, BBCStats returns %d\n", pname, rv);
			exit(1);
		}
		printf("%s: format card ...\n", pname);
		rv = BBCSetLed(h, BBC_LED_ORANGE);
		rv = BBCFormatCard(h);
		if (rv < 0) {
			fprintf(stderr, "%s: test fails, BBCFormatCard returns %d\n", pname, rv);
			exit(1);
	        }
		printf("%s: store files on card and verify ...\n", pname);
		do_fill_card(h);
		do_sksa_test(h);
		do_check_files(h);
		rv = BBCSetLed(h, BBC_LED_OFF);
		rv = BBCClose(h);
		printf("%s: test loop completed OK ...\n", pname);
	}
	printf("%s: test completed OK\n", pname);

	exit(0);
}