| 1 |
-- |
| 2 |
-- D_Bus/Ada - An Ada binding to D-Bus |
| 3 |
-- |
| 4 |
-- Copyright (C) 2011, 2012 Reto Buerki <[email protected]> |
| 5 |
-- |
| 6 |
-- This program is free software; you can redistribute it and/or |
| 7 |
-- modify it under the terms of the GNU General Public License |
| 8 |
-- as published by the Free Software Foundation; either version 2 |
| 9 |
-- of the License, or (at your option) any later version. |
| 10 |
-- |
| 11 |
-- This program is distributed in the hope that it will be useful, |
| 12 |
-- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
-- GNU General Public License for more details. |
| 15 |
-- |
| 16 |
-- You should have received a copy of the GNU General Public License |
| 17 |
-- along with this program; if not, write to the Free Software |
| 18 |
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 19 |
-- USA. |
| 20 |
-- |
| 21 |
-- As a special exception, if other files instantiate generics from this |
| 22 |
-- unit, or you link this unit with other files to produce an |
| 23 |
-- executable this unit does not by itself cause the resulting |
| 24 |
-- executable to be covered by the GNU General Public License. This |
| 25 |
-- exception does not however invalidate any other reasons why the |
| 26 |
-- executable file might be covered by the GNU Public License. |
| 27 |
-- |
| 28 |
|
| 29 |
with Ahven; |
| 30 |
|
| 31 |
with Config; |
| 32 |
|
| 33 |
with D_Bus.Types; |
| 34 |
with D_Bus.Connection; |
| 35 |
with D_Bus.Arguments.Basic; |
| 36 |
with D_Bus.Arguments.Containers; |
| 37 |
|
| 38 |
package body Arguments_Container_Tests is |
| 39 |
|
| 40 |
use Ahven; |
| 41 |
use D_Bus; |
| 42 |
use D_Bus.Arguments.Basic; |
| 43 |
use D_Bus.Arguments.Containers; |
| 44 |
use type D_Bus.Types.Obj_Path; |
| 45 |
|
| 46 |
------------------------------------------------------------------------- |
| 47 |
|
| 48 |
procedure Append_To_Array |
| 49 |
is |
| 50 |
A : Array_Type; |
| 51 |
begin |
| 52 |
A.Append (New_Item => +Unsigned_32'(1232)); |
| 53 |
|
| 54 |
begin |
| 55 |
A.Append (New_Item => +"testmessage"); |
| 56 |
Fail (Message => "Expected D-Bus error"); |
| 57 |
|
| 58 |
exception |
| 59 |
when D_Bus_Error => null; |
| 60 |
end; |
| 61 |
end Append_To_Array; |
| 62 |
|
| 63 |
------------------------------------------------------------------------- |
| 64 |
|
| 65 |
procedure Initialize (T : in out Testcase) is |
| 66 |
begin |
| 67 |
T.Set_Name (Name => "Container arguments handling"); |
| 68 |
T.Add_Test_Routine |
| 69 |
(Routine => Append_To_Array'Access, |
| 70 |
Name => "Append to array"); |
| 71 |
T.Add_Test_Routine |
| 72 |
(Routine => Marshal_Array_Of_Strings'Access, |
| 73 |
Name => "Marshal/unmarshal array of strings"); |
| 74 |
T.Add_Test_Routine |
| 75 |
(Routine => Marshal_Array_Of_Arrays_Of_Strings'Access, |
| 76 |
Name => "Marshal/unmarshal array of arrays of strings"); |
| 77 |
T.Add_Test_Routine |
| 78 |
(Routine => Marshal_Struct'Access, |
| 79 |
Name => "Marshal/unmarshal struct"); |
| 80 |
T.Add_Test_Routine |
| 81 |
(Routine => Marshal_Dict_Entries'Access, |
| 82 |
Name => "Marshal/unmarshal dict entries"); |
| 83 |
T.Add_Test_Routine |
| 84 |
(Routine => Marshal_Dict_Entries_With_Variant_Value'Access, |
| 85 |
Name => "Marshal/unmarshal dict entries with variants"); |
| 86 |
T.Add_Test_Routine |
| 87 |
(Routine => Marshal_Variant'Access, |
| 88 |
Name => "Marshal/unmarshal variant"); |
| 89 |
end Initialize; |
| 90 |
|
| 91 |
------------------------------------------------------------------------- |
| 92 |
|
| 93 |
procedure Marshal_Array_Of_Arrays_Of_Strings |
| 94 |
is |
| 95 |
-- Marshal/unmarshal an array of arrays of strings. |
| 96 |
|
| 97 |
Conn : constant Connection.Connection_Type |
| 98 |
:= Connection.Connect (Address => Config.Service_Addr); |
| 99 |
Result : Arguments.Argument_List_Type; |
| 100 |
A1 : Array_Type; |
| 101 |
A2 : Array_Type; |
| 102 |
begin |
| 103 |
A2.Append (New_Item => +"string1"); |
| 104 |
A2.Append (New_Item => +"string2"); |
| 105 |
A2.Append (New_Item => +"string3"); |
| 106 |
|
| 107 |
A1.Append (New_Item => A2); |
| 108 |
A1.Append (New_Item => A2); |
| 109 |
|
| 110 |
Result := Connection.Call_Blocking |
| 111 |
(Connection => Conn, |
| 112 |
Destination => "dbus.ada.server", |
| 113 |
Path => +"/", |
| 114 |
Iface => "dbus.ada.server.type", |
| 115 |
Method => "rebound", |
| 116 |
Args => +A1); |
| 117 |
|
| 118 |
declare |
| 119 |
R_A : constant Array_Type := Array_Type (Result.First_Element); |
| 120 |
begin |
| 121 |
Assert (Condition => R_A.Get_Count = 2, |
| 122 |
Message => "Count not 2"); |
| 123 |
Assert (Condition => Array_Type (R_A.First_Element).Get_Count = 3, |
| 124 |
Message => "String count not 3 (1)"); |
| 125 |
Assert (Condition => Array_Type (R_A.Last_Element).Get_Count = 3, |
| 126 |
Message => "String count not 3 (2)"); |
| 127 |
end; |
| 128 |
end Marshal_Array_Of_Arrays_Of_Strings; |
| 129 |
|
| 130 |
------------------------------------------------------------------------- |
| 131 |
|
| 132 |
procedure Marshal_Array_Of_Strings |
| 133 |
is |
| 134 |
Conn : constant Connection.Connection_Type |
| 135 |
:= Connection.Connect (Address => Config.Service_Addr); |
| 136 |
Result : Arguments.Argument_List_Type; |
| 137 |
A : Array_Type; |
| 138 |
begin |
| 139 |
A.Append (New_Item => +"string1"); |
| 140 |
A.Append (New_Item => +"string2"); |
| 141 |
A.Append (New_Item => +"string3"); |
| 142 |
|
| 143 |
Result := Connection.Call_Blocking |
| 144 |
(Connection => Conn, |
| 145 |
Destination => "dbus.ada.server", |
| 146 |
Path => +"/", |
| 147 |
Iface => "dbus.ada.server.type", |
| 148 |
Method => "rebound", |
| 149 |
Args => +A); |
| 150 |
|
| 151 |
declare |
| 152 |
R_A : constant Array_Type := Array_Type (Result.First_Element); |
| 153 |
begin |
| 154 |
Assert (Condition => R_A.Get_Count = 3, |
| 155 |
Message => "Count not 3"); |
| 156 |
Assert (Condition => String_Type |
| 157 |
(R_A.First_Element).To_String = "string1", |
| 158 |
Message => "First element not string1"); |
| 159 |
Assert (Condition => String_Type |
| 160 |
(R_A.Last_Element).To_String = "string3", |
| 161 |
Message => "Last element not string3"); |
| 162 |
end; |
| 163 |
end Marshal_Array_Of_Strings; |
| 164 |
|
| 165 |
------------------------------------------------------------------------- |
| 166 |
|
| 167 |
procedure Marshal_Dict_Entries |
| 168 |
is |
| 169 |
Conn : constant Connection.Connection_Type |
| 170 |
:= Connection.Connect (Address => Config.Service_Addr); |
| 171 |
Result : Arguments.Argument_List_Type; |
| 172 |
A : Array_Type; |
| 173 |
D : Dict_Entry_Type; |
| 174 |
begin |
| 175 |
D := Create (Key => +Signed_16'(1234), |
| 176 |
Value => +"value"); |
| 177 |
|
| 178 |
A.Append (New_Item => D); |
| 179 |
A.Append (New_Item => D); |
| 180 |
|
| 181 |
Result := Connection.Call_Blocking |
| 182 |
(Connection => Conn, |
| 183 |
Destination => "dbus.ada.server", |
| 184 |
Path => +"/", |
| 185 |
Iface => "dbus.ada.server.type", |
| 186 |
Method => "rebound", |
| 187 |
Args => +A); |
| 188 |
|
| 189 |
declare |
| 190 |
R_A : constant Array_Type := Array_Type (Result.First_Element); |
| 191 |
D1, D2 : Dict_Entry_Type; |
| 192 |
begin |
| 193 |
Assert (Condition => R_A.Get_Count = 2, |
| 194 |
Message => "Count not 2"); |
| 195 |
|
| 196 |
D1 := Dict_Entry_Type (R_A.First_Element); |
| 197 |
D2 := Dict_Entry_Type (R_A.Last_Element); |
| 198 |
|
| 199 |
Assert (Condition => Int16_Type (D1.Get_Key).To_Ada = 1234, |
| 200 |
Message => "D1 key not 1234"); |
| 201 |
Assert (Condition => Int16_Type (D2.Get_Key).To_Ada = 1234, |
| 202 |
Message => "D2 key not 1234"); |
| 203 |
Assert (Condition => String_Type (D1.Get_Value).To_String = "value", |
| 204 |
Message => "D1 value mismatch"); |
| 205 |
Assert (Condition => String_Type (D2.Get_Value).To_String = "value", |
| 206 |
Message => "D2 value mismatch"); |
| 207 |
end; |
| 208 |
end Marshal_Dict_Entries; |
| 209 |
|
| 210 |
------------------------------------------------------------------------- |
| 211 |
|
| 212 |
procedure Marshal_Dict_Entries_With_Variant_Value |
| 213 |
is |
| 214 |
Conn : constant Connection.Connection_Type |
| 215 |
:= Connection.Connect (Address => Config.Service_Addr); |
| 216 |
Result : Arguments.Argument_List_Type; |
| 217 |
A : Array_Type; |
| 218 |
D : Dict_Entry_Type; |
| 219 |
V : Variant_Type; |
| 220 |
begin |
| 221 |
V := Create (Source => +Unsigned_32'(12)); |
| 222 |
|
| 223 |
D := Create (Key => +Signed_16'(1234), |
| 224 |
Value => V); |
| 225 |
|
| 226 |
A.Append (New_Item => D); |
| 227 |
A.Append (New_Item => D); |
| 228 |
|
| 229 |
Result := Connection.Call_Blocking |
| 230 |
(Connection => Conn, |
| 231 |
Destination => "dbus.ada.server", |
| 232 |
Path => +"/", |
| 233 |
Iface => "dbus.ada.server.type", |
| 234 |
Method => "rebound", |
| 235 |
Args => +A); |
| 236 |
|
| 237 |
declare |
| 238 |
R_A : constant Array_Type := Array_Type (Result.First_Element); |
| 239 |
D1, D2 : Dict_Entry_Type; |
| 240 |
begin |
| 241 |
Assert (Condition => R_A.Get_Count = 2, |
| 242 |
Message => "Count not 2"); |
| 243 |
|
| 244 |
D1 := Dict_Entry_Type (R_A.First_Element); |
| 245 |
D2 := Dict_Entry_Type (R_A.Last_Element); |
| 246 |
|
| 247 |
Assert (Condition => Int16_Type (D1.Get_Key).To_Ada = 1234, |
| 248 |
Message => "D1 key not 1234"); |
| 249 |
Assert (Condition => Int16_Type (D2.Get_Key).To_Ada = 1234, |
| 250 |
Message => "D2 key not 1234"); |
| 251 |
Assert (Condition => Variant_Type (D1.Get_Value).To_String = "u, 12", |
| 252 |
Message => "D1 value mismatch"); |
| 253 |
Assert (Condition => Variant_Type (D2.Get_Value).To_String = "u, 12", |
| 254 |
Message => "D2 value mismatch"); |
| 255 |
end; |
| 256 |
end Marshal_Dict_Entries_With_Variant_Value; |
| 257 |
|
| 258 |
------------------------------------------------------------------------- |
| 259 |
|
| 260 |
procedure Marshal_Struct |
| 261 |
is |
| 262 |
Conn : constant Connection.Connection_Type |
| 263 |
:= Connection.Connect (Address => Config.Service_Addr); |
| 264 |
Result : Arguments.Argument_List_Type; |
| 265 |
R : Struct_Type; |
| 266 |
begin |
| 267 |
R.Append (New_Item => +Signed_32'(-45653)); |
| 268 |
R.Append (New_Item => +Boolean'(False)); |
| 269 |
R.Append (New_Item => +Byte'(254)); |
| 270 |
|
| 271 |
Result := Connection.Call_Blocking |
| 272 |
(Connection => Conn, |
| 273 |
Destination => "dbus.ada.server", |
| 274 |
Path => +"/", |
| 275 |
Iface => "dbus.ada.server.type", |
| 276 |
Method => "rebound", |
| 277 |
Args => +R); |
| 278 |
|
| 279 |
declare |
| 280 |
R_R : constant Struct_Type := Struct_Type (Result.First_Element); |
| 281 |
begin |
| 282 |
Assert (Condition => R_R.Get_Count = 3, |
| 283 |
Message => "Count not 3"); |
| 284 |
Assert (Condition => Int32_Type (R_R.First_Element).To_Ada = -45653, |
| 285 |
Message => "First element mismatch"); |
| 286 |
Assert (Condition => Byte_Type (R_R.Last_Element).To_Ada = 254, |
| 287 |
Message => "Last element mismatch"); |
| 288 |
end; |
| 289 |
end Marshal_Struct; |
| 290 |
|
| 291 |
------------------------------------------------------------------------- |
| 292 |
|
| 293 |
procedure Marshal_Variant |
| 294 |
is |
| 295 |
Conn : constant Connection.Connection_Type |
| 296 |
:= Connection.Connect (Address => Config.Service_Addr); |
| 297 |
Result : Arguments.Argument_List_Type; |
| 298 |
V : Variant_Type; |
| 299 |
begin |
| 300 |
V := Create (Source => +Unsigned_32'(123456)); |
| 301 |
|
| 302 |
Result := Connection.Call_Blocking |
| 303 |
(Connection => Conn, |
| 304 |
Destination => "dbus.ada.server", |
| 305 |
Path => +"/", |
| 306 |
Iface => "dbus.ada.server.type", |
| 307 |
Method => "rebound", |
| 308 |
Args => +V); |
| 309 |
|
| 310 |
declare |
| 311 |
R_V : constant Variant_Type := Variant_Type (Result.First_Element); |
| 312 |
begin |
| 313 |
Assert (Condition => U_Int32_Type (R_V.Get_Argument).To_Ada = 123456, |
| 314 |
Message => "Arg mismatch"); |
| 315 |
end; |
| 316 |
end Marshal_Variant; |
| 317 |
|
| 318 |
end Arguments_Container_Tests; |