-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwts-dump.go
470 lines (414 loc) · 11.5 KB
/
wts-dump.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
////////////////////////////////////////////////////////////////////////////
// Porgram: wts-dump
// Purpose: wts (web test script) dump handling
// authors: Antonio Sun (c) 2015-16, All rights reserved
////////////////////////////////////////////////////////////////////////////
package main
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/xml"
"fmt"
"html"
"io"
"io/ioutil"
"os"
"regexp"
"sort"
"strconv"
"strings"
"unicode/utf16"
)
import (
"github.com/AntonioSun/shaper"
)
////////////////////////////////////////////////////////////////////////////
// Extending shaper.Shaper
type Shaper struct {
*shaper.Shaper
}
// Make a new Shaper filter and start adding bits
func NewFilter() *Shaper {
//return &Shaper{ShaperStack: PassThrough}
return &Shaper{Shaper: shaper.NewFilter()}
}
func (me *Shaper) ApplyXMLDecode() *Shaper {
me.AddFilter(html.UnescapeString)
return me
}
func (sp *Shaper) GetDFSID() *Shaper {
sp.AddFilter(func(s string) string {
dfSIDRe := regexp.MustCompile(`<SessionTicket>(.*?)</SessionTicket>`)
dfSID := dfSIDRe.FindStringSubmatch(s)
if len(dfSID) < 2 {
return ""
}
return dfSID[1]
})
return sp
}
func (sp *Shaper) GetDFCBID() *Shaper {
sp.AddFilter(func(s string) string {
re := regexp.MustCompile(`force/u/(.*?)/`)
id := re.FindStringSubmatch(s)
if len(id) < 2 {
return ""
}
//debug(id[1], 1)
return id[1]
})
return sp
}
////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions
////////////////////////////////////////////////////////////////////////////
// Function definitions
func getDecoder(Script *os.File) *xml.Decoder {
defer Script.Close()
content, err := ioutil.ReadFile(Script.Name())
check(err)
return xml.NewDecoder(bytes.NewBuffer(content))
}
type current struct {
transaction string
comment string
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Script-wide processing
func treatWtsXml(w io.Writer, checkOnly bool, decoder *xml.Decoder) error {
inloop := false
var cur current
for {
// Read tokens from the XML document in a stream.
token, _ := decoder.Token()
if token == nil {
break
}
// Inspect the type of the token just read.
switch t := token.(type) {
case xml.StartElement:
// If we just read a StartElement token
switch inElement := t.Name.Local; inElement {
case "Comment":
{
var c Comment
// decode a whole chunk of following XML into the
// variable c which is a Comment (t above)
decoder.DecodeElement(&c, &t)
cur.comment = c.Comment
treatComment(w, cur.comment)
}
case "ContextParameter":
{
var r ContextParameter
decoder.DecodeElement(&r, &t)
fmt.Fprintf(w, "CP: %s=%s\r\n", r.Name, r.Value)
}
case "DataSource":
{
var r DataSource
decoder.DecodeElement(&r, &t)
fmt.Fprintf(w, "DS: (%s, %s) %s\r\n",
r.Name, r.Connection, minify.Process(r.Tables.DataSourceTable))
}
case "ConditionalRule":
{
var r ConditionalRule
decoder.DecodeElement(&r, &t)
// The ConditionalRule might be under Condition or Loop
if inloop {
fmt.Fprintf(w, "\r\n<=\r\nLP")
inloop = false
} else {
fmt.Fprintf(w, "\r\n<=\r\nCB")
}
fmt.Fprintf(w, ": (%s) %s\r\n",
r.Name, minify.Process(r.RuleParameters.Xml))
}
case "IncludedWebTest":
{
var r IncludedWebTest
decoder.DecodeElement(&r, &t)
fmt.Fprintf(w, "I: %s\r\n", r.Included)
}
case "Loop":
inloop = true
case "Request":
treatRequest(w, checkOnly, decoder, t, cur)
case "TransactionTimer":
// <TransactionTimer Name="the transaction name">
cur.transaction = t.Attr[0].Value
treatTransaction(w, cur.transaction)
case "ValidationRules":
{
var r ValidationRules
decoder.DecodeElement(&r, &t)
for _, v := range r.ValidationRule {
fmt.Fprintf(w, "VR: (%s) %s\r\n",
v.Name, minify.Process(v.RuleParameters.Xml))
}
}
}
case xml.EndElement:
switch t.Name.Local {
case "Condition":
fmt.Fprintf(w, "CE: \r\n=>\r\n\r\n")
case "Loop":
fmt.Fprintf(w, "LP: \r\n=>\r\n\r\n")
}
default:
}
}
if options.Dump.Tsr {
// list of all date time strings used in the script in sorted order
var keys []string
for k := range dateCol {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(w, "TS: %s: %d\n", k, dateCol[k])
}
}
return nil
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Item-level processing
func treatComment(w io.Writer, v string) {
if options.Dump.Cnr {
v = cmtRe.ReplaceAllString(v, "[]")
}
fmt.Fprintf(w, "C: %s\r\n", v)
}
var DFClientBrowserId = ""
var DFClientBrowserIdFound = false
var dfCBIDReplace = shaper.NewFilter()
var urlFix = shaper.NewFilter().ApplyRegexpReplaceAll(
`http.*\w+\.\w+\.com`, "{{Param_TestServer}}")
// treatRequest will process requests like
// <Request Method="GET" or <Request Method="POST"
func treatRequest(wi io.Writer, checkOnly bool,
decoder *xml.Decoder, t xml.StartElement, cur current) {
w := bytes.NewBuffer([]byte{})
switch t.Attr[0].Value {
case "GET":
{
var r GetRequest
decoder.DecodeElement(&r, &t)
if options.Dump.Raw {
r.ThinkTime = "0"
r.Timeout = "0"
if len(DFClientBrowserId) == 0 {
DFClientBrowserId = NewFilter().GetDFCBID().Process(r.Url)
}
if len(DFClientBrowserId) != 0 {
if !DFClientBrowserIdFound {
DFClientBrowserIdFound = true
urlFix.ApplyReplace(DFClientBrowserId,
"{{Param_ClientBrowserId}}", -1)
}
}
r.Url = urlFix.Process(r.Url)
}
//fmt.Fprintf(w,"R: %q\r\n", r)
fmt.Fprintf(w, "G: (%s,%s) %s (%s):%s\r\n",
r.ThinkTime, r.Timeout, r.Url, r.ReportingName, r.RecordResult)
dealReqAddons(w, r.Request)
checkRequest(checkOnly, r.Request, w, cur)
}
case "POST":
{
var r PostRequest
decoder.DecodeElement(&r, &t)
stringBody := DecodeStringBody(r.StringBody)
coreService := ""
if options.Dump.Raw {
r.ThinkTime = "0"
r.Timeout = "0"
r.Url = urlFix.Process(r.Url)
if len(r.StringBody) != 0 {
regReplace := shaper.NewFilter().ApplyRegexpReplaceAll(
`.*(Get)</ReadableRequestName><RequestName>(.*?)</RequestName>.*<MethodName>(.*?)</MethodName>.*`,
"$1.$2.$3").ApplyRegexpReplaceAll(
`.*<ReadableCorrelator>|</ReadableCorrelator>.*`,
"").ApplyRegexpReplaceAll(
`.*<ReadableRequestName>|</ReadableRequestName>.*`, "")
coreService = regReplace.Process(stringBody)
}
}
//fmt.Fprintf(w,"R: %q\r\n", r)
fmt.Fprintf(w, "P: (%s,%s) %s %s (%s):%s\r\n", r.ThinkTime, r.Timeout,
r.Url, coreService, r.ReportingName, r.RecordResult)
if len(r.StringBody) != 0 {
fmt.Fprintf(w, "%s\r\n",
dealRequest(stringBodyDump.Process(stringBody)))
}
dealReqAddons(w, r.Request)
checkRequest(checkOnly, r.Request, w, cur)
}
default:
panic("Internal error parsing Request")
}
if !checkOnly {
wi.Write(w.Bytes())
}
}
func treatTransaction(w io.Writer, v string) {
fmt.Fprintf(w, "\r\nT: %s\r\n", v)
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Request specific processing
func dealReqAddons(w io.Writer, r Request) {
if len(r.QueryStringParameters.QueryStringParameter) != 0 {
prefixTag := " Q: "
split := minify.Copy()
split.ApplyRegexpReplaceAll(`( />)(<)`, "$1\r\n"+prefixTag+"$2")
fmt.Fprintf(w, "%s%s\r\n", prefixTag,
split.Process(r.QueryStringParameters.QueryStringParameter))
}
if len(r.FormPostHttpBody.FormPostParameter) != 0 {
fmt.Fprintf(w, " F: %s\r\n",
minify.Process(r.FormPostHttpBody.FormPostParameter))
}
if len(r.RequestPlugins.RequestPlugin) != 0 {
for _, v := range r.RequestPlugins.RequestPlugin {
fmt.Fprintf(w, " R: (%s) %s\r\n",
v.Name, minify.Process(v.RuleParameters.Xml))
}
}
if len(r.ExtractionRules.ExtractionRule) != 0 {
for _, v := range r.ExtractionRules.ExtractionRule {
fmt.Fprintf(w, " E: (%s: %s) %s\r\n",
v.Name, v.VariableName, minify.Process(v.RuleParameters.Xml))
}
}
if len(r.ValidationRules.ValidationRule) != 0 {
for _, v := range r.ValidationRules.ValidationRule {
fmt.Fprintf(w, " V: (%s) %s\r\n",
v.Name, minify.Process(v.RuleParameters.Xml))
}
}
w.Write([]byte("\r\n"))
}
var DFSessionTicket = ""
var DFSessionTicketFound = false
var dfSidReplace = shaper.NewFilter()
// date string collection
var dateCol map[string]int
func init() {
dateCol = make(map[string]int)
}
// dealRequest
// a filter to deal with POST StringBody and GET QueryStringParameters
// Functionality:
// - collect and replace date strings
func dealRequest(v string) string {
// Get the 1st DFSessionTicket
if options.Dump.Raw {
if len(DFSessionTicket) == 0 {
DFSessionTicket = NewFilter().GetDFSID().Process(v)
// debug(DFSessionTicket, 1)
}
if len(DFSessionTicket) != 0 {
if !DFSessionTicketFound {
DFSessionTicketFound = true
dfSidReplace.ApplyReplace(DFSessionTicket, "{{Param_SessionId}}", -1)
}
v = dfSidReplace.Process(v)
}
v = stringBodyFix.Process(v)
}
if !options.Dump.Tsr {
return v
}
for _, m := range tmsRe.FindAllString(v, -1) {
//debug(m, 1)
dateCol[m]++
}
v = tmsRe.ReplaceAllString(v, "-time-string-")
return v
}
func checkRequest(checkOnly bool, r Request, buf *bytes.Buffer, cur current) {
if !checkOnly {
return
}
reqs := buf.String()
tt, _ := strconv.Atoi(r.ThinkTime)
to, _ := strconv.Atoi(r.Timeout)
if tt != options.Check.ThinkTime ||
to < options.Check.Timeout ||
checkRe.MatchString(reqs) {
treatTransaction(os.Stdout, cur.transaction)
treatComment(os.Stdout, cur.comment)
fmt.Printf(reqs)
}
}
func DecodeStringBody(s string) string {
uDec, _ := base64.StdEncoding.DecodeString(s)
return DecodeUTF16(uDec)
}
func DecodeUTF16(s []byte) string {
u16s := make([]uint16, len(s)/2)
for i := range u16s {
u16s[i] = binary.LittleEndian.Uint16([]byte(s[i*2:]))
}
return string(utf16.Decode(u16s))
}
var minify *shaper.Shaper
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Main dispatch functions
var cmtRe *regexp.Regexp
var tmsRe *regexp.Regexp
var stringBodyDump *Shaper
var stringBodyFix *shaper.Shaper
func dumpCmd() error {
fileo := options.Dump.Fileo
if fileo == nil {
var err error
fileo, err = os.Create(
strings.Replace(options.Dump.Filei.Name(), ".webtest", ".webtext", 1))
check(err)
}
defer fileo.Close()
stringBodyDump = NewFilter()
stringBodyFix = shaper.NewFilter()
if !options.Dump.Asis {
stringBodyDump.ApplyXMLDecode()
}
if options.Dump.Raw {
options.Dump.Cnr = true
rawRuleRead(
strings.Replace(options.Dump.Filei.Name(), ".webtest", ".rawrule", 1))
}
if options.Dump.Cnr {
cmtRe = regexp.MustCompile(`\[#\d+]`)
}
if options.Dump.Tsr {
tmsRe = regexp.MustCompile(`(20\d{2}-\d{1,2}-\d{1,2}[T0-9:.]*|\d{1,2}/\d{1,2}/20\d{2})`)
}
minify = shaper.NewFilter().ApplyRegexpReplaceAll("\r*\n *", "")
return treatWtsXml(fileo, false, getDecoder(options.Dump.Filei))
}
var checkRe *regexp.Regexp
func checkCmd() error {
checkRe = regexp.MustCompile(options.Check.Checks)
//fmt.Printf("] %#v %#v\r\n", options.Check.Checks, checkRe)
return treatWtsXml(ioutil.Discard, true, getDecoder(options.Check.Filei))
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Support functions
func check(e error) {
if e != nil {
fmt.Printf("%s error: ", progname)
panic(e)
}
}
func debug(input string, threshold int) {
if !(VERBOSITY >= threshold) {
return
}
print("] ")
print(input)
print("\n")
}