Skip to content

Commit

Permalink
Add SecureCodeGen
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDenisov committed Jun 22, 2015
1 parent 4e1f404 commit 7faa1cf
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 47 deletions.
14 changes: 14 additions & 0 deletions xcconf.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
4661611C1B38151400386ACD /* XCCDriverOptionsBuilderSpec.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4661611B1B38151400386ACD /* XCCDriverOptionsBuilderSpec.mm */; };
4661611D1B38181800386ACD /* XCCDriverOptionsBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = 466161191B3814D500386ACD /* XCCDriverOptionsBuilder.m */; };
466161341B38225800386ACD /* XCCDriverOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 4661610A1B38131D00386ACD /* XCCDriverOptions.m */; };
466161381B3824B800386ACD /* XCCSecureParametersCodeGenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = 466161371B3824B800386ACD /* XCCSecureParametersCodeGenerator.m */; };
466161391B3824B800386ACD /* XCCSecureParametersCodeGenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = 466161371B3824B800386ACD /* XCCSecureParametersCodeGenerator.m */; };
4661613B1B38251300386ACD /* XCCSecureParametersCodeGeneratorSpec.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4661613A1B38251300386ACD /* XCCSecureParametersCodeGeneratorSpec.mm */; };
46C02AEB1A82CBB0004DEA43 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 46C02AEA1A82CBB0004DEA43 /* main.m */; };
46C02B421A82D205004DEA43 /* Cedar.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46C02B2B1A82D1E8004DEA43 /* Cedar.framework */; };
46C02B461A82D2E4004DEA43 /* YAML.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46C02B451A82D2E4004DEA43 /* YAML.framework */; };
Expand Down Expand Up @@ -164,6 +167,10 @@
466161191B3814D500386ACD /* XCCDriverOptionsBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XCCDriverOptionsBuilder.m; sourceTree = "<group>"; };
4661611B1B38151400386ACD /* XCCDriverOptionsBuilderSpec.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = XCCDriverOptionsBuilderSpec.mm; sourceTree = "<group>"; };
466161351B3822A800386ACD /* XCCDriverOptionsBuilder_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XCCDriverOptionsBuilder_Internal.h; sourceTree = "<group>"; };
466161361B3824B800386ACD /* XCCSecureParametersCodeGenerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XCCSecureParametersCodeGenerator.h; sourceTree = "<group>"; };
466161371B3824B800386ACD /* XCCSecureParametersCodeGenerator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XCCSecureParametersCodeGenerator.m; sourceTree = "<group>"; };
4661613A1B38251300386ACD /* XCCSecureParametersCodeGeneratorSpec.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = XCCSecureParametersCodeGeneratorSpec.mm; sourceTree = "<group>"; };
4661613C1B38303D00386ACD /* XCCParametersCodeGeneratorProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XCCParametersCodeGeneratorProtocol.h; sourceTree = "<group>"; };
46C02AE71A82CBB0004DEA43 /* xcconf */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = xcconf; sourceTree = BUILT_PRODUCTS_DIR; };
46C02AEA1A82CBB0004DEA43 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
46C02B0B1A82D0B5004DEA43 /* xcconf_tests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = xcconf_tests; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -296,6 +303,7 @@
children = (
6B876ACA622537DA2A81924C /* XCCConfigurationCodeGeneratorSpec.mm */,
6B876754199E6DE92F0A7A62 /* XCCParameterCodeGeneratorSpec.mm */,
4661613A1B38251300386ACD /* XCCSecureParametersCodeGeneratorSpec.mm */,
);
path = CodeGen;
sourceTree = "<group>";
Expand All @@ -322,8 +330,11 @@
children = (
6B87652CC09997AE8E8BF777 /* XCCConfigurationCodeGenerator.h */,
6B8766AA7AC6A17635668DC3 /* XCCConfigurationCodeGenerator.m */,
4661613C1B38303D00386ACD /* XCCParametersCodeGeneratorProtocol.h */,
6B8761C1DFD97345FAF26699 /* XCCParametersCodeGenerator.h */,
6B87682FEA3C7E8E469FD41D /* XCCParametersCodeGenerator.m */,
466161361B3824B800386ACD /* XCCSecureParametersCodeGenerator.h */,
466161371B3824B800386ACD /* XCCSecureParametersCodeGenerator.m */,
);
path = CodeGen;
sourceTree = "<group>";
Expand Down Expand Up @@ -585,6 +596,7 @@
6B87686317B12341ED5F44B5 /* XCCYAMLConfiguration.m in Sources */,
4661611A1B3814D500386ACD /* XCCDriverOptionsBuilder.m in Sources */,
6B87650EA0469979CE4A8BB3 /* XCCEnvironment.m in Sources */,
466161381B3824B800386ACD /* XCCSecureParametersCodeGenerator.m in Sources */,
46C02BE01A83D275004DEA43 /* XCCDriver.m in Sources */,
6B876BDE919DEFF90AB89D5F /* XCCConfigurationParser.m in Sources */,
46033E761AAB7F3A00046796 /* XCCDiagnosticsEngine.m in Sources */,
Expand All @@ -600,6 +612,7 @@
6B87685105B16FBB956207FE /* main.m in Sources */,
46033E771AAB7F3A00046796 /* XCCDiagnosticsEngine.m in Sources */,
6B876974325C7F23F26319E6 /* XCCYAMLConfiguration.m in Sources */,
466161391B3824B800386ACD /* XCCSecureParametersCodeGenerator.m in Sources */,
466161341B38225800386ACD /* XCCDriverOptions.m in Sources */,
4661611D1B38181800386ACD /* XCCDriverOptionsBuilder.m in Sources */,
6B876CA05648E28C44ADED6C /* XCCEnvironment.m in Sources */,
Expand All @@ -611,6 +624,7 @@
6B87662A110FBAF34B9BE28D /* XCCConfigurationCodeGeneratorSpec.mm in Sources */,
6B876CACF50AE301E28B8015 /* XCCParameterCodeGeneratorSpec.mm in Sources */,
6B8762C7F247B9AF83E4358E /* XCCConfigurationParserSpec.mm in Sources */,
4661613B1B38251300386ACD /* XCCSecureParametersCodeGeneratorSpec.mm in Sources */,
6B876E95BEA8C1EC11AD9797 /* XCCYAMLConfigurationSpec.mm in Sources */,
6B8767A663447D9CAE63CCE1 /* XCCEnvironmentSpec.mm in Sources */,
);
Expand Down
1 change: 1 addition & 0 deletions xcconf/Sources/CodeGen/XCCConfigurationCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@property (nonatomic, strong) XCCDiagnosticsEngine *diagnosticEngine;

