int main() { pthread_t id[N]; int i; printf("licznik = %d\n", licznik); errno = pthread_mutex_init(&blokada, NULL); test_errno("pthread_mutex_init"); /* utworzenie w±tku */ for (i=0; i < N; i++) { errno = pthread_create(&id[i], NULL, watek, (void*)i); test_errno("pthread_create"); } /* oczekiwanie na jego zakoñczenie */ for (i=0; i < N; i++) { errno = pthread_join(id[i], NULL); test_errno("pthread_join"); } printf("licznik = %d, spodziewana warto¶æ = %d %s\n", licznik, N*K, (licznik != N*K ? "B£¡D!!!" : "") ); return EXIT_SUCCESS; }
int main(int argc, char* argv[]) { pthread_t id; pthread_attr_t attr; size_t rozmiar; errno = pthread_attr_init(&attr); if (errno) { perror("pthread_attr_init"); return EXIT_FAILURE; } if (argc > 1) { rozmiar = atoi(argv[1]); printf("rozmiar stosu ustalony przez u¿ytkownika: %u\n", rozmiar); printf("minimalny rozmiar stosu: %u\n", PTHREAD_STACK_MIN); errno = pthread_attr_setstacksize(&attr, rozmiar); test_errno("pthread_attr_setstacksize"); } else { pthread_attr_getstacksize(&attr, &rozmiar); printf("domy¶lny rozmiar stosu: %u\n", rozmiar); } errno = pthread_create(&id, &attr, watek, NULL); test_errno("pthread_create"); pthread_join(id, NULL); puts("w±tek zakoñczony"); pthread_attr_destroy(&attr); }
static void test_io_close(int with_timer, bool from_path) { #define chunks 4 #define READSIZE (512*1024) unsigned int i; const char *path = LARGE_FILE; int fd = open(path, O_RDONLY); if (fd == -1) { if (errno == ENOENT) { test_skip("Large file not found"); return; } test_errno("open", errno, 0); test_stop(); } #ifdef F_GLOBAL_NOCACHE if (fcntl(fd, F_GLOBAL_NOCACHE, 1) == -1) { test_errno("fcntl F_GLOBAL_NOCACHE", errno, 0); test_stop(); } #endif struct stat sb; if (fstat(fd, &sb)) { test_errno("fstat", errno, 0); test_stop(); } const size_t size = (size_t)sb.st_size / chunks; const int expected_error = with_timer? ECANCELED : 0; dispatch_source_t t = NULL; dispatch_group_t g = dispatch_group_create(); dispatch_group_enter(g); void (^cleanup_handler)(int error) = ^(int error) { test_errno("create error", error, 0); dispatch_group_leave(g); close(fd); }; dispatch_io_t io; if (!from_path) { io = dispatch_io_create(DISPATCH_IO_RANDOM, fd, dispatch_get_global_queue(0, 0), cleanup_handler); } else { #if DISPATCHTEST_IO_PATH io = dispatch_io_create_with_path(DISPATCH_IO_RANDOM, path, O_RDONLY, 0, dispatch_get_global_queue(0, 0), cleanup_handler); #endif } dispatch_io_set_high_water(io, READSIZE); if (with_timer == 1) { dispatch_io_set_low_water(io, READSIZE); dispatch_io_set_interval(io, 2 * NSEC_PER_SEC, DISPATCH_IO_STRICT_INTERVAL); } else if (with_timer == 2) { t = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0,0)); dispatch_retain(io); dispatch_source_set_event_handler(t, ^{ dispatch_io_close(io, DISPATCH_IO_STOP); dispatch_source_cancel(t); });
int main() { pid_t pid; puts("pocz±tek programu"); inicjalizacja_watkow(); /* rejestrowanie funkcji wykonywanej w procesie potomnym */ errno = pthread_atfork(NULL, NULL, inicjalizacja_watkow); test_errno("pthread_atfork"); sleep(1); pid = fork(); printf("fork => %d\n", pid); switch (pid) { case -1: test_errno("fork"); break; case 0: // proces potomny sleep(2); break; default: // proces nadrzêdny waitpid(pid, NULL, 0); test_errno("waitpid"); break; } /* koñczymy proces, bez ogl±dania siê na w±tki */ return EXIT_SUCCESS; }
int main() { pthread_t id; /* utworzenie w±tku */ errno = pthread_create(&id, NULL, watek, NULL); test_errno("pthread_create"); /* oczekiwanie na jego zakoñczenie */ errno = pthread_join(id, NULL); test_errno("pthread_join"); return EXIT_SUCCESS; }
/** Directory tree is now top / topfile middle2 / middle1 / bottom1 / absfile / bottom2 / file -- read-only / read-only / sub-read-only / file @SYMTestCaseID SYSLIB-STDLIB-CT-1049 @SYMTestCaseDesc Tests for renaming operations @SYMTestPriority High @SYMTestActions Tests by renaming files.Tests for the error code returned while renaming files @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void renaming() { int err; test_Next("Renaming"); err=chdir(rootpath); test(err==0); err=rename("middle1","middle2"); test_errno(err<0,EEXIST); err=rename("middle1/bottom1/absfile","middle2/absfile"); test(err==0); err=rename("middle2/absfile","middle1/bottom1/absfile"); test(err==0); err=rename("middle1/bottom1/absfile","middle2/nonsuch/newname"); test_errno(err<0,ENOENT); err=rename("middle1","middle1/bottom1/subdirectory_of_self"); test_errno(err<0,EACCES); err=rename("middle1","newname"); test(err==0); err=rename("newname/bottom2/file","middle2/file"); test(err==0); err=rename("newname","middle1"); test(err==0); err=rename("middle2/file","middle1/bottom2/file"); test(err==0); err=rename("no such file","valid new name"); test_errno(err<0,ENOENT); err=rename("no such file","topfile"); test_errno(err<0,ENOENT); err=rename(".","../different top"); /* test_errno(err<0,EACCES); -- can't change "." */ test(err==0); /* STDLIB resolves "." to full path, so this works */ err=rename("../different top",rootpath); test(err==0); }
long get_thread_time(pthread_t id) { clockid_t id_zegara; errno = pthread_getcpuclockid(id, &id_zegara); test_errno("pthread_getcpuclockid"); return clock_ms(id_zegara); }
void* watek(void* numer) { int i; for (i=0; i < K; i++) { #ifdef BLOKADA errno = pthread_mutex_lock(&blokada); test_errno("pthread_mutex_lock"); #endif licznik = licznik + 1; ms_sleep(1); #ifdef BLOKADA errno = pthread_mutex_unlock(&blokada); test_errno("pthread_mutex_unlock"); #endif } return NULL; }
int main() { pthread_t id1; pthread_t id2; /* utworzenie 2 w±tków */ errno = pthread_create(&id1, NULL, watek, (void*)(0)); test_errno("pthread_create (1)"); errno = pthread_create(&id2, NULL, watek, (void*)(1)); test_errno("pthread_create (2)"); /* oczekiwanie na zakoñczenie */ pthread_join(id1, NULL); pthread_join(id2, NULL); return EXIT_SUCCESS; }
int main() { pthread_t id[N]; int i; /* utworzenie kilku w±tków w±tku */ for (i=0; i < N; i++) { errno = pthread_create(&id[i], NULL, watek, NULL); test_errno("Nie powiod³o siê pthread_create"); } /* oczekiwanie na jego zakoñczenie */ for (i=0; i < N; i++) { errno = pthread_join(id[i], NULL); test_errno("pthread_join"); } return EXIT_SUCCESS; }
void interrupt(pthread_t id, const char* napis) { struct timespec tim; tim.tv_sec = 0; tim.tv_nsec = 5000000; nanosleep(&tim, NULL); printf("%s: interrupt start\n", napis); errno = pthread_cancel(id); test_errno("pthread_cancel"); printf("%s: wating for thread \n", napis); errno = pthread_join(id, NULL); test_errno("pthread_join"); printf("%s: interrupt end\n", napis); }
void inicjalizacja_watkow() { int i; printf("tworzenie %d w±tków w procesie %d\n", N, getpid()); /* utworzenie w±tków */ for (i=0; i < N; i++) { errno = pthread_create(&id[i], NULL, watek, (void*)(i+1)); test_errno("pthread_create"); } }
void* thread_03(void* num) { pthread_cleanup_push(thread_cleanup, num); errno = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); test_errno("pthread_setcancelstate"); printf("thread #%d - cancel disabled for 2s\n", (int)(size_t)num); sleep(2); printf("thread #%d - cancel enabled\n", (int)(size_t)num); errno = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); test_errno("pthread_setcancelstate"); pause(); //pthread_cleanup_pop(1); return NULL; }
bool torture_local_replace(struct torture_context *ctx) { bool ret = true; ret &= test_ftruncate(); ret &= test_strlcpy(); ret &= test_strlcat(); ret &= test_mktime(); ret &= test_initgroups(); ret &= test_memmove(); ret &= test_strdup(); ret &= test_setlinebuf(); ret &= test_vsyslog(); ret &= test_timegm(); ret &= test_setenv(); ret &= test_strndup(); ret &= test_strnlen(); ret &= test_waitpid(); ret &= test_seteuid(); ret &= test_setegid(); ret &= test_asprintf(); ret &= test_snprintf(); ret &= test_vasprintf(); ret &= test_vsnprintf(); ret &= test_opendir(); ret &= test_readdir(); ret &= test_telldir(); ret &= test_seekdir(); ret &= test_dlopen(); ret &= test_chroot(); ret &= test_bzero(); ret &= test_strerror(); ret &= test_errno(); ret &= test_mkdtemp(); ret &= test_mkstemp(); ret &= test_pread(); ret &= test_pwrite(); ret &= test_getpass(); ret &= test_inet_ntoa(); ret &= test_strtoll(); ret &= test_strtoull(); ret &= test_va_copy(); ret &= test_FUNCTION(); ret &= test_MIN(); ret &= test_MAX(); ret &= test_socketpair(); ret &= test_strptime(); ret &= test_getifaddrs(); ret &= test_utime(); ret &= test_utimes(); ret &= test_memmem(); return ret; }
void* thread_01(void* num) { int i, n; pthread_cleanup_push(thread_cleanup, num); errno = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); test_errno("pthread_setcancelstate"); errno = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); test_errno("pthread_setcanceltype"); printf("thread #%d (PTHREAD_CANCEL_ASYNCHRONOUS)\n", (int)(size_t)num); while (1) { n = 1000000; for (i=0; i < n; i++); } pthread_cleanup_pop(1); return NULL; }
int main() { pthread_t id[N]; parametry param[N]; int i; srand(time(NULL)); printf("pocz±tek programu, uruchomianie zostanie %d w±tków\n", N); /* utworzenie w±tku */ for (i=0; i < N; i++) { param[i].id = i; param[i].n = rand() % 100000000 + 1; errno = pthread_create(&id[i], NULL, watek, ¶m[i]); test_errno("pthread_create"); } /* stan na mniej wiêcej pó³metku */ sleep(1); puts("po oko³o sekundzie w±tki zu¿y³y:"); for (i=0; i < N; i++) printf("* #%d: %ldms\n", i, get_thread_time(id[i])); /* oczekiwanie na zakoñczenie w±tków */ for (i=0; i < N; i++) { errno = pthread_join(id[i], NULL); test_errno("pthread_join"); } /* jeszcze podsumowanie */ puts(""); printf("g³ówny w±tek zu¿y³ %ldms czasu procesora\n", clock_ms(CLOCK_THREAD_CPUTIME_ID)); printf("proces zu¿y³ %ldms czasu procesora\n", clock_ms(CLOCK_PROCESS_CPUTIME_ID)); return EXIT_SUCCESS; }
int main(int argc, char* argv[]) { pthread_t id; if (argc < 1) exit(1); switch( atoi(argv[1]) ) { case 1: // async errno = pthread_create(&id, NULL, thread_01, (void*)(size_t)(0)); test_errno("pthread_create (1)"); interrupt(id, "#0"); break; case 2: // deffered errno = pthread_create(&id, NULL, thread_02, (void*)(size_t)(1)); test_errno("pthread_create (2)"); interrupt(id, "#1"); break; default: // disable enable errno = pthread_create(&id, NULL, thread_03, (void*)(size_t)(2)); test_errno("pthread_create (3)"); interrupt(id, "#2"); } return EXIT_SUCCESS; }
void* thread_02(void* num) { int i, n; pthread_cleanup_push(thread_cleanup, num); errno = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); test_errno("pthread_setcancelstate"); errno = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); test_errno("pthread_setcanceltype"); printf("thread #%d (PTHREAD_CANCEL_DEFERRED)\n", (int)(size_t)num); while (1) { n = 1000000; for (i=0; i < n; i++); printf("pthread_testcancel\n"); pthread_testcancel(); // interruption point } pthread_cleanup_pop(1); return NULL; }
/** Directory tree is now top / topfile middle2 / extrafile middle1 / bottom1 / absfile / bottom2 / file -- read-only / read-only / sub-read-only / file @SYMTestCaseID SYSLIB-STDLIB-CT-1052 @SYMTestCaseDesc Tests for searching on different drives @SYMTestPriority High @SYMTestActions Tests by searching on z drive,test for the error codes @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void searching() { int err,fd; char name[MAXPATHLEN+1]; test_Next("Searching across drives"); sprintf(name,"%s/middle2/extrafile",rootpath); err=chdir("z:/"); test(err==0); fd=open(name+2, O_RDONLY, 0); test_errno(fd<0,ENOENT); // doesn't exist on z: name[0]='?'; fd=open(name, O_RDWR, 0); test(fd>=0); // found it on the original drive err=close(fd); test(err==0); }
/** Directory tree is now top / topfile middle2 / extrafile middle1 / bottom1 / absfile / bottom2 / file -- read-only / read-only / sub-read-only / file @SYMTestCaseID SYSLIB-STDLIB-CT-1053 @SYMTestCaseDesc Tests for deleting files @SYMTestPriority High @SYMTestActions Tests by deleting files and directories.Test for error codes @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void deletion() { int err; char namebuf[MAXPATHLEN]; wchar_t widename[] = WIDENAME; test_Next("Deleting - files"); err=chdir(rootpath); test(err==0); err=unlink("middle1/bottom2/file"); test_errno(err<0,EACCES); /* file is read-only */ err=chmod("middle1/bottom2/file",0777); test(err==0); err=unlink("middle1/bottom2/file"); test(err==0); err=unlink("middle2/extrafile"); test(err==0); err=unlink("middle1/read-only/file"); /* test_errno(err<0,EPERM); parent directory is read-only */ test(err==0); /* Omission - EPOC32 uses Win32 semantics for read-only directories */ test_Next("Deleting - directories"); err=chdir(rootpath); test(err==0); //delete the dir with a wide character in the name err = wcstombs(namebuf, widename, MAXPATHLEN); test(err!=-1); err=rmdir(namebuf); test(err==0); err=rmdir("middle1"); test_errno(err<0,EEXIST); /* not empty */ err=rmdir("middle1/bottom1"); test_errno(err<0,EEXIST); /* not empty */ err=unlink("middle1/bottom1/absfile"); test(err==0); err=rmdir("middle1/bottom1"); test(err==0); err=rmdir("middle1/bottom1"); test_errno(err<0,ENOENT); /* already deleted */ err=rmdir("middle1"); test_errno(err<0,EEXIST); err=rmdir("middle1/bottom2"); test(err==0); test_Next("Deleting - read-only directories"); err=rmdir("middle1/read-only/sub-read-only"); /* test_errno(err!=0,EACCES); -- permission denied - read-only parent */ test_errno(err<0,EACCES); /* Omission - EPOC32 uses Win32 semantics */ err=chmod("middle1/read-only",0777); test(err==0); err=rmdir("middle1/read-only/sub-read-only"); /* test(err==0); */ /* EPOC32 doesn't use the writeability of the parent directory, but instead looks * at the attributes of the directory itself. */ test_errno(err!=0,EACCES); err=chmod("middle1/read-only/sub-read-only",0777); test(err==0); err=rmdir("middle1/read-only/sub-read-only"); test(err==0); err=rmdir("middle1/read-only"); test(err==0); err=rmdir("middle?"); test_errno(err<0,EINVAL); /* no wild cards please */ err=rmdir("middle1"); test(err==0); err=rmdir("../top/middle2"); test(err==0); err=rmdir("."); test_errno(err<0,EEXIST); /* not empty */ err=unlink("topfile"); test(err==0); err=rmdir("."); test(err==0); }
/** Directory tree is now top / topfile middle2 / middle1 / bottom1 / absfile / bottom2 / file -- read-only / read-only / sub-read-only / file @SYMTestCaseID SYSLIB-STDLIB-CT-1050 @SYMTestCaseDesc Tests for enumeration on directories @SYMTestPriority High @SYMTestActions Tests by opening directories @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void directory() { int err, count, i, j, fd; DIR *dp; struct dirent *ep; char name[MAXPATHLEN+1]; off_t pos; test_Next("Enumerating Directories"); err=chdir(rootpath); test(err==0); dp=opendir("topfile"); /* test_errno(dp==0,ENOTDIR); -- not convinced about this anyway */ test_errno(dp==0,ENOENT); dp=opendir("no such file"); test_errno(dp==0,ENOENT); //test something sensible happens if someone uses a WDIR inplace of a DIR { WDIR *wp = wopendir((wchar_t*)L"."); test(wp!=0); // Test wants a WDIR passed but won't compile under CW. // Force the compile by casting. The function will still get a WDIR. // DIR inherits from WDIR. ep=readdir((DIR*)wp); test_errno(ep==0,EINVAL); wclosedir(wp); } dp=opendir("."); test(dp!=0); count=0; do { ep=readdir(dp); if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0) count++; } while (ep!=0); test(count==4); for (i=0; i<4; i++) { rewinddir(dp); for (j=0; j<=i; j++) { ep=readdir(dp); test(ep!=0); } strcpy(name,ep->d_name); rewinddir(dp); for (j=0; j<=i; j++) { ep=readdir(dp); test(ep!=0); } test(strcmp(name,ep->d_name)==0); } for (i=0; i<4; i++) { rewinddir(dp); pos=telldir(dp); for (j=0; j<=i; j++) { pos=telldir(dp); ep=readdir(dp); test(ep!=0); } strcpy(name,ep->d_name); rewinddir(dp); seekdir(dp, pos); ep=readdir(dp); test(ep!=0); test(strcmp(name,ep->d_name)==0); } err=closedir(dp); test(err==0); dp=opendir("middle2"); test(dp!=0); count=0; do { ep=readdir(dp); if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0) count++; } while (ep!=0); test(count==0); /* empty directory */ rewinddir(dp); fd = open("middle2/extrafile",O_RDWR+O_CREAT,0777); test(fd>=0); err=close(fd); test(err==0); count=0; do { ep=readdir(dp); if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0) count++; } while (ep!=0); test(count==0); /* shouldn't have noticed the change */ rewinddir(dp); /* and spot the new file */ count=0; do { ep=readdir(dp); if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0) count++; } while (ep!=0); test(count==1); closedir(dp); dp=opendir("/"); test(dp!=0); count=0; do { ep=readdir(dp); if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0) count++; } while (ep!=0); test(count>0); closedir(dp); }
int main(int argc, char** argv) { struct hostent *he; int sockfd, clientfd; struct sockaddr_in addr1, addr2, server; socklen_t addr2len; socklen_t addr1len; pid_t clientid; const char *path = "/usr/share/dict/words"; int read_fd, fd; if (argc == 2) { // Client dispatch_test_start(NULL); if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { test_errno("Client-socket()", errno, 0); test_stop(); } if ((he = gethostbyname("localhost")) == NULL) { fprintf(stderr, "Client-gethostbyname() failed\n"); test_stop(); } memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length); server.sin_family = AF_INET; server.sin_port = atoi(argv[1]); fprintf(stderr, "Client-connecting on port ... %d\n", server.sin_port); if (connect(sockfd, (struct sockaddr *)&server, sizeof(server))) { test_errno("client-connect()", errno, 0); test_stop(); } // Read from the socket and compare the contents are what we expect fd = open(path, O_RDONLY); if (fd == -1) { test_errno("client-open", errno, 0); test_stop(); } #ifdef F_NOCACHE if (fcntl(fd, F_NOCACHE, 1)) { test_errno("client-fcntl F_NOCACHE", errno, 0); test_stop(); } #else // investigate what the impact of lack of file cache disabling has // for this test #endif struct stat sb; if (fstat(fd, &sb)) { test_errno("client-fstat", errno, 0); test_stop(); } size_t size = sb.st_size; __block dispatch_data_t g_d1 = dispatch_data_empty; __block dispatch_data_t g_d2 = dispatch_data_empty; __block int g_error = 0; dispatch_group_t g = dispatch_group_create(); dispatch_group_enter(g); dispatch_read(fd, size, dispatch_get_global_queue(0, 0), ^(dispatch_data_t d1, int error) { test_errno("Client-dict-read error", error, 0); test_long("Client-dict-dispatch data size", dispatch_data_get_size(d1), size); dispatch_retain(d1); g_d1 = d1; dispatch_group_leave(g); });
/** @SYMTestCaseID SYSLIB-STDLIB-CT-1073 @SYMTestCaseDesc Tests for ARPA net functions @SYMTestPriority High @SYMTestActions Tests for get host by name and host by address functions @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void testNetDB() { char hostname[128]; struct in_addr addr, *addrp; int err, i; struct hostent *hp; test_Next("Get Host Name"); err=gethostname(hostname,sizeof(hostname)); test(err==0); printf(" hostname = >%s<\n", hostname); test_Next("Get Host By Name"); for (i=0; i<N_NAMES; i++) { hp=gethostbyname(names[i][0]); test_ok(hp!=0); addrp=(struct in_addr*)(hp->h_addr_list[0]); printf(" %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp)); test(strcasecmp(hp->h_name,names[i][0])==0); test(addrp->s_addr==inet_addr(names[i][1])); } hp=gethostbyname("nosuchname.symbian.com"); test_errno(hp==0,ENOENT); test(errno==HOST_NOT_FOUND); test_Next("Get Address of \"\""); hp=gethostbyname(""); test_ok(hp!=0); addrp=(struct in_addr*)(hp->h_addr_list[0]); printf(" %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp)); test_Next("Get Host By Addr"); for (i=0; i<N_NAMES; i++) { addr.s_addr=inet_addr(names[i][1]); hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET); test_ok(hp!=0); addrp=(struct in_addr*)(hp->h_addr_list[0]); printf(" address %-15s => %s\n", inet_ntoa(*addrp), hp->h_name); test(addrp->s_addr==addr.s_addr); test(strcasecmp(hp->h_name,names[i][2])==0); } addr.s_addr=inet_addr("10.11.199.255"); hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET); test_errno(hp==0,ENOENT); test(errno==HOST_NOT_FOUND); /* struct sockaddr_in testaddr; int fd; test_Next("Connect to the Imperial College Echo server"); fd=socket(AF_INET, SOCK_STREAM, 0); test_ok(fd>=0); testaddr.sin_family=AF_INET; testaddr.sin_addr.s_addr=inet_addr("193.63.255.1"); testaddr.sin_port=htons(7); // echo err=connect(fd,(struct sockaddr*)&testaddr, sizeof(testaddr)); test(err==0); close(fd); */ test_Next("Get Address of roundrobin.test.intra which has multiple address"); hp=gethostbyname("roundrobin.test.intra"); test_ok(hp!=0); if (hp) { if (hp->h_addr_list) { int Index = 0; while (hp->h_addr_list[Index]) { addrp = (struct in_addr*)(hp->h_addr_list[Index]); printf(" %-30s => address %-15s\n", hp->h_name, inet_ntoa(*addrp)); Index++; } } } test_Next("Get Host name of 192.168.255.4 which has multiple host name"); addr.s_addr=inet_addr("192.168.255.4"); hp=gethostbyaddr((const char *)&addr,sizeof(addr),AF_INET); test_ok(hp!=0); if (hp) { addrp=(struct in_addr*)(hp->h_addr_list[0]); printf(" address %-15s => %s\n", inet_ntoa(*addrp), hp->h_name); if (hp->h_aliases) { int Index = 0; while (hp->h_aliases[Index]) { printf(" address %-15s => %s\n", inet_ntoa(*addrp), hp->h_aliases[Index]); Index++; } } } if (close_console) { test_Close(); close(0); close(1); close(2); } }
int unistd_access_test(){ //check the access to the executable int fd; int ret; test_intro("access", 0, "/app/flash/posix, F_OK"); ret = access(EXEC_PATH, F_OK); if( test_success(ret) < 0 ){ return -1; } test_intro("access", "ENOENT", "/nomount/some/file, F_OK"); ret = access("/nomount/some/file", F_OK); if( test_errno(ret, "ENOENT", ENOENT) < 0 ){ return -1; } test_intro("access", "ENOENT", "/nomount, F_OK"); ret = access("/nomount", F_OK); if( test_errno(ret, "ENOENT", ENOENT) < 0 ){ return -1; } test_intro("access", "ENAMETOOLONG", "/0123456789012345678901234567890123456789012345678901234567890123456789, F_OK"); ret = access("/0123456789012345678901234567890123456789012345678901234567890123456789", F_OK); if( test_errno(ret, "ENAMETOOLONG", ENAMETOOLONG) < 0 ){ return -1; } test_intro("access", "ENAMETOOLONG", "/0123456789/01234567890/123456789/0123/4567890/123456789012345/67890123/456/789, F_OK"); ret = access("/0123456789/01234567890/123456789/0123/4567890/123456789012345/67890123/456/789", F_OK); if( test_errno(ret, "ENAMETOOLONG", ENAMETOOLONG) < 0 ){ return -1; } //create a file in home test_intro("access", 0, "/home/test.txt, W_OK"); fd = open("/home/test.txt", O_RDWR | O_CREAT, 0666); if( fd < 0 ){ return test_failed_perror(); } if( write(fd, "test", 4) < 0 ){ close(fd); return test_failed_perror(); } if( close(fd) < 0 ){ return test_failed_perror(); } ret = access("/home/test.txt", W_OK); if( test_success(ret) < 0 ){ return -1; } test_intro("access", "EACCESS", "app/flash/posix, W_OK"); ret = access(EXEC_PATH, W_OK); if( test_errno(ret, "EACCES", EACCES) < 0 ){ return -1; } test_intro("access", "EACCESS", "/, W_OK"); ret = access("/", W_OK); if( test_errno(ret, "EACCES", EACCES) < 0 ){ return -1; } test_intro("access", 0, "/app/flash/posix, R_OK"); ret = access(EXEC_PATH, R_OK); if( test_success(ret) < 0 ){ return -1; } test_intro("access", "EACCES", "/app/.install, R_OK"); ret = access("/app/.install", R_OK); if( test_errno(ret, "EACCES", EACCES) < 0 ){ return -1; } test_intro("access", 0, "/app/flash/posix, X_OK"); ret = access(EXEC_PATH, X_OK); if( test_success(ret) < 0 ){ return -1; } test_intro("access", "EACCES", "/app/.install, X_OK"); ret = access("/app/.install", X_OK); if( test_errno(ret, "EACCES", EACCES) < 0 ){ return -1; } return 0; }
/** @SYMTestCaseID SYSLIB-STDLIB-CT-1047 @SYMTestCaseDesc Tests for operations on directory @SYMTestPriority High @SYMTestActions Tests by creating directory with invalid name,existing directory name,wide characters Tests for the error code returned while creating directories. @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void make_tree() { int err; char namebuf[MAXPATHLEN], namebuf2[MAXPATHLEN]; char toobig[MAXPATHLEN+MAXPATHLEN+1]; char *p; wchar_t *wp; wchar_t widenamebuf[MAXPATHLEN+1]; wchar_t widename[] = WIDENAME; test_Next("Create Directory Tree - relative paths"); err=mkdir("***", 0777); test_errno(err==-1,EINVAL); /* bad directory name */ err=mkdir("top", 0777); test(err==0); err=mkdir("top", 0777); test_errno(err==-1,EEXIST); /* directory already exists */ //make a dir with a wide character in the name err = wcstombs(namebuf, widename, MAXPATHLEN); test(err!=-1); err=mkdir(namebuf, 0777); test(err==0); err=mkdir("top/middle1/bottom1", 0777); test_errno(err==-1,ENOENT); /* missing middle bit of path */ err=mkdir("top/middle1", 0777); test(err==0); err=chdir("./top//\\/.\\.\\../top/."); test(err==0); p = getcwd(rootpath,sizeof(rootpath)); /* save name of toplevel directory */ test(p==rootpath); err=chdir("middle1"); test(err==0); err=chdir("bottom2"); test_errno(err==-1,ENOENT); /* directory doesn't exist yet */ p = getcwd(namebuf,sizeof(namebuf)); /* prepare name for tests later */ test(p==namebuf); err=mkdir("bottom1",0777); test(err==0); err=mkdir("read-only",0444); test(err==0); err=mkdir("read-only/sub-read-only",0444); /* test_errno(err==-1,EACCES); */ test(err==0); /* Omission - EPOC32 has Win32 semantics for read-only directories */ err=chdir("../../top/middle1/bottom1"); test(err==0); test_Next("Create Directory Tree - absolute paths"); p = strcat(namebuf,"/bottom2"); test(p==namebuf); /* .../top/middle1/bottom2 */ err=chdir(namebuf); test_errno(err==-1,ENOENT); /* directory doesn't exist yet */ err=mkdir(namebuf, 0777); test(err==0); err=chdir(namebuf); test(err==0); p = getcwd(namebuf,sizeof(namebuf)); test(p==namebuf); err=mkdir("../../middle2", 0777); test(err==0); p = getcwd(namebuf2,sizeof(namebuf2)); test(p==namebuf2); test(strcmp(namebuf,namebuf2)==0); /* mkdir shouldn't change cwd */ memset(toobig,'a', sizeof(toobig)); toobig[sizeof(toobig)-1]='\0'; err=mkdir(toobig,0777); test_errno(err<0,ENAMETOOLONG); test_Next("Test getcwd"); //size too small p = getcwd(namebuf, 4); test_errno(0==p, ERANGE); //make it alloc a buffer p = getcwd(NULL, 300); test (NULL != p); free(p); //alloc a buffer then fail with a too small size p = getcwd(NULL, 10); test_errno(0==p, ERANGE); wp = wgetcwd(widenamebuf, MAXPATHLEN-1); test (NULL != wp); p = getcwd(namebuf2, MAXPATHLEN-1); test (NULL != p); wcstombs(namebuf, widenamebuf, MAXPATHLEN-1); test(strcmp(namebuf, namebuf2) == 0); //test realpath strcpy(namebuf,"bobby.dog"); test( (0==strcmp("C:\\top\\", realpath("/top/../top/../top/./",namebuf))) || (0==strcmp("D:\\top\\", realpath("/top/../top/../top/./",namebuf)))); }
/** Directory tree is now top / middle2 middle1 / bottom1 / bottom2 / read-only / sub-read-only @SYMTestCaseID SYSLIB-STDLIB-CT-1048 @SYMTestCaseDesc Tests for operations on creating files @SYMTestPriority High @SYMTestActions Tests by opening files which does not exists,check for closing a file twice Tests for the error code returned while creating files @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void create_files() { int err; int fd; char namebuf[MAXPATHLEN],*p; test_Next("Creating Files - relative paths"); err=chdir(rootpath); test(err==0); fd = open("topfile",O_RDWR+O_APPEND,0777); test_errno(fd<0,ENOENT); /* doesn't exist */ fd = open("topfile",O_RDWR+O_CREAT,0777); test(fd>=0); err=close(fd); test(err==0); err=close(fd); test_errno(err<0,EBADF); /* can't close it twice */ fd = open("topfile",O_RDWR+O_APPEND,0777); test(fd>=0); err=close(fd); test(err==0); fd = open("topfile",O_RDWR+O_CREAT+O_EXCL,0777); test_errno(fd<0,EEXIST); /* already exists */ fd = open("middle1/bottom2/file",O_RDONLY+O_CREAT,0444); test(fd>=0); err=close(fd); test(err==0); fd = open("middle1/bottom2/file",O_RDWR+O_APPEND,0777); /* test_errno(fd<0,EACCES); */ test(fd>=0); /* Omission - the original O_CREAT ignores the 0444 permissions */ if (fd>=0) { err=close(fd); test(err==0); } err=chmod("middle1/bottom2/file",0444); test(err==0); fd = open("middle1/bottom2/file",O_RDWR+O_APPEND,0777); test_errno(fd<0,EACCES); /* not writeable */ fd = open("middle2",O_RDWR+O_CREAT,0777); /* test_errno(fd<0,EISDIR); */ test_errno(fd<0,EACCES); /* Omission - we don't do EISDIR */ test_Next("Creating Files - absolute paths"); err=chdir("middle1/bottom1"); test(err==0); p = getcwd(namebuf,sizeof(namebuf)); /* prepare name for tests later */ test(p==namebuf); p = strcat(namebuf,"absfile"); test(p==namebuf); fd = open(namebuf,O_RDWR+O_CREAT,0777); test(fd>=0); err=close(fd); test(err==0); fd = open("../read-only/file",O_RDWR+O_CREAT,0444); /* test_errno(fd<0,EACCES); */ test(fd>=0); /* Omission - EPOC32 has Win32 semantics for read-only directories */ if (fd>=0) { err=close(fd); test(err==0); } }
/** Directory tree is now top / topfile middle2 / extrafile middle1 / bottom1 / absfile / bottom2 / file -- read-only / read-only / sub-read-only / file @SYMTestCaseID SYSLIB-STDLIB-CT-1051 @SYMTestCaseDesc Tests for file attributes @SYMTestPriority High @SYMTestActions Tests the attributes on files and directories @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void attributes() { int err; struct stat s1,s2; int fd; double diff; test_Next("File Attributes"); err=chdir(rootpath); test(err==0); err=stat("middle earth/bag end/hobbit",&s1); test_errno(err<0,ENOENT); err=stat("middle1/bottom2/file",&s1); test(err==0); test(S_ISREG(s1.st_mode)!=0); test(S_ISDIR(s1.st_mode)==0); test((s1.st_mode&S_IWUSR)==0); test(s1.st_size==0); err=stat("topfile",&s1); test(err==0); test(S_ISREG(s1.st_mode)!=0); test(S_ISDIR(s1.st_mode)==0); test((s1.st_mode&S_IWUSR)!=0); test(s1.st_size==0); err=stat("topfile",&s2); test(err==0); test(s1.st_mode==s2.st_mode); test(s1.st_size==s2.st_size); diff=difftime(s1.st_mtime,s2.st_mtime); test(diff==(double)0.0); fd=open("topfile", O_RDONLY, 0); test(fd>=0); err=fstat(fd,&s2); test(err==0); test(s1.st_mode==s2.st_mode); test(s1.st_size==s2.st_size); diff=difftime(s1.st_mtime,s2.st_mtime); test(diff==(double)0.0); err=stat("topfile",&s2); test(err==0); test(s1.st_mode==s2.st_mode); test(s1.st_size==s2.st_size); diff=difftime(s1.st_mtime,s2.st_mtime); test(diff==(double)0.0); err=close(fd); test(err==0); sleep(1); /* to ensure that the modify time changes */ fd=open("topfile", O_RDWR+O_APPEND, 0); test(fd>=0); err=stat("topfile",&s2); test(err==0); test(s1.st_mode==s2.st_mode); test(s1.st_size==s2.st_size); /* probably not guaranteeed to have changed the modtime at this point */ err=write(fd,rootpath,3); test(err==3); err=fsync(fd); test(err==0); err=close(fd); test(err==0); err=stat("topfile",&s2); test(err==0); test(s1.st_mode==s2.st_mode); test(s2.st_size==3); diff=difftime(s2.st_mtime,s1.st_mtime); test(diff>(double)0.0); test_Next("Directory Attributes"); err=stat("middle1",&s1); test(err==0); test(S_ISREG(s1.st_mode)==0); test(S_ISDIR(s1.st_mode)==1); test((s1.st_mode&S_IWUSR)!=0); err=stat("middle1/read-only",&s1); test(err==0); test(S_ISREG(s1.st_mode)==0); test(S_ISDIR(s1.st_mode)==1); test((s1.st_mode&S_IWUSR)==0); err=stat("/",&s1); test(err==0); test(S_ISREG(s1.st_mode)==0); test(S_ISDIR(s1.st_mode)==1); err=access("middle1/bottom1/absfile",W_OK); test(err==0); err=access("middle1/bottom1/absfile",R_OK); test(err==0); err=access("middle1/bottom2/file",W_OK); test(err!=0); err=access("middle1/bottom2/file",R_OK); test(err==0); err=access("middle1/read-only",W_OK); test(err==0); err=access("middle1/read-only",R_OK); test(err==0); err=access("middle1/no such directory",R_OK); test(err!=0); }