forked from snarfed/p4sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.cpp
More file actions
139 lines (114 loc) · 3.67 KB
/
Copy pathserver_test.cpp
File metadata and controls
139 lines (114 loc) · 3.67 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
/**
* p4sync
* http://snarfed.org/p4sync
*
* Copyright 2005 Ryan Barrett <p4sync@ryanb.org>
*
* @file server_test.cpp
*
* Unit test for server.cpp.
*
* Note that libwebserver redirects stdout for its handlers, so this test
* redirects stdout to an internal stringbuf so it can check the handler
* output.
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "cxxtest/cxxtest/TestSuite.h"
#include "server.h"
#include "settings.h"
#include "libwebserver/web_server.h"
#include <iostream>
#include <sstream>
#include <string>
// defined in server.cpp
extern void root_handler();
extern void playlist_handler();
extern void cursong_handler();
extern void curpos_handler();
extern void state_handler();
extern void version_handler();
extern struct web_server gweb_server;
class cserver_test : public CxxTest::TestSuite
{
private:
stringbuf output;
streambuf *original_cout;
public:
void setUp() {
// need to start libwebserver because the playlist handler calls
// web_server_aliasdir()
if (gweb_server.socket <= 0)
web_server_init(&gweb_server, 12345 /* port */, NULL /* log file */,
0 /* flags */);
if (!cplayer::instance().is_plugin_enabled())
cplayer::instance().plugin_enabled();
cplayer::instance().play();
// redirect cout to a stringbuf, so it can be checked by tests
output.str("");
original_cout = cout.rdbuf(&output);
}
void tearDown() {
// restore cout
cout.rdbuf(original_cout);
}
void test_init() {
// server should refuse to start if the player plugin is not enabled
cplayer::instance().plugin_disabled();
TS_ASSERT_EQUALS(-1, server_init(12345 /* port */));
}
void test_root_handler() {
root_handler();
const string start("p4sync</a> is serving!");
TS_ASSERT_DIFFERS(string::npos, output.str().find(start));
}
void test_playlist_handler() {
playlist_handler();
TS_ASSERT_EQUALS("Content-type: text/plain\r\n\r\n./foo\n./bar\n./baz z\n",
output.str());
}
void test_cursong_handler() {
cursong_handler();
TS_ASSERT_EQUALS("Content-type: text/plain\r\n\r\n0", output.str());
}
void test_curpos_handler() {
// pause to let it get farther into the song
sleep_ms(100);
curpos_handler();
istream received(&output);
string buf;
received >> buf;
TS_ASSERT_EQUALS("Content-type:", buf);
received >> buf;
TS_ASSERT_EQUALS("text/plain", buf);
struct timeval time;
received >> buf >> time.tv_sec >> time.tv_usec;
TS_ASSERT_EQUALS("server_time", buf);
TS_ASSERT_DELTA(gettimeofday().tv_sec, time.tv_sec, 1);
int pos;
received >> buf >> pos;
TS_ASSERT_EQUALS("song_pos_ms", buf);
TS_ASSERT_EQUALS(cmock_player::kdefault_pos_ms, pos);
}
void test_state_handler() {
state_handler();
TS_ASSERT_EQUALS("Content-type: text/plain\r\n\r\nplaying", output.str());
}
void test_version_handler() {
version_handler();
TS_ASSERT_EQUALS("Content-type: text/plain\r\n\r\n" VERSION, output.str());
}
};