- (instancetype)initWithConfig:(XCCYAMLConfiguration *)config environmentName:(NSString *)environmentName;
- (instancetype)initWithConfig:(XCCYAMLConfiguration *)config environmentName:(NSString *)environmentName secureMode:(BOOL)isSecureMode;
- (NSString *)generateCode;

@end
22 changes: 19 additions & 3 deletions xcconf/Sources/CodeGen/XCCConfigurationCodeGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@
//

#import "XCCConfigurationCodeGenerator.h"
#import "XCCParametersCodeGeneratorProtocol.h"
#import "XCCParametersCodeGenerator.h"
#import "XCCSecureParametersCodeGenerator.h"
#import "XCCYAMLConfiguration.h"
#import "XCCDiagnosticsEngine.h"

@interface XCCConfigurationCodeGenerator ()

@property (strong) XCCYAMLConfiguration *config;
@property (copy) NSString *environmentName;
@property BOOL secure;

@end

@implementation XCCConfigurationCodeGenerator

- (instancetype)initWithConfig:(XCCYAMLConfiguration *)config environmentName:(NSString *)environmentName {
return [self initWithConfig:config environmentName:environmentName secureMode:NO];
}

- (instancetype)initWithConfig:(XCCYAMLConfiguration *)config environmentName:(NSString *)environmentName secureMode:(BOOL)isSecureMode {
self = [super init];

self.config = config;
self.environmentName = environmentName;

self.secure = isSecureMode;

return self;
}

Expand All @@ -45,7 +53,7 @@ - (NSString *)parameters {
self.environmentName];
[self.diagnosticEngine criticalError:error];
}
XCCParametersCodeGenerator *codeGen = [[XCCParametersCodeGenerator alloc] initWithEnvironment:environment];
id<XCCParametersCodeGeneratorProtocol> codeGen = [self codeGenWithEnvironment:environment];
NSString *code = [codeGen generateCode];
if (code.length) {
code = [code stringByAppendingString:@"\n"];
Expand All @@ -54,6 +62,14 @@ - (NSString *)parameters {
return code;
}

- (id<XCCParametersCodeGeneratorProtocol>)codeGenWithEnvironment:(XCCEnvironment *)environment {
if (self.secure) {
return [[XCCSecureParametersCodeGenerator alloc] initWithEnvironment:environment];
} else {
return [[XCCParametersCodeGenerator alloc] initWithEnvironment:environment];
}
}

- (NSString *)import {
return @"#import <Foundation/Foundation.h>\n";
}
Expand Down
7 changes: 2 additions & 5 deletions xcconf/Sources/CodeGen/XCCParametersCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
//

#import <Foundation/Foundation.h>

@class XCCEnvironment;
#import "XCCParametersCodeGeneratorProtocol.h"

@interface XCCParametersCodeGenerator : NSObject

- (instancetype)initWithEnvironment:(XCCEnvironment *)environment;
- (NSString *)generateCode;
<XCCParametersCodeGeneratorProtocol>

@end
18 changes: 18 additions & 0 deletions xcconf/Sources/CodeGen/XCCParametersCodeGeneratorProtocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// XCCParametersCodeGeneratorProtocol.h
// xcconf
//
// Created by AlexDenisov on 22/06/15.
// Copyright (c) 2015 AlexDenisov. All rights reserved.
//

#import <Foundation/Foundation.h>

@class XCCEnvironment;

@protocol XCCParametersCodeGeneratorProtocol <NSObject>

- (instancetype)initWithEnvironment:(XCCEnvironment *)environment;
- (NSString *)generateCode;

@end
15 changes: 15 additions & 0 deletions xcconf/Sources/CodeGen/XCCSecureParametersCodeGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// XCCSecureParametersCodeGenerator.h
// xcconf
//
// Created by AlexDenisov on 22/06/15.
// Copyright (c) 2015 AlexDenisov. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "XCCParametersCodeGeneratorProtocol.h"

@interface XCCSecureParametersCodeGenerator : NSObject
<XCCParametersCodeGeneratorProtocol>

@end
66 changes: 66 additions & 0 deletions xcconf/Sources/CodeGen/XCCSecureParametersCodeGenerator.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// XCCSecureParametersCodeGenerator.m
// xcconf
//
// Created by AlexDenisov on 22/06/15.
// Copyright (c) 2015 AlexDenisov. All rights reserved.
//

#import "XCCSecureParametersCodeGenerator.h"
#import "XCCEnvironment.h"

@interface XCCSecureParametersCodeGenerator ()

@property (strong) XCCEnvironment *environment;

@end

@implementation XCCSecureParametersCodeGenerator

- (instancetype)initWithEnvironment:(XCCEnvironment *)environment {
self = [super init];

self.environment = environment;

return self;
}

- (NSString *)generateCode {
NSDictionary *parameters = self.environment.parameters;
NSMutableArray *codeChunks = [NSMutableArray new];

if (self.environment.name) {
NSString *environment = [self codeForValue:self.environment.name withKey:@"environment"];
[codeChunks addObject:environment];
}

for (NSString *key in parameters.allKeys) {
NSString *chunk = [self codeForValue:parameters[key] withKey:key];
[codeChunks addObject:chunk];
}
return [codeChunks componentsJoinedByString:@"\n"];
}

- (NSString *)codeForValue:(NSString *)value withKey:(NSString *)key {
NSString *format = @"- (NSString *)%@ { %@ }";
NSString *encryptedBody = [self encryptBody:value];
NSString *method = [NSString stringWithFormat:format, key, encryptedBody];
return method;
}

- (NSString *)encryptBody:(NSString *)value {
const char *rawValue = value.UTF8String;
const unsigned long valueLength = strlen(rawValue);

NSMutableString *encryptedValue = [NSMutableString new];
for (unsigned long i = 0; i < valueLength; i++) {
NSString *encryptedCharacter = [NSString stringWithFormat:@"s[%lu] = '%c'; ", i, rawValue[i]];
[encryptedValue appendString:encryptedCharacter];
}
NSString *encryptedCharacter = [NSString stringWithFormat:@"s[%lu] = 0x00; ", valueLength];
[encryptedValue appendString:encryptedCharacter];

return [NSString stringWithFormat:@"char s[%lu]; %@return @(s);", valueLength + 1, encryptedValue];
}

@end
3 changes: 2 additions & 1 deletion xcconf/Sources/Driver/XCCDriver.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ - (void)generateAndSaveOutputFile {
XCCYAMLConfiguration *configuration = [parser parseYAML:yaml];

XCCConfigurationCodeGenerator *codeGen = [[XCCConfigurationCodeGenerator alloc] initWithConfig:configuration
environmentName:self.options.configurationName];
environmentName:self.options.configurationName
secureMode:self.options.isSecure];
codeGen.diagnosticEngine = self.diagnosticsEngine;
NSString *code = [codeGen generateCode];
[code writeToFile:self.options.outputPath
Expand Down
123 changes: 86 additions & 37 deletions xcconf_tests/Specs/CodeGen/XCCConfigurationCodeGeneratorSpec.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,96 @@
});

