Example #1
0
void test_checksum_is_verified_when_correct(void)
{
	uint8_t data[0x10] = {0x21,0x46,0x01,0x36,0x01,0x21,0x47,0x01,0x36,0x00,0x7E,0xFE,0x09,0xD2,0x19,0x01};
	ihex_record_t r = {
		.ihr_length = 0x10, .ihr_type = IHEX_DATA, .ihr_address = 0x0100,
		.ihr_data = (ihex_rdata_t) &data, .ihr_checksum = 0x40
	};
	
	CU_ASSERT_EQUAL(ihex_check_record(&r), 0);
}

void test_checksum_is_not_verified_when_incorrect(void)
{
	uint8_t data[0x10] = {0x21,0x46,0x01,0x36,0x01,0x21,0x47,0x01,0x36,0x00,0x7E,0xFE,0x09,0xD2,0x19,0x01};
	ihex_record_t r = {
		.ihr_length = 0x10, .ihr_type = IHEX_DATA, .ihr_address = 0x0100,
		.ihr_data = (ihex_rdata_t) &data, .ihr_checksum = 0x20
	};
	
	CU_ASSERT_EQUAL(ihex_check_record(&r), 1);
}

static void test_can_read_ihex_rs_from_string(char* s)
{
	ihex_recordset_t *records = ihex_rs_from_string(s);

	CU_ASSERT_PTR_NOT_NULL_FATAL(records);
	CU_ASSERT_EQUAL_FATAL(records->ihrs_count, 2);

	CU_ASSERT_EQUAL(records->ihrs_records[0].ihr_length, 0x10);
	CU_ASSERT_EQUAL(records->ihrs_records[0].ihr_data[0], 0x21);
	CU_ASSERT_EQUAL(records->ihrs_records[0].ihr_type, IHEX_DATA);
	CU_ASSERT_EQUAL(records->ihrs_records[0].ihr_address, 0x0100);
}
Example #2
0
void test_error_on_incorrect_record_length(void)
{
	//                                                v--- Missing byte!
	char* s = ":10010000214601360121470136007EFE09D21940\r\n:00000001FF\r\n";
	ihex_recordset_t *records UNUSED = ihex_rs_from_string(s);

	CU_ASSERT_EQUAL(ihex_errno(), IHEX_ERR_WRONG_RECORD_LENGTH);
}
Example #3
0
void test_error_on_incorrect_checksum(void)
{
	//                                                   v--- Wrong byte!
	char* s = ":10010000214601360121470136007EFE09D2190141\r\n:00000001FF\r\n";
	ihex_recordset_t *records UNUSED = ihex_rs_from_string(s);

	CU_ASSERT_EQUAL(ihex_errno(), IHEX_ERR_INCORRECT_CHECKSUM);
}
Example #4
0
void test_error_on_missing_eof(void)
{
	char* s = ":10010000214601360121470136007EFE09D2190140\r\n";
	ihex_recordset_t *records UNUSED = ihex_rs_from_string(s);

	CU_ASSERT_EQUAL(ihex_errno(), IHEX_ERR_NO_EOF);
	CU_ASSERT_PTR_NOT_NULL(ihex_error());
}
Example #5
0
void test_no_error_on_correct_checksum(void)
{
	char* s = ":10010000214601360121470136007EFE09D2190140\r\n:00000001FF\r\n";
	ihex_recordset_t *records UNUSED = ihex_rs_from_string(s);

	CU_ASSERT_EQUAL(ihex_errno(), 0);
	CU_ASSERT_PTR_NULL(ihex_error());
}
Example #6
0
ihex_recordset_t* ihex_rs_from_file(char* filename)
{
	struct stat s;
	int         fd;
	ulong_t     l;
	char*       c;
	
	ihex_recordset_t* r;
	
	fd = open(filename, O_RDONLY);
	if (fd < 0)
	{
		IHEX_SET_ERROR(IHEX_ERR_NO_INPUT, "Input file %s does not exist.", filename);
		goto open_failed;
	}
	
	if (fstat (fd, &s) != 0)
	{
		IHEX_SET_ERROR(IHEX_ERR_NO_INPUT, "Could not stat input file %s.", filename);
		goto stat_failed;
	}
	
	l = s.st_size;
#ifdef HAVE_MMAP
	if ((c = (char*) mmap(NULL, l, PROT_READ, MAP_PRIVATE, fd, 0)) == (void*) -1)
	{
		IHEX_SET_ERROR(IHEX_ERR_MMAP_FAILED, "Could not map file %s.", filename);
		goto mmap_failed;
	}
#else
	if ((c = (char*) malloc(l)) == NULL)
	{
		IHEX_SET_ERROR(IHEX_ERR_READ_FAILED, "Could not allocate memory for reading file %s.", filename);
		goto malloc_failed;
	}

	if (read(fd, c, l) != l)
	{
		IHEX_SET_ERROR(IHEX_ERR_READ_FAILED, "Could not read file %s.", filename);
		goto read_failed;
	}
#endif
	
	r = ihex_rs_from_string(c);
	
	// No special error treatment necessary, we need to unmap and close
	// the file anyway.
#ifdef HAVE_MMAP
	munmap((void*) c, l);
#else
	free(c);
#endif
	close(fd);
	
	return r;
	
	// Clean up on error.
#ifdef HAVE_MMAP
	mmap_failed:
#else
	read_failed:
		free(c);
	malloc_failed:
#endif
	stat_failed:
		close(fd);
	open_failed:
	
	return NULL;
}