0

I am writing unit test for my c++ project where there are many functions which call glibc functions (malloc, read, open, write, close etc.) and also some external library functions such as openssl (SHA256 functions).

For example, Following function computes SHA256 checksum of a given file. Now this function has several calls to standard/external library functions such as, fopen, SHA256_Init, malloc etc. Now how to stub calls to these functions as our main objective is to unit test SHA256file, not the functions which it calls.

int MyClass::SHA256File(const char *path, char outputBuffer[65])
{
  FILE *file = fopen(path, "rb");
  if(!file) {
    throw exception("Failed to open file for SHA256 computation.");
  }

  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  if ( !SHA256_Init(&sha256) ) {
    throw exception("SHA256_Init failed.");
  }

  const int bufSize = 32768;
  unsigned char *buffer = (unsigned char*) malloc(bufSize);
  int bytesRead = 0;
  if(!buffer) { 
    throw exception("Failed to allocate buffer for SHA256 computation.");
  }

  while( fgets((char*)buffer, bufSize, file) != NULL ) { 
    curr_pos = ftell(file); 
    bytesRead = curr_pos - last_pos;
    last_pos = curr_pos;
    if( !SHA256_Update(&sha256, buffer, bytesRead) ) {
        throw exception("SHA256_Update failed..");
    }
  }

  if ( ferror(file) ) {
    throw exception("Failed to read file for SHA256 computation.");
  }

  if( !SHA256_Final(hash, &sha256) ) {
    throw exception("SHA256_Final failed..");
  }

  int i = 0;
  for(i = 0; i < SHA256_DIGEST_LENGTH; i++) {
    sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
  }

  outputBuffer[64] = 0;
  fclose(file);
  free(buffer);
  return 0;
}

I got few pointers like glibcmock but would prefer to have some standard support if glibc/gmock can provide.

Thanks in advance.

Quarra
  • 2,527
  • 1
  • 19
  • 27
user1228352
  • 569
  • 6
  • 21
  • Please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Nov 05 '19 at 11:27
  • And why are you using C I/O functions instead of C++ streams? And why `malloc` instead of vectors or strings? C++ is much more than "C with classes". – Some programmer dude Nov 05 '19 at 11:29
  • @Someprogrammerdude Yes, feof thing is wrong. I will fix it. But the problem in focus here is to stub standard library function in gtest. – user1228352 Nov 05 '19 at 17:37
  • To unit-test the `SHA256File` function you don't need to stub the standard library (or other) functions it calls. Instead use a set of predefined files for which you know the expected output from the function. Use those files and check the result compared to the precalculated expected output. – Some programmer dude Nov 05 '19 at 17:43
  • Thanks for the reply. Can you provide simple example of using a set of predefined files? – user1228352 Nov 06 '19 at 04:40
  • Ok. Got what do you mean by set of predefined files. So does it also mean that we won't be able to know if malloc fails (available memory < 32768) in unit test? Also, we will not be able to fail functions like SHA256_Init, SHA256_Update, fgets and SHA256_Final. Is it correct? – user1228352 Nov 06 '19 at 10:33
  • Good point! Some mocking and stubbing might be good after all to test the error handling. – Some programmer dude Nov 06 '19 at 10:41

0 Answers0