Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Added cpp as an implementation to interop testing
Browse files Browse the repository at this point in the history
  • Loading branch information
devinlundberg committed Aug 16, 2013
1 parent b1c2efc commit 2bca4c1
Show file tree
Hide file tree
Showing 11 changed files with 809 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cpp/src/keyczar/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ kctests:
keyczart:
$(BUILD_CMD) keyczart

interop:
$(BUILD_CMD) interop

clean:
$(BUILD_CMD) -c
17 changes: 17 additions & 0 deletions cpp/src/keyczar/build.scons
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ keyzart_sources = [
out_program = keyczart_env.ComponentProgram('keyczart', keyzart_sources)
installer.AddProgram(out_program)

#### interop
interop_env = env.Clone()

interop_env.Append(
LIBS = [
lib_name,
'crypto',
],
)

interop_sources = [
'interop/interop_test.cc',
'interop/operation.cc'
]

interop_env.ComponentProgram('interop_test', interop_sources)


#### testdata_gen*

Expand Down
164 changes: 164 additions & 0 deletions cpp/src/keyczar/interop/interop_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright 2013 Google Inc.
//
// 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.
#include <keyczar/interop/interop_test.h>

#include <keyczar/base/base64w.h>
#include <keyczar/base/json_reader.h>
#include <keyczar/base/json_writer.h>
#include <keyczar/base/logging.h>
#include <keyczar/crypto_factory.h>
#include <keyczar/keyczar_tool/keyczar_tool.h>
#include <keyczar/interop/operation.h>

#include <iostream>

int main(int argc, char** argv) {
// Before any cryptographic operation initialize the random engine
// (seeding...). However this step is useless under Linux with OpenSSL.
keyczar::CryptoFactory::Rand();

// Turn off all logging, perhaps there is a better way to do this.
SetLogHandler(NULL);

if (!keyczar::interop::Interop::ProcessCommandLine(argc, argv))
return 1;
return 0;
}

namespace keyczar {
namespace interop {


// static
bool Interop::ProcessCommandLine(int argc, char** argv) {
Interop interop_tester;
return interop_tester.DoProcessCommandLine(argc, argv);
}

bool Interop::DoProcessCommandLine(int argc, char** argv) {
if (argc != 2) {
std::cout << "Incorrect Number of Arguments" << std::endl;
return false;
}
scoped_ptr<const Value> command_json(
base::JSONReader::Read(argv[1], false /* allow_trailing_comma */));
if (command_json.get() == NULL
|| !command_json->IsType(Value::TYPE_DICTIONARY))
return false;

const DictionaryValue* command_dict
= static_cast<const DictionaryValue*>(command_json.get());
std::string command;

if (!command_dict->GetString("command", &command)) {
std::cout << "Command argument not present" << std::endl;
return false;
}
if (command == "create") {
return CmdCreate(command_dict);
} else if (command == "generate") {
std::string output;
if (!CmdGenerate(command_dict, &output)) {
return false;
}
std::cout << output << std::endl;
return true;
} else if (command == "test") {
return CmdTest(command_dict);
} else {
std::cout << "Command " << command << " does not exist" << std::endl;
return false;
}
}

bool Interop::CallKeyczarTool(const ListValue * args) const {
std::string cpp_string;
int argc = args->GetSize() + 1;
const char* argv[argc];
argv[0] = "keyczart";
for (int i = 0; i < args->GetSize(); i++) {
if (!args->GetString(i, &cpp_string))
return false;
argv[i + 1] = cpp_string.c_str();
}
return keyczar::keyczar_tool::KeyczarTool::ProcessCommandLine(
keyczar::keyczar_tool::KeyczarTool::JSON_FILE, argc, argv);
}

bool Interop::CmdCreate(const DictionaryValue* json) const {
ListValue* create_flags;
ListValue* add_key_flags;

if (!json->GetList("createFlags", &create_flags))
return false;
if (!json->GetList("addKeyFlags", &add_key_flags))
return false;

CallKeyczarTool(create_flags);
CallKeyczarTool(add_key_flags);
return true;
}

bool Interop::CmdGenerate(
const DictionaryValue* json, std::string * json_output) const {
std::string output, operation, key_path, algorithm, test_data;
DictionaryValue * generate_options;

if (!json->GetString("operation", &operation) ||
!json->GetString("keyPath", &key_path) ||
!json->GetString("algorithm", &algorithm) ||
!json->GetString("testData", &test_data) ||
!json->GetDictionary("generateOptions", &generate_options)) {
std::cout << "Incorrect parameters in generate json" << std::endl;
return false;
}

Operation* op = Operation::GetOperationByName(operation, key_path, test_data);
if (!op->Generate(algorithm, generate_options, &output) ||
!op->OutputToJson(output, json_output)) {
return false;
}
return true;
}

bool Interop::CmdTest(const DictionaryValue* json) const {
std::string output, json_output, operation, key_path, algorithm, test_data;
DictionaryValue* generate_options;
DictionaryValue* test_options;

if (!json->GetString("operation", &operation) ||
!json->GetString("keyPath", &key_path) ||
!json->GetString("algorithm", &algorithm) ||
!json->GetString("testData", &test_data) ||
!json->GetString("output", &json_output) ||
!json->GetDictionary("generateOptions", &generate_options) ||
!json->GetDictionary("testOptions", &test_options)) {
std::cout << "Incorrect parameters in test json" << std::endl;
return false;
}

Operation* op = Operation::GetOperationByName(operation, key_path, test_data);
if (!op->InputFromJson(json_output, &output)) {
std::cout << "Failed to parse output from json" << std::endl;
return false;
}
if (!op->Test(output, algorithm, generate_options, test_options)) {
std::cout << "Test failed" << std::endl;
return false;
}
return true;
}

} // namespace interop
} // namespace keyczar
58 changes: 58 additions & 0 deletions cpp/src/keyczar/interop/interop_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2013 Google Inc.
//
// 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.
#ifndef KEYCZAR_INTEROP_INTEROP_H_
#define KEYCZAR_INTEROP_INTEROP_H_

#include <string>

#include <keyczar/base/basictypes.h>
#include <keyczar/base/scoped_ptr.h>
#include <keyczar/base/values.h>

namespace keyczar {
namespace interop {

// Class used by interop for processing the command line and executing
// the appropriate commands.
class Interop {
public:
// |location_type| is used for instanciating the right reader and writer
// when a key set is loaded or has to be written.
Interop() {}

// Processes command lines arguments |argv|
static bool ProcessCommandLine(int argc, char** argv);

bool DoProcessCommandLine(int argc, char** argv);

// Creates the keys with the given parameters
bool CmdCreate(const DictionaryValue* json) const;

// Outputs the output of the generate operation for the given parameters.
bool CmdGenerate(
const DictionaryValue* json, std::string * json_output) const;

// Tests the generate output provided
bool CmdTest(const DictionaryValue* json) const;

private:
bool CallKeyczarTool(const ListValue * args) const;

DISALLOW_COPY_AND_ASSIGN(Interop);
};

} // namespace interop
} // namespace keyczar

#endif // KEYCZAR_INTEROP_INTEROP_H_
Loading

0 comments on commit 2bca4c1

Please sign in to comment.