git.fiddlerwoaroof.com
token_test.cc
8ab94c71
 #include <cstring>
69be5e4e
 #include <pwd.h>
8ab94c71
 #include <cstdio>
69be5e4e
 #include <sys/stat.h>
 
de34378f
 #include "token.h"
1c7f8bf0
 #include "test_util.h"
de34378f
 
6f0de833
 const char *fake_user = "";
 const char *fake_user_token = "";
 
 // all the fake system calls
8ab94c71
 const char *fake_home_dir = "";
cb403154
 int fake_getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result) {
69be5e4e
   strcpy(buffer, fake_home_dir);
   pwd->pw_dir = buffer;
cb403154
   int ok = !strcmp(nam, fake_user);
69be5e4e
   *result = ok ? pwd : 0;
cb403154
   return !ok;
 }
de34378f
 
69be5e4e
 
8ab94c71
 const char *fake_stat_path = "";
69be5e4e
 int fake_stat(const char *path, struct stat *stat) {
     return (strcmp(fake_stat_path, path));
 }
 
8ab94c71
 const char *fake_fopen_path = "";
 const char *fake_fopen_mode = "";
69be5e4e
 FILE *_fhandle = 0;
 FILE *fake_fopen(const char *path, const char *mode) {
     static FILE handle;
     int path_matches = !strcmp(fake_fopen_path, path);
     int mode_matches = !strcmp(fake_fopen_mode, mode);
     if(path_matches && mode_matches) {
         _fhandle = &handle;
         return &handle;
     } else {
         _fhandle = 0;
         return 0;
     }
 }
 
 char *fake_fgets(char *buf, int n, FILE *fp) {
     if (_fhandle == fp && fp != 0) {
        strncpy(buf, fake_user_token, n - 1);
         return buf;
     } else {
         return 0;
     }
 }
 
 int fake_fclose(FILE *fp) {
     return 0;
 }
 
 
 // STDIO
 
 
 
de34378f
 RESET_VARS_START
69be5e4e
 fake_user = "msmith";
 fake_user_token = "123456";
 fake_home_dir = "/home/msmith";
 fake_stat_path = "/home/msmith/.dual_control";
 fake_fopen_path = fake_stat_path;
 fake_fopen_mode = "r";
de34378f
 RESET_VARS_END
 
6f0de833
 
 int validate_compares_to_user_token() {
 
     // given
 
     // when
34ba20e5
     int valid = validate_token("msmith", "123456");
6f0de833
 
     // then
     check(valid, "expected result to be valid");
 
     succeed();
 
 }
 
cb403154
 int validates_from_the_right_user() {
     //given
 
     //when
34ba20e5
     int valid = validate_token("jbalcita", "12346");
69be5e4e
 
     //then
     check(!valid, "expected result to be invalid");
     succeed();
 }
 
 int validates_user_specific_token() {
     //given
 
     //when
34ba20e5
     int valid = validate_token("msmith", "654321");
cb403154
 
     //then
     check(!valid, "expected result to be invalid");
     succeed();
 }
6f0de833
 
de34378f
 int runtests() {
     test(validate_compares_to_user_token);
cb403154
     test(validates_from_the_right_user);
69be5e4e
     test(validates_user_specific_token);
de34378f
     succeed();
 }
 
 int main(int argc, char **argv) {
69be5e4e
     int rval = !runtests();
     return rval;
de34378f
 }