File: policy_tests.adb

package info (click to toggle)
libalog 0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,056 kB
  • ctags: 465
  • sloc: ada: 5,203; makefile: 166; sql: 9; sh: 2
file content (238 lines) | stat: -rw-r--r-- 7,945 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
--
--  Copyright (c) 2009,
--  Reto Buerki, Adrian-Ken Rueegsegger
--  secunet SwissIT AG
--
--  This file is part of Alog.
--
--  Alog is free software; you can redistribute it and/or modify
--  it under the terms of the GNU Lesser General Public License as published
--  by the Free Software Foundation; either version 2.1 of the License, or
--  (at your option) any later version.
--
--  Alog is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU Lesser General Public License for more details.
--
--  You should have received a copy of the GNU Lesser General Public License
--  along with Alog; if not, write to the Free Software
--  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
--  MA  02110-1301  USA
--

with Ahven;

with Alog.Maps;
with Alog.Policy_DB;

package body Policy_Tests is

   use Ahven;
   use Alog;

   package DB renames Alog.Policy_DB;

   -------------------------------------------------------------------------

   procedure Default_Loglevel_Handling is
      Before    : constant Log_Level := DB.Get_Default_Loglevel;
      New_Level : constant Log_Level := Critical;
   begin
      DB.Set_Default_Loglevel (Level => New_Level);
      Assert (Condition => DB.Get_Default_Loglevel = New_Level,
              Message   => "Default level mismatch");

      DB.Set_Default_Loglevel (Level => Before);
   end Default_Loglevel_Handling;

   -------------------------------------------------------------------------

   procedure Finalize (T : in out Testcase) is
      pragma Unreferenced (T);
   begin
      DB.Reset;
   end Finalize;

   -------------------------------------------------------------------------

   procedure Ident_Loglevel_Handling is
   begin
      DB.Set_Loglevel (Identifier => "Foo",
                       Level      => Info);
      Assert (Condition => DB.Get_Loglevel (Identifier => "Foo") = Info,
              Message   => "Ident loglevel mismatch");

      DB.Set_Loglevel (Identifier => "Foo",
                       Level      => Error);
      Assert (Condition => DB.Get_Loglevel (Identifier => "Foo") = Error,
              Message   => "Unable to update identifier loglevel");

      declare
         Level : Log_Level;
         pragma Unreferenced (Level);
      begin
         Level := DB.Get_Loglevel (Identifier => "Bar");
         Fail (Message => "Expected No_Ident_Loglevel");

      exception
         when DB.No_Ident_Loglevel =>
            null;
      end;
   end Ident_Loglevel_Handling;

   -------------------------------------------------------------------------

   procedure Initialize (T : in out Testcase) is
   begin
      T.Set_Name (Name => "Tests for the logging policy database");
      T.Add_Test_Routine
        (Routine => Reset_Policy_DB'Access,
         Name    => "reset policy database");
      T.Add_Test_Routine
        (Routine => Default_Loglevel_Handling'Access,
         Name    => "default loglevel handling");
      T.Add_Test_Routine
        (Routine => Ident_Loglevel_Handling'Access,
         Name    => "identifier based loglevel handling");
      T.Add_Test_Routine
        (Routine => Set_Identifier_Map'Access,
         Name    => "set identifier loglevel map");
      T.Add_Test_Routine
        (Routine => Verify_Accept_Src'Access,
         Name    => "accept source");
      T.Add_Test_Routine
        (Routine => Verify_Accept_Dst'Access,
         Name    => "accept destination");
      T.Add_Test_Routine
        (Routine => Lookup_Ident'Access,
         Name    => "lookup identifier");
   end Initialize;

   -------------------------------------------------------------------------

   procedure Lookup_Ident is
   begin
      DB.Set_Default_Loglevel (Level => Error);
      DB.Set_Loglevel (Identifier => "Lookup.*",
                       Level      => Warning);

      Assert (Condition => DB.Lookup
              (Identifier => "Lookup") = Warning,
              Message   => "Lookup mismatch");
      Assert (Condition => DB.Lookup
              (Identifier => "Nonexistent") = Error,
              Message   => "Nonexistent mismatch");
   end Lookup_Ident;

   -------------------------------------------------------------------------

   procedure Reset_Policy_DB is
   begin
      DB.Set_Default_Loglevel (Level => Error);
      DB.Set_Loglevel (Identifier => "Foo",
                       Level      => Warning);

      DB.Reset;

      Assert (Condition => DB.Get_Default_Loglevel = DB.Alog_Default_Level,
              Message   => "Default loglevel mismatch");

      declare
         Src_Level : Log_Level;
         pragma Unreferenced (Src_Level);
      begin
         Src_Level := DB.Get_Loglevel (Identifier => "Foo");
         Fail (Message => "Src levels not reset");

      exception
         when DB.No_Ident_Loglevel =>
            null;
      end;
   end Reset_Policy_DB;

   -------------------------------------------------------------------------

   procedure Set_Identifier_Map is
      Map : Maps.Wildcard_Level_Map;
   begin
      Map.Insert (Key  => "Foo",
                  Item => Notice);
      Map.Insert (Key  => "Bar",
                  Item => Warning);

      DB.Set_Loglevel (Identifiers => Map);

      Assert (Condition => DB.Get_Loglevel (Identifier => "Foo") = Notice,
              Message   => "Foo identifier loglevel mismatch");
      Assert (Condition => DB.Get_Loglevel (Identifier => "Bar") = Warning,
              Message   => "Bar identifier loglevel mismatch");
   end Set_Identifier_Map;

   -------------------------------------------------------------------------

   procedure Verify_Accept_Dst is
   begin
      DB.Reset;

      --  Default loglevel should be ignored

      DB.Set_Default_Loglevel (Level => Info);
      Assert (Condition => DB.Accept_Dst
              (Identifier => "Foobar",
               Level      => Debug),
              Message   => "Debug not accepted");

      DB.Set_Loglevel (Identifier => "Foo.*",
                       Level      => Error);
      Assert (Condition => not DB.Accept_Dst
              (Identifier => "Foo",
               Level      => Debug),
              Message   => "Dst debug accepted");
      Assert (Condition => DB.Accept_Src
              (Identifier => "Foo",
               Level      => Critical),
              Message   => "Dst critical not accepted");
      Assert (Condition => DB.Accept_Dst
              (Identifier => "Bar",
               Level      => Info),
              Message   => "Dst bar not accepted");
   end Verify_Accept_Dst;

   -------------------------------------------------------------------------

   procedure Verify_Accept_Src is
   begin
      DB.Reset;

      DB.Set_Default_Loglevel (Level => Info);

      Assert (Condition => not DB.Accept_Src (Level => Debug),
              Message   => "Debug accepted");
      Assert (Condition => DB.Accept_Src (Level => Warning),
              Message   => "Warning not accepted");

      DB.Set_Loglevel (Identifier => "Foo.*",
                       Level      => Error);
      Assert (Condition => not DB.Accept_Src
              (Identifier => "Foo",
               Level      => Debug),
              Message   => "Src debug accepted");
      Assert (Condition => DB.Accept_Src
              (Identifier => "Foo",
               Level      => Critical),
              Message   => "Src critical not accepted");
      Assert (Condition => DB.Accept_Src
              (Identifier => "Bar",
               Level      => Info),
              Message   => "Src bar not accepted");

      DB.Set_Loglevel (Identifier => "Foobar.*",
                       Level      => Debug);
      Assert (Condition => DB.Accept_Src
              (Identifier => "Foobar",
               Level      => Debug),
              Message   => "Src foobar not accepted");
   end Verify_Accept_Src;

end Policy_Tests;