END_TEST ////////////////////////////////////////////////////////////////////// ///////////// bank unit tests ////////////////////////////////////////////////////////////////////// START_TEST(test_bank_connect) { int atmfd[2]; pipe(atmfd); int atm_read = atmfd[0]; int atm_write[1] = { atmfd[1] }; // The command buffer. Command cmd; if (fork() == 0) { bank_open(1, 1); MSG_CONNECT(&cmd, 0); int atm_cnt = 1; int status = bank(atm_write, &cmd, &atm_cnt); ck_assert_msg(status == SUCCESS, "status should be SUCCESS, received %d", status); bank_close(); } else { // Need to set the read end of pipe to non-blocking otherwise // students without an implementation will not be able to run // tests. int flags = fcntl(atm_read, F_GETFL, 0); fcntl(atm_read, F_SETFL, flags | O_NONBLOCK); sleep(1); int status = read(atm_read, &cmd, MESSAGE_SIZE); ck_assert_msg(status != -1, "BANK should have received data from the ATM"); cmd_t c; int i, f, t, a; cmd_unpack(&cmd, &c, &i, &f, &t, &a); ck_assert_msg(c == OK, "received message should be ok, got %d", c); ck_assert_int_eq(i, 0); ck_assert_int_eq(f, -1); ck_assert_int_eq(t, -1); ck_assert_int_eq(a, -1); } }
END_TEST START_TEST(test_bank_balance_bad_account) { int atmfd[2]; pipe(atmfd); int atm_write[1] = { atmfd[1] }; // The command buffer. Command cmd; if (fork() == 0) { bank_open(1, 1); int* accounts = get_accounts(); accounts[0] = 200; MSG_BALANCE(&cmd, 0, 1); int atm_cnt = 1; int status = bank(atm_write, &cmd, &atm_cnt); ck_assert_msg(status == SUCCESS, "status should be ERR_UNKNOWN_ACCOUNT, received %d", status); ck_assert_msg(accounts[0] == 200, "account should have 200, found %d", accounts[0]); bank_close(); } else { // Need to set the read end of pipe to non-blocking otherwise // students without an implementation will not be able to run // tests. int flags = fcntl(atmfd[0], F_GETFL, 0); fcntl(atmfd[0], F_SETFL, flags | O_NONBLOCK); sleep(1); int status = read(atmfd[0], &cmd, MESSAGE_SIZE); ck_assert_msg(status != -1, "ATM should have received data from the BANK"); cmd_t c; int i, f, t, a; cmd_unpack(&cmd, &c, &i, &f, &t, &a); ck_assert_msg(c == ACCUNKN, "bank should have sent ACCUNKN, received %d", c); } }
END_TEST START_TEST(test_bank_balance_bad_atm) { int atmfd[2]; pipe(atmfd); int atm_write[1] = { atmfd[1] }; // The command buffer. Command cmd; bank_open(1, 1); int* accounts = get_accounts(); accounts[0] = 200; MSG_BALANCE(&cmd, 1, 0); int atm_cnt = 1; int status = bank(atm_write, &cmd, &atm_cnt); ck_assert_msg(status == ERR_UNKNOWN_ATM, "status should be ERR_UNKNOWN_ATM, received %d", status); ck_assert_msg(accounts[0] == 200, "account should have 200, found %d", accounts[0]); bank_close(); }
int main (int argc, char *argv[]) { if (argc != 2) { printf("usage: %s trace_file\n", argv[0]); exit(1); } int result = 0; int atm_count = 0; int account_count = 0; // open the trace file result = trace_open(argv[1]); if (result == -1) { printf("%s: could not open file %s\n", argv[0], argv[1]); exit (1); } // Get the number of ATMs and accounts: atm_count = trace_atm_count(); account_count = trace_account_count(); trace_close(); // Setup bank pipes: int bankfd[2]; pipe(bankfd); // This is a table of atm_out file descriptors. It will be used by // the bank process to communicate to each of the ATM processes. int atm_out_fd[atm_count]; // TODO: ATM PROCESS FORKING // START YOUR IMPLEMENTATION for(int i =0; i < atm_count; i++){ int atmfd[2]; pipe(atmfd); atm_out_fd[i] =atmfd[1]; if(fork()== 0){ close(atmfd[1]); close(bankfd[0]); int result = atm_run(argv[1],bankfd[1],atmfd[0],i); if(result != SUCCESS){ error_print(); } else{ close(atmfd[0]); } exit(0); } } // END YOUR IMPLEMENTATION // TODO: BANK PROCESS FORKING // START YOUR IMPLEMENTATION //pid_t pid2 = fork(); if(fork() == 0){ close(bankfd[1]); bank_open(atm_count,account_count); int result =run_bank(bankfd[0],atm_out_fd); if(result != SUCCESS){ error_print(); } bank_dump(); bank_close(); exit(0); } // END YOUR IMPLEMENTATION // Wait for each of the child processes to complete. We include // atm_count to include the bank process (i.e., this is not a // fence post error!) for (int i = 0; i <= atm_count; i++) { wait(NULL); } return 0; }