#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include <sys/poll.h>

#include <assert.h>

#include "swissdef.h"
#include "swissvault.h"
#include "swisscrypt.h"


#define SWISSSERVADRSTR "127.0.0.1"


int testnetwork(int argc, char** argv){
      int sock = 0;
      struct sockaddr_in swiss_addr;
      struct pollfd* conlst;
      struct hostent* swissservent;
      
      swissservent = gethostbyname("localhost");
      
     
      sock = SWISSSOCK;
  
      swiss_addr.sin_family=AF_INET;
      swiss_addr.sin_port=htons(SWISSPORT);
      inet_aton(SWISSSERVADRSTR, &(swiss_addr.sin_addr));
      bzero(&(swiss_addr.sin_zero),8);

      assert(printf("connecting...\n"));
      connect(sock, (struct sockaddr *)&swiss_addr, sizeof(struct sockaddr_in));

      sleep(10);
      printf("bye\n");
      
      return 0; 
}


int testvault(){
      //storing stuff
      //retrieving stuff
      char *data = "Some Data to store";
      char auth[SWISS_PWDLEN];
      char *seed = "qwdfrgthyjukiloijuht";
      struct account acc;
      int ret;

      acc.acc_id = 0;
      acc.data = data;
      acc.dsize = strlen(data)+1;
      acc.auth = swiss_pwd_hash_iterate(auth, 10,seed);
      acc.asize = SWISS_PWDLEN;
      acc.ttl = 0;
      
      printf("swisstest: storing...");
      ret = store(&acc);
      if(ret > 0)
	    printf(" success.\n");
      else
	    printf(" FAILURE!\n");
      
      printf("swisstest: retrieving...\n");
      
      acc.data = (char*)0;
      acc.dsize = 0;
      acc.auth = swiss_pwd_hash_iterate(auth, 9,seed);
      ret = retrieve(&acc);
      if(ret > 0){
	    printf("retrieved data(%d): %s\n", acc.dsize, acc.data);
	    printf("retrieved auth(%d): %s\n", acc.asize, acc.auth);
      } else
	    printf("error %d retrieving data\n", ret);

      // storing something new
      acc.data = "Some other Data";
      acc.dsize = strlen(acc.data)+1;
      acc.auth = swiss_pwd_hash_iterate(auth, 8,seed);
      
      
      printf("swisstest: 2nd storing...");
      ret =store(&acc);
      if(ret > 0)
	    printf(" success.\n");
      else
	    printf(" FAILURE!\n");

      //
      acc.data = (char*)0;
      acc.dsize = 0;
      acc.auth = swiss_pwd_hash_iterate(auth, 7,seed);
      ret = retrieve(&acc);
      if(ret > 0){
	    printf("\nretrieved data(%d): %s\n", acc.dsize, acc.data);
	    printf("\nretrieved auth(%d): %s\n", acc.asize, acc.auth);
      } else
	    printf("error %d retrieving data\n", ret);

      close_vault();

      printf("reopening retrieving\n");
      
      free(acc.data);
      acc.data = (char*)0;
      acc.dsize = 0;
      acc.auth = swiss_pwd_hash_iterate(auth, 6,seed);
      ret = retrieve(&acc);
      if(ret > 0){
	    printf("\nretrieved data(%d): %s\n", acc.dsize, acc.data);
	    printf("\nretrieved auth(%d): %s\n", acc.asize, acc.auth);
      } else
	    printf("error %d retrieving data\n", ret);

      close_vault();

      return 0;
      

}

int main(int arg, char*** argv){
      testvault();
}

