Skip to content

Commit

Permalink
Set GACAppCheckLogger.logLevel in Firebase App Check (#12066)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored Nov 7, 2023
1 parent 3db9651 commit 4ff62df
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
3 changes: 3 additions & 0 deletions FirebaseAppCheck/Sources/Core/FIRAppCheck.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<FIRAppCheckProviderFactory> providerFactory = [FIRAppCheck providerFactory];

if (providerFactory == nil) {
Expand Down
4 changes: 4 additions & 0 deletions FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import <Foundation/Foundation.h>

#import <AppCheckCore/AppCheckCore.h>

#import "FirebaseCore/Extension/FirebaseCoreInternal.h"

extern FIRLoggerService kFIRLoggerAppCheck;
Expand All @@ -34,3 +36,5 @@ FOUNDATION_EXPORT NSString *const kFIRLoggerAppCheckMessageCodeDebugToken;
FOUNDATION_EXPORT NSString *const kFIRLoggerAppCheckMessageDeviceCheckProviderIncompleteFIROptions;

void FIRAppCheckDebugLog(NSString *messageCode, NSString *message, ...);

GACAppCheckLogLevel FIRGetGACAppCheckLogLevel(void);
20 changes: 18 additions & 2 deletions FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h"

#import <AppCheckCore/AppCheckCore.h>

#import "FirebaseCore/Extension/FirebaseCoreInternal.h"

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -38,11 +36,29 @@
NSString *const kFIRLoggerAppCheckMessageDeviceCheckProviderIncompleteFIROptions = @"I-FAA006001";

#pragma mark - Log functions

void FIRAppCheckDebugLog(NSString *messageCode, NSString *message, ...) {
va_list args_ptr;
va_start(args_ptr, message);
FIRLogBasic(FIRLoggerLevelDebug, kFIRLoggerAppCheck, messageCode, message, args_ptr);
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
68 changes: 68 additions & 0 deletions FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckLoggerTests.m
Original file line number Diff line number Diff line change
@@ -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 <XCTest/XCTest.h>

#import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h"

#import <AppCheckCore/AppCheckCore.h>

@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
17 changes: 15 additions & 2 deletions FirebaseAppCheck/Tests/Unit/Core/FIRAppCheckTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) <FIRAppCheckInterop, GACAppCheckTokenDelegate>
- (instancetype)initWithAppName:(NSString *)appName
appCheckCore:(GACAppCheck *)appCheckCore
Expand Down Expand Up @@ -67,6 +69,8 @@ @implementation FIRAppCheckTests
- (void)setUp {
[super setUp];

FIRResetLogger();

self.appName = @"FIRAppCheckTests";
self.mockAppCheckProvider = OCMStrictProtocolMock(@protocol(FIRAppCheckProvider));
self.mockSettings = OCMStrictClassMock([FIRAppCheckSettings class]);
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 4ff62df

Please sign in to comment.