forked from xdg-go/scram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdata_test.go
162 lines (136 loc) · 3.52 KB
/
testdata_test.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
// Copyright 2018 by David A. Golden. All rights reserved.
//
// 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
package scram
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)
type TestCase struct {
Label string
Digest string
User string
Pass string
AuthzID string
SkipSASLprep bool
Salt64 string
Iters int
ClientNonce string
ServerNonce string
Valid bool
Steps []string
}
type testStep struct {
Input string
Expect string
IsError bool
}
func getHGF(s string) (HashGeneratorFcn, error) {
switch s {
case "SHA-1":
return SHA1, nil
case "SHA-256":
return SHA256, nil
default:
panic(fmt.Sprintf("Unknown hash function '%s'", s))
}
}
func decodeFile(s string) (TestCase, error) {
var tc TestCase
data, err := ioutil.ReadFile(s)
if err != nil {
return tc, err
}
err = json.Unmarshal(data, &tc)
if err != nil {
return tc, fmt.Errorf("error unmarshaling '%s': %v", s, err)
}
return tc, nil
}
func getTestFiles(dir string) ([]string, error) {
subdir := filepath.Join("testdata", dir)
files, err := ioutil.ReadDir(subdir)
if err != nil {
return nil, err
}
filenames := make([]string, len(files))
for i, v := range files {
filenames[i] = filepath.Join(subdir, v.Name())
}
return filenames, nil
}
func getTestData(dirs ...string) ([]TestCase, error) {
var err error
filenames := make([]string, 0)
for _, v := range dirs {
names, err := getTestFiles(v)
if err != nil {
return nil, err
}
filenames = append(filenames, names...)
}
cases := make([]TestCase, len(filenames))
for i, v := range filenames {
cases[i], err = decodeFile(v)
if err != nil {
return nil, err
}
}
return cases, nil
}
// Even steps are client messages; odd steps are server responses.
func clientSteps(c TestCase) []testStep {
n := len(c.Steps)
// Test case requires at least two steps: the first client step
// (which cannot fail) and the first server response -- after which
// an error would prevent further client steps.
if n < 2 {
panic("Incomplete conversation for this test case")
}
// First step needs empty input.
steps := []testStep{{Input: "", Expect: c.Steps[0]}}
// From i==1 until end, construct conversations from pairs of steps. We
// know that (n >= 2). If the last pair is incomplete (no client Expect)
// that indicates error.
last := n - 1
for i := 1; i <= last; i += 2 {
steps = append(steps, assembleStep(c, i, last))
}
return steps
}
// Even steps are client messages; odd steps are server responses.
func serverSteps(c TestCase) []testStep {
n := len(c.Steps)
// Test case requires at least one step: the first client step
// after which an error would prevent further server steps.
if n == 0 {
panic("Incomplete conversation for this test case")
}
steps := make([]testStep, 0, 1)
// From i==0 until end, construct conversations from pairs of steps. We
// know that (n >= 1). If the last pair is incomplete (no server Expect)
// that indicates error.
last := n - 1
for i := 0; i < last; i += 2 {
ts := assembleStep(c, i, last)
steps = append(steps, ts)
}
return steps
}
func assembleStep(c TestCase, i int, last int) testStep {
ts := testStep{Input: c.Steps[i]}
if i == last {
ts.IsError = true
} else {
ts.Expect = c.Steps[i+1]
if strings.HasPrefix(ts.Expect, "e=") {
ts.IsError = true
}
}
return ts
}