xref: /aosp_15_r20/external/cronet/net/android/dummy_spnego_authenticator.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_ANDROID_DUMMY_SPNEGO_AUTHENTICATOR_H_
6 #define NET_ANDROID_DUMMY_SPNEGO_AUTHENTICATOR_H_
7 
8 #include <jni.h>
9 #include <stdint.h>
10 
11 #include <list>
12 #include <string>
13 
14 #include "base/android/scoped_java_ref.h"
15 #include "base/memory/raw_ptr_exclusion.h"
16 
17 // Provides an interface for controlling the DummySpnegoAuthenticator service.
18 // This includes a basic stub of the Mock GSSAPI library, so that OS independent
19 // Negotiate authentication tests can be run on Android.
20 namespace net {
21 
22 // These constant values are arbitrary, and different from the real GSSAPI
23 // values, but must match those used in DummySpnegoAuthenticator.java
24 #define GSS_S_COMPLETE 0
25 #define GSS_S_CONTINUE_NEEDED 1
26 #define GSS_S_FAILURE 2
27 
28 typedef struct gss_OID_desc_struct {
29   uint32_t length;
30   // This field is not a raw_ptr<> because it was filtered by the rewriter for:
31   // #global-scope
32   RAW_PTR_EXCLUSION void* elements;
33 } gss_OID_desc, *gss_OID;
34 
35 extern gss_OID CHROME_GSS_SPNEGO_MECH_OID_DESC;
36 
37 namespace test {
38 
39 // Copy of class in Mock GSSAPI library.
40 class GssContextMockImpl {
41  public:
42   GssContextMockImpl();
43   GssContextMockImpl(const GssContextMockImpl& other);
44   GssContextMockImpl(const char* src_name,
45                      const char* targ_name,
46                      uint32_t lifetime_rec,
47                      const gss_OID_desc& mech_type,
48                      uint32_t ctx_flags,
49                      int locally_initiated,
50                      int open);
51   ~GssContextMockImpl();
52 
53   void Assign(const GssContextMockImpl& other);
54 
55   std::string src_name;
56   std::string targ_name;
57   int32_t lifetime_rec;
58   gss_OID_desc mech_type;
59   int32_t ctx_flags;
60   int locally_initiated;
61   int open;
62 };
63 
64 }  // namespace test
65 
66 namespace android {
67 
68 // Interface to Java DummySpnegoAuthenticator.
69 class DummySpnegoAuthenticator {
70  public:
71   struct SecurityContextQuery {
72     SecurityContextQuery(const std::string& expected_package,
73                          uint32_t response_code,
74                          uint32_t minor_response_code,
75                          const test::GssContextMockImpl& context_info,
76                          const std::string& expected_input_token,
77                          const std::string& output_token);
78     SecurityContextQuery(const std::string& expected_package,
79                          uint32_t response_code,
80                          uint32_t minor_response_code,
81                          const test::GssContextMockImpl& context_info,
82                          const char* expected_input_token,
83                          const char* output_token);
84     SecurityContextQuery();
85     SecurityContextQuery(const SecurityContextQuery& other);
86     ~SecurityContextQuery();
87 
88     // Note that many of these fields only exist for compatibility with the
89     // non-Android version of the tests. Only the response_code and tokens are
90     // used or checked on Android.
91     std::string expected_package;
92     uint32_t response_code;
93     uint32_t minor_response_code;
94     test::GssContextMockImpl context_info;
95     std::string expected_input_token;
96     std::string output_token;
97 
98     // Java callable members
99     base::android::ScopedJavaLocalRef<jstring> GetTokenToReturn(JNIEnv* env);
100     int GetResult(JNIEnv* env);
101 
102     // Called from Java to check the arguments passed to the GetToken. Has to
103     // be in C++ since these tests are driven by googletest, and can only report
104     // failures through the googletest C++ API.
105     void CheckGetTokenArguments(
106         JNIEnv* env,
107         const base::android::JavaParamRef<jstring>& incoming_token);
108   };
109 
110   DummySpnegoAuthenticator();
111 
112   ~DummySpnegoAuthenticator();
113 
114   void ExpectSecurityContext(const std::string& expected_package,
115                              uint32_t response_code,
116                              uint32_t minor_response_code,
117                              const test::GssContextMockImpl& context_info,
118                              const std::string& expected_input_token,
119                              const std::string& output_token);
120 
121   static void EnsureTestAccountExists();
122   static void RemoveTestAccounts();
123 
124   long GetNextQuery(JNIEnv* env);
125 
126  private:
127   // Abandon the test if the query queue is empty. Has to be a void function to
128   // allow use of ASSERT_FALSE.
129   void CheckQueueNotEmpty();
130 
131   std::list<SecurityContextQuery> expected_security_queries_;
132   // Needed to keep the current query alive once it has been pulled from the
133   // queue. This is simpler than transferring its ownership to Java.
134   SecurityContextQuery current_query_;
135 };
136 
137 }  // namespace android
138 
139 using MockAuthLibrary = android::DummySpnegoAuthenticator;
140 
141 }  // namespace net
142 
143 #endif  // NET_ANDROID_DUMMY_SPNEGO_AUTHENTICATOR_H_
144