Skip to content

Instantly share code, notes, and snippets.

@YaSuenag
Created March 12, 2021 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YaSuenag/bdc73c3540335a096fa289ead36ca8b5 to your computer and use it in GitHub Desktop.
Save YaSuenag/bdc73c3540335a096fa289ead36ca8b5 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
#ifndef O_RDONLY
#define O_RDONLY 0 // for debug
#endif
// Function pointer for stub functions
int (*open)(const char *pathname, int flags);
ssize_t(*read)(int fd, void *buf, size_t count);
// close(2) stub
int close(int fd){
std::cout << "close() called" << std::endl;
}
/*********** test function **********************************/
void get_compatible_board(char *buf, int buflen) {
*buf = '\0';
int fd = open("/proc/device-tree/compatible", O_RDONLY);
if (fd != -1) {
ssize_t read_sz = read(fd, buf, buflen - 1);
if (read_sz > 0) {
buf[read_sz] = '\0';
// Replace '\0' to ' '
for (char *ch = buf; ch < buf + read_sz; ch++) {
if (*ch == '\0') {
*ch = ' ';
}
}
} else {
*buf = '\0';
}
close(fd);
}
}
/************************************************************/
// open(2) stub which returns error
int open_failed(const char *pathname, int flags){
return -1;
}
// open(2) stub which succeeds
int open_succeeded(const char *pathname, int flags){
return 0;
}
// read(2) stub which returns error
ssize_t read_failed(int fd, void *buf, size_t count){
return -1;
}
// read(2) stub which sets "t\0est" to buf
ssize_t read_succeeded(int fd, void *buf, size_t count){
((char *)buf)[0] = 't';
((char *)buf)[1] = '\0';
((char *)buf)[2] = 'e';
((char *)buf)[3] = 's';
((char *)buf)[4] = 't';
return 5;
}
// read(2) stub which behaves to read nothing
ssize_t read_zero(int fd, void *buf, size_t count){
return 0;
}
// read(2) stub which fills buf
ssize_t read_full(int fd, void *buf, size_t count){
for(char *ch = (char *)buf; ch < (char *)buf + count; ch++){
*ch = 'A';
}
return count;
}
// Set stub functions to read() and open(), and also clear buf with non-ascii value.
void init(int (*open_func)(const char *pathname, int flags),
ssize_t (*read_func)(int fd, void *buf, size_t count),
char *buf, int buflen){
open = open_func;
read = read_func;
for(char *ch = buf; ch < buf + buflen; ch++){
*ch = 0xff;
}
}
// test driver
main(){
const int BUFLEN = 7;
char buf[BUFLEN];
std::cout << "BUFLEN = " << BUFLEN << std::endl;
std::cout << "test 1 (open failed): ";
init(open_failed, NULL, buf, BUFLEN);
get_compatible_board(buf, BUFLEN);
std::cout << buf << " strlen: " << strlen(buf) << std::endl;
std::cout << "test 2 (read failed): ";
init(open_succeeded, read_failed, buf, BUFLEN);
get_compatible_board(buf, BUFLEN);
std::cout << buf << " strlen: " << strlen(buf) << std::endl;
std::cout << "test 3 (read succeeded): ";
init(open_succeeded, read_succeeded, buf, BUFLEN);
get_compatible_board(buf, BUFLEN);
std::cout << buf << " strlen: " << strlen(buf) << std::endl;
std::cout << "test 4 (read zero): ";
init(open_succeeded, read_zero, buf, BUFLEN);
get_compatible_board(buf, BUFLEN);
std::cout << buf << " strlen: " << strlen(buf) << std::endl;
std::cout << "test 5 (read full): ";
init(open_succeeded, read_full, buf, BUFLEN);
get_compatible_board(buf, BUFLEN);
std::cout << buf << " strlen: " << strlen(buf) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment