diff --git a/FirebaseAppCheck/Sources/Core/FIRAppCheck.m b/FirebaseAppCheck/Sources/Core/FIRAppCheck.m index 1cf6aa30d24..f1ccdab2b6d 100644 --- a/FirebaseAppCheck/Sources/Core/FIRAppCheck.m +++ b/FirebaseAppCheck/Sources/Core/FIRAppCheck.m @@ -65,6 +65,9 @@ @implementation FIRAppCheck #pragma mark - Internal - (nullable instancetype)initWithApp:(FIRApp *)app { + // Set the App Check Core logging level to the current equivalent Firebase logging level. + GACAppCheckLogger.logLevel = FIRGetGACAppCheckLogLevel(); + id providerFactory = [FIRAppCheck providerFactory]; if (providerFactory == nil) { diff --git a/FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h b/FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h index 62e02f06af6..3f2fce59da5 100644 --- a/FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h +++ b/FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h @@ -16,6 +16,8 @@ #import +#import + #import "FirebaseCore/Extension/FirebaseCoreInternal.h" extern FIRLoggerService kFIRLoggerAppCheck; @@ -34,3 +36,5 @@ FOUNDATION_EXPORT NSString *const kFIRLoggerAppCheckMessageCodeDebugToken; FOUNDATION_EXPORT NSString *const kFIRLoggerAppCheckMessageDeviceCheckProviderIncompleteFIROptions; void FIRAppCheckDebugLog(NSString *messageCode, NSString *message, ...); + +GACAppCheckLogLevel FIRGetGACAppCheckLogLevel(void); diff --git a/FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.m b/FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.m index a2d345927be..3c001b22326 100644 --- a/FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.m +++ b/FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.m @@ -16,8 +16,6 @@ #import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h" -#import - #import "FirebaseCore/Extension/FirebaseCoreInternal.h" NS_ASSUME_NONNULL_BEGIN @@ -38,6 +36,7 @@ NSString *const kFIRLoggerAppCheckMessageDeviceCheckProviderIncompleteFIROptions = @"I-FAA006001"; #pragma mark - Log functions + void FIRAppCheckDebugLog(NSString *messageCode, NSString *message, ...) { va_list args_ptr; va_start(args_ptr, message); @@ -45,4 +44,21 @@ void FIRAppCheckDebugLog(NSString *messageCode, NSString *message, ...) { va_end(args_ptr); } +#pragma mark - Helper functions + +GACAppCheckLogLevel FIRGetGACAppCheckLogLevel(void) { + FIRLoggerLevel loggerLevel = FIRGetLoggerLevel(); + switch (loggerLevel) { + case FIRLoggerLevelError: + return GACAppCheckLogLevelError; + case FIRLoggerLevelWarning: + case FIRLoggerLevelNotice: + return GACAppCheckLogLevelWarning; + case FIRLoggerLevelInfo: + return GACAppCheckLogLevelInfo; + case FIRLoggerLevelDebug: + return GACAppCheckLogLevelDebug; + } +} + NS_ASSUME_NONNULL_END diff --git a/FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckLoggerTests.m b/FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckLoggerTests.m new file mode 100644 index 00000000000..e1f72fc5c2b --- /dev/null +++ b/FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckLoggerTests.m @@ -0,0 +1,68 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import + +#import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h" + +#import + +@interface FIRAppCheckLoggerTests : XCTestCase +@end + +@implementation FIRAppCheckLoggerTests + +- (void)testGetGACAppCheckLogLevel_Error { + FIRSetLoggerLevel(FIRLoggerLevelError); + + GACAppCheckLogLevel logLevel = FIRGetGACAppCheckLogLevel(); + + XCTAssertEqual(logLevel, GACAppCheckLogLevelError); +} + +- (void)testGetGACAppCheckLogLevel_Warning { + FIRSetLoggerLevel(FIRLoggerLevelWarning); + + GACAppCheckLogLevel logLevel = FIRGetGACAppCheckLogLevel(); + + XCTAssertEqual(logLevel, GACAppCheckLogLevelWarning); +} + +- (void)testGetGACAppCheckLogLevel_Notice { + FIRSetLoggerLevel(FIRLoggerLevelNotice); + + GACAppCheckLogLevel logLevel = FIRGetGACAppCheckLogLevel(); + + XCTAssertEqual(logLevel, GACAppCheckLogLevelWarning); +} + +- (void)testGetGACAppCheckLogLevel_Info { + FIRSetLoggerLevel(FIRLoggerLevelInfo); + + GACAppCheckLogLevel logLevel = FIRGetGACAppCheckLogLevel(); + + XCTAssertEqual(logLevel, GACAppCheckLogLevelInfo); +} + +- (void)testGetGACAppCheckLogLevel_Debug { + FIRSetLoggerLevel(FIRLoggerLevelDebug); + + GACAppCheckLogLevel logLevel = FIRGetGACAppCheckLogLevel(); + + XCTAssertEqual(logLevel, GACAppCheckLogLevelDebug); +} + +@end diff --git a/FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckTests.m b/FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckTests.m index 87346d5aa9e..3b80258f078 100644 --- a/FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckTests.m +++ b/FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckTests.m @@ -37,6 +37,8 @@ // The FAC token value returned when an error occurs. static NSString *const kDummyToken = @"eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ=="; +extern void FIRResetLogger(void); + @interface FIRAppCheck (Tests) - (instancetype)initWithAppName:(NSString *)appName appCheckCore:(GACAppCheck *)appCheckCore @@ -67,6 +69,8 @@ @implementation FIRAppCheckTests - (void)setUp { [super setUp]; + FIRResetLogger(); + self.appName = @"FIRAppCheckTests"; self.mockAppCheckProvider = OCMStrictProtocolMock(@protocol(FIRAppCheckProvider)); self.mockSettings = OCMStrictClassMock([FIRAppCheckSettings class]); @@ -112,13 +116,19 @@ - (void)testInitWithApp { [FIRAppCheck setAppCheckProviderFactory:mockProviderFactory]; - // 3. Call init. + // 3. Set the Firebase logging level to Debug. + FIRSetLoggerLevel(FIRLoggerLevelDebug); + + // 4. Call init. FIRAppCheck *appCheck = [[FIRAppCheck alloc] initWithApp:app]; XCTAssert([appCheck isKindOfClass:[FIRAppCheck class]]); - // 4. Verify mocks. + // 5. Verify mocks. OCMVerifyAll(mockProviderFactory); OCMVerifyAll(mockProvider); + + // 6. Verify that the App Check Core logging level is also Debug. + XCTAssertEqual(GACAppCheckLogger.logLevel, GACAppCheckLogLevelDebug); } - (void)testAppCheckInstanceForApp { @@ -134,6 +144,9 @@ - (void)testAppCheckInstanceForApp { XCTAssertNotNil(app); XCTAssertNotNil([FIRAppCheck appCheckWithApp:app]); + + // Verify that the App Check Core logging level is the default (Warning). + XCTAssertEqual(GACAppCheckLogger.logLevel, GACAppCheckLogLevelWarning); } #pragma mark - Public Get Token