mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
20 hours ago 400fb2694641b05e53217d3c742a6e49e3a254c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.server;
 
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
 
import org.testng.annotations.Test;
 
/** Tests for the audit logs included in test failure diagnostics. */
@SuppressWarnings("javadoc")
public class TestCaseUtilsAuditLogTest extends DirectoryServerTestCase
{
  @Test
  public void shortAuditLogIsIncludedInFull() throws IOException
  {
    final String audit = "dn: cn=Jos\u00e9\nchangetype: add\n";
 
    assertTrue(getAuditDiagnostics(appendDiagnosticsForLog("audit", audit)).startsWith(audit));
  }
 
  @Test
  public void oversizedAuditRecordIsTruncatedByBytes() throws IOException
  {
    final String ending = " last value";
    final String audit = "old audit record\n" + "\u00e9".repeat(128 * 1024) + ending;
 
    final String diagnostics = getAuditDiagnostics(appendDiagnosticsForLog("audit", audit));
    final int suffix = diagnostics.indexOf(ending);
    assertTrue(suffix >= 0, "Missing audit log suffix");
    final String excerpt = diagnostics.substring(0, suffix + ending.length());
    final int bytes = excerpt.getBytes(UTF_8).length;
    assertTrue(bytes <= 16 * 1024 + 128, "Audit diagnostics printed " + bytes + " bytes");
    assertTrue(excerpt.startsWith("[truncated"), "Missing audit truncation notice");
    assertFalse(excerpt.contains("\ufffd"), "Audit log suffix starts inside a UTF-8 character");
  }
 
  @Test
  public void rotatedAuditLogIsKeptOnDisk() throws IOException
  {
    final String audit = "previous audit record\n";
 
    final String diagnostics = appendDiagnosticsForLog("audit.20261127163239Z", audit);
    assertFalse(diagnostics.contains(audit), "Rotated audit record was printed");
    assertTrue(diagnostics.contains("Rotated audit logs"), "Missing rotated audit log notice");
  }
 
  private String getAuditDiagnostics(String diagnostics)
  {
    final String header = "audit contents:" + System.lineSeparator();
    final int start = diagnostics.indexOf(header);
    assertTrue(start >= 0, "Missing diagnostics for audit");
    return diagnostics.substring(start + header.length());
  }
 
  private String appendDiagnosticsForLog(String fileName, String audit) throws IOException
  {
    final Path logDirectory = Files.createDirectories(TestCaseUtils.paths.testInstanceRoot.toPath().resolve("logs"));
    final Path auditFile = Files.writeString(logDirectory.resolve(fileName), audit, UTF_8, CREATE_NEW);
    try
    {
      final StringBuilder diagnostics = new StringBuilder();
      TestCaseUtils.appendLogsContents(diagnostics);
      assertTrue(audit.equals(Files.readString(auditFile, UTF_8)), "Diagnostic changed the audit log");
      return diagnostics.toString();
    }
    finally
    {
      Files.deleteIfExists(auditFile);
    }
  }
}