openafs/tests/common/config.c
Andrew Deason dcf8af0b22 tests: Add test keys in afstest_BuildTestConfig
Change afstest_BuildTestConfig to add the local keys into the
generated config dir, unless the info->skipkeys is set. This just
makes afstest_BuildTestConfig a little easier to use for the common
case of generating a fully-usable config dir with usable keys (only
some callers want to skip generating keys in order to test
key-populating functionality).

Change-Id: I1ce9d062ea30c391a93562fc90bc18997de75383
Reviewed-on: https://gerrit.openafs.org/14835
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
2021-12-27 22:46:12 -05:00

138 lines
3.8 KiB
C

/*
* Copyright (c) 2010 Your File System Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*!
* Common functions for building a configuration directory
*/
#include <afsconfig.h>
#include <afs/param.h>
#include <roken.h>
#include <afs/cellconfig.h>
#include <hcrypto/des.h>
#include <tests/tap/basic.h>
#include "common.h"
static FILE *
openConfigFile(char *dirname, char *filename) {
char *path = NULL;
FILE *file;
path = afstest_asprintf("%s/%s", dirname, filename);
file = fopen(path, "w");
free(path);
return file;
}
/*!
* Build a test configuration directory, containing a CellServDB and ThisCell
* file for the "example.org" cell. Also populates the KeyFile unless
* info->skipkeys is set.
*
* @param[in] info Various details for how to create the config dir. If NULL,
* use a default zeroed struct.
* @return
* The path to the configuration directory. This should be freed by the caller
* using free()
*/
char *
afstest_BuildTestConfig(struct afstest_configinfo *info)
{
char *dir = NULL;
FILE *file;
struct afsconf_dir *confdir = NULL;
struct afstest_configinfo info_defaults;
struct in_addr iaddr;
int code;
memset(&info_defaults, 0, sizeof(info_defaults));
memset(&iaddr, 0, sizeof(iaddr));
if (info == NULL) {
info = &info_defaults;
}
dir = afstest_mkdtemp();
if (dir == NULL) {
goto error;
}
/* Work out which IP address to use in our CellServDB. We figure this out
* according to the IP address which ubik is most likely to pick for one of
* our db servers */
iaddr.s_addr = afstest_MyHostAddr();
file = openConfigFile(dir, "CellServDB");
fprintf(file, ">example.org # An example cell\n");
fprintf(file, "%s #test.example.org\n", inet_ntoa(iaddr));
fclose(file);
/* Create a ThisCell file */
file = openConfigFile(dir, "ThisCell");
fprintf(file, "example.org");
fclose(file);
if (!info->skipkeys) {
confdir = afsconf_Open(dir);
if (confdir == NULL) {
goto error;
}
code = afstest_AddDESKeyFile(confdir);
if (code != 0) {
goto error;
}
afsconf_Close(confdir);
confdir = NULL;
}
return dir;
error:
if (confdir != NULL) {
afsconf_Close(confdir);
}
if (dir != NULL) {
afstest_rmdtemp(dir);
free(dir);
}
return NULL;
}
int
afstest_AddDESKeyFile(struct afsconf_dir *dir)
{
char keymaterial[]="\x19\x17\xff\xe6\xbb\x77\x2e\xfc";
/* Make sure that it is actually a valid key */
DES_set_odd_parity((DES_cblock *)keymaterial);
return afsconf_AddKey(dir, 1, keymaterial, 1);
}