Skip to content

Commit 0233c8d

Browse files
authored
Merge pull request #7882 from mikaleppanen/nw_if_gt_tests
Add greentea tests for network interface status and connect/disconnect
2 parents c4e0fc6 + fef0218 commit 0233c8d

File tree

4 files changed

+284
-0
lines changed

4 files changed

+284
-0
lines changed

TESTS/network/interface/main.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#define WIFI 2
19+
#if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \
20+
(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID))
21+
#error [NOT_SUPPORTED] No network configuration found for this target.
22+
#endif
23+
24+
#include "mbed.h"
25+
#include "greentea-client/test_env.h"
26+
#include "unity/unity.h"
27+
#include "utest.h"
28+
#include "utest/utest_stack_trace.h"
29+
#include "networkinterface_tests.h"
30+
31+
using namespace utest::v1;
32+
33+
// Test setup
34+
utest::v1::status_t test_setup(const size_t number_of_cases)
35+
{
36+
GREENTEA_SETUP(480, "default_auto");
37+
return verbose_test_setup_handler(number_of_cases);
38+
}
39+
40+
Case cases[] = {
41+
Case("NETWORKINTERFACE_STATUS", NETWORKINTERFACE_STATUS),
42+
Case("NETWORKINTERFACE_STATUS_NONBLOCK", NETWORKINTERFACE_STATUS_NONBLOCK),
43+
Case("NETWORKINTERFACE_STATUS_GET", NETWORKINTERFACE_STATUS_GET),
44+
Case("NETWORKINTERFACE_CONN_DISC_REPEAT", NETWORKINTERFACE_CONN_DISC_REPEAT),
45+
};
46+
47+
Specification specification(test_setup, cases);
48+
49+
int main()
50+
{
51+
return !Harness::run(specification);
52+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "networkinterface_tests.h"
21+
#include "unity/unity.h"
22+
#include "utest.h"
23+
24+
using namespace utest::v1;
25+
26+
namespace
27+
{
28+
NetworkInterface* net;
29+
const int repeats = 5;
30+
}
31+
32+
void NETWORKINTERFACE_CONN_DISC_REPEAT()
33+
{
34+
net = NetworkInterface::get_default_instance();
35+
36+
for (int i = 0; i < repeats; i++) {
37+
nsapi_error_t err = net->connect();
38+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
39+
40+
err = net->disconnect();
41+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
42+
}
43+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "networkinterface_tests.h"
21+
#include "unity/unity.h"
22+
#include "utest.h"
23+
24+
using namespace utest::v1;
25+
26+
namespace
27+
{
28+
NetworkInterface* net;
29+
rtos::Semaphore status_semaphore;
30+
int status_write_counter = 0;
31+
int status_read_counter = 0;
32+
const int repeats = 5;
33+
const int status_buffer_size = 100;
34+
nsapi_connection_status_t current_status = NSAPI_STATUS_ERROR_UNSUPPORTED;
35+
nsapi_connection_status_t statuses[status_buffer_size];
36+
}
37+
38+
void status_cb(nsapi_event_t event, intptr_t value)
39+
{
40+
TEST_ASSERT_EQUAL(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, event);
41+
42+
statuses[status_write_counter] = static_cast<nsapi_connection_status_t>(value);
43+
status_write_counter++;
44+
if (status_write_counter >= status_buffer_size) {
45+
TEST_ASSERT(0);
46+
}
47+
48+
status_semaphore.release();
49+
}
50+
51+
nsapi_connection_status_t wait_status_callback()
52+
{
53+
nsapi_connection_status_t status;
54+
55+
while (true) {
56+
status_semaphore.wait();
57+
58+
status = statuses[status_read_counter];
59+
status_read_counter++;
60+
61+
if (status != current_status) {
62+
current_status = status;
63+
return status;
64+
}
65+
}
66+
}
67+
68+
void NETWORKINTERFACE_STATUS()
69+
{
70+
nsapi_connection_status_t status;
71+
72+
status_write_counter = 0;
73+
status_read_counter = 0;
74+
current_status = NSAPI_STATUS_ERROR_UNSUPPORTED;
75+
76+
net = NetworkInterface::get_default_instance();
77+
net->attach(status_cb);
78+
net->set_blocking(true);
79+
80+
for (int i = 0; i < repeats; i++) {
81+
nsapi_error_t err = net->connect();
82+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
83+
84+
status = wait_status_callback();
85+
TEST_ASSERT_EQUAL(NSAPI_STATUS_CONNECTING, status);
86+
87+
status = wait_status_callback();
88+
if (status == NSAPI_STATUS_LOCAL_UP) {
89+
status = wait_status_callback();
90+
}
91+
TEST_ASSERT_EQUAL(NSAPI_STATUS_GLOBAL_UP, status);
92+
93+
net->disconnect();
94+
95+
status = wait_status_callback();
96+
TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED, status);
97+
}
98+
99+
net->attach(NULL);
100+
}
101+
102+
void NETWORKINTERFACE_STATUS_NONBLOCK()
103+
{
104+
nsapi_connection_status_t status;
105+
106+
status_write_counter = 0;
107+
status_read_counter = 0;
108+
current_status = NSAPI_STATUS_ERROR_UNSUPPORTED;
109+
110+
net = NetworkInterface::get_default_instance();
111+
net->attach(status_cb);
112+
net->set_blocking(false);
113+
114+
for (int i = 0; i < repeats; i++) {
115+
nsapi_error_t err = net->connect();
116+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
117+
118+
status = wait_status_callback();
119+
TEST_ASSERT_EQUAL(NSAPI_STATUS_CONNECTING, status);
120+
121+
status = wait_status_callback();
122+
if (status == NSAPI_STATUS_LOCAL_UP) {
123+
status = wait_status_callback();
124+
}
125+
TEST_ASSERT_EQUAL(NSAPI_STATUS_GLOBAL_UP, status);
126+
127+
net->disconnect();
128+
129+
status = wait_status_callback();
130+
TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED, status);
131+
}
132+
133+
net->attach(NULL);
134+
net->set_blocking(true);
135+
}
136+
137+
void NETWORKINTERFACE_STATUS_GET()
138+
{
139+
nsapi_connection_status_t status;
140+
141+
net = NetworkInterface::get_default_instance();
142+
net->set_blocking(true);
143+
144+
TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED, net->get_connection_status());
145+
146+
for (int i = 0; i < repeats; i++) {
147+
nsapi_error_t err = net->connect();
148+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
149+
150+
while (net->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) {
151+
wait(0.5);
152+
}
153+
154+
net->disconnect();
155+
156+
TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED, net->get_connection_status());
157+
}
158+
159+
net->attach(NULL);
160+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef NETWORKINTERFACE_TESTS_H
19+
#define NETWORKINTERFACE_TESTS_H
20+
21+
/*
22+
* Test cases
23+
*/
24+
void NETWORKINTERFACE_STATUS();
25+
void NETWORKINTERFACE_STATUS_NONBLOCK();
26+
void NETWORKINTERFACE_STATUS_GET();
27+
void NETWORKINTERFACE_CONN_DISC_REPEAT();
28+
29+
#endif //NETWORKINTERFACE_TESTS_H

0 commit comments

Comments
 (0)