SyslogAuditServiceTest.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Password Management Servlets (PWM)
  3. * http://www.pwm-project.org
  4. *
  5. * Copyright (c) 2006-2009 Novell, Inc.
  6. * Copyright (c) 2009-2018 The PWM Project
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. package password.pwm.svc.event;
  23. import com.github.tomakehurst.wiremock.junit.WireMockRule;
  24. import org.junit.Assert;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.mockito.Mockito;
  28. import password.pwm.AppProperty;
  29. import password.pwm.PwmApplication;
  30. import password.pwm.bean.UserIdentity;
  31. import password.pwm.config.Configuration;
  32. import password.pwm.config.stored.StoredConfigurationImpl;
  33. import password.pwm.util.secure.PwmRandom;
  34. import java.lang.reflect.Method;
  35. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
  36. import static org.mockito.Mockito.spy;
  37. import static org.mockito.Mockito.when;
  38. public class SyslogAuditServiceTest {
  39. @Rule
  40. public WireMockRule wm = new WireMockRule(wireMockConfig()
  41. .dynamicPort());
  42. @Test
  43. public void test_convertAuditRecordToSyslogMessage() throws Exception {
  44. {
  45. final int maxLength = 1024;
  46. final AuditRecord record = new AuditRecordFactory(Mockito.mock(PwmApplication.class)).createUserAuditRecord(
  47. AuditEvent.AUTHENTICATE,
  48. new UserIdentity("cn=user,o=org","default"),
  49. PwmRandom.getInstance().alphaNumericString(maxLength),
  50. "127.0.0.1",
  51. "localhost"
  52. );
  53. String msg = invokeConvertAuditRecordToSyslogMessage(record, maxLength);
  54. Assert.assertTrue(msg.length() <= maxLength);
  55. Assert.assertTrue(msg.length() > maxLength - 100);
  56. }
  57. { // msg copied to narrative, so more work for method to do.
  58. final int maxLength = 1024;
  59. final AuditRecord record = new AuditRecordFactory(Mockito.mock(PwmApplication.class)).createSystemAuditRecord(
  60. AuditEvent.MODIFY_CONFIGURATION,
  61. PwmRandom.getInstance().alphaNumericString(maxLength)
  62. );
  63. String msg = invokeConvertAuditRecordToSyslogMessage(record, maxLength);
  64. Assert.assertTrue(msg.length() <= maxLength);
  65. Assert.assertTrue(msg.length() > maxLength - 100);
  66. }
  67. {
  68. final int maxLength = 2048;
  69. final AuditRecord record = new AuditRecordFactory(Mockito.mock(PwmApplication.class)).createSystemAuditRecord(
  70. AuditEvent.MODIFY_CONFIGURATION,
  71. PwmRandom.getInstance().alphaNumericString(maxLength)
  72. );
  73. String msg = invokeConvertAuditRecordToSyslogMessage(record, maxLength);
  74. Assert.assertTrue(msg.length() <= maxLength);
  75. Assert.assertTrue(msg.length() > maxLength - 100);
  76. }
  77. }
  78. private String invokeConvertAuditRecordToSyslogMessage(final AuditRecord record, final int maxMsgLength)
  79. throws Exception
  80. {
  81. final Method method = SyslogAuditService.class.getDeclaredMethod(
  82. "convertAuditRecordToSyslogMessage",
  83. AuditRecord.class,
  84. Configuration.class
  85. );
  86. method.setAccessible(true);
  87. final Configuration configuration = spy(new Configuration(StoredConfigurationImpl.newStoredConfiguration()));
  88. when(configuration.readAppProperty(AppProperty.AUDIT_SYSLOG_MAX_MESSAGE_LENGTH)).thenReturn(Integer.toString(maxMsgLength));
  89. return (String)method.invoke(null, record, configuration);
  90. }
  91. }