| 1 | package httpsig |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestAddDigest(t *testing.T) { |
| 10 | tests := []struct { |
| 11 | name string |
| 12 | r func() *http.Request |
| 13 | algo DigestAlgorithm |
| 14 | body []byte |
| 15 | expectedDigest string |
| 16 | expectError bool |
| 17 | }{ |
| 18 | { |
| 19 | name: "adds sha256 digest", |
| 20 | r: func() *http.Request { |
| 21 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 22 | return r |
| 23 | }, |
| 24 | algo: "SHA-256", |
| 25 | body: []byte("johnny grab your gun"), |
| 26 | expectedDigest: "SHA-256=RYiuVuVdRpU+BWcNUUg3sf0EbJjQ9LDj9tUqR546hhk=", |
| 27 | }, |
| 28 | { |
| 29 | name: "adds sha512 digest", |
| 30 | r: func() *http.Request { |
| 31 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 32 | return r |
| 33 | }, |
| 34 | algo: "SHA-512", |
| 35 | body: []byte("yours is the drill that will pierce the heavens"), |
| 36 | expectedDigest: "SHA-512=bM0eBRnZkuiOTsejYNb/UpvFozde+Do1ZqlXfRTS39aGmoEzoXBpjmIIuznPslc3kaprUtI/VXH8/5HsD+thGg==", |
| 37 | }, |
| 38 | { |
| 39 | name: "digest already set", |
| 40 | r: func() *http.Request { |
| 41 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 42 | r.Header.Set("Digest", "oops") |
| 43 | return r |
| 44 | }, |
| 45 | algo: "SHA-512", |
| 46 | body: []byte("did bob ewell fall on his knife"), |
| 47 | expectError: true, |
| 48 | }, |
| 49 | { |
| 50 | name: "unknown/unsupported digest algorithm", |
| 51 | r: func() *http.Request { |
| 52 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 53 | return r |
| 54 | }, |
| 55 | algo: "MD5", |
| 56 | body: []byte("two times Cuchulainn almost drowned"), |
| 57 | expectError: true, |
| 58 | }, |
| 59 | } |
| 60 | for _, test := range tests { |
| 61 | t.Run(test.name, func(t *testing.T) { |
| 62 | test := test |
| 63 | req := test.r() |
| 64 | err := addDigest(req, test.algo, test.body) |
| 65 | gotErr := err != nil |
| 66 | if gotErr != test.expectError { |
| 67 | if test.expectError { |
| 68 | t.Fatalf("expected error, got: %s", err) |
| 69 | } else { |
| 70 | t.Fatalf("expected no error, got: %s", err) |
| 71 | } |
| 72 | } else if !gotErr { |
| 73 | d := req.Header.Get("Digest") |
| 74 | if d != test.expectedDigest { |
| 75 | t.Fatalf("unexpected digest: want %s, got %s", test.expectedDigest, d) |
| 76 | } |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestVerifyDigest(t *testing.T) { |
| 83 | tests := []struct { |
| 84 | name string |
| 85 | r func() *http.Request |
| 86 | body []byte |
| 87 | expectError bool |
| 88 | }{ |
| 89 | { |
| 90 | name: "verify sha256", |
| 91 | r: func() *http.Request { |
| 92 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 93 | r.Header.Set("Digest", "SHA-256=RYiuVuVdRpU+BWcNUUg3sf0EbJjQ9LDj9tUqR546hhk=") |
| 94 | return r |
| 95 | }, |
| 96 | body: []byte("johnny grab your gun"), |
| 97 | }, |
| 98 | { |
| 99 | name: "verify sha512", |
| 100 | r: func() *http.Request { |
| 101 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 102 | r.Header.Set("Digest", "SHA-512=bM0eBRnZkuiOTsejYNb/UpvFozde+Do1ZqlXfRTS39aGmoEzoXBpjmIIuznPslc3kaprUtI/VXH8/5HsD+thGg==") |
| 103 | return r |
| 104 | }, |
| 105 | body: []byte("yours is the drill that will pierce the heavens"), |
| 106 | }, |
| 107 | { |
| 108 | name: "no digest header", |
| 109 | r: func() *http.Request { |
| 110 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 111 | return r |
| 112 | }, |
| 113 | body: []byte("Yuji's gender is blue"), |
| 114 | expectError: true, |
| 115 | }, |
| 116 | { |
| 117 | name: "malformed digest", |
| 118 | r: func() *http.Request { |
| 119 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 120 | r.Header.Set("Digest", "SHA-256am9obm55IGdyYWIgeW91ciBndW7jsMRCmPwcFJr79MiZb7kkJ65B5GSbk0yklZkbeFK4VQ==") |
| 121 | return r |
| 122 | }, |
| 123 | body: []byte("Tochee and Ozzie BFFs forever"), |
| 124 | expectError: true, |
| 125 | }, |
| 126 | { |
| 127 | name: "unsupported/unknown algo", |
| 128 | r: func() *http.Request { |
| 129 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 130 | r.Header.Set("Digest", "MD5=poo") |
| 131 | return r |
| 132 | }, |
| 133 | body: []byte("what is a man? a miserable pile of secrets"), |
| 134 | expectError: true, |
| 135 | }, |
| 136 | { |
| 137 | name: "bad digest", |
| 138 | r: func() *http.Request { |
| 139 | r, _ := http.NewRequest("POST", "example.com", nil) |
| 140 | r.Header.Set("Digest", "SHA-256=bm9obm55IGdyYWIgeW91ciBndW7jsMRCmPwcFJr79MiZb7kkJ65B5GSbk0yklZkbeFK4VQ==") |
| 141 | return r |
| 142 | }, |
| 143 | body: []byte("johnny grab your gun"), |
| 144 | expectError: true, |
| 145 | }, |
| 146 | } |
| 147 | for _, test := range tests { |
| 148 | t.Run(test.name, func(t *testing.T) { |
| 149 | test := test |
| 150 | req := test.r() |
| 151 | buf := bytes.NewBuffer(test.body) |
| 152 | err := verifyDigest(req, buf) |
| 153 | gotErr := err != nil |
| 154 | if gotErr != test.expectError { |
| 155 | if test.expectError { |
| 156 | t.Fatalf("expected error, got: %s", err) |
| 157 | } else { |
| 158 | t.Fatalf("expected no error, got: %s", err) |
| 159 | } |
| 160 | } |
| 161 | }) |
| 162 | } |
| 163 | } |