1%%%------------------------------------------------------------------- 2%%% File : ts_test_pgsql.erl 3%%% Author : Nicolas Niclausse <nicolas@niclux.org> 4%%% Description : 5%%% 6%%% Created : 10 Apr 2008 by Nicolas Niclausse <nicolas@niclux.org> 7%%%------------------------------------------------------------------- 8-module(ts_test_pgsql). 9 10-compile(export_all). 11 12-include("ts_profile.hrl"). 13-include("ts_config.hrl"). 14-include("ts_pgsql.hrl"). 15-include("ts_recorder.hrl"). 16 17-include_lib("eunit/include/eunit.hrl"). 18 19-define(PARSEBIN,<< 115,99,117,49,0,100,101,99,108,97, 20 114,101,32,115,99,117,49,32,99,117,114,115,111, 21 114,32,119,105,116,104,32,104,111,108,100,32,102, 22 111,114,32,115,101,108,101,99,116,32,98,114,110, 23 95,99,100,44,32,112,114,101,118,95,112,114,95, 24 100,116,44,32,99,117,114,114,95,112,114,95,100, 25 116,44,32,110,101,120,116,95,112,114,95,100,116, 26 44,32,98,114,110,95,110,109,44,32,98,114,110,95, 27 97,100,100,114,49,44,32,98,114,110,95,97,100,100, 28 114,50,44,32,98,114,110,95,97,100,100,114,51,44, 29 32,99,111,109,112,95,110,109,44,32,99,97,115,104, 30 95,97,99,44,32,105,98,116,95,103,114,112,95,99, 31 100,44,32,98,97,110,107,95,99,100,44,32,108,111, 32 103,95,112,97,116,104,44,32,99,111,95,98,114,110, 33 95,99,100,32,102,114,111,109,32,32,32,98,114,110, 34 32,32,119,104,101,114,101,32,98,114,110,46,98, 35 114,110,95,99,100,32,61,32,36,49,0,0,1,0,0,4,18 >>). 36 37test()-> 38 ok. 39 40 41utils_md5_test()-> 42 myset_env(), 43 Password="sesame", 44 User="benchmd5", 45 Salt= << 54,195,212,197 >>, 46 Hash= list_to_binary(["md5967c89f451d1d504a1f02fc69fb65cb5",0]), 47 PacketSize= 4+size(Hash), 48 Bin= <<$p,PacketSize:32/integer, Hash/binary>>, 49 ?assertMatch(Bin, pgsql_proto:encode_message(pass_md5, {User,Password,Salt} ) ). 50 51extended_test()-> 52 Data= << 80,0,0,0,75,115,99,117,49,0,100,101,99,108,97,114,101,32,115,99,117,49,32,99,117,114, 53 115,111,114,32,119,105,116,104,32,104,111,108,100,32,102,111,114,32,115,101,108,101, 54 99,116,32,67,79,85,78,84,40,42,41,32,102,114,111,109,32,32,32,98,114,46,97,104,32,0,0, 55 0,83,0,0,0,4 >>, 56 Result=ts_proxy_pgsql:process_data(#proxy{},Data), 57 ?assertMatch(#proxy{}, Result). 58 59extended2_test()-> 60 Data = <<66,0,0,0,28, 0, 115,99,117,49,0, 0,1, 0,0, 0,1, 0,0,0,4, 78,68,83,66, 61 0,1,0,0, 62 68,0,0,0,6,80,0,69,0,0,0,9,0,0,0,0,0,83,0,0,0,4>>, 63 Result=ts_proxy_pgsql:process_data(#proxy{},Data), 64 ?assertMatch(#proxy{}, Result). 65 66extended3_test()-> 67 Data = <<0, 99,117,51,0, 0,10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 68 0,10, 69 0,0,0,26, 50,48,48,56,45,49,50,45,48,53,32,48,57,58,49,57,58,48,48,46,48,48,48,48,48,48, 70 0,0,0,26, 50,48,49,49,45,48,56,45,50,51,32,48,56,58,52,55,58,48,48,46,48,48,48,48,48,48, 71 0,0,0,10, 49,51,52,52,51,55,32,32,32,32, 72 0,0,0,1, 69, 73 0,0,0,5, 75,78,32,32,32, 74 0,0,0,35, 75,79,78,84,69,78,65,32,78,65,83,73,79,78,65,76,32,66,72,68,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 75 0,0,0,1, 89, 76 0,0,0,1, 89, 77 255,255,255,255, 78 0,0,0,5, 75,78,32,32,32, 79 0,1, 0,0 >>, 80 Result=ts_proxy_pgsql:decode_packet($B,Data), 81 Portal = <<>>, 82 Statement = <<"cu3">>, 83 Params= [<<"2008-12-05 09:19:00.000000">>, 84 <<"2011-08-23 08:47:00.000000">>, 85 <<"134437 ">>, 86 <<"E">>, 87 <<"KN ">>, 88 <<"KONTENA NASIONAL BHD ">>, 89 <<"Y">>, 90 <<"Y">>, 91 'null', 92 <<"KN ">>], 93 Bind = {bind, {Portal, Statement, Params , auto, [text]}}, 94 ?assertEqual(Bind, Result). 95 96extended_parse_test()-> 97 Prep = <<"scu1">>, 98 Query = <<"declare scu1 cursor with hold for select brn_cd, prev_pr_dt, curr_pr_dt, next_pr_dt, brn_nm, brn_addr1, brn_addr2, brn_addr3, comp_nm, cash_ac, ibt_grp_cd, bank_cd, log_path, co_brn_cd from brn where brn.brn_cd = $1">>, 99 Result = ts_proxy_pgsql:decode_packet($P,?PARSEBIN), 100 ?assertMatch({parse,{Prep, Query,[1042]}}, Result). 101 %% {ok,Dev}=file:open("/tmp/toto.erl.log",[write]), 102 %% State=#state_rec{logfd=Dev}, 103 %% Rec = #pgsql_request{type=parse, parameters=[1042], name_prepared=Prep, equery=Query}, 104 %% ?assertMatch({ok,State}, ts_proxy_pgsql:record_request(State,Rec)). 105 106encode_parse_test()-> 107 Prep = <<"scu1">>, 108 Query = <<"declare scu1 cursor with hold for select brn_cd, prev_pr_dt, curr_pr_dt, next_pr_dt, brn_nm, brn_addr1, brn_addr2, brn_addr3, comp_nm, cash_ac, ibt_grp_cd, bank_cd, log_path, co_brn_cd from brn where brn.brn_cd = $1">>, 109 Bin=?PARSEBIN, 110 Res= << 80,0,0,0,234,Bin/binary>>, 111 Rep=pgsql_proto:encode_message(parse,{Prep,Query,[1042]}), 112 ?assertEqual(Res,Rep). 113 114encode_parse2_test()-> 115 Rep=pgsql_proto:encode_message(parse,{<< >>,<< >>,[]}), 116 ?assertEqual( << 80,0,0,0,8,0,0,0,0 >> ,Rep). 117 118 119subst_parameters_test()-> 120 myset_env(), 121 Proto=#pgsql_session{}, 122 DynVars=ts_dynvars:new(param,"42"), 123 Params=["%%_param%%","1"], 124 Req=ts_pgsql:add_dynparams(true,{DynVars,Proto}, 125 #pgsql_request{type=bind,name_portal="",name_prepared="P0_10", formats=none,formats_results=[text],parameters=Params}, 126 {"pgsql.org",5432,gen_tcp}), 127 Str=[66,0,0,0,30,0,80,48,95,49,48,0,0,0,0,2,0,0,0,2,52,50,0,0,0,1,49,0,1,0,0], 128 {Res,_}=ts_pgsql:get_message(Req,#state_rcv{}), 129 ?assertEqual(Str, binary_to_list(Res)). 130 131subst_parameters2_test()-> 132 myset_env(), 133 Proto=#pgsql_session{}, 134 Params=[42,1], 135 Req=ts_pgsql:add_dynparams(true,{[], Proto}, 136 #pgsql_request{type=bind,name_portal="",name_prepared="P0_10", formats=none,formats_results=[text],parameters=Params}, 137 {"pgsql.org",5432,gen_tcp}), 138 Str=[66,0,0,0,30,0,80,48,95,49,48,0,0,0,0,2,0,0,0,2,52,50,0,0,0,1,49,0,1,0,0], 139 {Res,_}=ts_pgsql:get_message(Req,#state_rcv{}), 140 ?assertEqual(Str, binary_to_list(Res)). 141 142 143 144myset_env()-> 145 myset_env(0). 146myset_env(Val)-> 147 application:set_env(stdlib,debug_level,Val). 148 149