describe(@"generateCode generates code", ^{

context(@"non-secure", ^{

it(@"without environments", ^{
XCCYAMLConfiguration *config = [[XCCYAMLConfiguration alloc] initWithPrincipalClassName:@"Configuration"
environments:nil];
subject = [[XCCConfigurationCodeGenerator alloc] initWithConfig:config environmentName:@"Debug"];

NSString *expectedCode = @"#import <Foundation/Foundation.h>\n"
@"@interface Configuration : NSObject @end\n"
@"@implementation Configuration\n"
@"@end\n";
NSString *actualCode = [subject generateCode];

actualCode should equal(expectedCode);
});

it(@"with environment", ^{
NSDictionary *debugParameters = @{ @"debugKey" : @"debugValue" };
XCCEnvironment *debug = [[XCCEnvironment alloc] initWithName:@"Debug"
parameters:debugParameters];

NSDictionary *releaseParameters = @{ @"releaseKey" : @"releaseValue" };
XCCEnvironment *release = [[XCCEnvironment alloc] initWithName:@"Release"
parameters:releaseParameters];

NSArray *environments = @[ debug, release ];

XCCYAMLConfiguration *config = [[XCCYAMLConfiguration alloc] initWithPrincipalClassName:@"Configuration"
environments:environments];
subject = [[XCCConfigurationCodeGenerator alloc] initWithConfig:config environmentName:@"Debug"];

NSString *expectedCode = @"#import <Foundation/Foundation.h>\n"
@"@interface Configuration : NSObject @end\n"
@"@implementation Configuration\n"
@"- (NSString *)environment { return @\"Debug\"; }\n"
@"- (NSString *)debugKey { return @\"debugValue\"; }\n"
@"@end\n";
NSString *actualCode = [subject generateCode];

actualCode should equal(expectedCode);
});

it(@"without environments", ^{
XCCYAMLConfiguration *config = [[XCCYAMLConfiguration alloc] initWithPrincipalClassName:@"Configuration"
environments:nil];
subject = [[XCCConfigurationCodeGenerator alloc] initWithConfig:config environmentName:@"Debug"];

NSString *expectedCode = @"#import <Foundation/Foundation.h>\n"
@"@interface Configuration : NSObject @end\n"
@"@implementation Configuration\n"
@"@end\n";
NSString *actualCode = [subject generateCode];

actualCode should equal(expectedCode);
});

it(@"with environment", ^{
NSDictionary *debugParameters = @{ @"debugKey" : @"debugValue" };
XCCEnvironment *debug = [[XCCEnvironment alloc] initWithName:@"Debug"
parameters:debugParameters];

NSDictionary *releaseParameters = @{ @"releaseKey" : @"releaseValue" };
XCCEnvironment *release = [[XCCEnvironment alloc] initWithName:@"Release"
parameters:releaseParameters];

NSArray *environments = @[ debug, release ];

XCCYAMLConfiguration *config = [[XCCYAMLConfiguration alloc] initWithPrincipalClassName:@"Configuration"
environments:environments];
subject = [[XCCConfigurationCodeGenerator alloc] initWithConfig:config environmentName:@"Debug"];

NSString *expectedCode = @"#import <Foundation/Foundation.h>\n"
@"@interface Configuration : NSObject @end\n"
@"@implementation Configuration\n"
@"- (NSString *)environment { return @\"Debug\"; }\n"
@"- (NSString *)debugKey { return @\"debugValue\"; }\n"
@"@end\n";
NSString *actualCode = [subject generateCode];

actualCode should equal(expectedCode);

context(@"secure", ^{

it(@"without environments", ^{
XCCYAMLConfiguration *config = [[XCCYAMLConfiguration alloc] initWithPrincipalClassName:@"Configuration"
environments:nil];
subject = [[XCCConfigurationCodeGenerator alloc] initWithConfig:config environmentName:@"Debug" secureMode:YES];

NSString *expectedCode = @"#import <Foundation/Foundation.h>\n"
@"@interface Configuration : NSObject @end\n"
@"@implementation Configuration\n"
@"@end\n";
NSString *actualCode = [subject generateCode];

actualCode should equal(expectedCode);
});

it(@"with environment", ^{
NSDictionary *debugParameters = @{ @"debugKey" : @"debugValue" };
XCCEnvironment *debug = [[XCCEnvironment alloc] initWithName:@"Debug"
parameters:debugParameters];

NSDictionary *releaseParameters = @{ @"releaseKey" : @"releaseValue" };
XCCEnvironment *release = [[XCCEnvironment alloc] initWithName:@"Release"
parameters:releaseParameters];

NSArray *environments = @[ debug, release ];

XCCYAMLConfiguration *config = [[XCCYAMLConfiguration alloc] initWithPrincipalClassName:@"Configuration"
environments:environments];
subject = [[XCCConfigurationCodeGenerator alloc] initWithConfig:config environmentName:@"Debug" secureMode:YES];

NSString *expectedCode = @"#import <Foundation/Foundation.h>\n"
@"@interface Configuration : NSObject @end\n"
@"@implementation Configuration\n"
@"- (NSString *)environment { char s[6]; s[0] = 'D'; s[1] = 'e'; s[2] = 'b'; s[3] = 'u'; s[4] = 'g'; s[5] = 0x00; return @(s); }\n"
@"- (NSString *)debugKey { char s[11]; s[0] = 'd'; s[1] = 'e'; s[2] = 'b'; s[3] = 'u'; s[4] = 'g'; s[5] = 'V'; s[6] = 'a'; s[7] = 'l'; s[8] = 'u'; s[9] = 'e'; s[10] = 0x00; return @(s); }\n"
@"@end\n";
NSString *actualCode = [subject generateCode];

actualCode should equal(expectedCode);
});

});


});

});
Expand Down

0 comments on commit 7faa1cf

Please sign in to comment.