File: dns_unittest.cc

package info (click to toggle)
workflow 0.11.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,756 kB
  • sloc: cpp: 33,895; ansic: 9,397; makefile: 9; sh: 6
file content (127 lines) | stat: -rw-r--r-- 2,778 bytes parent folder | download | duplicates (2)
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
/*
  Copyright (c) 2021 Sogou, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  Author: Liu Kai (liukaidx@sogou-inc.com)
*/

#include <future>
#include <gtest/gtest.h>
#include "workflow/WFTaskFactory.h"
#include "workflow/WFDnsClient.h"

#define RETRY_MAX	3

TEST(dns_unittest, WFDnsTaskCreate1)
{
	std::string url = "dns://119.29.29.29/www.sogou.com";
	auto *task = WFTaskFactory::create_dns_task(url, 0, NULL);
	task->dismiss();
}

TEST(dns_unittest, WFDnsTaskCreate2)
{
	std::string url = "http://119.29.29.29:dns/";
	std::promise<void> done;
	auto *task = WFTaskFactory::create_dns_task(url, 0,
	[&done] (WFDnsTask *task)
	{
		done.set_value();
	});
	task->start();
	done.get_future().get();
}

TEST(dns_unittest, WFDnsTask)
{
	std::string url = "dns://119.29.29.29/www.sogou.com";
	unsigned short req_id = 0x1234;
	std::promise<void> done;

	auto *task = WFTaskFactory::create_dns_task(url, RETRY_MAX,
	[&done, req_id] (WFDnsTask *task)
	{
		int state = task->get_state();

		if (state == WFT_STATE_SUCCESS)
		{
			unsigned short resp_id = task->get_resp()->get_id();
			EXPECT_TRUE(req_id == resp_id);
		}

		done.set_value();
	});

	auto *req = task->get_req();
	req->set_id(req_id);
	req->set_rd(1);
	req->set_question_type(DNS_TYPE_A);
	task->start();

	auto fut = done.get_future();
	fut.get();
}

TEST(dns_unittest, WFDnsClientInit1)
{
	WFDnsClient client;
	if (client.init("bad") >= 0)
		client.deinit();
}

TEST(dns_unittest, WFDnsClientInit2)
{
	WFDnsClient client;
	int ret = client.init("0.0.0.0,0.0.0.1:1,dns://0.0.0.2,dnss://0.0.0.3");
	EXPECT_TRUE(ret >= 0);
	client.deinit();
}

TEST(dns_unittest, WFDnsClient)
{
	unsigned short req_id = 0x4321;
	std::promise<void> done;
	WFDnsClient client;

	client.init("dns://119.29.29.29/");

	auto *task = client.create_dns_task("www.sogou.com",
	[&done, req_id] (WFDnsTask *task)
	{
		int state = task->get_state();

		if (state == WFT_STATE_SUCCESS)
		{
			unsigned short resp_id = task->get_resp()->get_id();
			EXPECT_TRUE(req_id == resp_id);
		}

		done.set_value();
	});

	client.deinit();

	auto *req = task->get_req();
	req->set_id(req_id);
	task->start();

	auto fut = done.get_future();
	fut.get();
}

int main(int argc, char *argv[])
{
	::testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}