dummy_spnego_authenticator.h 4.8 KB

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