- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_encoding_prefs.c
More file actions
Latest commit
120 lines (99 loc) · 3.19 KB
/
Copy pathtest_encoding_prefs.c
File metadata and controls
120 lines (99 loc) · 3.19 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
#include"encoding_prefs.h"
#include<assert.h>
#include<stdio.h>
#include<string.h>
voidtest_parse_qstring() {
//qvalue = ( "0" [ "." *3DIGIT ] ) / ( "1" [ "." *3"0" ] )
//test valid
assert(parse_qvalue("0") ==0);
assert(parse_qvalue("0.000") ==0);
assert(parse_qvalue("1") ==1000);
assert(parse_qvalue("1.000") ==1000);
assert(parse_qvalue("0.8") ==800);
assert(parse_qvalue("0.888") ==888);
//decimal without following digits
assert(parse_qvalue("1.") ==-1);
assert(parse_qvalue("0.") ==-1);
//too many digits
assert(parse_qvalue("0.0000") ==-1);
assert(parse_qvalue("1.0000") ==-1);
//not in range 0-1
assert(parse_qvalue("1.05") ==-1);
assert(parse_qvalue("10") ==-1);
//not digits
assert(parse_qvalue(" 0.8") ==-1);
assert(parse_qvalue("=0.8") ==-1);
assert(parse_qvalue("q0.8") ==-1);
assert(parse_qvalue("0.8 ") ==-1);
}
voidtest_parse_weight() {
assert(parse_weight(";q=0") ==0);
assert(parse_weight("; q=0") ==0);
assert(parse_weight("; q=1.0") ==1000);
assert(parse_weight("; q=1.000") ==1000);
assert(parse_weight("; \t q=0.8") ==800);
assert(parse_weight(";q=0.001") ==1);
//invalid
assert(parse_weight("; Q=0") ==-1);
assert(parse_weight("; q =0") ==-1);
assert(parse_weight(";q=0.001f") ==-1);
}
voidtest_parse_accepted_encodings() {
//Accept-Encoding: compress;q=0.5, gzip;q=1.0
//Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
//
//test default weigth is 1 rfc7231#section-5.3.1
encoding_prefs_tprefs;
charbuf[128];
//test empty list Accept-Encoding:
init_encoding_prefs(&prefs);
strncpy(buf, "", 128);
assert(parse_accepted_encodings(&prefs, buf) !=-1);
assert(prefs.gzip==-1);
assert(prefs.identity==-1);
assert(prefs.catch_all==-1);
//Accept-Encoding: *
init_encoding_prefs(&prefs);
strncpy(buf, "*", 128);
assert(parse_accepted_encodings(&prefs, buf) !=-1);
assert(prefs.gzip==-1);
assert(prefs.identity==-1);
assert(prefs.catch_all==1000);
//Accept-Encoding: compress;q=0.5, gzip;q=1.0
init_encoding_prefs(&prefs);
strncpy(buf, "compress;q=0.5, gzip;q=1.0", 128);
assert(parse_accepted_encodings(&prefs, buf) !=-1);
assert(prefs.gzip==1000);
assert(prefs.identity==-1);
assert(prefs.catch_all==-1);
init_encoding_prefs(&prefs);
strncpy(buf, "compress\t;\tq=0.5 , gzip ;q=0.8, *", 128);
assert(parse_accepted_encodings(&prefs, buf) !=-1);
assert(prefs.gzip==800);
assert(prefs.identity==-1);
assert(prefs.catch_all==1000);
init_encoding_prefs(&prefs);
strncpy(buf, "identity, *;q=0", 128);
assert(parse_accepted_encodings(&prefs, buf) !=-1);
assert(prefs.gzip==-1);
assert(prefs.identity==1000);
assert(prefs.catch_all==0);
//test empty element
init_encoding_prefs(&prefs);
strncpy(buf, "identity,, *;q=0", 128);
assert(parse_accepted_encodings(&prefs, buf) ==-1);
//test bad weight
init_encoding_prefs(&prefs);
strncpy(buf, "identity, *;q=0.8a", 128);
assert(parse_accepted_encodings(&prefs, buf) ==-1);
//test missing weigth
init_encoding_prefs(&prefs);
strncpy(buf, "identity;, gzip;q=0.8", 128);
assert(parse_accepted_encodings(&prefs, buf) ==-1);
}
intmain() {
test_parse_qstring();
test_parse_weight();
test_parse_accepted_encodings();
return0;
}