/* Test the function pac_find_sof() from file linux/drivers/media/video/gspca/pac_common.h Test based on Linux kernel 2.6.32-rc1 Written by Márton Németh , 4 Oct 2009 Released under GPL */ #include struct sd { unsigned int sof_read; }; struct gspca_dev { struct sd* sd; }; #define PDEBUG(level, fmt, args...) \ do {\ printf("PDEBUG: " fmt "\n", ## args); \ } while (0) #include "pac_common.h" int tc1() { static unsigned char test[] = { 0xff, 0xff, 0x00, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 1: exact match\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == &test[5]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc2() { static unsigned char test[] = { 0x00, 0xff, 0xff, 0x00, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 2: offset 1\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == &test[6]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc3() { static unsigned char test[] = { 0xff, 0xff, 0xff, 0x00, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 3: offset 1, first byte may be misleading\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == &test[6]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc4() { static unsigned char test[] = { 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 4: offset 2, first two bytes may be misleading\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == &test[7]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc5() { static unsigned char test[] = { 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 5: offset 3, first three bytes may be misleading\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == &test[8]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc6() { static unsigned char test[] = { 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 6: offset 4, first four bytes may be misleading\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == &test[9]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc7() { static unsigned char test1[] = { 0xff, 0xff, 0x00, 0xff }; static unsigned char test2[] = { 0x96 }; unsigned char* p1; unsigned char* p2; struct sd sd; int result = 0; printf("Test case 7: pattern starts at end of packet and continues in the next one\n"); sd.sof_read = 0; p1 = pac_find_sof((struct gspca_dev*)&sd, test1, sizeof(test1)); p2 = pac_find_sof((struct gspca_dev*)&sd, test2, sizeof(test2)); if (p1 == NULL && p2 == &test2[1]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc8() { static unsigned char test1[] = { 0xff, 0xff, 0xff, 0x00, 0xff }; static unsigned char test2[] = { 0x96 }; unsigned char* p1; unsigned char* p2; struct sd sd; int result = 0; printf("Test case 8: splited pattern, with misleading first byte\n"); sd.sof_read = 0; p1 = pac_find_sof((struct gspca_dev*)&sd, test1, sizeof(test1)); p2 = pac_find_sof((struct gspca_dev*)&sd, test2, sizeof(test2)); if (p1 == NULL && p2 == &test2[1]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc9() { static unsigned char test1[] = { 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff }; static unsigned char test2[] = { 0x96 }; unsigned char* p1; unsigned char* p2; struct sd sd; int result = 0; printf("Test case 9: splited pattern, with misleading first three bytes\n"); sd.sof_read = 0; p1 = pac_find_sof((struct gspca_dev*)&sd, test1, sizeof(test1)); p2 = pac_find_sof((struct gspca_dev*)&sd, test2, sizeof(test2)); if (p1 == NULL && p2 == &test2[1]) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc10() { static unsigned char test[] = { 0xff, 0xaa, 0xff, 0x00, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 10: no match, extra byte at offset 1\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == NULL) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc11() { static unsigned char test[] = { 0xff, 0xff, 0xaa, 0x00, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 11: no match, extra byte at offset 2\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == NULL) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc12() { static unsigned char test[] = { 0xff, 0xff, 0x00, 0xaa, 0xff, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 12: no match, extra byte at offset 3\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == NULL) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int tc13() { static unsigned char test[] = { 0xff, 0xff, 0x00, 0xff, 0xaa, 0x96 }; unsigned char* p; struct sd sd; int result = 0; printf("Test case 13: no match, extra byte at offset 4\n"); sd.sof_read = 0; p = pac_find_sof((struct gspca_dev*)&sd, test, sizeof(test)); if (p == NULL) { printf("PASSED\n"); } else { printf("FAILED\n"); result = 1; } printf("\n"); return result; } int main() { int result = 0; result += tc1(); result += tc2(); result += tc3(); result += tc4(); result += tc5(); result += tc6(); result += tc7(); result += tc8(); result += tc9(); result += tc10(); result += tc11(); result += tc12(); result += tc13(); return result; }