24
24
import static org .mockito .Mockito .when ;
25
25
26
26
import io .grpc .Attributes ;
27
+ import io .grpc .ClientCall ;
27
28
import io .grpc .ServerCall ;
28
29
import io .grpc .alts .AltsContext .SecurityLevel ;
29
30
import io .grpc .alts .internal .AltsInternalContext ;
37
38
/** Unit tests for {@link AltsContextUtil}. */
38
39
@ RunWith (JUnit4 .class )
39
40
public class AltsContextUtilTest {
40
-
41
- private final ServerCall <?,?> call = mock (ServerCall .class );
42
-
43
41
@ Test
44
42
public void check_noAttributeValue () {
45
- when (call .getAttributes ()).thenReturn (Attributes .newBuilder ().build ());
43
+ assertFalse (AltsContextUtil .check (Attributes .newBuilder ().build ()));
44
+ }
46
45
47
- assertFalse (AltsContextUtil .check (call ));
46
+ @ Test
47
+ public void check_unexpectedAttributeValueType () {
48
+ assertFalse (AltsContextUtil .check (Attributes .newBuilder ()
49
+ .set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , new Object ())
50
+ .build ()));
48
51
}
49
52
50
53
@ Test
51
- public void contains_unexpectedAttributeValueType () {
54
+ public void check_altsInternalContext () {
55
+ assertTrue (AltsContextUtil .check (Attributes .newBuilder ()
56
+ .set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , AltsInternalContext .getDefaultInstance ())
57
+ .build ()));
58
+ }
59
+
60
+ @ Test
61
+ public void checkServer_altsInternalContext () {
62
+ ServerCall <?,?> call = mock (ServerCall .class );
52
63
when (call .getAttributes ()).thenReturn (Attributes .newBuilder ()
53
- .set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , new Object ())
64
+ .set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , AltsInternalContext . getDefaultInstance ())
54
65
.build ());
55
66
56
- assertFalse (AltsContextUtil .check (call ));
67
+ assertTrue (AltsContextUtil .check (call ));
57
68
}
58
69
59
70
@ Test
60
- public void contains_altsInternalContext () {
71
+ public void checkClient_altsInternalContext () {
72
+ ClientCall <?,?> call = mock (ClientCall .class );
61
73
when (call .getAttributes ()).thenReturn (Attributes .newBuilder ()
62
74
.set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , AltsInternalContext .getDefaultInstance ())
63
75
.build ());
@@ -66,26 +78,57 @@ public void contains_altsInternalContext() {
66
78
}
67
79
68
80
@ Test
69
- public void from_altsInternalContext () {
81
+ public void createFrom_altsInternalContext () {
70
82
HandshakerResult handshakerResult =
71
83
HandshakerResult .newBuilder ()
72
84
.setPeerIdentity (Identity .newBuilder ().setServiceAccount ("remote@peer" ))
73
85
.setLocalIdentity (Identity .newBuilder ().setServiceAccount ("local@peer" ))
74
86
.build ();
75
- when (call .getAttributes ()).thenReturn (Attributes .newBuilder ()
76
- .set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , new AltsInternalContext (handshakerResult ))
77
- .build ());
78
87
79
- AltsContext context = AltsContextUtil .createFrom (call );
88
+ AltsContext context = AltsContextUtil .createFrom (Attributes .newBuilder ()
89
+ .set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , new AltsInternalContext (handshakerResult ))
90
+ .build ());
80
91
assertEquals ("remote@peer" , context .getPeerServiceAccount ());
81
92
assertEquals ("local@peer" , context .getLocalServiceAccount ());
82
93
assertEquals (SecurityLevel .INTEGRITY_AND_PRIVACY , context .getSecurityLevel ());
83
94
}
84
95
85
96
@ Test (expected = IllegalArgumentException .class )
86
- public void from_noAttributeValue () {
87
- when (call .getAttributes ()).thenReturn (Attributes .newBuilder ().build ());
97
+ public void createFrom_noAttributeValue () {
98
+ AltsContextUtil .createFrom (Attributes .newBuilder ().build ());
99
+ }
88
100
89
- AltsContextUtil .createFrom (call );
101
+ @ Test
102
+ public void createFromServer_altsInternalContext () {
103
+ HandshakerResult handshakerResult =
104
+ HandshakerResult .newBuilder ()
105
+ .setPeerIdentity (Identity .newBuilder ().setServiceAccount ("remote@peer" ))
106
+ .setLocalIdentity (Identity .newBuilder ().setServiceAccount ("local@peer" ))
107
+ .build ();
108
+
109
+ ServerCall <?,?> call = mock (ServerCall .class );
110
+ when (call .getAttributes ()).thenReturn (Attributes .newBuilder ()
111
+ .set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , new AltsInternalContext (handshakerResult ))
112
+ .build ());
113
+
114
+ AltsContext context = AltsContextUtil .createFrom (call );
115
+ assertEquals ("remote@peer" , context .getPeerServiceAccount ());
116
+ }
117
+
118
+ @ Test
119
+ public void createFromClient_altsInternalContext () {
120
+ HandshakerResult handshakerResult =
121
+ HandshakerResult .newBuilder ()
122
+ .setPeerIdentity (Identity .newBuilder ().setServiceAccount ("remote@peer" ))
123
+ .setLocalIdentity (Identity .newBuilder ().setServiceAccount ("local@peer" ))
124
+ .build ();
125
+
126
+ ClientCall <?,?> call = mock (ClientCall .class );
127
+ when (call .getAttributes ()).thenReturn (Attributes .newBuilder ()
128
+ .set (AltsProtocolNegotiator .AUTH_CONTEXT_KEY , new AltsInternalContext (handshakerResult ))
129
+ .build ());
130
+
131
+ AltsContext context = AltsContextUtil .createFrom (call );
132
+ assertEquals ("remote@peer" , context .getPeerServiceAccount ());
90
133
}
91
134
}
0 commit comments