File: client_message_test.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (104 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download
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
// Copyright 2013-2015 Apcera Inc. All rights reserved.

// +build clienttest

package test

import (
	"encoding/base64"
	"io/ioutil"
	"net/http"
	"testing"

	"github.com/apcera/gssapi"
)

func TestClientWrap(t *testing.T) {
	b := "test message in body"

	bodyf := func(ctx *gssapi.CtxId) string {
		// Wrap and send a message to the service
		buf, err := c.MakeBufferString(b)
		if err != nil {
			t.Fatal(err)
		}
		defer buf.Release()

		_, wrapped, err := ctx.Wrap(true, gssapi.GSS_C_QOP_DEFAULT, buf)
		if err != nil {
			t.Fatal(err)
		}
		defer wrapped.Release()

		return base64.StdEncoding.EncodeToString(wrapped.Bytes())
	}

	ctx, r := initClientContext(t, "POST", "/unwrap/", bodyf)
	defer ctx.Release()

	resp, err := http.DefaultClient.Do(r)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()

	// if successful, the response body is the same message, re-wrapped by
	// the service, unwrap and compare
	wrapped64bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}
	wrappedbytes, err := base64.StdEncoding.DecodeString(string(wrapped64bytes))
	if err != nil {
		t.Fatal(err)
	}
	wrapped, err := c.MakeBufferBytes(wrappedbytes)
	if err != nil {
		t.Fatal(err)
	}
	defer wrapped.Release()
	unwrapped, _, _, err := ctx.Unwrap(wrapped)
	if err != nil {
		t.Fatal(err)
	}
	defer unwrapped.Release()

	if unwrapped.String() != b {
		t.Fatalf("Got %q, expected %q", unwrapped.String(), b)
	}
}

func TestClientMIC(t *testing.T) {
	b := "test message in body"

	ctx, r := initClientContext(t, "POST", "/verify_mic/",
		func(ctx *gssapi.CtxId) string {
			return b
		})
	defer ctx.Release()

	body, err := c.MakeBufferString(b)
	if err != nil {
		t.Fatal(err)
	}
	defer body.Release()

	mic, err := ctx.GetMIC(gssapi.GSS_C_QOP_DEFAULT, body)
	if err != nil {
		t.Fatal(err)
	}
	defer mic.Release()

	r.Header.Set(micHeader,
		base64.StdEncoding.EncodeToString(mic.Bytes()))

	resp, err := http.DefaultClient.Do(r)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		t.Fatalf("Expected %v, got %v", http.StatusOK, resp.StatusCode)
	}
}