Skip to content

Commit

Permalink
Merge pull request #7033 from zmstone/feat-emqx-license-allow-comma-i…
Browse files Browse the repository at this point in the history
…n-numbers

feat(emqx_license): allow comma in integer numbers in license body
  • Loading branch information
zmstone committed Feb 16, 2022
2 parents e2e92e9 + fa9300e commit ea1bf58
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
51 changes: 35 additions & 16 deletions lib-ee/emqx_license/src/emqx_license_parser_v20220101.erl
Expand Up @@ -12,6 +12,12 @@
-define(DIGEST_TYPE, sha256).
-define(LICENSE_VERSION, <<"220111">>).

%% This is the earliest license start date for version 220111
%% in theory it should be the same as ?LICENSE_VERSION (20220111),
%% however, in order to make expiration test easier (before Mar.2022),
%% allow it to start from Nov.2021
-define(MIN_START_DATE, 20211101).

-export([parse/2,
dump/1,
customer_type/1,
Expand Down Expand Up @@ -103,35 +109,48 @@ parse_payload(Payload) ->
end.

parse_type(TypeStr) ->
case string:to_integer(TypeStr) of
{Type, <<"">>} -> {ok, Type};
case parse_int(TypeStr) of
{ok, Type} -> {ok, Type};
_ -> {error, invalid_license_type}
end.

parse_customer_type(CTypeStr) ->
case string:to_integer(CTypeStr) of
{CType, <<"">>} -> {ok, CType};
case parse_int(CTypeStr) of
{ok, CType} -> {ok, CType};
_ -> {error, invalid_customer_type}
end.

parse_date_start(<<Y:4/binary, M:2/binary, D:2/binary>>) ->
Date = list_to_tuple([N || {N, <<>>} <- [string:to_integer(S) || S <- [Y, M, D]]]),
case calendar:valid_date(Date) of
true -> {ok, Date};
false -> {error, invalid_date}
end;
parse_date_start(_) -> {error, invalid_date}.
parse_date_start(DateStr) ->
case parse_int(DateStr) of
{ok, Num} when Num >= ?MIN_START_DATE ->
Y = Num div 1_00_00,
M = (Num rem 1_00_00) div 1_00,
D = Num rem 1_00,
case calendar:valid_date({Y, M, D}) of
true -> {ok, {Y, M, D}};
false -> {error, invalid_date}
end;
_ ->
{error, invalid_date}
end.

parse_days(DaysStr) ->
case string:to_integer(DaysStr) of
{Days, <<"">>} when Days > 0 -> {ok, Days};
case parse_int(DaysStr) of
{ok, Days} when Days > 0 -> {ok, Days};
_ -> {error, invalid_int_value}
end.

parse_max_connections(MaxConnStr) ->
case string:to_integer(MaxConnStr) of
{MaxConns, <<"">>} when MaxConns > 0 -> {ok, MaxConns};
_ -> {error, invalid_int_value}
case parse_int(MaxConnStr) of
{ok, MaxConns} when MaxConns > 0 -> {ok, MaxConns};
_ -> {error, invalid_connection_limit}
end.

parse_int(Str0) ->
Str = iolist_to_binary(string:replace(Str0, ",", "")),
case string:to_integer(Str) of
{Num, <<"">>} -> {ok, Num};
_ -> error
end.

collect_fields(Fields) ->
Expand Down
2 changes: 1 addition & 1 deletion lib-ee/emqx_license/test/emqx_license_SUITE.erl
Expand Up @@ -141,7 +141,7 @@ t_check_expired(_Config) ->
"0", %% Small customer
"Foo",
"contact@foo.com",
"20210101", %% Expired long ago
"20211101", %% Expired long ago
"10",
"10"]),
#{} = emqx_license_checker:update(License),
Expand Down
22 changes: 21 additions & 1 deletion lib-ee/emqx_license/test/emqx_license_parser_SUITE.erl
Expand Up @@ -96,6 +96,26 @@ t_parse(_Config) ->
]),
public_key_pem())),

?assertMatch(
{error,
[{emqx_license_parser_v20220101,
[{type,invalid_license_type},
{customer_type,invalid_customer_type},
{date_start,invalid_date},
{days,invalid_int_value}]}]},
emqx_license_parser:parse(
emqx_license_test_lib:make_license(
["220111",
"zero",
"ten",
"Foo",
"contact@foo.com",
"2022-02-1st",
"-10",
"10"
]),
public_key_pem())),

%% invalid signature
[LicensePart, _] = binary:split(
emqx_license_test_lib:make_license(
Expand Down Expand Up @@ -189,5 +209,5 @@ sample_license() ->
"Foo",
"contact@foo.com",
"20220111",
"100000",
"100,000",
"10"]).

0 comments on commit ea1bf58

Please sign in to comment.