Skip to content

Commit

Permalink
Merge pull request #7449 from u3s/kuba/ct/ct_telnet_unicode_fix/OTP-1…
Browse files Browse the repository at this point in the history
…8664

Kuba/ct/ct telnet unicode fix/otp 18664
  • Loading branch information
u3s authored Jul 14, 2023
2 parents afb7713 + 1242070 commit f211cce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
59 changes: 29 additions & 30 deletions lib/common_test/src/ct_telnet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1224,37 +1224,36 @@ split_lines([Char|Rest],Line,Lines) ->
split_lines([],Line,Lines) ->
{Lines,lists:reverse(Line)}.


match_prompt(Str,Prx) ->
match_prompt(Str,Prx,[]).
match_prompt(Str,Prx,Acc) ->
match_prompt(Str, Prx) ->
match_prompt(unicode:characters_to_binary(Str), Prx, []).
match_prompt(Str, Prx, Acc) ->
case re:run(Str,Prx,[unicode]) of
nomatch ->
noprompt;
{match,[{Start,Len}]} ->
case split_prompt_string(Str,Start+1,Start+Len,1,[],[]) of
{noprompt,Done,Rest} ->
match_prompt(Rest,Prx,Done);
{prompt,UptoPrompt,Prompt,Rest} ->
{prompt,lists:reverse(UptoPrompt++Acc),
lists:reverse(Prompt),Rest}
end
nomatch ->
noprompt;
{match,[{Start,Len}]} ->
<<UptoPrompt:Start/binary, Prompt:Len/binary, Rest/binary>> = Str,
case validate_prompt(Start, UptoPrompt, Prompt) of
ok ->
{prompt,
unicode:characters_to_list([lists:reverse(Acc), UptoPrompt, Prompt]),
unicode:characters_to_list(Prompt),
unicode:characters_to_list(Rest)};
recurse ->
<<Skip:(Start+Len)/binary, Cont/binary>> = Str,
match_prompt(Cont, Prx, [Skip|Acc])
end
end.

split_prompt_string([Ch|Str],Start,End,N,UptoPrompt,Prompt) when N<Start ->
split_prompt_string(Str,Start,End,N+1,[Ch|UptoPrompt],Prompt);
split_prompt_string([Ch|Str],Start,End,N,UptoPrompt,Prompt)
when N>=Start, N<End->
split_prompt_string(Str,Start,End,N+1,UptoPrompt,[Ch|Prompt]);
split_prompt_string([Ch|Rest],_Start,End,N,UptoPrompt,Prompt) when N==End ->
case UptoPrompt of
[$",$=,$T,$P,$M,$O,$R,$P|_] ->
%% This is a line from "listenv", it is not a real prompt
{noprompt,[Ch|Prompt]++UptoPrompt,Rest};
[$\s,$t,$s,$a|_] when Prompt==":nigol" ->
%% This is probably the "Last login:" statement which is
%% written when telnet connection is opened.
{noprompt,[Ch|Prompt]++UptoPrompt,Rest};
_ ->
{prompt,[Ch|Prompt]++UptoPrompt,[Ch|Prompt],Rest}
validate_prompt(Size, PrePrompt, Prompt) ->
case PrePrompt of
%% This is a line from "listenv", it is not a real prompt
<<_:(Size-8)/binary, "PROMPT=\"", _/binary>> ->
recurse;
%% This is probably the "Last login:" statement which is
%% written when telnet connection is opened.
<<_:(Size-5)/binary, _L:8, "ast ", _/binary>>
when Prompt =:= <<"login: ">> ->
recurse;
_ ->
ok
end.
2 changes: 1 addition & 1 deletion lib/common_test/test/telnet_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ check_user(User,State) ->

check_pwd(Pwd,#state{authorized={user,Pwd}}=State) ->
dbg("password ok\n"),
send("Welcome to the ultimate telnet server!\r\n> ",State),
send("Welcomé to the ultimate telnet server!\r\n> ",State),
{ok,State#state{authorized=true}};
check_pwd(_,_State) ->
throw({error,authentication}).
Expand Down

0 comments on commit f211cce

Please sign in to comment.