summaryrefslogtreecommitdiff
path: root/lib/public_key/test/pubkey_cert_SUITE.erl
blob: 7d51d5afda179871a2d5865527ba0807f783c8c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2011-2023. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

-module(pubkey_cert_SUITE).

-include_lib("common_test/include/ct.hrl").
%% -include_lib("public_key/include/public_key.hrl").

%% Note: This directive should only be used in test suites.
-compile(export_all).

%%--------------------------------------------------------------------
%% Common Test interface functions -----------------------------------
%%--------------------------------------------------------------------

all() ->
    [{group, time_str_2_gregorian_sec}].

groups() ->
    [{time_str_2_gregorian_sec, [], time_str_two_gregorian()}].

time_str_two_gregorian() ->
    [ time_str_2_gregorian_utc_post2000
    , time_str_2_gregorian_utc_limit_50_years_before_current_time
    , time_str_2_gregorian_utc_limit_51_years_before_current_time
    , time_str_2_gregorian_utc_limit_50_years_from_current_time
    , time_str_2_gregorian_utc_limit_49_years_from_current_time
    , time_str_2_gregorian_generaltime_50_years_before_current_time
    , time_str_2_gregorian_generaltime_50_years_from_current_time
    ].

%%--------------------------------------------------------------------
%% Test Cases --------------------------------------------------------
%%--------------------------------------------------------------------
time_str_2_gregorian_utc_post2000() ->
    [{doc, "Tests a valid gregorian Utc time"}].
time_str_2_gregorian_utc_post2000(_) ->
    YYMMDD = "450101",
    HHMMSSZ = "000000Z",
    ExpectedYear = 2045,
    UtcTime = {utcTime, YYMMDD ++ HHMMSSZ},
    {ExpectedDate, _} = convert_to_datetime_format(UtcTime, ExpectedYear),

    Result = pubkey_cert:time_str_2_gregorian_sec(UtcTime),
    {ExpectedDate, _} = calendar:gregorian_seconds_to_datetime(Result).


time_str_2_gregorian_utc_limit_50_years_before_current_time() ->
    [{doc, "Tests limit of gregorian Utc time 50 years before current time"}].
time_str_2_gregorian_utc_limit_50_years_before_current_time(_) ->
    {ExpectedDate, UtcTime} = get_date(utcTime, -50),
    Result = pubkey_cert:time_str_2_gregorian_sec(UtcTime),
    {ExpectedDate, _} = calendar:gregorian_seconds_to_datetime(Result).

time_str_2_gregorian_utc_limit_51_years_before_current_time() ->
    [{doc, "Tests limit of gregorian Utc time 51 years before current time"}].
time_str_2_gregorian_utc_limit_51_years_before_current_time(_) ->
    {{Y, M, D}, UtcTime} = get_date(utcTime, -51),
    Result = pubkey_cert:time_str_2_gregorian_sec(UtcTime),
    %% the sliding window method from pubkey_cert reaches its limit and
    %% reverses the year from 19XX to 20XX. Because of this, the expected
    %% year is current_year + 50, or ExpectedYear + 100 (they are equivalent)
    ExpectedYear = Y + 100,
    {{ExpectedYear, M, D}, _} = calendar:gregorian_seconds_to_datetime(Result).

time_str_2_gregorian_utc_limit_50_years_from_current_time() ->
    [{doc, "Tests a valid gregorian Utc time 50 years from now"}].
time_str_2_gregorian_utc_limit_50_years_from_current_time(_) ->
    {{Y, M, D}, UtcTime} = get_date(utcTime, 50),
    Result = pubkey_cert:time_str_2_gregorian_sec(UtcTime),
    %% the sliding window method from pubkey_cert reaches its limit and
    %% reverses the year from 20XX to 19XX. Because of this, the expected
    %% year is current_year - 50, or ExpectedYear - 100 (they are equivalent)
    ExpectedYear = Y - 100,
    {{ExpectedYear, M, D}, _} = calendar:gregorian_seconds_to_datetime(Result).

time_str_2_gregorian_utc_limit_49_years_from_current_time() ->
    [{doc, "Tests a valid gregorian Utc time 49 years from now"}].
