Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / ubstack / test / ubstacktests.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6
7 #include "ocinternalapi.h"
8 extern "C" {
9     #include "logger.h"
10     #include "ocstack.h"
11     #include "ocstackinternal.h"
12 }
13
14 #include "gtest/gtest.h"
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <iostream>
25 #include <stdint.h>
26 using namespace std;
27
28
29 static const char TAG[] = "TestHarness";
30 static const std::string SERVICE_URI_1 = "coap://127.0.0.1:5683/";
31 static const std::string SERVICE_URI_2 = "coap://127.0.0.2:5683/";
32 static const std::string SERVICE_URI_3 = "coap://127.0.0.3:5683/";
33
34 void discoverServicesAsync(const oc::ub::OCDiscoverServicesResult& result) {
35     OC_LOG(INFO, TAG, "Entering discoverServicesAsync");
36
37     EXPECT_EQ(oc::ub::OCQueryResultType::SUCCESS, result.getResult());
38
39     if (result.getResult() == oc::ub::OCQueryResultType::SUCCESS) {
40         OC_LOG(INFO, TAG, "discoverServicesAsync, OCQueryResultType::SUCCESS");
41
42         std::list<std::string> serviceList = result.getServiceList();
43         EXPECT_GE((int)serviceList.size(), 1);
44
45         if (serviceList.size() > 0) {
46             for (auto iter = serviceList.begin(); iter != serviceList.end(); ++iter) {
47                 OC_LOG_V(INFO, TAG, "discoverServicesAsync, Service = %s", iter->c_str());
48             }
49         }
50     }
51 }
52 //-----------------------------------------------------------------------------
53 //  Tests
54 //-----------------------------------------------------------------------------
55 TEST(UBStackTest, ServiceDiscovery) {
56     OC_LOG(INFO, TAG, "Running ServiceDiscovery");
57
58     oc::ub::OCModel::WeakPtr modelWeak = oc::ub::OCModel::createModel();
59     oc::ub::OCModel::SharedPtr modelShared = modelWeak.lock();
60     EXPECT_TRUE((modelShared != 0));
61     if (modelShared) {
62         // Start model
63         EXPECT_TRUE(modelShared->start("127.0.0.1", 5683, oc::ub::OC_CLIENT));
64
65         // Create an async discovery function
66         oc::ub::OCDiscoverServicesFunction serviceFunction = discoverServicesAsync;
67
68         // Discover
69         modelShared->discoverServices(serviceFunction);
70
71         // Add service
72         EXPECT_EQ(1, (int)modelShared->addService(SERVICE_URI_1));
73
74         // Add service
75         EXPECT_EQ(2, (int)modelShared->addService(SERVICE_URI_2));
76
77         // Add service via OC Stack
78         OCUpdateResources(SERVICE_URI_3.c_str());
79     }
80 }
81
82