~ubuntu-branches/ubuntu/saucy/curl/saucy-201307251546

« back to all changes in this revision

Viewing changes to tests/unit/unit1303.c

  • Committer: Bazaar Package Importer
  • Author(s): Ramakrishnan Muthukrishnan
  • Date: 2011-02-28 19:35:36 UTC
  • mto: (3.6.1 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: james.westby@ubuntu.com-20110228193536-p3a9jawxxofcsz7o
Tags: upstream-7.21.4
ImportĀ upstreamĀ versionĀ 7.21.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
#include "curl_config.h"
 
3
#include "setup.h"
 
4
 
 
5
#include "urldata.h"
 
6
#include "connect.h"
 
7
#include "curlcheck.h"
 
8
#include "memdebug.h" /* LAST include file */
 
9
 
 
10
static struct SessionHandle *data;
 
11
 
 
12
static CURLcode unit_setup( void )
 
13
{
 
14
  data = curl_easy_init();
 
15
  if (!data)
 
16
    return CURLE_OUT_OF_MEMORY;
 
17
  return CURLE_OK;
 
18
}
 
19
 
 
20
static void unit_stop( void )
 
21
{
 
22
  curl_easy_cleanup(data);
 
23
}
 
24
 
 
25
/* BASE is just a define to make us fool around with decently large number so
 
26
   that we aren't zero-based */
 
27
#define BASE 1000000
 
28
 
 
29
/* macro to set the pretended current time */
 
30
#define NOW(x,y) now.tv_sec = x; now.tv_usec = y
 
31
/* macro to set the millisecond based timeouts to use */
 
32
#define TIMEOUTS(x,y) data->set.timeout = x; data->set.connecttimeout = y
 
33
 
 
34
/*
 
35
 * To test:
 
36
 *
 
37
 * 00/10/01/11 timeouts set
 
38
 * 0/1         during connect
 
39
 * T           various values on the timeouts
 
40
 * N           various values of now
 
41
 */
 
42
 
 
43
struct timetest {
 
44
  int now_s;
 
45
  int now_us;
 
46
  int timeout_ms;
 
47
  int connecttimeout_ms;
 
48
  int connecting;
 
49
  long result;
 
50
  const char *comment;
 
51
};
 
52
 
 
53
UNITTEST_START
 
54
 
 
55
struct timeval now;
 
56
long timeout;
 
57
unsigned int i;
 
58
 
 
59
const struct timetest run[] = {
 
60
  /* both timeouts set, not connecting */
 
61
  {BASE + 4, 0,      10000, 8000, FALSE, 6000, "6 seconds should be left"},
 
62
  {BASE + 4, 990000, 10000, 8000, FALSE, 5010, "5010 ms should be left"},
 
63
  {BASE + 10, 0,     10000, 8000, FALSE, -1,   "timeout is -1, expired"},
 
64
  {BASE + 12, 0,     10000, 8000, FALSE, -2000, "-2000, overdue 2 seconds"},
 
65
 
 
66
  /* both timeouts set, connecting */
 
67
  {BASE + 4, 0,      10000, 8000, TRUE, 4000, "4 seconds should be left"},
 
68
  {BASE + 4, 990000, 10000, 8000, TRUE, 3010, "3010 ms should be left"},
 
69
  {BASE + 8, 0,      10000, 8000, TRUE, -1,   "timeout is -1, expired"},
 
70
  {BASE + 10, 0,     10000, 8000, TRUE, -2000, "-2000, overdue 2 seconds"},
 
71
 
 
72
  /* no connect timeout set, not connecting */
 
73
  {BASE + 4, 0,      10000, 0, FALSE, 6000, "6 seconds should be left"},
 
74
  {BASE + 4, 990000, 10000, 0, FALSE, 5010, "5010 ms should be left"},
 
75
  {BASE + 10, 0,     10000, 0, FALSE, -1,   "timeout is -1, expired"},
 
76
  {BASE + 12, 0,     10000, 0, FALSE, -2000, "-2000, overdue 2 seconds"},
 
77
 
 
78
  /* no connect timeout set, connecting */
 
79
  {BASE + 4, 0,      10000, 0, FALSE, 6000, "6 seconds should be left"},
 
80
  {BASE + 4, 990000, 10000, 0, FALSE, 5010, "5010 ms should be left"},
 
81
  {BASE + 10, 0,     10000, 0, FALSE, -1,   "timeout is -1, expired"},
 
82
  {BASE + 12, 0,     10000, 0, FALSE, -2000, "-2000, overdue 2 seconds"},
 
83
 
 
84
  /* only connect timeout set, not connecting */
 
85
  {BASE + 4, 0,      0, 10000, FALSE, 0, "no timeout active"},
 
86
  {BASE + 4, 990000, 0, 10000, FALSE, 0, "no timeout active"},
 
87
  {BASE + 10, 0,     0, 10000, FALSE, 0, "no timeout active"},
 
88
  {BASE + 12, 0,     0, 10000, FALSE, 0, "no timeout active"},
 
89
 
 
90
  /* only connect timeout set, connecting */
 
91
  {BASE + 4, 0,      0, 10000, TRUE, 6000, "6 seconds should be left"},
 
92
  {BASE + 4, 990000, 0, 10000, TRUE, 5010, "5010 ms should be left"},
 
93
  {BASE + 10, 0,     0, 10000, TRUE, -1,   "timeout is -1, expired"},
 
94
  {BASE + 12, 0,     0, 10000, TRUE, -2000, "-2000, overdue 2 seconds"},
 
95
 
 
96
  /* no timeout set, not connecting */
 
97
  {BASE + 4, 0,      0, 0, FALSE, 0, "no timeout active"},
 
98
  {BASE + 4, 990000, 0, 0, FALSE, 0, "no timeout active"},
 
99
  {BASE + 10, 0,     0, 0, FALSE, 0, "no timeout active"},
 
100
  {BASE + 12, 0,     0, 0, FALSE, 0, "no timeout active"},
 
101
 
 
102
  /* no timeout set, connecting */
 
103
  {BASE + 4, 0,      0, 0, TRUE, 296000, "no timeout active"},
 
104
  {BASE + 4, 990000, 0, 0, TRUE, 295010, "no timeout active"},
 
105
  {BASE + 10, 0,     0, 0, TRUE, 290000, "no timeout active"},
 
106
  {BASE + 12, 0,     0, 0, TRUE, 288000, "no timeout active"},
 
107
 
 
108
  /* both timeouts set, connecting, connect timeout the longer one */
 
109
  {BASE + 4, 0,      10000, 12000, TRUE, 6000, "6 seconds should be left"},
 
110
 
 
111
};
 
112
 
 
113
/* this is the pretended start time of the transfer */
 
114
data->progress.t_startsingle.tv_sec = BASE;
 
115
data->progress.t_startsingle.tv_usec = 0;
 
116
 
 
117
for(i=0; i < sizeof(run)/sizeof(run[0]); i++) {
 
118
  NOW(run[i].now_s, run[i].now_us);
 
119
  TIMEOUTS(run[i].timeout_ms, run[i].connecttimeout_ms);
 
120
  timeout =  Curl_timeleft(data, &now, run[i].connecting);
 
121
  if(timeout != run[i].result)
 
122
    fail(run[i].comment);
 
123
}
 
124
 
 
125
 
 
126
UNITTEST_STOP