/* test that the correct line configuration is created for a valid config file */ void test_load_correct_line_configuration_for_valid_config() { const char *filename; unsigned long address; fh_shr_cfg_lh_proc_t lh_config; fh_cfg_node_t *config; memset(&lh_config, 0, sizeof(fh_shr_cfg_lh_proc_t)); filename = valid_config(); config = fh_cfg_load(filename); delete_config(filename); /* load configuration and make some basic assertions */ FH_TEST_ASSERT_NOTNULL(config); FH_TEST_ASSERT_EQUAL(fh_shr_cfg_lh_load("foo", "itch", config, &lh_config), FH_OK); FH_TEST_ASSERT_EQUAL(lh_config.num_lines, 1); /* assert that the proper lines are enabled/disabled */ FH_TEST_ASSERT_TRUE(lh_config.lines[0].primary.enabled); FH_TEST_ASSERT_FALSE(lh_config.lines[0].secondary.enabled); /* assert that the proper options are set for the line(s) that is/are enabled */ address = inet_addr(fh_cfg_get_string(config, "itch.lines.foo.primary.address")); FH_TEST_ASSERT_LEQUAL((long int)lh_config.lines[0].primary.address, (long int)address); FH_TEST_ASSERT_LEQUAL((long int)lh_config.lines[0].primary.port, (long int)12345); FH_TEST_ASSERT_STREQUAL(lh_config.lines[0].primary.interface, "eth0"); }
/* test to make sure that the proper error is returned when an invalid table name is given */ void test_invalid_table_name_produces_error() { fh_shr_cfg_tbl_t tbl_config; fh_cfg_node_t *config = valid_config(); memset(&tbl_config, 0, sizeof(fh_shr_cfg_tbl_t)); FH_TEST_ASSERT_NOTNULL(config); FH_TEST_ASSERT_EQUAL(fh_shr_cfg_tbl_load(config, "foo", &tbl_config), FH_ERR_NOTFOUND); }
/* make sure that a valid 'enabled' property is reflected in resulting configuration */ void test_size_property_sets_table_config_properly() { fh_shr_cfg_tbl_t tbl_config; fh_cfg_node_t *config = valid_config(); memset(&tbl_config, 0, sizeof(fh_shr_cfg_tbl_t)); FH_TEST_ASSERT_NOTNULL(config); FH_TEST_ASSERT_EQUAL(fh_shr_cfg_tbl_load(config, "foo_table", &tbl_config), FH_OK); FH_TEST_ASSERT_TRUE(tbl_config.enabled); FH_TEST_ASSERT_EQUAL(tbl_config.size, 100); }
/* test that ...load() returns FH_ERROR when given a process name that doesn't exist */ void test_load_failure_with_nonexistent_name() { const char *filename; fh_shr_cfg_lh_proc_t lh_config; fh_cfg_node_t *config; filename = valid_config(); config = fh_cfg_load(filename); delete_config(filename); FH_TEST_ASSERT_NOTNULL(config); FH_TEST_ASSERT_EQUAL(fh_shr_cfg_lh_load("bar", "itch", config, &lh_config), FH_ERROR); }
/* test that ...get_process() returns FH_OK when given a process name that exists */ void test_get_proc_success_with_existent_name() { const char *filename; fh_cfg_node_t *config; char process[MAX_PROPERTY_LENGTH] = "foo"; filename = valid_config(); config = fh_cfg_load(filename); delete_config(filename); FH_TEST_ASSERT_NOTNULL(config); FH_TEST_ASSERT_EQUAL(fh_shr_cfg_lh_get_proc(process, "itch", config), FH_OK); }
/* process name */ void test_load_success_with_existent_name() { const char *filename; fh_shr_cfg_lh_proc_t lh_config; fh_cfg_node_t *config; memset(&lh_config, 0, sizeof(fh_shr_cfg_lh_proc_t)); filename = valid_config(); config = fh_cfg_load(filename); delete_config(filename); FH_TEST_ASSERT_NOTNULL(config); FH_TEST_ASSERT_EQUAL(fh_shr_cfg_lh_load("foo", "itch", config, &lh_config), FH_OK); FH_TEST_ASSERT_STREQUAL(lh_config.name, "foo"); }
fh_shr_cfg_tbl_t *valid_config() { static fh_shr_cfg_tbl_t config = { .name = "dummy_config", .enabled = 1, .size = 100 }; return &config; } fh_shr_lkp_tbl_t *valid_table() { static fh_shr_lkp_tbl_t table; fh_shr_lkp_ord_init(valid_config(), &table); return &table; } fh_shr_lkp_ord_t *valid_entry() { static fh_shr_lkp_ord_t entry = { .order_no = 1, .price = 1005000, .shares = 100, .buy_sell_ind = 'B', .stock = "AAPL", .sym_entry = NULL, .context = NULL }; return &entry; } fh_shr_lkp_ord_key_t *valid_key() { static fh_shr_lkp_ord_key_t key = { .order_no = 1 }; return &key; } void test_count_responds_to_entry_insertion() { fh_shr_lkp_tbl_t *table = valid_table(); fh_shr_lkp_ord_t *tblentry; FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, valid_entry(), &tblentry), (int)FH_OK); FH_TEST_ASSERT_EQUAL(table->count, 1); } void test_get_after_insertion_yields_correct_values() { fh_shr_lkp_ord_t *tblentry; fh_shr_lkp_tbl_t *table = valid_table(); fh_shr_lkp_ord_t *entry = valid_entry(); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, entry, &tblentry), (int)FH_OK); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_get(table, valid_key(), &tblentry), (int)FH_OK); FH_TEST_ASSERT_LEQUAL(tblentry->order_no, entry->order_no); FH_TEST_ASSERT_LEQUAL(tblentry->price, entry->price); FH_TEST_ASSERT_EQUAL(tblentry->shares, entry->shares); FH_TEST_ASSERT_EQUAL(tblentry->buy_sell_ind, entry->buy_sell_ind); FH_TEST_ASSERT_STREQUAL(tblentry->stock, entry->stock); FH_TEST_ASSERT_EQUAL(table->count, 1); } void test_retrieving_entry_with_freed_key_works() { fh_shr_lkp_ord_key_t *key; fh_shr_lkp_ord_t *entry; fh_shr_lkp_tbl_t *table = valid_table(); key = (fh_shr_lkp_ord_key_t *)malloc(sizeof(fh_shr_lkp_ord_key_t)); memcpy(key, valid_key(), sizeof(fh_shr_lkp_ord_key_t)); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_add(table, valid_entry(), &entry), (int)FH_OK); free(key); FH_TEST_ASSERT_EQUAL((int)fh_shr_lkp_ord_get(table, valid_key(), &entry), (int)FH_OK); FH_TEST_ASSERT_EQUAL(table->count, 1); }
NOEXPORT LRESULT CALLBACK window_proc(HWND main_window_handle, UINT message, WPARAM wParam, LPARAM lParam) { POINT pt; RECT rect; PAINTSTRUCT ps; SERVICE_OPTIONS *section; unsigned section_number; LPTSTR txt; #if 0 switch(message) { case WM_CTLCOLORSTATIC: case WM_TIMER: case WM_LOG: break; default: s_log(LOG_DEBUG, "Window message: 0x%x(0x%hx,0x%lx)", message, wParam, lParam); } #endif switch(message) { case WM_CREATE: #ifdef _WIN32_WCE /* create command bar */ command_bar_handle=CommandBar_Create(ghInst, main_window_handle, 1); if(!command_bar_handle) error_box(TEXT("CommandBar_Create")); if(!CommandBar_InsertMenubar(command_bar_handle, ghInst, IDM_MAINMENU, 0)) error_box(TEXT("CommandBar_InsertMenubar")); if(!CommandBar_AddAdornments(command_bar_handle, 0, 0)) error_box(TEXT("CommandBar_AddAdornments")); #endif /* create child edit window */ edit_handle=CreateWindowEx(WS_EX_STATICEDGE, TEXT("EDIT"), NULL, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE|ES_READONLY, 0, 0, 0, 0, main_window_handle, (HMENU)IDE_EDIT, ghInst, NULL); #ifndef _WIN32_WCE SendMessage(edit_handle, WM_SETFONT, (WPARAM)CreateFont(-12, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_RASTER_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH, TEXT("Courier")), MAKELPARAM(FALSE, 0)); /* no need to redraw right now */ #endif /* NOTE: there's no return statement here -> proceeding with resize */ case WM_SIZE: GetClientRect(main_window_handle, &rect); #ifdef _WIN32_WCE MoveWindow(edit_handle, 0, CommandBar_Height(command_bar_handle), rect.right, rect.bottom-CommandBar_Height(command_bar_handle), TRUE); SendMessage(command_bar_handle, TB_AUTOSIZE, 0L, 0L); CommandBar_AlignAdornments(command_bar_handle); #else MoveWindow(edit_handle, 0, 0, rect.right, rect.bottom, TRUE); #endif UpdateWindow(edit_handle); /* CommandBar_Show(command_bar_handle, TRUE); */ return 0; case WM_SETFOCUS: SetFocus(edit_handle); return 0; case WM_PAINT: BeginPaint(hwnd, &ps); EndPaint(hwnd, &ps); break; case WM_CLOSE: ShowWindow(main_window_handle, SW_HIDE); return 0; #ifdef WM_SHOWWINDOW case WM_SHOWWINDOW: visible=(BOOL)wParam; #else /* this works for Pierre Delaage, but not for me... */ case WM_WINDOWPOSCHANGED: visible=IsWindowVisible(main_window_handle); #endif if(tray_menu_handle) CheckMenuItem(tray_menu_handle, IDM_SHOW_LOG, visible ? MF_CHECKED : MF_UNCHECKED); if(visible) update_logs(); #ifdef WM_SHOWWINDOW return 0; #else break; /* proceed to DefWindowProc() */ #endif case WM_DESTROY: #ifdef _WIN32_WCE CommandBar_Destroy(command_bar_handle); #else if(main_menu_handle) { if(!DestroyMenu(main_menu_handle)) ioerror("DestroyMenu"); main_menu_handle=NULL; } #endif tray_delete(); /* remove the taskbark icon if exists */ PostQuitMessage(0); return 0; case WM_COMMAND: if(wParam>=IDM_PEER_MENU && wParam<IDM_PEER_MENU+number_of_sections) { for(section=service_options.next, section_number=0; section && wParam!=IDM_PEER_MENU+section_number; section=section->next, ++section_number) ; if(!section) return 0; if(save_text_file(section->file, section->chain)) return 0; #ifndef _WIN32_WCE if(main_menu_handle) CheckMenuItem(main_menu_handle, (UINT)wParam, MF_CHECKED); #endif if(tray_menu_handle) CheckMenuItem(tray_menu_handle, (UINT)wParam, MF_CHECKED); message_box(section->help, MB_ICONINFORMATION); return 0; } switch(wParam) { case IDM_ABOUT: DialogBox(ghInst, TEXT("AboutBox"), main_window_handle, (DLGPROC)about_proc); break; case IDM_SHOW_LOG: if(visible) { ShowWindow(main_window_handle, SW_HIDE); /* hide window */ } else { ShowWindow(main_window_handle, SW_SHOWNORMAL); /* show window */ SetForegroundWindow(main_window_handle); /* bring on top */ } break; case IDM_CLOSE: ShowWindow(main_window_handle, SW_HIDE); /* hide window */ break; case IDM_EXIT: if(num_clients>=0) /* signal_pipe is active */ signal_post(SIGNAL_TERMINATE); DestroyWindow(main_window_handle); break; case IDM_SAVE_LOG: if(!cmdline.service) /* security */ save_log(); break; case IDM_EDIT_CONFIG: #ifndef _WIN32_WCE if(!cmdline.service) /* security */ edit_config(main_window_handle); #endif break; case IDM_RELOAD_CONFIG: if(num_clients>=0) /* signal_pipe is active */ signal_post(SIGNAL_RELOAD_CONFIG); else SetEvent(config_ready); /* unlock daemon_thread() */ break; case IDM_REOPEN_LOG: signal_post(SIGNAL_REOPEN_LOG); break; case IDM_MANPAGE: #ifndef _WIN32_WCE if(!cmdline.service) /* security */ ShellExecute(main_window_handle, TEXT("open"), TEXT("..\\doc\\stunnel.html"), NULL, NULL, SW_SHOWNORMAL); #endif break; case IDM_HOMEPAGE: #ifndef _WIN32_WCE if(!cmdline.service) /* security */ ShellExecute(main_window_handle, TEXT("open"), TEXT("http://www.stunnel.org/"), NULL, NULL, SW_SHOWNORMAL); #endif break; } return 0; case WM_SYSTRAY: /* a taskbar event */ switch(lParam) { #ifdef _WIN32_WCE case WM_LBUTTONDOWN: /* no right mouse button on Windows CE */ GetWindowRect(GetDesktopWindow(), &rect); /* no cursor position */ pt.x=rect.right; pt.y=rect.bottom-25; #else case WM_RBUTTONDOWN: GetCursorPos(&pt); #endif SetForegroundWindow(main_window_handle); if(tray_menu_handle) TrackPopupMenuEx(GetSubMenu(tray_menu_handle, 0), TPM_BOTTOMALIGN, pt.x, pt.y, main_window_handle, NULL); PostMessage(main_window_handle, WM_NULL, 0, 0); break; #ifndef _WIN32_WCE case WM_LBUTTONDBLCLK: /* switch log window visibility */ if(visible) { ShowWindow(main_window_handle, SW_HIDE); /* hide window */ } else { ShowWindow(main_window_handle, SW_SHOWNORMAL); /* show window */ SetForegroundWindow(main_window_handle); /* bring on top */ } break; #endif } return 0; case WM_VALID_CONFIG: valid_config(); return 0; case WM_INVALID_CONFIG: invalid_config(); return 0; case WM_LOG: txt=(LPTSTR)wParam; win_log(txt); str_free(txt); return 0; case WM_NEW_CHAIN: #ifndef _WIN32_WCE if(main_menu_handle) EnableMenuItem(main_menu_handle, (UINT)(IDM_PEER_MENU+wParam), MF_ENABLED); #endif if(tray_menu_handle) EnableMenuItem(tray_menu_handle, (UINT)(IDM_PEER_MENU+wParam), MF_ENABLED); return 0; case WM_CLIENTS: tray_update((int)wParam); return 0; } return DefWindowProc(main_window_handle, message, wParam, lParam); }
NOEXPORT LRESULT CALLBACK window_proc(HWND main_window_handle, UINT message, WPARAM wParam, LPARAM lParam) { NOTIFYICONDATA nid; POINT pt; RECT rect; SERVICE_OPTIONS *section; unsigned int section_number; #if 0 if(message!=WM_CTLCOLORSTATIC && message!=WM_TIMER) s_log(LOG_DEBUG, "Window message: %d", message); #endif switch(message) { case WM_CREATE: #ifdef _WIN32_WCE /* create command bar */ command_bar_handle=CommandBar_Create(ghInst, main_window_handle, 1); if(!command_bar_handle) error_box("CommandBar_Create"); if(!CommandBar_InsertMenubar(command_bar_handle, ghInst, IDM_MAINMENU, 0)) error_box("CommandBar_InsertMenubar"); if(!CommandBar_AddAdornments(command_bar_handle, 0, 0)) error_box("CommandBar_AddAdornments"); #endif /* create child edit window */ edit_handle=CreateWindow(TEXT("EDIT"), NULL, WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE|ES_READONLY, 0, 0, 0, 0, main_window_handle, (HMENU)IDE_EDIT, ghInst, NULL); #ifndef _WIN32_WCE SendMessage(edit_handle, WM_SETFONT, (WPARAM)CreateFont(-12, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_RASTER_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH, TEXT("Courier")), MAKELPARAM(FALSE, 0)); /* no need to redraw right, now */ #endif /* NOTE: there's no return statement here -> proceeding with resize */ case WM_SIZE: GetClientRect(main_window_handle, &rect); #ifdef _WIN32_WCE MoveWindow(edit_handle, 0, CommandBar_Height(command_bar_handle), rect.right, rect.bottom-CommandBar_Height(command_bar_handle), TRUE); #else MoveWindow(edit_handle, 0, 0, rect.right, rect.bottom, TRUE); #endif UpdateWindow(edit_handle); /* CommandBar_Show(command_bar_handle, TRUE); */ return TRUE; case WM_SETFOCUS: SetFocus(edit_handle); return TRUE; case WM_TIMER: if(visible) update_logs(); return TRUE; case WM_CLOSE: ShowWindow(main_window_handle, SW_HIDE); return TRUE; case WM_SHOWWINDOW: visible=wParam; /* setup global variable */ if(tray_menu_handle) CheckMenuItem(tray_menu_handle, IDM_SHOW_LOG, visible ? MF_CHECKED : MF_UNCHECKED); if(visible) update_logs(); return TRUE; case WM_DESTROY: #ifdef _WIN32_WCE CommandBar_Destroy(command_bar_handle); #else if(main_menu_handle) DestroyMenu(main_menu_handle); #endif if(tray_menu_handle) DestroyMenu(tray_menu_handle); ZeroMemory(&nid, sizeof nid); nid.cbSize=sizeof nid; nid.hWnd=main_window_handle; nid.uID=1; nid.uFlags=NIF_TIP; /* not really sure what to put here, but it works */ Shell_NotifyIcon(NIM_DELETE, &nid); /* this removes the icon */ PostQuitMessage(0); KillTimer(main_window_handle, 0x29a); return TRUE; case WM_COMMAND: if(wParam>=IDM_PEER_MENU && wParam<IDM_PEER_MENU+number_of_sections) { for(section=service_options.next, section_number=0; section && wParam!=IDM_PEER_MENU+section_number; section=section->next, ++section_number) ; if(!section) return TRUE; if(save_text_file(section->file, section->chain)) return TRUE; #ifndef _WIN32_WCE if(main_menu_handle) CheckMenuItem(main_menu_handle, wParam, MF_CHECKED); #endif if(tray_menu_handle) CheckMenuItem(tray_menu_handle, wParam, MF_CHECKED); message_box(section->help, MB_ICONINFORMATION); return TRUE; } switch(wParam) { case IDM_ABOUT: DialogBox(ghInst, TEXT("AboutBox"), main_window_handle, (DLGPROC)about_proc); break; case IDM_SHOW_LOG: if(visible) { ShowWindow(main_window_handle, SW_HIDE); /* hide window */ } else { ShowWindow(main_window_handle, SW_SHOWNORMAL); /* show window */ SetForegroundWindow(main_window_handle); /* bring on top */ } break; case IDM_CLOSE: ShowWindow(main_window_handle, SW_HIDE); /* hide window */ break; case IDM_EXIT: if(!error_mode) /* signal_pipe is active */ signal_post(SIGNAL_TERMINATE); DestroyWindow(main_window_handle); break; case IDM_SAVE_LOG: if(!cmdline.service) /* security */ save_log(); break; case IDM_EDIT_CONFIG: #ifndef _WIN32_WCE if(!cmdline.service) /* security */ edit_config(main_window_handle); #endif break; case IDM_RELOAD_CONFIG: if(error_mode) /* unlock daemon_thread */ SetEvent(config_ready); else /* signal_pipe is active */ signal_post(SIGNAL_RELOAD_CONFIG); break; case IDM_REOPEN_LOG: signal_post(SIGNAL_REOPEN_LOG); break; case IDM_MANPAGE: #ifndef _WIN32_WCE if(!cmdline.service) /* security */ ShellExecute(main_window_handle, TEXT("open"), TEXT("stunnel.html"), NULL, NULL, SW_SHOWNORMAL); #endif break; case IDM_HOMEPAGE: #ifndef _WIN32_WCE if(!cmdline.service) /* security */ ShellExecute(main_window_handle, TEXT("open"), TEXT("http://www.stunnel.org/"), NULL, NULL, SW_SHOWNORMAL); #endif break; } return TRUE; case WM_SYSTRAY: /* a taskbar event */ switch(lParam) { #ifdef _WIN32_WCE case WM_LBUTTONDOWN: /* no right mouse button on Windows CE */ GetWindowRect(GetDesktopWindow(), &rect); /* no cursor position */ pt.x=rect.right; pt.y=rect.bottom-25; #else case WM_RBUTTONDOWN: GetCursorPos(&pt); #endif SetForegroundWindow(main_window_handle); TrackPopupMenuEx(GetSubMenu(tray_menu_handle, 0), TPM_BOTTOMALIGN, pt.x, pt.y, main_window_handle, NULL); PostMessage(main_window_handle, WM_NULL, 0, 0); break; #ifndef _WIN32_WCE case WM_LBUTTONDBLCLK: /* switch log window visibility */ if(visible) { ShowWindow(main_window_handle, SW_HIDE); /* hide window */ } else { ShowWindow(main_window_handle, SW_SHOWNORMAL); /* show window */ SetForegroundWindow(main_window_handle); /* bring on top */ } break; #endif } return TRUE; case WM_VALID_CONFIG: valid_config(); return TRUE; case WM_INVALID_CONFIG: invalid_config(); return TRUE; case WM_LOG: win_log((LPSTR)wParam); return TRUE; case WM_NEW_CHAIN: #ifndef _WIN32_WCE if(main_menu_handle) EnableMenuItem(main_menu_handle, IDM_PEER_MENU+wParam, MF_ENABLED); #endif if(tray_menu_handle) EnableMenuItem(tray_menu_handle, IDM_PEER_MENU+wParam, MF_ENABLED); return TRUE; case WM_CLIENTS: update_tray_icon((int)wParam); return TRUE; } return DefWindowProc(main_window_handle, message, wParam, lParam); }