time_str_2_gregorian_utc_limit_49_years_from_current_time(_) ->
    {ExpectedDate, UtcTime} = get_date(utcTime, 49),
    Result = pubkey_cert:time_str_2_gregorian_sec(UtcTime),
    {ExpectedDate, _} = calendar:gregorian_seconds_to_datetime(Result).


time_str_2_gregorian_generaltime_50_years_before_current_time() ->
    [{doc, "Tests a valid general time 50 years before current time"}].
time_str_2_gregorian_generaltime_50_years_before_current_time(_) ->
    {ExpectedDate, UtcTime} = get_date(generalTime, -50),
    Result = pubkey_cert:time_str_2_gregorian_sec(UtcTime),
    {ExpectedDate, _} = calendar:gregorian_seconds_to_datetime(Result).


time_str_2_gregorian_generaltime_50_years_from_current_time() ->
    [{doc, "Tests a valid general time 50 years from now"}].
time_str_2_gregorian_generaltime_50_years_from_current_time(_) ->
    {ExpectedDate, UtcTime} = get_date(generalTime, 50),
    Result = pubkey_cert:time_str_2_gregorian_sec(UtcTime),
    {ExpectedDate, _} = calendar:gregorian_seconds_to_datetime(Result).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Helper functions
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec convert_to_datetime_format({Format, Date}, ExpectedYear) -> Result when
      Format :: generalTime | utcTime,
      Date :: YYMMDDHHMMSS | YYYYMMDDHHMMSS,
      YYMMDDHHMMSS :: string(),
      YYYYMMDDHHMMSS :: string(),
      ExpectedYear :: non_neg_integer(),
      Result :: {{non_neg_integer(), 1..12, 1..31}, {0, 0, 0}}.
convert_to_datetime_format({Format, Date}, ExpectedYear) ->
    YYMMDD = group_year(Format, Date),
    [Y, M, D] = lists:map(fun (Str) -> erlang:list_to_integer(Str) end, YYMMDD),
    %% assertions to test that the result is the expected one
    case Format of
        utcTime -> (ExpectedYear rem 100) =:= Y;
        generalTime -> ExpectedYear =:= Y
    end,
    {{ExpectedYear, M, D}, {0, 0, 0}}.

-spec get_date(Format, YearsFromNow) -> {{Year, Month, Day}, UtcDate} when
      Format :: utcTime | generalTime,
      YearsFromNow :: integer(),
      Year :: non_neg_integer(),
      Month :: 1..12,
      Day :: 1..31,
      UtcDate :: string().
get_date(Format, Years) ->
    {YYYY, MM0, DD0} = date(),
    MM = io_lib:format("~2..0w", [MM0]),
    DD = io_lib:format("~2..0w", [DD0]),
    ExpectedYear = YYYY + Years,
    YYMMDD = format_year(Format, ExpectedYear, {MM, DD}),
    HHMMSSZ = "000000Z",
    FormattedTime = {Format, YYMMDD ++ HHMMSSZ},
    {ExpectedDate, _} = convert_to_datetime_format(FormattedTime, ExpectedYear),
    {ExpectedDate, FormattedTime}.

-spec format_year(Format, ExpectedYear, {Month, Day}) -> YYMMDD | YYYYMMDD when
      Format :: utcTime | generalTime,
      ExpectedYear :: non_neg_integer(),
      Month :: string(),
      Day :: string(),
      YYMMDD :: string(),
      YYYYMMDD :: string().
format_year(utcTime, ExpectedYear, {MM, DD}) ->
    YY = erlang:integer_to_list((ExpectedYear) rem 100),
    lists:flatten(YY++MM++DD);
format_year(generalTime, ExpectedYear, {MM, DD}) ->
    YY = erlang:integer_to_list(ExpectedYear),
    lists:flatten(YY++MM++DD).

-spec group_year(Format, Date) -> [list()] when
      Format :: utcTime | generalTime,
      Date :: [non_neg_integer()].
group_year(utcTime, [Y1, Y2, M1, M2, D1, D2 | _]) ->
    [[Y1, Y2], [M1, M2], [D1, D2]];
group_year(generalTime, [Y1, Y2, Y3, Y4, M1, M2, D1, D2 | _]) ->
    [[Y1, Y2, Y3, Y4], [M1, M2], [D1, D2]].