1/*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package alts
20
21import (
22	"context"
23	"io"
24	"strings"
25	"testing"
26
27	altspb "google.golang.org/grpc/credentials/alts/internal/proto/grpc_gcp"
28	"google.golang.org/grpc/peer"
29)
30
31func TestIsRunningOnGCP(t *testing.T) {
32	for _, tc := range []struct {
33		description string
34		testOS      string
35		testReader  io.Reader
36		out         bool
37	}{
38		// Linux tests.
39		{"linux: not a GCP platform", "linux", strings.NewReader("not GCP"), false},
40		{"Linux: GCP platform (Google)", "linux", strings.NewReader("Google"), true},
41		{"Linux: GCP platform (Google Compute Engine)", "linux", strings.NewReader("Google Compute Engine"), true},
42		{"Linux: GCP platform (Google Compute Engine) with extra spaces", "linux", strings.NewReader("  Google Compute Engine        "), true},
43		// Windows tests.
44		{"windows: not a GCP platform", "windows", strings.NewReader("not GCP"), false},
45		{"windows: GCP platform (Google)", "windows", strings.NewReader("Google"), true},
46		{"windows: GCP platform (Google) with extra spaces", "windows", strings.NewReader("  Google     "), true},
47	} {
48		reverseFunc := setup(tc.testOS, tc.testReader)
49		if got, want := isRunningOnGCP(), tc.out; got != want {
50			t.Errorf("%v: isRunningOnGCP()=%v, want %v", tc.description, got, want)
51		}
52		reverseFunc()
53	}
54}
55
56func setup(testOS string, testReader io.Reader) func() {
57	tmpOS := runningOS
58	tmpReader := manufacturerReader
59
60	// Set test OS and reader function.
61	runningOS = testOS
62	manufacturerReader = func() (io.Reader, error) {
63		return testReader, nil
64	}
65
66	return func() {
67		runningOS = tmpOS
68		manufacturerReader = tmpReader
69	}
70}
71
72func TestAuthInfoFromContext(t *testing.T) {
73	ctx := context.Background()
74	altsAuthInfo := &fakeALTSAuthInfo{}
75	p := &peer.Peer{
76		AuthInfo: altsAuthInfo,
77	}
78	for _, tc := range []struct {
79		desc    string
80		ctx     context.Context
81		success bool
82		out     AuthInfo
83	}{
84		{
85			"working case",
86			peer.NewContext(ctx, p),
87			true,
88			altsAuthInfo,
89		},
90	} {
91		authInfo, err := AuthInfoFromContext(tc.ctx)
92		if got, want := (err == nil), tc.success; got != want {
93			t.Errorf("%v: AuthInfoFromContext(_)=(err=nil)=%v, want %v", tc.desc, got, want)
94		}
95		if got, want := authInfo, tc.out; got != want {
96			t.Errorf("%v:, AuthInfoFromContext(_)=(%v, _), want (%v, _)", tc.desc, got, want)
97		}
98	}
99}
100
101func TestAuthInfoFromPeer(t *testing.T) {
102	altsAuthInfo := &fakeALTSAuthInfo{}
103	p := &peer.Peer{
104		AuthInfo: altsAuthInfo,
105	}
106	for _, tc := range []struct {
107		desc    string
108		p       *peer.Peer
109		success bool
110		out     AuthInfo
111	}{
112		{
113			"working case",
114			p,
115			true,
116			altsAuthInfo,
117		},
118	} {
119		authInfo, err := AuthInfoFromPeer(tc.p)
120		if got, want := (err == nil), tc.success; got != want {
121			t.Errorf("%v: AuthInfoFromPeer(_)=(err=nil)=%v, want %v", tc.desc, got, want)
122		}
123		if got, want := authInfo, tc.out; got != want {
124			t.Errorf("%v:, AuthInfoFromPeer(_)=(%v, _), want (%v, _)", tc.desc, got, want)
125		}
126	}
127}
128
129type fakeALTSAuthInfo struct{}
130
131func (*fakeALTSAuthInfo) AuthType() string            { return "" }
132func (*fakeALTSAuthInfo) ApplicationProtocol() string { return "" }
133func (*fakeALTSAuthInfo) RecordProtocol() string      { return "" }
134func (*fakeALTSAuthInfo) SecurityLevel() altspb.SecurityLevel {
135	return altspb.SecurityLevel_SECURITY_NONE
136}
137func (*fakeALTSAuthInfo) PeerServiceAccount() string                   { return "" }
138func (*fakeALTSAuthInfo) LocalServiceAccount() string                  { return "" }
139func (*fakeALTSAuthInfo) PeerRPCVersions() *altspb.RpcProtocolVersions { return nil }
140