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 = "";
b017a4d2
 int fake_getpwnam_r (const char *nam, struct passwd *pwd, char *buffer,
                      size_t bufsize, struct passwd **result)
cdf7fd74
 {
b017a4d2
     strcpy (buffer, fake_home_dir);
cdf7fd74
     pwd->pw_dir = buffer;
b017a4d2
     int ok = !strcmp (nam, fake_user);
cdf7fd74
     *result = ok ? pwd : 0;
     return !ok;
cb403154
 }
de34378f
 
8ab94c71
 const char *fake_stat_path = "";
b017a4d2
 int fake_stat (const char *path, struct stat *stat)
cdf7fd74
 {
b017a4d2
     return (strcmp (fake_stat_path, path));
69be5e4e
 }
 
8ab94c71
 const char *fake_fopen_path = "";
 const char *fake_fopen_mode = "";
69be5e4e
 FILE *_fhandle = 0;
b017a4d2
 FILE *fake_fopen (const char *path, const char *mode)
cdf7fd74
 {
69be5e4e
     static FILE handle;
b017a4d2
     int path_matches = !strcmp (fake_fopen_path, path);
     int mode_matches = !strcmp (fake_fopen_mode, mode);
 
     if (path_matches && mode_matches) {
69be5e4e
         _fhandle = &handle;
         return &handle;
     } else {
         _fhandle = 0;
         return 0;
     }
 }
 
b017a4d2
 char *fake_fgets (char *buf, int n, FILE *fp)
cdf7fd74
 {
69be5e4e
     if (_fhandle == fp && fp != 0) {
b017a4d2
         strncpy (buf, fake_user_token, n - 1);
69be5e4e
         return buf;
     } else {
         return 0;
     }
 }
 
b017a4d2
 int fake_fclose (FILE *fp)
cdf7fd74
 {
69be5e4e
     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
 
cdf7fd74
 int validate_compares_to_user_token()
 {
6f0de833
 
     // given
 
     // when
b017a4d2
     int valid = validate_token ("msmith", "123456");
6f0de833
 
     // then
b017a4d2
     check (valid, "expected result to be valid");
6f0de833
 
     succeed();
 
 }
 
cdf7fd74
 int validates_from_the_right_user()
 {
cb403154
     //given
 
     //when
b017a4d2
     int valid = validate_token ("jbalcita", "12346");
69be5e4e
 
     //then
b017a4d2
     check (!valid, "expected result to be invalid");
69be5e4e
     succeed();
 }
 
cdf7fd74
 int validates_user_specific_token()
 {
69be5e4e
     //given
 
     //when
b017a4d2
     int valid = validate_token ("msmith", "654321");
cb403154
 
     //then
b017a4d2
     check (!valid, "expected result to be invalid");
cb403154
     succeed();
 }
6f0de833
 
cdf7fd74
 int runtests()
 {
b017a4d2
     test (validate_compares_to_user_token);
     test (validates_from_the_right_user);
     test (validates_user_specific_token);
de34378f
     succeed();
 }
 
b017a4d2
 int main (int argc, char **argv)
cdf7fd74
 {
69be5e4e
     int rval = !runtests();
     return rval;
de34378f
 }