Rearrange memory ownership of found device
[platform/core/connectivity/wifi-direct-manager.git] / unittest / method_injector.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <glib.h>
17 #include "wifi-direct-oem.h"
18 #include "method_injector.h"
19
20 const char *MethodInjector::peer_device_addr_str = "aa:bb:cc:dd:ee:ff";
21 const char *MethodInjector::legacy_peer_iface_addr_str = "aa:bb:cc:dd:ee:ee";
22 const char *MethodInjector::local_mac_str = "aa:bb:cc:dd:ee:dd";
23 const char *MethodInjector::device_name = "gunit_test_name";
24 const char *MethodInjector::pin_str = "12345678";
25 const int MethodInjector::frame_id = WFD_OEM_VSIE_FRAME_P2P_PD_REQ;
26 const char *MethodInjector::vsie_str = "ABCDEFGH";
27
28 const int MethodInjector::display_type = 3;
29 const int MethodInjector::display_port = 12345;
30 const int MethodInjector::display_hdcp = 0;
31 const int MethodInjector::display_avail = 0;
32 const char *MethodInjector::passphrase = "abcdefgh";
33
34 MethodInjector::MethodInjector()
35 {
36         this->result = NULL;
37 }
38
39 MethodInjector::MethodInjector(GVariant *input)
40 {
41         this->result = input;
42         g_variant_ref(input);
43 }
44
45 MethodInjector::MethodInjector(const MethodInjector & Other)
46 {
47         if (Other.result) {
48                 this->result = Other.result;
49                 g_variant_ref(Other.result);
50         }
51 }
52
53 MethodInjector::~MethodInjector()
54 {
55         if (this->result) {
56                 g_variant_unref(this->result);
57                 this->result = NULL;
58         }
59 }
60
61 GVariant *MethodInjector::GetResult()
62 {
63         return this->result;
64 }
65
66 const char *MethodInjector::GetDeviceName()
67 {
68         return this->device_name;
69 }
70
71 GVariant *MethodInjector::StartDiscoveryListen()
72 {
73         GVariantBuilder *builder = NULL;
74         GVariant *res = NULL;
75
76         gboolean listen_only = true;
77         int timeout = 0;
78         int frequency = 0;
79
80         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
81         g_variant_builder_add(builder, "{sv}", "Mode",
82                               g_variant_new("b", listen_only));
83         g_variant_builder_add(builder, "{sv}", "Timeout",
84                               g_variant_new("i", timeout));
85         g_variant_builder_add(builder, "{sv}", "Frequency",
86                               g_variant_new("i", frequency));
87         res = g_variant_new("(a{sv})", builder);
88         g_variant_builder_unref(builder);
89
90         if (this->result)
91                 g_variant_unref(this->result);
92         this->result = res;
93
94         return res;
95 }
96
97 GVariant *MethodInjector::StartDiscoveryFind()
98 {
99         GVariantBuilder *builder = NULL;
100         GVariant *res = NULL;
101
102         gboolean listen_only = false;
103         int timeout = 0;
104         int frequency = 0;
105
106         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
107         g_variant_builder_add(builder, "{sv}", "Mode",
108                               g_variant_new("b", listen_only));
109         g_variant_builder_add(builder, "{sv}", "Timeout",
110                               g_variant_new("i", timeout));
111         g_variant_builder_add(builder, "{sv}", "Frequency",
112                               g_variant_new("i", frequency));
113         res = g_variant_new("(a{sv})", builder);
114         g_variant_builder_unref(builder);
115
116         if (this->result)
117                 g_variant_unref(this->result);
118         this->result = res;
119
120         return res;
121 }
122
123 GVariant *MethodInjector::CreateGroup()
124 {
125         GVariantBuilder *builder = NULL;
126         GVariant *res = NULL;
127
128         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
129         g_variant_builder_add(builder, "{sv}", "ssid",
130                         g_variant_new_string("TIZEN"));
131         res = g_variant_new("(a{sv})", builder);
132         g_variant_builder_unref(builder);
133
134         if (this->result)
135                 g_variant_unref(this->result);
136         this->result = res;
137
138         return res;
139 }
140
141 GVariant *MethodInjector::Connect()
142 {
143         GVariant *res = NULL;
144
145         res = g_variant_new("(s)", this->peer_device_addr_str);
146
147         if (this->result)
148                 g_variant_unref(this->result);
149         this->result = res;
150
151         return res;
152 }
153
154 GVariant *MethodInjector::AcceptConnect()
155 {
156         GVariant *res = NULL;
157
158         res = g_variant_new("(s)", this->peer_device_addr_str);
159
160         if (this->result)
161                 g_variant_unref(this->result);
162         this->result = res;
163
164         return res;
165 }
166
167 GVariant *MethodInjector::RejectConnect()
168 {
169         GVariant *res = NULL;
170
171         res = g_variant_new("(s)", this->peer_device_addr_str);
172
173         if (this->result)
174                 g_variant_unref(this->result);
175         this->result = res;
176
177         return res;
178 }
179
180
181 GVariant *MethodInjector::CancelConnect()
182 {
183         GVariant *res = NULL;
184
185         res = g_variant_new("(s)", this->peer_device_addr_str);
186
187         if (this->result)
188                 g_variant_unref(this->result);
189         this->result = res;
190
191         return res;
192 }
193
194 GVariant *MethodInjector::SetDeviceName()
195 {
196         GVariant *res = NULL;
197
198         res = g_variant_new("(s)", this->device_name);
199
200         if (this->result)
201                 g_variant_unref(this->result);
202         this->result = res;
203
204         return res;
205 }
206
207 GVariant *MethodInjector::SetReqWpsMode(int req_wps_mode)
208 {
209         GVariant *res = NULL;
210
211         res = g_variant_new("(i)", req_wps_mode);
212
213         if (this->result)
214                 g_variant_unref(this->result);
215         this->result = res;
216
217         return res;
218 }
219
220 GVariant *MethodInjector::SetGoIntent(int go_intent)
221 {
222         GVariant *res = NULL;
223
224         res = g_variant_new("(i)", go_intent);
225
226         if (this->result)
227                 g_variant_unref(this->result);
228         this->result = res;
229
230         return res;
231 }
232
233 GVariant *MethodInjector::SetMaxClient(int max_client)
234 {
235         GVariant *res = NULL;
236
237         res = g_variant_new("(i)", max_client);
238
239         if (this->result)
240                 g_variant_unref(this->result);
241         this->result = res;
242
243         return res;
244 }
245
246 GVariant *MethodInjector::SetAutoConnection(bool mode)
247 {
248         GVariant *res = NULL;
249
250         res = g_variant_new("(b)", mode);
251
252         if (this->result)
253                 g_variant_unref(this->result);
254         this->result = res;
255
256         return res;
257 }
258
259 GVariant *MethodInjector::SetSessionTimer(int session_timer)
260 {
261         GVariant *res = NULL;
262
263         res = g_variant_new("(i)", session_timer);
264
265         if (this->result)
266                 g_variant_unref(this->result);
267         this->result = res;
268
269         return res;
270 }
271
272 GVariant *MethodInjector::GetPeerAddr()
273 {
274         GVariant *res = NULL;
275
276         res = g_variant_new("(s)", this->peer_device_addr_str);
277
278         if (this->result)
279                 g_variant_unref(this->result);
280         this->result = res;
281
282         return res;
283 }
284
285 GVariant *MethodInjector::GetLegacyPeerAddr()
286 {
287         GVariant *res = NULL;
288
289         res = g_variant_new("(s)", this->legacy_peer_iface_addr_str);
290
291         if (this->result)
292                 g_variant_unref(this->result);
293         this->result = res;
294
295         return res;
296 }
297
298 GVariant *MethodInjector::SetWpsPin()
299 {
300         GVariant *res = NULL;
301
302         res = g_variant_new("(s)", this->pin_str);
303
304         if (this->result)
305                 g_variant_unref(this->result);
306         this->result = res;
307
308         return res;
309 }
310
311 GVariant *MethodInjector::SetPassphrase()
312 {
313         GVariant *res = NULL;
314
315         res = g_variant_new("(s)", this->passphrase);
316
317         if (this->result)
318                 g_variant_unref(this->result);
319         this->result = res;
320
321         return res;
322 }
323
324 GVariant *MethodInjector::SetDisplay()
325 {
326         GVariant *res = NULL;
327
328         res = g_variant_new("(iii)", this->display_type, this->display_port, this->display_hdcp);
329
330         if (this->result)
331                 g_variant_unref(this->result);
332         this->result = res;
333
334         return res;
335
336 }
337
338 GVariant *MethodInjector::SetDisplayAvail()
339 {
340         GVariant *res = NULL;
341
342         res = g_variant_new("(i)", this->display_avail);
343
344         if (this->result)
345                 g_variant_unref(this->result);
346         this->result = res;
347
348         return res;
349 }
350
351 GVariant *MethodInjector::SetAutoGroupRemoval(bool enable)
352 {
353         GVariant *res = NULL;
354
355         res = g_variant_new("(b)", enable);
356
357         if (this->result)
358                 g_variant_unref(this->result);
359         this->result = res;
360
361         return res;
362 }
363
364 GVariant *MethodInjector::SetPersistentGroup(bool enable)
365 {
366         GVariant *res = NULL;
367
368         res = g_variant_new("(b)", enable);
369
370         if (this->result)
371                 g_variant_unref(this->result);
372         this->result = res;
373
374         return res;
375 }
376
377 GVariant *MethodInjector::AddVsie()
378 {
379         GVariant *res = NULL;
380
381         res = g_variant_new("(is)", this->frame_id, this->vsie_str);
382
383         if (this->result)
384                 g_variant_unref(this->result);
385         this->result = res;
386
387         return res;
388 }
389
390 GVariant *MethodInjector::GetVsie()
391 {
392         GVariant *res = NULL;
393
394         res = g_variant_new("(i)", this->frame_id);
395
396         if (this->result)
397                 g_variant_unref(this->result);
398         this->result = res;
399
400         return res;
401 }
402
403 GVariant *MethodInjector::RemoveVsie()
404 {
405         GVariant *res = NULL;
406
407         res = g_variant_new("(is)", this->frame_id, this->vsie_str);
408
409         if (this->result)
410                 g_variant_unref(this->result);
411         this->result = res;
412
413         return res;
414 }
415
416 GVariant *MethodInjector::SrvStartDiscovery()
417 {
418         GVariant *res = NULL;
419         int srv_type = 2;
420         char *mac_addr = "00:00:00:00:00:00";
421
422         res = g_variant_new("(is)", srv_type, mac_addr);
423
424         if (this->result)
425                 g_variant_unref(this->result);
426         this->result = res;
427
428         return res;
429 }
430
431 GVariant *MethodInjector::SrvStopDiscovery()
432 {
433         GVariant *res = NULL;
434         int srv_type = 2;
435         char *mac_addr = "00:00:00:00:00:00";
436
437         res = g_variant_new("(is)", srv_type, mac_addr);
438
439         if (this->result)
440                 g_variant_unref(this->result);
441         this->result = res;
442
443         return res;
444 }
445
446 GVariant *MethodInjector::SrvRegister()
447 {
448         GVariant *res = NULL;
449         int srv_type = 2;
450         char *srv = "10|uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootFdevice";
451
452         res = g_variant_new("(is)", srv_type, srv);
453
454         if (this->result)
455                 g_variant_unref(this->result);
456         this->result = res;
457
458         return res;
459 }
460
461 GVariant *MethodInjector::SrvRegisterBonjourPtr()
462 {
463         GVariant *res = NULL;
464         int srv_type = 1;
465         char *srv = "_http._tcp|ptrLocal._http._tcp";
466
467         res = g_variant_new("(is)", srv_type, srv);
468
469         if (this->result)
470                 g_variant_unref(this->result);
471         this->result = res;
472
473         return res;
474 }
475
476 GVariant *MethodInjector::SrvRegisterBonjourTxt()
477 {
478         GVariant *res = NULL;
479         int srv_type = 1;
480         char *srv = "_http._tcp|key=value";
481
482         res = g_variant_new("(is)", srv_type, srv);
483
484         if (this->result)
485                 g_variant_unref(this->result);
486         this->result = res;
487
488         return res;
489 }
490
491 GVariant *MethodInjector::SrvRegister(int type, char *srv_str)
492 {
493         GVariant *res = NULL;
494
495         res = g_variant_new("(is)", type, srv_str);
496
497         if (this->result)
498                 g_variant_unref(this->result);
499         this->result = res;
500
501         return res;
502 }
503
504 GVariant *MethodInjector::SrvDeregister()
505 {
506         GVariant *res = NULL;
507         int srv_id = 1;
508
509         res = g_variant_new("(i)", srv_id);
510
511         if (this->result)
512                 g_variant_unref(this->result);
513         this->result = res;
514
515         return res;
516 }
517
518 GVariant *MethodInjector::AspAdvertise()
519 {
520         GVariant *res = NULL;
521         GVariantBuilder *builder = NULL;
522
523         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
524         g_variant_builder_add(builder, "{sv}", "adv_id", g_variant_new("u", 0));
525         g_variant_builder_add(builder, "{sv}", "discovery_tech", g_variant_new("i", 0));
526         g_variant_builder_add(builder, "{sv}", "preferred_connection", g_variant_new("y", 0));
527         g_variant_builder_add(builder, "{sv}", "auto_accept", g_variant_new("i", 0));
528         g_variant_builder_add(builder, "{sv}", "status", g_variant_new("y", 0));
529         g_variant_builder_add(builder, "{sv}", "role", g_variant_new("y", 0));
530         g_variant_builder_add(builder, "{sv}", "replace", g_variant_new("i", 0));
531         g_variant_builder_add(builder, "{sv}", "config_method", g_variant_new("u", 0));
532         g_variant_builder_add(builder, "{sv}", "instance_name", g_variant_new("s", "instance_name"));
533         g_variant_builder_add(builder, "{sv}", "service_type", g_variant_new("s", "service_type"));
534         g_variant_builder_add(builder, "{sv}", "rsp_info", g_variant_new("s", "rsp_info"));
535         res = g_variant_new("(a{sv})", builder);
536         g_variant_builder_unref(builder);
537
538         if (this->result)
539                 g_variant_unref(this->result);
540         this->result = res;
541
542         return res;
543 }
544
545 GVariant *MethodInjector::AspAdvertiseCancel()
546 {
547         GVariant *res = NULL;
548
549         res = g_variant_new("(u)", 0);
550
551         if (this->result)
552                 g_variant_unref(this->result);
553         this->result = res;
554
555         return res;
556
557 }
558
559 GVariant *MethodInjector::AspSeek()
560 {
561         GVariant *res = NULL;
562         GVariantBuilder *builder = NULL;
563
564         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
565         g_variant_builder_add(builder, "{sv}", "discovery_tech", g_variant_new("i", 0));
566         g_variant_builder_add(builder, "{sv}", "preferred_connection", g_variant_new("y", 0));
567         g_variant_builder_add(builder, "{sv}", "search_id", g_variant_new("t", 0));
568         g_variant_builder_add(builder, "{sv}", "service_type", g_variant_new("s", "service_type"));
569         res = g_variant_new("(a{sv})", builder);
570         g_variant_builder_unref(builder);
571
572         if (this->result)
573                 g_variant_unref(this->result);
574         this->result = res;
575
576         return res;
577 }
578
579 GVariant *MethodInjector::AspSeekCancel()
580 {
581         GVariant *res = NULL;
582         res = g_variant_new("(t)", 0);
583
584         if (this->result)
585                 g_variant_unref(this->result);
586         this->result = res;
587
588         return res;
589 }
590
591 GVariant *MethodInjector::AspSessionConnect(const char *session_mac_str, unsigned char network_role, unsigned int network_config)
592 {
593         GVariant *res = NULL;
594         GVariantBuilder *builder = NULL;
595
596         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
597
598         g_variant_builder_add(builder, "{sv}", "service_mac", g_variant_new("s", this->peer_device_addr_str));
599         g_variant_builder_add(builder, "{sv}", "adv_id", g_variant_new("u", 0));
600
601         g_variant_builder_add(builder, "{sv}", "session_mac", g_variant_new("s", session_mac_str));
602         g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", 0));
603         g_variant_builder_add(builder, "{sv}", "role", g_variant_new("y", network_role));
604         g_variant_builder_add(builder, "{sv}", "config_method", g_variant_new("u", network_config));
605         g_variant_builder_add(builder, "{sv}", "session _info", g_variant_new("s", "session_info"));
606
607         res = g_variant_new("(a{sv})", builder);
608         g_variant_builder_unref(builder);
609
610         if (this->result)
611                 g_variant_unref(this->result);
612         this->result = res;
613
614         return res;
615
616 }
617
618 GVariant *MethodInjector::AspSessionConfirm(const char *session_mac_str, int confirm, char *pin)
619 {
620         GVariant *res = NULL;
621         GVariantBuilder *builder = NULL;
622         gchar pin_str[8 + 1] = {0, };
623
624         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
625
626         g_variant_builder_add(builder, "{sv}", "session_mac", g_variant_new("s", this->peer_device_addr_str));
627         g_variant_builder_add(builder, "{sv}", "session_id", g_variant_new("u", 0));
628         g_variant_builder_add(builder, "{sv}", "confirm", g_variant_new("i", confirm));
629         if (pin != 0) {
630                 g_snprintf(pin_str, 8 + 1, "%s", (gchar *)pin);
631                 g_variant_builder_add(builder, "{sv}", "pin", g_variant_new("s", pin_str));
632         }
633         g_variant_builder_add(builder, "{sv}", "service_mac", g_variant_new("s", this->local_mac_str));
634         g_variant_builder_add(builder, "{sv}", "adv_id", g_variant_new("u", 0));
635
636         res = g_variant_new("(a{sv})", builder);
637         g_variant_builder_unref(builder);
638
639         if (this->result)
640                 g_variant_unref(this->result);
641         this->result = res;
642
643         return res;
644 }